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
39 * $FreeBSD: src/sys/kern/vfs_subr.c,v 1.249.2.30 2003/04/04 20:35:57 tegge Exp $
40 * $DragonFly: src/sys/kern/vfs_subr.c,v 1.5 2003/06/23 17:55:41 dillon Exp $
44 * External virtual filesystem routines
48 #include <sys/param.h>
49 #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>
60 #include <sys/mount.h>
61 #include <sys/namei.h>
63 #include <sys/reboot.h>
64 #include <sys/socket.h>
66 #include <sys/sysctl.h>
67 #include <sys/syslog.h>
68 #include <sys/vmmeter.h>
69 #include <sys/vnode.h>
71 #include <machine/limits.h>
74 #include <vm/vm_object.h>
75 #include <vm/vm_extern.h>
77 #include <vm/vm_map.h>
78 #include <vm/vm_page.h>
79 #include <vm/vm_pager.h>
80 #include <vm/vnode_pager.h>
81 #include <vm/vm_zone.h>
85 static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
87 static void insmntque __P((struct vnode *vp, struct mount *mp));
88 static void vclean __P((struct vnode *vp, int flags, struct proc *p));
89 static unsigned long numvnodes;
90 static void vlruvp(struct vnode *vp);
91 SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, "");
93 enum vtype iftovt_tab[16] = {
94 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
95 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
98 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
99 S_IFSOCK, S_IFIFO, S_IFMT,
102 static TAILQ_HEAD(freelst, vnode) vnode_free_list; /* vnode free list */
104 static u_long wantfreevnodes = 25;
105 SYSCTL_INT(_debug, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, "");
106 static u_long freevnodes = 0;
107 SYSCTL_INT(_debug, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0, "");
109 static int reassignbufcalls;
110 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0, "");
111 static int reassignbufloops;
112 SYSCTL_INT(_vfs, OID_AUTO, reassignbufloops, CTLFLAG_RW, &reassignbufloops, 0, "");
113 static int reassignbufsortgood;
114 SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortgood, CTLFLAG_RW, &reassignbufsortgood, 0, "");
115 static int reassignbufsortbad;
116 SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortbad, CTLFLAG_RW, &reassignbufsortbad, 0, "");
117 static int reassignbufmethod = 1;
118 SYSCTL_INT(_vfs, OID_AUTO, reassignbufmethod, CTLFLAG_RW, &reassignbufmethod, 0, "");
119 static int nameileafonly = 0;
120 SYSCTL_INT(_vfs, OID_AUTO, nameileafonly, CTLFLAG_RW, &nameileafonly, 0, "");
122 #ifdef ENABLE_VFS_IOOPT
124 SYSCTL_INT(_vfs, OID_AUTO, ioopt, CTLFLAG_RW, &vfs_ioopt, 0, "");
127 struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist); /* mounted fs */
128 struct simplelock mountlist_slock;
129 struct simplelock mntvnode_slock;
130 int nfs_mount_type = -1;
131 #ifndef NULL_SIMPLELOCKS
132 static struct simplelock mntid_slock;
133 static struct simplelock vnode_free_list_slock;
134 static struct simplelock spechash_slock;
136 struct nfs_public nfs_pub; /* publicly exported FS */
137 static vm_zone_t vnode_zone;
140 * The workitem queue.
142 #define SYNCER_MAXDELAY 32
143 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
144 time_t syncdelay = 30; /* max time to delay syncing data */
145 time_t filedelay = 30; /* time to delay syncing files */
146 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0, "");
147 time_t dirdelay = 29; /* time to delay syncing directories */
148 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0, "");
149 time_t metadelay = 28; /* time to delay syncing metadata */
150 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0, "");
151 static int rushjob; /* number of slots to run ASAP */
152 static int stat_rush_requests; /* number of times I/O speeded up */
153 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0, "");
155 static int syncer_delayno = 0;
156 static long syncer_mask;
157 LIST_HEAD(synclist, vnode);
158 static struct synclist *syncer_workitem_pending;
161 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
162 &desiredvnodes, 0, "Maximum number of vnodes");
163 static int minvnodes;
164 SYSCTL_INT(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
165 &minvnodes, 0, "Minimum number of vnodes");
166 static int vnlru_nowhere = 0;
167 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW, &vnlru_nowhere, 0,
168 "Number of times the vnlru process ran without success");
170 static void vfs_free_addrlist __P((struct netexport *nep));
171 static int vfs_free_netcred __P((struct radix_node *rn, void *w));
172 static int vfs_hang_addrlist __P((struct mount *mp, struct netexport *nep,
173 struct export_args *argp));
176 * Initialize the vnode management data structures.
182 desiredvnodes = maxproc + cnt.v_page_count / 4;
183 minvnodes = desiredvnodes / 4;
184 simple_lock_init(&mntvnode_slock);
185 simple_lock_init(&mntid_slock);
186 simple_lock_init(&spechash_slock);
187 TAILQ_INIT(&vnode_free_list);
188 simple_lock_init(&vnode_free_list_slock);
189 vnode_zone = zinit("VNODE", sizeof (struct vnode), 0, 0, 5);
191 * Initialize the filesystem syncer.
193 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE,
195 syncer_maxdelay = syncer_mask + 1;
199 * Mark a mount point as busy. Used to synchronize access and to delay
200 * unmounting. Interlock is not released on failure.
203 vfs_busy(mp, flags, interlkp, p)
206 struct simplelock *interlkp;
211 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
212 if (flags & LK_NOWAIT)
214 mp->mnt_kern_flag |= MNTK_MWAIT;
216 simple_unlock(interlkp);
219 * Since all busy locks are shared except the exclusive
220 * lock granted when unmounting, the only place that a
221 * wakeup needs to be done is at the release of the
222 * exclusive lock at the end of dounmount.
224 tsleep((caddr_t)mp, PVFS, "vfs_busy", 0);
226 simple_lock(interlkp);
230 lkflags = LK_SHARED | LK_NOPAUSE;
232 lkflags |= LK_INTERLOCK;
233 if (lockmgr(&mp->mnt_lock, lkflags, interlkp, p))
234 panic("vfs_busy: unexpected lock failure");
239 * Free a busy filesystem.
247 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, p);
251 * Lookup a filesystem type, and if found allocate and initialize
252 * a mount structure for it.
254 * Devname is usually updated by mount(8) after booting.
257 vfs_rootmountalloc(fstypename, devname, mpp)
262 struct proc *p = curproc; /* XXX */
263 struct vfsconf *vfsp;
266 if (fstypename == NULL)
268 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
269 if (!strcmp(vfsp->vfc_name, fstypename))
273 mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
274 bzero((char *)mp, (u_long)sizeof(struct mount));
275 lockinit(&mp->mnt_lock, PVFS, "vfslock", VLKTIMEOUT, LK_NOPAUSE);
276 (void)vfs_busy(mp, LK_NOWAIT, 0, p);
277 TAILQ_INIT(&mp->mnt_nvnodelist);
278 TAILQ_INIT(&mp->mnt_reservedvnlist);
279 mp->mnt_nvnodelistsize = 0;
281 mp->mnt_op = vfsp->vfc_vfsops;
282 mp->mnt_flag = MNT_RDONLY;
283 mp->mnt_vnodecovered = NULLVP;
284 vfsp->vfc_refcount++;
285 mp->mnt_iosize_max = DFLTPHYS;
286 mp->mnt_stat.f_type = vfsp->vfc_typenum;
287 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
288 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
289 mp->mnt_stat.f_mntonname[0] = '/';
290 mp->mnt_stat.f_mntonname[1] = 0;
291 (void) copystr(devname, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 0);
297 * Find an appropriate filesystem to use for the root. If a filesystem
298 * has not been preselected, walk through the list of known filesystems
299 * trying those that have mountroot routines, and try them until one
300 * works or we have tried them all.
302 #ifdef notdef /* XXX JH */
304 lite2_vfs_mountroot()
306 struct vfsconf *vfsp;
307 extern int (*lite2_mountroot) __P((void));
310 if (lite2_mountroot != NULL)
311 return ((*lite2_mountroot)());
312 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
313 if (vfsp->vfc_mountroot == NULL)
315 if ((error = (*vfsp->vfc_mountroot)()) == 0)
317 printf("%s_mountroot failed: %d\n", vfsp->vfc_name, error);
324 * Lookup a mount point by filesystem identifier.
330 register struct mount *mp;
332 simple_lock(&mountlist_slock);
333 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
334 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
335 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
336 simple_unlock(&mountlist_slock);
340 simple_unlock(&mountlist_slock);
341 return ((struct mount *) 0);
345 * Get a new unique fsid. Try to make its val[0] unique, since this value
346 * will be used to create fake device numbers for stat(). Also try (but
347 * not so hard) make its val[0] unique mod 2^16, since some emulators only
348 * support 16-bit device numbers. We end up with unique val[0]'s for the
349 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
351 * Keep in mind that several mounts may be running in parallel. Starting
352 * the search one past where the previous search terminated is both a
353 * micro-optimization and a defense against returning the same fsid to
360 static u_int16_t mntid_base;
364 simple_lock(&mntid_slock);
365 mtype = mp->mnt_vfc->vfc_typenum;
366 tfsid.val[1] = mtype;
367 mtype = (mtype & 0xFF) << 24;
369 tfsid.val[0] = makeudev(255,
370 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
372 if (vfs_getvfs(&tfsid) == NULL)
375 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
376 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
377 simple_unlock(&mntid_slock);
381 * Knob to control the precision of file timestamps:
383 * 0 = seconds only; nanoseconds zeroed.
384 * 1 = seconds and nanoseconds, accurate within 1/HZ.
385 * 2 = seconds and nanoseconds, truncated to microseconds.
386 * >=3 = seconds and nanoseconds, maximum precision.
388 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
390 static int timestamp_precision = TSP_SEC;
391 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
392 ×tamp_precision, 0, "");
395 * Get a current timestamp.
399 struct timespec *tsp;
403 switch (timestamp_precision) {
405 tsp->tv_sec = time_second;
413 TIMEVAL_TO_TIMESPEC(&tv, tsp);
423 * Set vnode attributes to VNOVAL
427 register struct vattr *vap;
431 vap->va_size = VNOVAL;
432 vap->va_bytes = VNOVAL;
433 vap->va_mode = VNOVAL;
434 vap->va_nlink = VNOVAL;
435 vap->va_uid = VNOVAL;
436 vap->va_gid = VNOVAL;
437 vap->va_fsid = VNOVAL;
438 vap->va_fileid = VNOVAL;
439 vap->va_blocksize = VNOVAL;
440 vap->va_rdev = VNOVAL;
441 vap->va_atime.tv_sec = VNOVAL;
442 vap->va_atime.tv_nsec = VNOVAL;
443 vap->va_mtime.tv_sec = VNOVAL;
444 vap->va_mtime.tv_nsec = VNOVAL;
445 vap->va_ctime.tv_sec = VNOVAL;
446 vap->va_ctime.tv_nsec = VNOVAL;
447 vap->va_flags = VNOVAL;
448 vap->va_gen = VNOVAL;
453 * This routine is called when we have too many vnodes. It attempts
454 * to free <count> vnodes and will potentially free vnodes that still
455 * have VM backing store (VM backing store is typically the cause
456 * of a vnode blowout so we want to do this). Therefore, this operation
457 * is not considered cheap.
459 * A number of conditions may prevent a vnode from being reclaimed.
460 * the buffer cache may have references on the vnode, a directory
461 * vnode may still have references due to the namei cache representing
462 * underlying files, or the vnode may be in active use. It is not
463 * desireable to reuse such vnodes. These conditions may cause the
464 * number of vnodes to reach some minimum value regardless of what
465 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
468 vlrureclaim(struct mount *mp)
477 * Calculate the trigger point, don't allow user
478 * screwups to blow us up. This prevents us from
479 * recycling vnodes with lots of resident pages. We
480 * aren't trying to free memory, we are trying to
483 usevnodes = desiredvnodes;
486 trigger = cnt.v_page_count * 2 / usevnodes;
489 simple_lock(&mntvnode_slock);
490 count = mp->mnt_nvnodelistsize / 10 + 1;
491 while (count && (vp = TAILQ_FIRST(&mp->mnt_nvnodelist)) != NULL) {
492 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
493 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
495 if (vp->v_type != VNON &&
496 vp->v_type != VBAD &&
497 VMIGHTFREE(vp) && /* critical path opt */
498 (vp->v_object == NULL || vp->v_object->resident_page_count < trigger) &&
499 simple_lock_try(&vp->v_interlock)
501 simple_unlock(&mntvnode_slock);
502 if (VMIGHTFREE(vp)) {
506 simple_unlock(&vp->v_interlock);
508 simple_lock(&mntvnode_slock);
512 simple_unlock(&mntvnode_slock);
517 * Attempt to recycle vnodes in a context that is always safe to block.
518 * Calling vlrurecycle() from the bowels of file system code has some
519 * interesting deadlock problems.
521 static struct thread *vnlruthread;
522 static int vnlruproc_sig;
527 struct mount *mp, *nmp;
530 struct thread *td = vnlruthread;
531 struct proc *p = td->td_proc;
533 EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc, td,
538 kproc_suspend_loop(td);
539 if (numvnodes - freevnodes <= desiredvnodes * 9 / 10) {
541 wakeup(&vnlruproc_sig);
542 tsleep(p, PVFS, "vlruwt", hz);
546 simple_lock(&mountlist_slock);
547 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
548 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
549 nmp = TAILQ_NEXT(mp, mnt_list);
552 done += vlrureclaim(mp);
553 simple_lock(&mountlist_slock);
554 nmp = TAILQ_NEXT(mp, mnt_list);
557 simple_unlock(&mountlist_slock);
560 tsleep(p, PPAUSE, "vlrup", hz * 3);
566 static struct kproc_desc vnlru_kp = {
571 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &vnlru_kp)
574 * Routines having to do with the management of the vnode table.
576 extern vop_t **dead_vnodeop_p;
579 * Return the next vnode from the free list.
582 getnewvnode(tag, mp, vops, vpp)
589 struct proc *p = curproc; /* XXX */
590 struct vnode *vp = NULL;
596 * Try to reuse vnodes if we hit the max. This situation only
597 * occurs in certain large-memory (2G+) situations. We cannot
598 * attempt to directly reclaim vnodes due to nasty recursion
601 while (numvnodes - freevnodes > desiredvnodes) {
602 if (vnlruproc_sig == 0) {
603 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
606 tsleep(&vnlruproc_sig, PVFS, "vlruwk", hz);
611 * Attempt to reuse a vnode already on the free list, allocating
612 * a new vnode if we can't find one or if we have not reached a
613 * good minimum for good LRU performance.
615 simple_lock(&vnode_free_list_slock);
616 if (freevnodes >= wantfreevnodes && numvnodes >= minvnodes) {
619 for (count = 0; count < freevnodes; count++) {
620 vp = TAILQ_FIRST(&vnode_free_list);
621 if (vp == NULL || vp->v_usecount)
622 panic("getnewvnode: free vnode isn't");
624 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
625 if ((VOP_GETVOBJECT(vp, &object) == 0 &&
626 (object->resident_page_count || object->ref_count)) ||
627 !simple_lock_try(&vp->v_interlock)) {
628 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
632 if (LIST_FIRST(&vp->v_cache_src)) {
634 * note: nameileafonly sysctl is temporary,
635 * for debugging only, and will eventually be
638 if (nameileafonly > 0) {
640 * Do not reuse namei-cached directory
641 * vnodes that have cached
644 if (cache_leaf_test(vp) < 0) {
645 simple_unlock(&vp->v_interlock);
646 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
650 } else if (nameileafonly < 0 ||
651 vmiodirenable == 0) {
653 * Do not reuse namei-cached directory
654 * vnodes if nameileafonly is -1 or
655 * if VMIO backing for directories is
656 * turned off (otherwise we reuse them
659 simple_unlock(&vp->v_interlock);
660 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
670 vp->v_flag |= VDOOMED;
671 vp->v_flag &= ~VFREE;
673 simple_unlock(&vnode_free_list_slock);
676 if (vp->v_type != VBAD) {
679 simple_unlock(&vp->v_interlock);
687 panic("cleaned vnode isn't");
690 panic("Clean vnode has pending I/O's");
700 vp->v_writecount = 0; /* XXX */
702 simple_unlock(&vnode_free_list_slock);
703 vp = (struct vnode *) zalloc(vnode_zone);
704 bzero((char *) vp, sizeof *vp);
705 simple_lock_init(&vp->v_interlock);
708 LIST_INIT(&vp->v_cache_src);
709 TAILQ_INIT(&vp->v_cache_dst);
713 TAILQ_INIT(&vp->v_cleanblkhd);
714 TAILQ_INIT(&vp->v_dirtyblkhd);
724 vfs_object_create(vp, p, p->p_ucred);
729 * Move a vnode from one mount queue to another.
733 register struct vnode *vp;
734 register struct mount *mp;
737 simple_lock(&mntvnode_slock);
739 * Delete from old mount point vnode list, if on one.
741 if (vp->v_mount != NULL) {
742 KASSERT(vp->v_mount->mnt_nvnodelistsize > 0,
743 ("bad mount point vnode list size"));
744 TAILQ_REMOVE(&vp->v_mount->mnt_nvnodelist, vp, v_nmntvnodes);
745 vp->v_mount->mnt_nvnodelistsize--;
748 * Insert into list of vnodes for the new mount point, if available.
750 if ((vp->v_mount = mp) == NULL) {
751 simple_unlock(&mntvnode_slock);
754 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
755 mp->mnt_nvnodelistsize++;
756 simple_unlock(&mntvnode_slock);
760 * Update outstanding I/O count and do wakeup if requested.
764 register struct buf *bp;
766 register struct vnode *vp;
768 bp->b_flags &= ~B_WRITEINPROG;
769 if ((vp = bp->b_vp)) {
771 if (vp->v_numoutput < 0)
772 panic("vwakeup: neg numoutput");
773 if ((vp->v_numoutput == 0) && (vp->v_flag & VBWAIT)) {
774 vp->v_flag &= ~VBWAIT;
775 wakeup((caddr_t) &vp->v_numoutput);
781 * Flush out and invalidate all buffers associated with a vnode.
782 * Called with the underlying object locked.
785 vinvalbuf(vp, flags, cred, p, slpflag, slptimeo)
786 register struct vnode *vp;
790 int slpflag, slptimeo;
792 register struct buf *bp;
793 struct buf *nbp, *blist;
797 if (flags & V_SAVE) {
799 while (vp->v_numoutput) {
800 vp->v_flag |= VBWAIT;
801 error = tsleep((caddr_t)&vp->v_numoutput,
802 slpflag | (PRIBIO + 1), "vinvlbuf", slptimeo);
808 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
810 if ((error = VOP_FSYNC(vp, cred, MNT_WAIT, p)) != 0)
813 if (vp->v_numoutput > 0 ||
814 !TAILQ_EMPTY(&vp->v_dirtyblkhd))
815 panic("vinvalbuf: dirty bufs");
821 blist = TAILQ_FIRST(&vp->v_cleanblkhd);
823 blist = TAILQ_FIRST(&vp->v_dirtyblkhd);
827 for (bp = blist; bp; bp = nbp) {
828 nbp = TAILQ_NEXT(bp, b_vnbufs);
829 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
830 error = BUF_TIMELOCK(bp,
831 LK_EXCLUSIVE | LK_SLEEPFAIL,
832 "vinvalbuf", slpflag, slptimeo);
839 * XXX Since there are no node locks for NFS, I
840 * believe there is a slight chance that a delayed
841 * write will occur while sleeping just above, so
842 * check for it. Note that vfs_bio_awrite expects
843 * buffers to reside on a queue, while VOP_BWRITE and
846 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
849 if (bp->b_vp == vp) {
850 if (bp->b_flags & B_CLUSTEROK) {
855 bp->b_flags |= B_ASYNC;
856 VOP_BWRITE(bp->b_vp, bp);
860 (void) VOP_BWRITE(bp->b_vp, bp);
865 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
866 bp->b_flags &= ~B_ASYNC;
872 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
873 * have write I/O in-progress but if there is a VM object then the
874 * VM object can also have read-I/O in-progress.
877 while (vp->v_numoutput > 0) {
878 vp->v_flag |= VBWAIT;
879 tsleep(&vp->v_numoutput, PVM, "vnvlbv", 0);
881 if (VOP_GETVOBJECT(vp, &object) == 0) {
882 while (object->paging_in_progress)
883 vm_object_pip_sleep(object, "vnvlbx");
885 } while (vp->v_numoutput > 0);
890 * Destroy the copy in the VM cache, too.
892 simple_lock(&vp->v_interlock);
893 if (VOP_GETVOBJECT(vp, &object) == 0) {
894 vm_object_page_remove(object, 0, 0,
895 (flags & V_SAVE) ? TRUE : FALSE);
897 simple_unlock(&vp->v_interlock);
899 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd) || !TAILQ_EMPTY(&vp->v_cleanblkhd))
900 panic("vinvalbuf: flush failed");
905 * Truncate a file's buffer and pages to a specified length. This
906 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
910 vtruncbuf(vp, cred, p, length, blksize)
911 register struct vnode *vp;
917 register struct buf *bp;
923 * Round up to the *next* lbn.
925 trunclbn = (length + blksize - 1) / blksize;
932 for (bp = TAILQ_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
933 nbp = TAILQ_NEXT(bp, b_vnbufs);
934 if (bp->b_lblkno >= trunclbn) {
935 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
936 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
940 bp->b_flags |= (B_INVAL | B_RELBUF);
941 bp->b_flags &= ~B_ASYNC;
946 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
948 (nbp->b_flags & B_DELWRI))) {
954 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
955 nbp = TAILQ_NEXT(bp, b_vnbufs);
956 if (bp->b_lblkno >= trunclbn) {
957 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
958 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
962 bp->b_flags |= (B_INVAL | B_RELBUF);
963 bp->b_flags &= ~B_ASYNC;
968 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
970 (nbp->b_flags & B_DELWRI) == 0)) {
979 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
980 nbp = TAILQ_NEXT(bp, b_vnbufs);
981 if ((bp->b_flags & B_DELWRI) && (bp->b_lblkno < 0)) {
982 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
983 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
987 if (bp->b_vp == vp) {
988 bp->b_flags |= B_ASYNC;
990 bp->b_flags &= ~B_ASYNC;
992 VOP_BWRITE(bp->b_vp, bp);
1000 while (vp->v_numoutput > 0) {
1001 vp->v_flag |= VBWAIT;
1002 tsleep(&vp->v_numoutput, PVM, "vbtrunc", 0);
1007 vnode_pager_setsize(vp, length);
1013 * Associate a buffer with a vnode.
1017 register struct vnode *vp;
1018 register struct buf *bp;
1022 KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
1026 bp->b_dev = vn_todev(vp);
1028 * Insert onto list for new vnode.
1031 bp->b_xflags |= BX_VNCLEAN;
1032 bp->b_xflags &= ~BX_VNDIRTY;
1033 TAILQ_INSERT_TAIL(&vp->v_cleanblkhd, bp, b_vnbufs);
1038 * Disassociate a buffer from a vnode.
1042 register struct buf *bp;
1045 struct buflists *listheadp;
1048 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1051 * Delete from old vnode list, if on one.
1055 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) {
1056 if (bp->b_xflags & BX_VNDIRTY)
1057 listheadp = &vp->v_dirtyblkhd;
1059 listheadp = &vp->v_cleanblkhd;
1060 TAILQ_REMOVE(listheadp, bp, b_vnbufs);
1061 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1063 if ((vp->v_flag & VONWORKLST) && TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
1064 vp->v_flag &= ~VONWORKLST;
1065 LIST_REMOVE(vp, v_synclist);
1068 bp->b_vp = (struct vnode *) 0;
1073 * The workitem queue.
1075 * It is useful to delay writes of file data and filesystem metadata
1076 * for tens of seconds so that quickly created and deleted files need
1077 * not waste disk bandwidth being created and removed. To realize this,
1078 * we append vnodes to a "workitem" queue. When running with a soft
1079 * updates implementation, most pending metadata dependencies should
1080 * not wait for more than a few seconds. Thus, mounted on block devices
1081 * are delayed only about a half the time that file data is delayed.
1082 * Similarly, directory updates are more critical, so are only delayed
1083 * about a third the time that file data is delayed. Thus, there are
1084 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
1085 * one each second (driven off the filesystem syncer process). The
1086 * syncer_delayno variable indicates the next queue that is to be processed.
1087 * Items that need to be processed soon are placed in this queue:
1089 * syncer_workitem_pending[syncer_delayno]
1091 * A delay of fifteen seconds is done by placing the request fifteen
1092 * entries later in the queue:
1094 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
1099 * Add an item to the syncer work queue.
1102 vn_syncer_add_to_worklist(struct vnode *vp, int delay)
1108 if (vp->v_flag & VONWORKLST) {
1109 LIST_REMOVE(vp, v_synclist);
1112 if (delay > syncer_maxdelay - 2)
1113 delay = syncer_maxdelay - 2;
1114 slot = (syncer_delayno + delay) & syncer_mask;
1116 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], vp, v_synclist);
1117 vp->v_flag |= VONWORKLST;
1121 struct thread *updatethread;
1122 static void sched_sync __P((void));
1123 static struct kproc_desc up_kp = {
1128 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp)
1131 * System filesystem synchronizer daemon.
1136 struct synclist *slp;
1140 struct thread *td = updatethread;
1141 struct proc *p = td->td_proc;
1143 EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc, td,
1147 kproc_suspend_loop(td);
1149 starttime = time_second;
1152 * Push files whose dirty time has expired. Be careful
1153 * of interrupt race on slp queue.
1156 slp = &syncer_workitem_pending[syncer_delayno];
1157 syncer_delayno += 1;
1158 if (syncer_delayno == syncer_maxdelay)
1162 while ((vp = LIST_FIRST(slp)) != NULL) {
1163 if (VOP_ISLOCKED(vp, NULL) == 0) {
1164 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1165 (void) VOP_FSYNC(vp, p->p_ucred, MNT_LAZY, p);
1166 VOP_UNLOCK(vp, 0, p);
1169 if (LIST_FIRST(slp) == vp) {
1171 * Note: v_tag VT_VFS vps can remain on the
1172 * worklist too with no dirty blocks, but
1173 * since sync_fsync() moves it to a different
1176 if (TAILQ_EMPTY(&vp->v_dirtyblkhd) &&
1177 !vn_isdisk(vp, NULL))
1178 panic("sched_sync: fsync failed vp %p tag %d", vp, vp->v_tag);
1180 * Put us back on the worklist. The worklist
1181 * routine will remove us from our current
1182 * position and then add us back in at a later
1185 vn_syncer_add_to_worklist(vp, syncdelay);
1191 * Do soft update processing.
1194 (*bioops.io_sync)(NULL);
1197 * The variable rushjob allows the kernel to speed up the
1198 * processing of the filesystem syncer process. A rushjob
1199 * value of N tells the filesystem syncer to process the next
1200 * N seconds worth of work on its queue ASAP. Currently rushjob
1201 * is used by the soft update code to speed up the filesystem
1202 * syncer process when the incore state is getting so far
1203 * ahead of the disk that the kernel memory pool is being
1204 * threatened with exhaustion.
1211 * If it has taken us less than a second to process the
1212 * current work, then wait. Otherwise start right over
1213 * again. We can still lose time if any single round
1214 * takes more than two seconds, but it does not really
1215 * matter as we are just trying to generally pace the
1216 * filesystem activity.
1218 if (time_second == starttime)
1219 tsleep(&lbolt, PPAUSE, "syncer", 0);
1224 * Request the syncer daemon to speed up its work.
1225 * We never push it to speed up more than half of its
1226 * normal turn time, otherwise it could take over the cpu.
1234 if (updatethread->td_proc->p_wchan == &lbolt) /* YYY */
1235 setrunnable(updatethread->td_proc);
1237 if (rushjob < syncdelay / 2) {
1239 stat_rush_requests += 1;
1246 * Associate a p-buffer with a vnode.
1248 * Also sets B_PAGING flag to indicate that vnode is not fully associated
1249 * with the buffer. i.e. the bp has not been linked into the vnode or
1254 register struct vnode *vp;
1255 register struct buf *bp;
1258 KASSERT(bp->b_vp == NULL, ("pbgetvp: not free"));
1261 bp->b_flags |= B_PAGING;
1262 bp->b_dev = vn_todev(vp);
1266 * Disassociate a p-buffer from a vnode.
1270 register struct buf *bp;
1273 KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL"));
1276 if (TAILQ_NEXT(bp, b_vnbufs) != NULL) {
1278 "relpbuf(): b_vp was probably reassignbuf()d %p %x",
1283 bp->b_vp = (struct vnode *) 0;
1284 bp->b_flags &= ~B_PAGING;
1288 pbreassignbuf(bp, newvp)
1290 struct vnode *newvp;
1292 if ((bp->b_flags & B_PAGING) == 0) {
1294 "pbreassignbuf() on non phys bp %p",
1302 * Reassign a buffer from one vnode to another.
1303 * Used to assign file specific control information
1304 * (indirect blocks) to the vnode to which they belong.
1307 reassignbuf(bp, newvp)
1308 register struct buf *bp;
1309 register struct vnode *newvp;
1311 struct buflists *listheadp;
1315 if (newvp == NULL) {
1316 printf("reassignbuf: NULL");
1322 * B_PAGING flagged buffers cannot be reassigned because their vp
1323 * is not fully linked in.
1325 if (bp->b_flags & B_PAGING)
1326 panic("cannot reassign paging buffer");
1330 * Delete from old vnode list, if on one.
1332 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) {
1333 if (bp->b_xflags & BX_VNDIRTY)
1334 listheadp = &bp->b_vp->v_dirtyblkhd;
1336 listheadp = &bp->b_vp->v_cleanblkhd;
1337 TAILQ_REMOVE(listheadp, bp, b_vnbufs);
1338 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1339 if (bp->b_vp != newvp) {
1341 bp->b_vp = NULL; /* for clarification */
1345 * If dirty, put on list of dirty buffers; otherwise insert onto list
1348 if (bp->b_flags & B_DELWRI) {
1351 listheadp = &newvp->v_dirtyblkhd;
1352 if ((newvp->v_flag & VONWORKLST) == 0) {
1353 switch (newvp->v_type) {
1359 if (newvp->v_specmountpoint != NULL) {
1367 vn_syncer_add_to_worklist(newvp, delay);
1369 bp->b_xflags |= BX_VNDIRTY;
1370 tbp = TAILQ_FIRST(listheadp);
1372 bp->b_lblkno == 0 ||
1373 (bp->b_lblkno > 0 && tbp->b_lblkno < 0) ||
1374 (bp->b_lblkno > 0 && bp->b_lblkno < tbp->b_lblkno)) {
1375 TAILQ_INSERT_HEAD(listheadp, bp, b_vnbufs);
1376 ++reassignbufsortgood;
1377 } else if (bp->b_lblkno < 0) {
1378 TAILQ_INSERT_TAIL(listheadp, bp, b_vnbufs);
1379 ++reassignbufsortgood;
1380 } else if (reassignbufmethod == 1) {
1382 * New sorting algorithm, only handle sequential case,
1383 * otherwise append to end (but before metadata)
1385 if ((tbp = gbincore(newvp, bp->b_lblkno - 1)) != NULL &&
1386 (tbp->b_xflags & BX_VNDIRTY)) {
1388 * Found the best place to insert the buffer
1390 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1391 ++reassignbufsortgood;
1394 * Missed, append to end, but before meta-data.
1395 * We know that the head buffer in the list is
1396 * not meta-data due to prior conditionals.
1398 * Indirect effects: NFS second stage write
1399 * tends to wind up here, giving maximum
1400 * distance between the unstable write and the
1403 tbp = TAILQ_LAST(listheadp, buflists);
1404 while (tbp && tbp->b_lblkno < 0)
1405 tbp = TAILQ_PREV(tbp, buflists, b_vnbufs);
1406 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1407 ++reassignbufsortbad;
1411 * Old sorting algorithm, scan queue and insert
1414 while ((ttbp = TAILQ_NEXT(tbp, b_vnbufs)) &&
1415 (ttbp->b_lblkno < bp->b_lblkno)) {
1419 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1422 bp->b_xflags |= BX_VNCLEAN;
1423 TAILQ_INSERT_TAIL(&newvp->v_cleanblkhd, bp, b_vnbufs);
1424 if ((newvp->v_flag & VONWORKLST) &&
1425 TAILQ_EMPTY(&newvp->v_dirtyblkhd)) {
1426 newvp->v_flag &= ~VONWORKLST;
1427 LIST_REMOVE(newvp, v_synclist);
1430 if (bp->b_vp != newvp) {
1438 * Create a vnode for a block device.
1439 * Used for mounting the root file system.
1446 register struct vnode *vp;
1454 error = getnewvnode(VT_NON, (struct mount *)0, spec_vnodeop_p, &nvp);
1467 * Add vnode to the alias list hung off the dev_t.
1469 * The reason for this gunk is that multiple vnodes can reference
1470 * the same physical device, so checking vp->v_usecount to see
1471 * how many users there are is inadequate; the v_usecount for
1472 * the vnodes need to be accumulated. vcount() does that.
1475 addaliasu(nvp, nvp_rdev)
1480 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1481 panic("addaliasu on non-special vnode");
1482 addalias(nvp, udev2dev(nvp_rdev, nvp->v_type == VBLK ? 1 : 0));
1491 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1492 panic("addalias on non-special vnode");
1495 simple_lock(&spechash_slock);
1496 SLIST_INSERT_HEAD(&dev->si_hlist, nvp, v_specnext);
1497 simple_unlock(&spechash_slock);
1501 * Grab a particular vnode from the free list, increment its
1502 * reference count and lock it. The vnode lock bit is set if the
1503 * vnode is being eliminated in vgone. The process is awakened
1504 * when the transition is completed, and an error returned to
1505 * indicate that the vnode is no longer usable (possibly having
1506 * been changed to a new file system type).
1510 register struct vnode *vp;
1517 * If the vnode is in the process of being cleaned out for
1518 * another use, we wait for the cleaning to finish and then
1519 * return failure. Cleaning is determined by checking that
1520 * the VXLOCK flag is set.
1522 if ((flags & LK_INTERLOCK) == 0) {
1523 simple_lock(&vp->v_interlock);
1525 if (vp->v_flag & VXLOCK) {
1526 if (vp->v_vxproc == curproc) {
1528 /* this can now occur in normal operation */
1529 log(LOG_INFO, "VXLOCK interlock avoided\n");
1532 vp->v_flag |= VXWANT;
1533 simple_unlock(&vp->v_interlock);
1534 tsleep((caddr_t)vp, PINOD, "vget", 0);
1541 if (VSHOULDBUSY(vp))
1543 if (flags & LK_TYPE_MASK) {
1544 if ((error = vn_lock(vp, flags | LK_INTERLOCK, p)) != 0) {
1546 * must expand vrele here because we do not want
1547 * to call VOP_INACTIVE if the reference count
1548 * drops back to zero since it was never really
1549 * active. We must remove it from the free list
1550 * before sleeping so that multiple processes do
1551 * not try to recycle it.
1553 simple_lock(&vp->v_interlock);
1555 if (VSHOULDFREE(vp))
1559 simple_unlock(&vp->v_interlock);
1563 simple_unlock(&vp->v_interlock);
1568 vref(struct vnode *vp)
1570 simple_lock(&vp->v_interlock);
1572 simple_unlock(&vp->v_interlock);
1576 * Vnode put/release.
1577 * If count drops to zero, call inactive routine and return to freelist.
1583 struct proc *p = curproc; /* XXX */
1585 KASSERT(vp != NULL, ("vrele: null vp"));
1587 simple_lock(&vp->v_interlock);
1589 if (vp->v_usecount > 1) {
1592 simple_unlock(&vp->v_interlock);
1597 if (vp->v_usecount == 1) {
1600 * We must call VOP_INACTIVE with the node locked.
1601 * If we are doing a vpu, the node is already locked,
1602 * but, in the case of vrele, we must explicitly lock
1603 * the vnode before calling VOP_INACTIVE
1606 if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK, p) == 0)
1607 VOP_INACTIVE(vp, p);
1608 if (VSHOULDFREE(vp))
1614 vprint("vrele: negative ref count", vp);
1615 simple_unlock(&vp->v_interlock);
1617 panic("vrele: negative ref cnt");
1625 struct proc *p = curproc; /* XXX */
1627 KASSERT(vp != NULL, ("vput: null vp"));
1629 simple_lock(&vp->v_interlock);
1631 if (vp->v_usecount > 1) {
1633 VOP_UNLOCK(vp, LK_INTERLOCK, p);
1637 if (vp->v_usecount == 1) {
1640 * We must call VOP_INACTIVE with the node locked.
1641 * If we are doing a vpu, the node is already locked,
1642 * so we just need to release the vnode mutex.
1644 simple_unlock(&vp->v_interlock);
1645 VOP_INACTIVE(vp, p);
1646 if (VSHOULDFREE(vp))
1652 vprint("vput: negative ref count", vp);
1654 panic("vput: negative ref cnt");
1659 * Somebody doesn't want the vnode recycled.
1663 register struct vnode *vp;
1669 if (VSHOULDBUSY(vp))
1675 * One less who cares about this vnode.
1679 register struct vnode *vp;
1684 if (vp->v_holdcnt <= 0)
1685 panic("vdrop: holdcnt");
1687 if (VSHOULDFREE(vp))
1693 * Remove any vnodes in the vnode table belonging to mount point mp.
1695 * If FORCECLOSE is not specified, there should not be any active ones,
1696 * return error if any are found (nb: this is a user error, not a
1697 * system error). If FORCECLOSE is specified, detach any active vnodes
1700 * If WRITECLOSE is set, only flush out regular file vnodes open for
1703 * SKIPSYSTEM causes any vnodes marked VSYSTEM to be skipped.
1705 * `rootrefs' specifies the base reference count for the root vnode
1706 * of this filesystem. The root vnode is considered busy if its
1707 * v_usecount exceeds this value. On a successful return, vflush()
1708 * will call vrele() on the root vnode exactly rootrefs times.
1709 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
1713 static int busyprt = 0; /* print out busy vnodes */
1714 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "");
1718 vflush(mp, rootrefs, flags)
1723 struct proc *p = curproc; /* XXX */
1724 struct vnode *vp, *nvp, *rootvp = NULL;
1726 int busy = 0, error;
1729 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
1730 ("vflush: bad args"));
1732 * Get the filesystem root vnode. We can vput() it
1733 * immediately, since with rootrefs > 0, it won't go away.
1735 if ((error = VFS_ROOT(mp, &rootvp)) != 0)
1739 simple_lock(&mntvnode_slock);
1741 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp; vp = nvp) {
1743 * Make sure this vnode wasn't reclaimed in getnewvnode().
1744 * Start over if it has (it won't be on the list anymore).
1746 if (vp->v_mount != mp)
1748 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
1750 simple_lock(&vp->v_interlock);
1752 * Skip over a vnodes marked VSYSTEM.
1754 if ((flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM)) {
1755 simple_unlock(&vp->v_interlock);
1759 * If WRITECLOSE is set, flush out unlinked but still open
1760 * files (even if open only for reading) and regular file
1761 * vnodes open for writing.
1763 if ((flags & WRITECLOSE) &&
1764 (vp->v_type == VNON ||
1765 (VOP_GETATTR(vp, &vattr, p->p_ucred, p) == 0 &&
1766 vattr.va_nlink > 0)) &&
1767 (vp->v_writecount == 0 || vp->v_type != VREG)) {
1768 simple_unlock(&vp->v_interlock);
1773 * With v_usecount == 0, all we need to do is clear out the
1774 * vnode data structures and we are done.
1776 if (vp->v_usecount == 0) {
1777 simple_unlock(&mntvnode_slock);
1779 simple_lock(&mntvnode_slock);
1784 * If FORCECLOSE is set, forcibly close the vnode. For block
1785 * or character devices, revert to an anonymous device. For
1786 * all other files, just kill them.
1788 if (flags & FORCECLOSE) {
1789 simple_unlock(&mntvnode_slock);
1790 if (vp->v_type != VBLK && vp->v_type != VCHR) {
1794 vp->v_op = spec_vnodeop_p;
1795 insmntque(vp, (struct mount *) 0);
1797 simple_lock(&mntvnode_slock);
1802 vprint("vflush: busy vnode", vp);
1804 simple_unlock(&vp->v_interlock);
1807 simple_unlock(&mntvnode_slock);
1808 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
1810 * If just the root vnode is busy, and if its refcount
1811 * is equal to `rootrefs', then go ahead and kill it.
1813 simple_lock(&rootvp->v_interlock);
1814 KASSERT(busy > 0, ("vflush: not busy"));
1815 KASSERT(rootvp->v_usecount >= rootrefs, ("vflush: rootrefs"));
1816 if (busy == 1 && rootvp->v_usecount == rootrefs) {
1820 simple_unlock(&rootvp->v_interlock);
1824 for (; rootrefs > 0; rootrefs--)
1830 * We do not want to recycle the vnode too quickly.
1832 * XXX we can't move vp's around the nvnodelist without really screwing
1833 * up the efficiency of filesystem SYNC and friends. This code is
1834 * disabled until we fix the syncing code's scanning algorithm.
1837 vlruvp(struct vnode *vp)
1842 if ((mp = vp->v_mount) != NULL) {
1843 simple_lock(&mntvnode_slock);
1844 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1845 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1846 simple_unlock(&mntvnode_slock);
1852 * Disassociate the underlying file system from a vnode.
1855 vclean(vp, flags, p)
1863 * Check to see if the vnode is in use. If so we have to reference it
1864 * before we clean it out so that its count cannot fall to zero and
1865 * generate a race against ourselves to recycle it.
1867 if ((active = vp->v_usecount))
1871 * Prevent the vnode from being recycled or brought into use while we
1874 if (vp->v_flag & VXLOCK)
1875 panic("vclean: deadlock");
1876 vp->v_flag |= VXLOCK;
1877 vp->v_vxproc = curproc;
1879 * Even if the count is zero, the VOP_INACTIVE routine may still
1880 * have the object locked while it cleans it out. The VOP_LOCK
1881 * ensures that the VOP_INACTIVE routine is done with its work.
1882 * For active vnodes, it ensures that no other activity can
1883 * occur while the underlying object is being cleaned out.
1885 VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK, p);
1888 * Clean out any buffers associated with the vnode.
1890 vinvalbuf(vp, V_SAVE, NOCRED, p, 0, 0);
1892 VOP_DESTROYVOBJECT(vp);
1895 * If purging an active vnode, it must be closed and
1896 * deactivated before being reclaimed. Note that the
1897 * VOP_INACTIVE will unlock the vnode.
1900 if (flags & DOCLOSE)
1901 VOP_CLOSE(vp, FNONBLOCK, NOCRED, p);
1902 VOP_INACTIVE(vp, p);
1905 * Any other processes trying to obtain this lock must first
1906 * wait for VXLOCK to clear, then call the new lock operation.
1908 VOP_UNLOCK(vp, 0, p);
1911 * Reclaim the vnode.
1913 if (VOP_RECLAIM(vp, p))
1914 panic("vclean: cannot reclaim");
1918 * Inline copy of vrele() since VOP_INACTIVE
1919 * has already been called.
1921 simple_lock(&vp->v_interlock);
1922 if (--vp->v_usecount <= 0) {
1924 if (vp->v_usecount < 0 || vp->v_writecount != 0) {
1925 vprint("vclean: bad ref count", vp);
1926 panic("vclean: ref cnt");
1931 simple_unlock(&vp->v_interlock);
1935 vp->v_vnlock = NULL;
1937 if (VSHOULDFREE(vp))
1941 * Done with purge, notify sleepers of the grim news.
1943 vp->v_op = dead_vnodeop_p;
1946 vp->v_flag &= ~VXLOCK;
1947 vp->v_vxproc = NULL;
1948 if (vp->v_flag & VXWANT) {
1949 vp->v_flag &= ~VXWANT;
1950 wakeup((caddr_t) vp);
1955 * Eliminate all activity associated with the requested vnode
1956 * and with all vnodes aliased to the requested vnode.
1960 struct vop_revoke_args /* {
1965 struct vnode *vp, *vq;
1968 KASSERT((ap->a_flags & REVOKEALL) != 0, ("vop_revoke"));
1972 * If a vgone (or vclean) is already in progress,
1973 * wait until it is done and return.
1975 if (vp->v_flag & VXLOCK) {
1976 vp->v_flag |= VXWANT;
1977 simple_unlock(&vp->v_interlock);
1978 tsleep((caddr_t)vp, PINOD, "vop_revokeall", 0);
1983 simple_lock(&spechash_slock);
1984 vq = SLIST_FIRST(&dev->si_hlist);
1985 simple_unlock(&spechash_slock);
1994 * Recycle an unused vnode to the front of the free list.
1995 * Release the passed interlock if the vnode will be recycled.
1998 vrecycle(vp, inter_lkp, p)
2000 struct simplelock *inter_lkp;
2004 simple_lock(&vp->v_interlock);
2005 if (vp->v_usecount == 0) {
2007 simple_unlock(inter_lkp);
2012 simple_unlock(&vp->v_interlock);
2017 * Eliminate all activity associated with a vnode
2018 * in preparation for reuse.
2022 register struct vnode *vp;
2024 struct proc *p = curproc; /* XXX */
2026 simple_lock(&vp->v_interlock);
2031 * vgone, with the vp interlock held.
2041 * If a vgone (or vclean) is already in progress,
2042 * wait until it is done and return.
2044 if (vp->v_flag & VXLOCK) {
2045 vp->v_flag |= VXWANT;
2046 simple_unlock(&vp->v_interlock);
2047 tsleep((caddr_t)vp, PINOD, "vgone", 0);
2052 * Clean out the filesystem specific data.
2054 vclean(vp, DOCLOSE, p);
2055 simple_lock(&vp->v_interlock);
2058 * Delete from old mount point vnode list, if on one.
2060 if (vp->v_mount != NULL)
2061 insmntque(vp, (struct mount *)0);
2063 * If special device, remove it from special device alias list
2066 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
2067 simple_lock(&spechash_slock);
2068 SLIST_REMOVE(&vp->v_hashchain, vp, vnode, v_specnext);
2069 freedev(vp->v_rdev);
2070 simple_unlock(&spechash_slock);
2075 * If it is on the freelist and not already at the head,
2076 * move it to the head of the list. The test of the
2077 * VDOOMED flag and the reference count of zero is because
2078 * it will be removed from the free list by getnewvnode,
2079 * but will not have its reference count incremented until
2080 * after calling vgone. If the reference count were
2081 * incremented first, vgone would (incorrectly) try to
2082 * close the previous instance of the underlying object.
2084 if (vp->v_usecount == 0 && !(vp->v_flag & VDOOMED)) {
2086 simple_lock(&vnode_free_list_slock);
2087 if (vp->v_flag & VFREE)
2088 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
2091 vp->v_flag |= VFREE;
2092 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2093 simple_unlock(&vnode_free_list_slock);
2098 simple_unlock(&vp->v_interlock);
2102 * Lookup a vnode by device number.
2105 vfinddev(dev, type, vpp)
2112 simple_lock(&spechash_slock);
2113 SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) {
2114 if (type == vp->v_type) {
2116 simple_unlock(&spechash_slock);
2120 simple_unlock(&spechash_slock);
2125 * Calculate the total number of references to a special device.
2135 simple_lock(&spechash_slock);
2136 SLIST_FOREACH(vq, &vp->v_hashchain, v_specnext)
2137 count += vq->v_usecount;
2138 simple_unlock(&spechash_slock);
2143 * Same as above, but using the dev_t as argument
2152 vp = SLIST_FIRST(&dev->si_hlist);
2159 * Print out a description of a vnode.
2161 static char *typename[] =
2162 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
2172 printf("%s: %p: ", label, (void *)vp);
2174 printf("%p: ", (void *)vp);
2175 printf("type %s, usecount %d, writecount %d, refcount %d,",
2176 typename[vp->v_type], vp->v_usecount, vp->v_writecount,
2179 if (vp->v_flag & VROOT)
2180 strcat(buf, "|VROOT");
2181 if (vp->v_flag & VTEXT)
2182 strcat(buf, "|VTEXT");
2183 if (vp->v_flag & VSYSTEM)
2184 strcat(buf, "|VSYSTEM");
2185 if (vp->v_flag & VXLOCK)
2186 strcat(buf, "|VXLOCK");
2187 if (vp->v_flag & VXWANT)
2188 strcat(buf, "|VXWANT");
2189 if (vp->v_flag & VBWAIT)
2190 strcat(buf, "|VBWAIT");
2191 if (vp->v_flag & VDOOMED)
2192 strcat(buf, "|VDOOMED");
2193 if (vp->v_flag & VFREE)
2194 strcat(buf, "|VFREE");
2195 if (vp->v_flag & VOBJBUF)
2196 strcat(buf, "|VOBJBUF");
2198 printf(" flags (%s)", &buf[1]);
2199 if (vp->v_data == NULL) {
2208 #include <ddb/ddb.h>
2210 * List all of the locked vnodes in the system.
2211 * Called when debugging the kernel.
2213 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
2215 struct proc *p = curproc; /* XXX */
2216 struct mount *mp, *nmp;
2219 printf("Locked vnodes\n");
2220 simple_lock(&mountlist_slock);
2221 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2222 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
2223 nmp = TAILQ_NEXT(mp, mnt_list);
2226 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
2227 if (VOP_ISLOCKED(vp, NULL))
2228 vprint((char *)0, vp);
2230 simple_lock(&mountlist_slock);
2231 nmp = TAILQ_NEXT(mp, mnt_list);
2234 simple_unlock(&mountlist_slock);
2239 * Top level filesystem related information gathering.
2241 static int sysctl_ovfs_conf __P((SYSCTL_HANDLER_ARGS));
2244 vfs_sysctl(SYSCTL_HANDLER_ARGS)
2246 int *name = (int *)arg1 - 1; /* XXX */
2247 u_int namelen = arg2 + 1; /* XXX */
2248 struct vfsconf *vfsp;
2250 #if 1 || defined(COMPAT_PRELITE2)
2251 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
2253 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
2257 /* all sysctl names at this level are at least name and field */
2259 return (ENOTDIR); /* overloaded */
2260 if (name[0] != VFS_GENERIC) {
2261 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2262 if (vfsp->vfc_typenum == name[0])
2265 return (EOPNOTSUPP);
2266 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
2267 oldp, oldlenp, newp, newlen, p));
2271 case VFS_MAXTYPENUM:
2274 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
2277 return (ENOTDIR); /* overloaded */
2278 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2279 if (vfsp->vfc_typenum == name[2])
2282 return (EOPNOTSUPP);
2283 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
2285 return (EOPNOTSUPP);
2288 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
2289 "Generic filesystem");
2291 #if 1 || defined(COMPAT_PRELITE2)
2294 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
2297 struct vfsconf *vfsp;
2298 struct ovfsconf ovfs;
2300 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
2301 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
2302 strcpy(ovfs.vfc_name, vfsp->vfc_name);
2303 ovfs.vfc_index = vfsp->vfc_typenum;
2304 ovfs.vfc_refcount = vfsp->vfc_refcount;
2305 ovfs.vfc_flags = vfsp->vfc_flags;
2306 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
2313 #endif /* 1 || COMPAT_PRELITE2 */
2316 #define KINFO_VNODESLOP 10
2318 * Dump vnode list (via sysctl).
2319 * Copyout address of vnode followed by vnode.
2323 sysctl_vnode(SYSCTL_HANDLER_ARGS)
2325 struct proc *p = curproc; /* XXX */
2326 struct mount *mp, *nmp;
2327 struct vnode *nvp, *vp;
2330 #define VPTRSZ sizeof (struct vnode *)
2331 #define VNODESZ sizeof (struct vnode)
2334 if (!req->oldptr) /* Make an estimate */
2335 return (SYSCTL_OUT(req, 0,
2336 (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ)));
2338 simple_lock(&mountlist_slock);
2339 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2340 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
2341 nmp = TAILQ_NEXT(mp, mnt_list);
2345 simple_lock(&mntvnode_slock);
2346 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
2350 * Check that the vp is still associated with
2351 * this filesystem. RACE: could have been
2352 * recycled onto the same filesystem.
2354 if (vp->v_mount != mp) {
2355 simple_unlock(&mntvnode_slock);
2358 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
2359 simple_unlock(&mntvnode_slock);
2360 if ((error = SYSCTL_OUT(req, &vp, VPTRSZ)) ||
2361 (error = SYSCTL_OUT(req, vp, VNODESZ)))
2363 simple_lock(&mntvnode_slock);
2365 simple_unlock(&mntvnode_slock);
2366 simple_lock(&mountlist_slock);
2367 nmp = TAILQ_NEXT(mp, mnt_list);
2370 simple_unlock(&mountlist_slock);
2378 * Exporting the vnode list on large systems causes them to crash.
2379 * Exporting the vnode list on medium systems causes sysctl to coredump.
2382 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD,
2383 0, 0, sysctl_vnode, "S,vnode", "");
2387 * Check to see if a filesystem is mounted on a block device.
2394 if (vp->v_specmountpoint != NULL)
2400 * Unmount all filesystems. The list is traversed in reverse order
2401 * of mounting to avoid dependencies.
2410 if (curproc != NULL)
2413 p = initproc; /* XXX XXX should this be proc0? */
2415 * Since this only runs when rebooting, it is not interlocked.
2417 while(!TAILQ_EMPTY(&mountlist)) {
2418 mp = TAILQ_LAST(&mountlist, mntlist);
2419 error = dounmount(mp, MNT_FORCE, p);
2421 TAILQ_REMOVE(&mountlist, mp, mnt_list);
2422 printf("unmount of %s failed (",
2423 mp->mnt_stat.f_mntonname);
2427 printf("%d)\n", error);
2429 /* The unmount has removed mp from the mountlist */
2435 * Build hash lists of net addresses and hang them off the mount point.
2436 * Called by ufs_mount() to set up the lists of export addresses.
2439 vfs_hang_addrlist(mp, nep, argp)
2441 struct netexport *nep;
2442 struct export_args *argp;
2444 register struct netcred *np;
2445 register struct radix_node_head *rnh;
2447 struct radix_node *rn;
2448 struct sockaddr *saddr, *smask = 0;
2452 if (argp->ex_addrlen == 0) {
2453 if (mp->mnt_flag & MNT_DEFEXPORTED)
2455 np = &nep->ne_defexported;
2456 np->netc_exflags = argp->ex_flags;
2457 np->netc_anon = argp->ex_anon;
2458 np->netc_anon.cr_ref = 1;
2459 mp->mnt_flag |= MNT_DEFEXPORTED;
2463 if (argp->ex_addrlen > MLEN)
2466 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
2467 np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK);
2468 bzero((caddr_t) np, i);
2469 saddr = (struct sockaddr *) (np + 1);
2470 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
2472 if (saddr->sa_len > argp->ex_addrlen)
2473 saddr->sa_len = argp->ex_addrlen;
2474 if (argp->ex_masklen) {
2475 smask = (struct sockaddr *) ((caddr_t) saddr + argp->ex_addrlen);
2476 error = copyin(argp->ex_mask, (caddr_t) smask, argp->ex_masklen);
2479 if (smask->sa_len > argp->ex_masklen)
2480 smask->sa_len = argp->ex_masklen;
2482 i = saddr->sa_family;
2483 if ((rnh = nep->ne_rtable[i]) == 0) {
2485 * Seems silly to initialize every AF when most are not used,
2486 * do so on demand here
2488 for (dom = domains; dom; dom = dom->dom_next)
2489 if (dom->dom_family == i && dom->dom_rtattach) {
2490 dom->dom_rtattach((void **) &nep->ne_rtable[i],
2494 if ((rnh = nep->ne_rtable[i]) == 0) {
2499 rn = (*rnh->rnh_addaddr) ((caddr_t) saddr, (caddr_t) smask, rnh,
2501 if (rn == 0 || np != (struct netcred *) rn) { /* already exists */
2505 np->netc_exflags = argp->ex_flags;
2506 np->netc_anon = argp->ex_anon;
2507 np->netc_anon.cr_ref = 1;
2510 free(np, M_NETADDR);
2516 vfs_free_netcred(rn, w)
2517 struct radix_node *rn;
2520 register struct radix_node_head *rnh = (struct radix_node_head *) w;
2522 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
2523 free((caddr_t) rn, M_NETADDR);
2528 * Free the net address hash lists that are hanging off the mount points.
2531 vfs_free_addrlist(nep)
2532 struct netexport *nep;
2535 register struct radix_node_head *rnh;
2537 for (i = 0; i <= AF_MAX; i++)
2538 if ((rnh = nep->ne_rtable[i])) {
2539 (*rnh->rnh_walktree) (rnh, vfs_free_netcred,
2541 free((caddr_t) rnh, M_RTABLE);
2542 nep->ne_rtable[i] = 0;
2547 vfs_export(mp, nep, argp)
2549 struct netexport *nep;
2550 struct export_args *argp;
2554 if (argp->ex_flags & MNT_DELEXPORT) {
2555 if (mp->mnt_flag & MNT_EXPUBLIC) {
2556 vfs_setpublicfs(NULL, NULL, NULL);
2557 mp->mnt_flag &= ~MNT_EXPUBLIC;
2559 vfs_free_addrlist(nep);
2560 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
2562 if (argp->ex_flags & MNT_EXPORTED) {
2563 if (argp->ex_flags & MNT_EXPUBLIC) {
2564 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
2566 mp->mnt_flag |= MNT_EXPUBLIC;
2568 if ((error = vfs_hang_addrlist(mp, nep, argp)))
2570 mp->mnt_flag |= MNT_EXPORTED;
2577 * Set the publicly exported filesystem (WebNFS). Currently, only
2578 * one public filesystem is possible in the spec (RFC 2054 and 2055)
2581 vfs_setpublicfs(mp, nep, argp)
2583 struct netexport *nep;
2584 struct export_args *argp;
2591 * mp == NULL -> invalidate the current info, the FS is
2592 * no longer exported. May be called from either vfs_export
2593 * or unmount, so check if it hasn't already been done.
2596 if (nfs_pub.np_valid) {
2597 nfs_pub.np_valid = 0;
2598 if (nfs_pub.np_index != NULL) {
2599 FREE(nfs_pub.np_index, M_TEMP);
2600 nfs_pub.np_index = NULL;
2607 * Only one allowed at a time.
2609 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
2613 * Get real filehandle for root of exported FS.
2615 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
2616 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
2618 if ((error = VFS_ROOT(mp, &rvp)))
2621 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
2627 * If an indexfile was specified, pull it in.
2629 if (argp->ex_indexfile != NULL) {
2630 MALLOC(nfs_pub.np_index, char *, MAXNAMLEN + 1, M_TEMP,
2632 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
2633 MAXNAMLEN, (size_t *)0);
2636 * Check for illegal filenames.
2638 for (cp = nfs_pub.np_index; *cp; cp++) {
2646 FREE(nfs_pub.np_index, M_TEMP);
2651 nfs_pub.np_mount = mp;
2652 nfs_pub.np_valid = 1;
2657 vfs_export_lookup(mp, nep, nam)
2658 register struct mount *mp;
2659 struct netexport *nep;
2660 struct sockaddr *nam;
2662 register struct netcred *np;
2663 register struct radix_node_head *rnh;
2664 struct sockaddr *saddr;
2667 if (mp->mnt_flag & MNT_EXPORTED) {
2669 * Lookup in the export list first.
2673 rnh = nep->ne_rtable[saddr->sa_family];
2675 np = (struct netcred *)
2676 (*rnh->rnh_matchaddr)((caddr_t)saddr,
2678 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
2683 * If no address match, use the default if it exists.
2685 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
2686 np = &nep->ne_defexported;
2692 * perform msync on all vnodes under a mount point
2693 * the mount point must be locked.
2696 vfs_msync(struct mount *mp, int flags)
2698 struct vnode *vp, *nvp;
2699 struct vm_object *obj;
2703 simple_lock(&mntvnode_slock);
2705 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nvp) {
2706 if (vp->v_mount != mp) {
2711 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
2713 if (vp->v_flag & VXLOCK) /* XXX: what if MNT_WAIT? */
2717 * There could be hundreds of thousands of vnodes, we cannot
2718 * afford to do anything heavy-weight until we have a fairly
2719 * good indication that there is something to do.
2721 if ((vp->v_flag & VOBJDIRTY) &&
2722 (flags == MNT_WAIT || VOP_ISLOCKED(vp, NULL) == 0)) {
2723 simple_unlock(&mntvnode_slock);
2725 LK_EXCLUSIVE | LK_RETRY | LK_NOOBJ, curproc)) {
2726 if (VOP_GETVOBJECT(vp, &obj) == 0) {
2727 vm_object_page_clean(obj, 0, 0, flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
2731 simple_lock(&mntvnode_slock);
2732 if (TAILQ_NEXT(vp, v_nmntvnodes) != nvp) {
2739 simple_unlock(&mntvnode_slock);
2743 * Create the VM object needed for VMIO and mmap support. This
2744 * is done for all VREG files in the system. Some filesystems might
2745 * afford the additional metadata buffering capability of the
2746 * VMIO code by making the device node be VMIO mode also.
2748 * vp must be locked when vfs_object_create is called.
2751 vfs_object_create(vp, p, cred)
2756 return (VOP_CREATEVOBJECT(vp, cred, p));
2766 simple_lock(&vnode_free_list_slock);
2767 KASSERT((vp->v_flag & VFREE) == 0, ("vnode already free"));
2768 if (vp->v_flag & VAGE) {
2769 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2771 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
2774 simple_unlock(&vnode_free_list_slock);
2775 vp->v_flag &= ~VAGE;
2776 vp->v_flag |= VFREE;
2787 simple_lock(&vnode_free_list_slock);
2788 KASSERT((vp->v_flag & VFREE) != 0, ("vnode not free"));
2789 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
2791 simple_unlock(&vnode_free_list_slock);
2792 vp->v_flag &= ~(VFREE|VAGE);
2797 * Record a process's interest in events which might happen to
2798 * a vnode. Because poll uses the historic select-style interface
2799 * internally, this routine serves as both the ``check for any
2800 * pending events'' and the ``record my interest in future events''
2801 * functions. (These are done together, while the lock is held,
2802 * to avoid race conditions.)
2805 vn_pollrecord(vp, p, events)
2810 simple_lock(&vp->v_pollinfo.vpi_lock);
2811 if (vp->v_pollinfo.vpi_revents & events) {
2813 * This leaves events we are not interested
2814 * in available for the other process which
2815 * which presumably had requested them
2816 * (otherwise they would never have been
2819 events &= vp->v_pollinfo.vpi_revents;
2820 vp->v_pollinfo.vpi_revents &= ~events;
2822 simple_unlock(&vp->v_pollinfo.vpi_lock);
2825 vp->v_pollinfo.vpi_events |= events;
2826 selrecord(p->p_thread, &vp->v_pollinfo.vpi_selinfo);
2827 simple_unlock(&vp->v_pollinfo.vpi_lock);
2832 * Note the occurrence of an event. If the VN_POLLEVENT macro is used,
2833 * it is possible for us to miss an event due to race conditions, but
2834 * that condition is expected to be rare, so for the moment it is the
2835 * preferred interface.
2838 vn_pollevent(vp, events)
2842 simple_lock(&vp->v_pollinfo.vpi_lock);
2843 if (vp->v_pollinfo.vpi_events & events) {
2845 * We clear vpi_events so that we don't
2846 * call selwakeup() twice if two events are
2847 * posted before the polling process(es) is
2848 * awakened. This also ensures that we take at
2849 * most one selwakeup() if the polling process
2850 * is no longer interested. However, it does
2851 * mean that only one event can be noticed at
2852 * a time. (Perhaps we should only clear those
2853 * event bits which we note?) XXX
2855 vp->v_pollinfo.vpi_events = 0; /* &= ~events ??? */
2856 vp->v_pollinfo.vpi_revents |= events;
2857 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2859 simple_unlock(&vp->v_pollinfo.vpi_lock);
2863 * Wake up anyone polling on vp because it is being revoked.
2864 * This depends on dead_poll() returning POLLHUP for correct
2871 simple_lock(&vp->v_pollinfo.vpi_lock);
2872 if (vp->v_pollinfo.vpi_events) {
2873 vp->v_pollinfo.vpi_events = 0;
2874 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2876 simple_unlock(&vp->v_pollinfo.vpi_lock);
2882 * Routine to create and manage a filesystem syncer vnode.
2884 #define sync_close ((int (*) __P((struct vop_close_args *)))nullop)
2885 static int sync_fsync __P((struct vop_fsync_args *));
2886 static int sync_inactive __P((struct vop_inactive_args *));
2887 static int sync_reclaim __P((struct vop_reclaim_args *));
2888 #define sync_lock ((int (*) __P((struct vop_lock_args *)))vop_nolock)
2889 #define sync_unlock ((int (*) __P((struct vop_unlock_args *)))vop_nounlock)
2890 static int sync_print __P((struct vop_print_args *));
2891 #define sync_islocked ((int(*) __P((struct vop_islocked_args *)))vop_noislocked)
2893 static vop_t **sync_vnodeop_p;
2894 static struct vnodeopv_entry_desc sync_vnodeop_entries[] = {
2895 { &vop_default_desc, (vop_t *) vop_eopnotsupp },
2896 { &vop_close_desc, (vop_t *) sync_close }, /* close */
2897 { &vop_fsync_desc, (vop_t *) sync_fsync }, /* fsync */
2898 { &vop_inactive_desc, (vop_t *) sync_inactive }, /* inactive */
2899 { &vop_reclaim_desc, (vop_t *) sync_reclaim }, /* reclaim */
2900 { &vop_lock_desc, (vop_t *) sync_lock }, /* lock */
2901 { &vop_unlock_desc, (vop_t *) sync_unlock }, /* unlock */
2902 { &vop_print_desc, (vop_t *) sync_print }, /* print */
2903 { &vop_islocked_desc, (vop_t *) sync_islocked }, /* islocked */
2906 static struct vnodeopv_desc sync_vnodeop_opv_desc =
2907 { &sync_vnodeop_p, sync_vnodeop_entries };
2909 VNODEOP_SET(sync_vnodeop_opv_desc);
2912 * Create a new filesystem syncer vnode for the specified mount point.
2915 vfs_allocate_syncvnode(mp)
2919 static long start, incr, next;
2922 /* Allocate a new vnode */
2923 if ((error = getnewvnode(VT_VFS, mp, sync_vnodeop_p, &vp)) != 0) {
2924 mp->mnt_syncer = NULL;
2929 * Place the vnode onto the syncer worklist. We attempt to
2930 * scatter them about on the list so that they will go off
2931 * at evenly distributed times even if all the filesystems
2932 * are mounted at once.
2935 if (next == 0 || next > syncer_maxdelay) {
2939 start = syncer_maxdelay / 2;
2940 incr = syncer_maxdelay;
2944 vn_syncer_add_to_worklist(vp, syncdelay > 0 ? next % syncdelay : 0);
2945 mp->mnt_syncer = vp;
2950 * Do a lazy sync of the filesystem.
2954 struct vop_fsync_args /* {
2956 struct ucred *a_cred;
2961 struct vnode *syncvp = ap->a_vp;
2962 struct mount *mp = syncvp->v_mount;
2963 struct proc *p = ap->a_p;
2967 * We only need to do something if this is a lazy evaluation.
2969 if (ap->a_waitfor != MNT_LAZY)
2973 * Move ourselves to the back of the sync list.
2975 vn_syncer_add_to_worklist(syncvp, syncdelay);
2978 * Walk the list of vnodes pushing all that are dirty and
2979 * not already on the sync list.
2981 simple_lock(&mountlist_slock);
2982 if (vfs_busy(mp, LK_EXCLUSIVE | LK_NOWAIT, &mountlist_slock, p) != 0) {
2983 simple_unlock(&mountlist_slock);
2986 asyncflag = mp->mnt_flag & MNT_ASYNC;
2987 mp->mnt_flag &= ~MNT_ASYNC;
2988 vfs_msync(mp, MNT_NOWAIT);
2989 VFS_SYNC(mp, MNT_LAZY, ap->a_cred, p);
2991 mp->mnt_flag |= MNT_ASYNC;
2997 * The syncer vnode is no referenced.
3001 struct vop_inactive_args /* {
3012 * The syncer vnode is no longer needed and is being decommissioned.
3014 * Modifications to the worklist must be protected at splbio().
3018 struct vop_reclaim_args /* {
3022 struct vnode *vp = ap->a_vp;
3026 vp->v_mount->mnt_syncer = NULL;
3027 if (vp->v_flag & VONWORKLST) {
3028 LIST_REMOVE(vp, v_synclist);
3029 vp->v_flag &= ~VONWORKLST;
3037 * Print out a syncer vnode.
3041 struct vop_print_args /* {
3045 struct vnode *vp = ap->a_vp;
3047 printf("syncer vnode");
3048 if (vp->v_vnlock != NULL)
3049 lockmgr_printinfo(vp->v_vnlock);
3055 * extract the dev_t from a VBLK or VCHR
3061 if (vp->v_type != VBLK && vp->v_type != VCHR)
3063 return (vp->v_rdev);
3067 * Check if vnode represents a disk device
3074 if (vp->v_type != VBLK && vp->v_type != VCHR) {
3079 if (vp->v_rdev == NULL) {
3084 if (!devsw(vp->v_rdev)) {
3089 if (!(devsw(vp->v_rdev)->d_flags & D_DISK)) {
3101 struct nameidata *ndp;
3104 if (!(flags & NDF_NO_FREE_PNBUF) &&
3105 (ndp->ni_cnd.cn_flags & HASBUF)) {
3106 zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
3107 ndp->ni_cnd.cn_flags &= ~HASBUF;
3109 if (!(flags & NDF_NO_DVP_UNLOCK) &&
3110 (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
3111 ndp->ni_dvp != ndp->ni_vp)
3112 VOP_UNLOCK(ndp->ni_dvp, 0, ndp->ni_cnd.cn_proc);
3113 if (!(flags & NDF_NO_DVP_RELE) &&
3114 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
3118 if (!(flags & NDF_NO_VP_UNLOCK) &&
3119 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
3120 VOP_UNLOCK(ndp->ni_vp, 0, ndp->ni_cnd.cn_proc);
3121 if (!(flags & NDF_NO_VP_RELE) &&
3126 if (!(flags & NDF_NO_STARTDIR_RELE) &&
3127 (ndp->ni_cnd.cn_flags & SAVESTART)) {
3128 vrele(ndp->ni_startdir);
3129 ndp->ni_startdir = NULL;