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.2 2003/06/17 04:28:42 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>
83 static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
85 static void insmntque __P((struct vnode *vp, struct mount *mp));
86 static void vclean __P((struct vnode *vp, int flags, struct proc *p));
87 static unsigned long numvnodes;
88 static void vlruvp(struct vnode *vp);
89 SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, "");
91 enum vtype iftovt_tab[16] = {
92 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
93 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
96 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
97 S_IFSOCK, S_IFIFO, S_IFMT,
100 static TAILQ_HEAD(freelst, vnode) vnode_free_list; /* vnode free list */
102 static u_long wantfreevnodes = 25;
103 SYSCTL_INT(_debug, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, "");
104 static u_long freevnodes = 0;
105 SYSCTL_INT(_debug, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0, "");
107 static int reassignbufcalls;
108 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0, "");
109 static int reassignbufloops;
110 SYSCTL_INT(_vfs, OID_AUTO, reassignbufloops, CTLFLAG_RW, &reassignbufloops, 0, "");
111 static int reassignbufsortgood;
112 SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortgood, CTLFLAG_RW, &reassignbufsortgood, 0, "");
113 static int reassignbufsortbad;
114 SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortbad, CTLFLAG_RW, &reassignbufsortbad, 0, "");
115 static int reassignbufmethod = 1;
116 SYSCTL_INT(_vfs, OID_AUTO, reassignbufmethod, CTLFLAG_RW, &reassignbufmethod, 0, "");
117 static int nameileafonly = 0;
118 SYSCTL_INT(_vfs, OID_AUTO, nameileafonly, CTLFLAG_RW, &nameileafonly, 0, "");
120 #ifdef ENABLE_VFS_IOOPT
122 SYSCTL_INT(_vfs, OID_AUTO, ioopt, CTLFLAG_RW, &vfs_ioopt, 0, "");
125 struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist); /* mounted fs */
126 struct simplelock mountlist_slock;
127 struct simplelock mntvnode_slock;
128 int nfs_mount_type = -1;
129 #ifndef NULL_SIMPLELOCKS
130 static struct simplelock mntid_slock;
131 static struct simplelock vnode_free_list_slock;
132 static struct simplelock spechash_slock;
134 struct nfs_public nfs_pub; /* publicly exported FS */
135 static vm_zone_t vnode_zone;
138 * The workitem queue.
140 #define SYNCER_MAXDELAY 32
141 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
142 time_t syncdelay = 30; /* max time to delay syncing data */
143 time_t filedelay = 30; /* time to delay syncing files */
144 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0, "");
145 time_t dirdelay = 29; /* time to delay syncing directories */
146 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0, "");
147 time_t metadelay = 28; /* time to delay syncing metadata */
148 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0, "");
149 static int rushjob; /* number of slots to run ASAP */
150 static int stat_rush_requests; /* number of times I/O speeded up */
151 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0, "");
153 static int syncer_delayno = 0;
154 static long syncer_mask;
155 LIST_HEAD(synclist, vnode);
156 static struct synclist *syncer_workitem_pending;
159 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
160 &desiredvnodes, 0, "Maximum number of vnodes");
161 static int minvnodes;
162 SYSCTL_INT(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
163 &minvnodes, 0, "Minimum number of vnodes");
164 static int vnlru_nowhere = 0;
165 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW, &vnlru_nowhere, 0,
166 "Number of times the vnlru process ran without success");
168 static void vfs_free_addrlist __P((struct netexport *nep));
169 static int vfs_free_netcred __P((struct radix_node *rn, void *w));
170 static int vfs_hang_addrlist __P((struct mount *mp, struct netexport *nep,
171 struct export_args *argp));
174 * Initialize the vnode management data structures.
180 desiredvnodes = maxproc + cnt.v_page_count / 4;
181 minvnodes = desiredvnodes / 4;
182 simple_lock_init(&mntvnode_slock);
183 simple_lock_init(&mntid_slock);
184 simple_lock_init(&spechash_slock);
185 TAILQ_INIT(&vnode_free_list);
186 simple_lock_init(&vnode_free_list_slock);
187 vnode_zone = zinit("VNODE", sizeof (struct vnode), 0, 0, 5);
189 * Initialize the filesystem syncer.
191 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE,
193 syncer_maxdelay = syncer_mask + 1;
197 * Mark a mount point as busy. Used to synchronize access and to delay
198 * unmounting. Interlock is not released on failure.
201 vfs_busy(mp, flags, interlkp, p)
204 struct simplelock *interlkp;
209 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
210 if (flags & LK_NOWAIT)
212 mp->mnt_kern_flag |= MNTK_MWAIT;
214 simple_unlock(interlkp);
217 * Since all busy locks are shared except the exclusive
218 * lock granted when unmounting, the only place that a
219 * wakeup needs to be done is at the release of the
220 * exclusive lock at the end of dounmount.
222 tsleep((caddr_t)mp, PVFS, "vfs_busy", 0);
224 simple_lock(interlkp);
228 lkflags = LK_SHARED | LK_NOPAUSE;
230 lkflags |= LK_INTERLOCK;
231 if (lockmgr(&mp->mnt_lock, lkflags, interlkp, p))
232 panic("vfs_busy: unexpected lock failure");
237 * Free a busy filesystem.
245 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, p);
249 * Lookup a filesystem type, and if found allocate and initialize
250 * a mount structure for it.
252 * Devname is usually updated by mount(8) after booting.
255 vfs_rootmountalloc(fstypename, devname, mpp)
260 struct proc *p = curproc; /* XXX */
261 struct vfsconf *vfsp;
264 if (fstypename == NULL)
266 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
267 if (!strcmp(vfsp->vfc_name, fstypename))
271 mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
272 bzero((char *)mp, (u_long)sizeof(struct mount));
273 lockinit(&mp->mnt_lock, PVFS, "vfslock", VLKTIMEOUT, LK_NOPAUSE);
274 (void)vfs_busy(mp, LK_NOWAIT, 0, p);
275 TAILQ_INIT(&mp->mnt_nvnodelist);
276 TAILQ_INIT(&mp->mnt_reservedvnlist);
277 mp->mnt_nvnodelistsize = 0;
279 mp->mnt_op = vfsp->vfc_vfsops;
280 mp->mnt_flag = MNT_RDONLY;
281 mp->mnt_vnodecovered = NULLVP;
282 vfsp->vfc_refcount++;
283 mp->mnt_iosize_max = DFLTPHYS;
284 mp->mnt_stat.f_type = vfsp->vfc_typenum;
285 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
286 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
287 mp->mnt_stat.f_mntonname[0] = '/';
288 mp->mnt_stat.f_mntonname[1] = 0;
289 (void) copystr(devname, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 0);
295 * Find an appropriate filesystem to use for the root. If a filesystem
296 * has not been preselected, walk through the list of known filesystems
297 * trying those that have mountroot routines, and try them until one
298 * works or we have tried them all.
300 #ifdef notdef /* XXX JH */
302 lite2_vfs_mountroot()
304 struct vfsconf *vfsp;
305 extern int (*lite2_mountroot) __P((void));
308 if (lite2_mountroot != NULL)
309 return ((*lite2_mountroot)());
310 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
311 if (vfsp->vfc_mountroot == NULL)
313 if ((error = (*vfsp->vfc_mountroot)()) == 0)
315 printf("%s_mountroot failed: %d\n", vfsp->vfc_name, error);
322 * Lookup a mount point by filesystem identifier.
328 register struct mount *mp;
330 simple_lock(&mountlist_slock);
331 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
332 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
333 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
334 simple_unlock(&mountlist_slock);
338 simple_unlock(&mountlist_slock);
339 return ((struct mount *) 0);
343 * Get a new unique fsid. Try to make its val[0] unique, since this value
344 * will be used to create fake device numbers for stat(). Also try (but
345 * not so hard) make its val[0] unique mod 2^16, since some emulators only
346 * support 16-bit device numbers. We end up with unique val[0]'s for the
347 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
349 * Keep in mind that several mounts may be running in parallel. Starting
350 * the search one past where the previous search terminated is both a
351 * micro-optimization and a defense against returning the same fsid to
358 static u_int16_t mntid_base;
362 simple_lock(&mntid_slock);
363 mtype = mp->mnt_vfc->vfc_typenum;
364 tfsid.val[1] = mtype;
365 mtype = (mtype & 0xFF) << 24;
367 tfsid.val[0] = makeudev(255,
368 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
370 if (vfs_getvfs(&tfsid) == NULL)
373 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
374 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
375 simple_unlock(&mntid_slock);
379 * Knob to control the precision of file timestamps:
381 * 0 = seconds only; nanoseconds zeroed.
382 * 1 = seconds and nanoseconds, accurate within 1/HZ.
383 * 2 = seconds and nanoseconds, truncated to microseconds.
384 * >=3 = seconds and nanoseconds, maximum precision.
386 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
388 static int timestamp_precision = TSP_SEC;
389 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
390 ×tamp_precision, 0, "");
393 * Get a current timestamp.
397 struct timespec *tsp;
401 switch (timestamp_precision) {
403 tsp->tv_sec = time_second;
411 TIMEVAL_TO_TIMESPEC(&tv, tsp);
421 * Set vnode attributes to VNOVAL
425 register struct vattr *vap;
429 vap->va_size = VNOVAL;
430 vap->va_bytes = VNOVAL;
431 vap->va_mode = VNOVAL;
432 vap->va_nlink = VNOVAL;
433 vap->va_uid = VNOVAL;
434 vap->va_gid = VNOVAL;
435 vap->va_fsid = VNOVAL;
436 vap->va_fileid = VNOVAL;
437 vap->va_blocksize = VNOVAL;
438 vap->va_rdev = VNOVAL;
439 vap->va_atime.tv_sec = VNOVAL;
440 vap->va_atime.tv_nsec = VNOVAL;
441 vap->va_mtime.tv_sec = VNOVAL;
442 vap->va_mtime.tv_nsec = VNOVAL;
443 vap->va_ctime.tv_sec = VNOVAL;
444 vap->va_ctime.tv_nsec = VNOVAL;
445 vap->va_flags = VNOVAL;
446 vap->va_gen = VNOVAL;
451 * This routine is called when we have too many vnodes. It attempts
452 * to free <count> vnodes and will potentially free vnodes that still
453 * have VM backing store (VM backing store is typically the cause
454 * of a vnode blowout so we want to do this). Therefore, this operation
455 * is not considered cheap.
457 * A number of conditions may prevent a vnode from being reclaimed.
458 * the buffer cache may have references on the vnode, a directory
459 * vnode may still have references due to the namei cache representing
460 * underlying files, or the vnode may be in active use. It is not
461 * desireable to reuse such vnodes. These conditions may cause the
462 * number of vnodes to reach some minimum value regardless of what
463 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
466 vlrureclaim(struct mount *mp)
475 * Calculate the trigger point, don't allow user
476 * screwups to blow us up. This prevents us from
477 * recycling vnodes with lots of resident pages. We
478 * aren't trying to free memory, we are trying to
481 usevnodes = desiredvnodes;
484 trigger = cnt.v_page_count * 2 / usevnodes;
487 simple_lock(&mntvnode_slock);
488 count = mp->mnt_nvnodelistsize / 10 + 1;
489 while (count && (vp = TAILQ_FIRST(&mp->mnt_nvnodelist)) != NULL) {
490 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
491 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
493 if (vp->v_type != VNON &&
494 vp->v_type != VBAD &&
495 VMIGHTFREE(vp) && /* critical path opt */
496 (vp->v_object == NULL || vp->v_object->resident_page_count < trigger) &&
497 simple_lock_try(&vp->v_interlock)
499 simple_unlock(&mntvnode_slock);
500 if (VMIGHTFREE(vp)) {
504 simple_unlock(&vp->v_interlock);
506 simple_lock(&mntvnode_slock);
510 simple_unlock(&mntvnode_slock);
515 * Attempt to recycle vnodes in a context that is always safe to block.
516 * Calling vlrurecycle() from the bowels of file system code has some
517 * interesting deadlock problems.
519 static struct proc *vnlruproc;
520 static int vnlruproc_sig;
525 struct mount *mp, *nmp;
528 struct proc *p = vnlruproc;
530 EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc, p,
535 kproc_suspend_loop(p);
536 if (numvnodes - freevnodes <= desiredvnodes * 9 / 10) {
538 wakeup(&vnlruproc_sig);
539 tsleep(vnlruproc, PVFS, "vlruwt", hz);
543 simple_lock(&mountlist_slock);
544 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
545 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
546 nmp = TAILQ_NEXT(mp, mnt_list);
549 done += vlrureclaim(mp);
550 simple_lock(&mountlist_slock);
551 nmp = TAILQ_NEXT(mp, mnt_list);
554 simple_unlock(&mountlist_slock);
557 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
563 static struct kproc_desc vnlru_kp = {
568 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &vnlru_kp)
571 * Routines having to do with the management of the vnode table.
573 extern vop_t **dead_vnodeop_p;
576 * Return the next vnode from the free list.
579 getnewvnode(tag, mp, vops, vpp)
586 struct proc *p = curproc; /* XXX */
587 struct vnode *vp = NULL;
593 * Try to reuse vnodes if we hit the max. This situation only
594 * occurs in certain large-memory (2G+) situations. We cannot
595 * attempt to directly reclaim vnodes due to nasty recursion
598 while (numvnodes - freevnodes > desiredvnodes) {
599 if (vnlruproc_sig == 0) {
600 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
603 tsleep(&vnlruproc_sig, PVFS, "vlruwk", hz);
608 * Attempt to reuse a vnode already on the free list, allocating
609 * a new vnode if we can't find one or if we have not reached a
610 * good minimum for good LRU performance.
612 simple_lock(&vnode_free_list_slock);
613 if (freevnodes >= wantfreevnodes && numvnodes >= minvnodes) {
616 for (count = 0; count < freevnodes; count++) {
617 vp = TAILQ_FIRST(&vnode_free_list);
618 if (vp == NULL || vp->v_usecount)
619 panic("getnewvnode: free vnode isn't");
621 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
622 if ((VOP_GETVOBJECT(vp, &object) == 0 &&
623 (object->resident_page_count || object->ref_count)) ||
624 !simple_lock_try(&vp->v_interlock)) {
625 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
629 if (LIST_FIRST(&vp->v_cache_src)) {
631 * note: nameileafonly sysctl is temporary,
632 * for debugging only, and will eventually be
635 if (nameileafonly > 0) {
637 * Do not reuse namei-cached directory
638 * vnodes that have cached
641 if (cache_leaf_test(vp) < 0) {
642 simple_unlock(&vp->v_interlock);
643 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
647 } else if (nameileafonly < 0 ||
648 vmiodirenable == 0) {
650 * Do not reuse namei-cached directory
651 * vnodes if nameileafonly is -1 or
652 * if VMIO backing for directories is
653 * turned off (otherwise we reuse them
656 simple_unlock(&vp->v_interlock);
657 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
667 vp->v_flag |= VDOOMED;
668 vp->v_flag &= ~VFREE;
670 simple_unlock(&vnode_free_list_slock);
673 if (vp->v_type != VBAD) {
676 simple_unlock(&vp->v_interlock);
684 panic("cleaned vnode isn't");
687 panic("Clean vnode has pending I/O's");
697 vp->v_writecount = 0; /* XXX */
699 simple_unlock(&vnode_free_list_slock);
700 vp = (struct vnode *) zalloc(vnode_zone);
701 bzero((char *) vp, sizeof *vp);
702 simple_lock_init(&vp->v_interlock);
705 LIST_INIT(&vp->v_cache_src);
706 TAILQ_INIT(&vp->v_cache_dst);
710 TAILQ_INIT(&vp->v_cleanblkhd);
711 TAILQ_INIT(&vp->v_dirtyblkhd);
721 vfs_object_create(vp, p, p->p_ucred);
726 * Move a vnode from one mount queue to another.
730 register struct vnode *vp;
731 register struct mount *mp;
734 simple_lock(&mntvnode_slock);
736 * Delete from old mount point vnode list, if on one.
738 if (vp->v_mount != NULL) {
739 KASSERT(vp->v_mount->mnt_nvnodelistsize > 0,
740 ("bad mount point vnode list size"));
741 TAILQ_REMOVE(&vp->v_mount->mnt_nvnodelist, vp, v_nmntvnodes);
742 vp->v_mount->mnt_nvnodelistsize--;
745 * Insert into list of vnodes for the new mount point, if available.
747 if ((vp->v_mount = mp) == NULL) {
748 simple_unlock(&mntvnode_slock);
751 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
752 mp->mnt_nvnodelistsize++;
753 simple_unlock(&mntvnode_slock);
757 * Update outstanding I/O count and do wakeup if requested.
761 register struct buf *bp;
763 register struct vnode *vp;
765 bp->b_flags &= ~B_WRITEINPROG;
766 if ((vp = bp->b_vp)) {
768 if (vp->v_numoutput < 0)
769 panic("vwakeup: neg numoutput");
770 if ((vp->v_numoutput == 0) && (vp->v_flag & VBWAIT)) {
771 vp->v_flag &= ~VBWAIT;
772 wakeup((caddr_t) &vp->v_numoutput);
778 * Flush out and invalidate all buffers associated with a vnode.
779 * Called with the underlying object locked.
782 vinvalbuf(vp, flags, cred, p, slpflag, slptimeo)
783 register struct vnode *vp;
787 int slpflag, slptimeo;
789 register struct buf *bp;
790 struct buf *nbp, *blist;
794 if (flags & V_SAVE) {
796 while (vp->v_numoutput) {
797 vp->v_flag |= VBWAIT;
798 error = tsleep((caddr_t)&vp->v_numoutput,
799 slpflag | (PRIBIO + 1), "vinvlbuf", slptimeo);
805 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
807 if ((error = VOP_FSYNC(vp, cred, MNT_WAIT, p)) != 0)
810 if (vp->v_numoutput > 0 ||
811 !TAILQ_EMPTY(&vp->v_dirtyblkhd))
812 panic("vinvalbuf: dirty bufs");
818 blist = TAILQ_FIRST(&vp->v_cleanblkhd);
820 blist = TAILQ_FIRST(&vp->v_dirtyblkhd);
824 for (bp = blist; bp; bp = nbp) {
825 nbp = TAILQ_NEXT(bp, b_vnbufs);
826 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
827 error = BUF_TIMELOCK(bp,
828 LK_EXCLUSIVE | LK_SLEEPFAIL,
829 "vinvalbuf", slpflag, slptimeo);
836 * XXX Since there are no node locks for NFS, I
837 * believe there is a slight chance that a delayed
838 * write will occur while sleeping just above, so
839 * check for it. Note that vfs_bio_awrite expects
840 * buffers to reside on a queue, while VOP_BWRITE and
843 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
846 if (bp->b_vp == vp) {
847 if (bp->b_flags & B_CLUSTEROK) {
852 bp->b_flags |= B_ASYNC;
853 VOP_BWRITE(bp->b_vp, bp);
857 (void) VOP_BWRITE(bp->b_vp, bp);
862 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
863 bp->b_flags &= ~B_ASYNC;
869 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
870 * have write I/O in-progress but if there is a VM object then the
871 * VM object can also have read-I/O in-progress.
874 while (vp->v_numoutput > 0) {
875 vp->v_flag |= VBWAIT;
876 tsleep(&vp->v_numoutput, PVM, "vnvlbv", 0);
878 if (VOP_GETVOBJECT(vp, &object) == 0) {
879 while (object->paging_in_progress)
880 vm_object_pip_sleep(object, "vnvlbx");
882 } while (vp->v_numoutput > 0);
887 * Destroy the copy in the VM cache, too.
889 simple_lock(&vp->v_interlock);
890 if (VOP_GETVOBJECT(vp, &object) == 0) {
891 vm_object_page_remove(object, 0, 0,
892 (flags & V_SAVE) ? TRUE : FALSE);
894 simple_unlock(&vp->v_interlock);
896 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd) || !TAILQ_EMPTY(&vp->v_cleanblkhd))
897 panic("vinvalbuf: flush failed");
902 * Truncate a file's buffer and pages to a specified length. This
903 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
907 vtruncbuf(vp, cred, p, length, blksize)
908 register struct vnode *vp;
914 register struct buf *bp;
920 * Round up to the *next* lbn.
922 trunclbn = (length + blksize - 1) / blksize;
929 for (bp = TAILQ_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
930 nbp = TAILQ_NEXT(bp, b_vnbufs);
931 if (bp->b_lblkno >= trunclbn) {
932 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
933 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
937 bp->b_flags |= (B_INVAL | B_RELBUF);
938 bp->b_flags &= ~B_ASYNC;
943 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
945 (nbp->b_flags & B_DELWRI))) {
951 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
952 nbp = TAILQ_NEXT(bp, b_vnbufs);
953 if (bp->b_lblkno >= trunclbn) {
954 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
955 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
959 bp->b_flags |= (B_INVAL | B_RELBUF);
960 bp->b_flags &= ~B_ASYNC;
965 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
967 (nbp->b_flags & B_DELWRI) == 0)) {
976 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
977 nbp = TAILQ_NEXT(bp, b_vnbufs);
978 if ((bp->b_flags & B_DELWRI) && (bp->b_lblkno < 0)) {
979 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
980 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
984 if (bp->b_vp == vp) {
985 bp->b_flags |= B_ASYNC;
987 bp->b_flags &= ~B_ASYNC;
989 VOP_BWRITE(bp->b_vp, bp);
997 while (vp->v_numoutput > 0) {
998 vp->v_flag |= VBWAIT;
999 tsleep(&vp->v_numoutput, PVM, "vbtrunc", 0);
1004 vnode_pager_setsize(vp, length);
1010 * Associate a buffer with a vnode.
1014 register struct vnode *vp;
1015 register struct buf *bp;
1019 KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
1023 bp->b_dev = vn_todev(vp);
1025 * Insert onto list for new vnode.
1028 bp->b_xflags |= BX_VNCLEAN;
1029 bp->b_xflags &= ~BX_VNDIRTY;
1030 TAILQ_INSERT_TAIL(&vp->v_cleanblkhd, bp, b_vnbufs);
1035 * Disassociate a buffer from a vnode.
1039 register struct buf *bp;
1042 struct buflists *listheadp;
1045 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1048 * Delete from old vnode list, if on one.
1052 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) {
1053 if (bp->b_xflags & BX_VNDIRTY)
1054 listheadp = &vp->v_dirtyblkhd;
1056 listheadp = &vp->v_cleanblkhd;
1057 TAILQ_REMOVE(listheadp, bp, b_vnbufs);
1058 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1060 if ((vp->v_flag & VONWORKLST) && TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
1061 vp->v_flag &= ~VONWORKLST;
1062 LIST_REMOVE(vp, v_synclist);
1065 bp->b_vp = (struct vnode *) 0;
1070 * The workitem queue.
1072 * It is useful to delay writes of file data and filesystem metadata
1073 * for tens of seconds so that quickly created and deleted files need
1074 * not waste disk bandwidth being created and removed. To realize this,
1075 * we append vnodes to a "workitem" queue. When running with a soft
1076 * updates implementation, most pending metadata dependencies should
1077 * not wait for more than a few seconds. Thus, mounted on block devices
1078 * are delayed only about a half the time that file data is delayed.
1079 * Similarly, directory updates are more critical, so are only delayed
1080 * about a third the time that file data is delayed. Thus, there are
1081 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
1082 * one each second (driven off the filesystem syncer process). The
1083 * syncer_delayno variable indicates the next queue that is to be processed.
1084 * Items that need to be processed soon are placed in this queue:
1086 * syncer_workitem_pending[syncer_delayno]
1088 * A delay of fifteen seconds is done by placing the request fifteen
1089 * entries later in the queue:
1091 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
1096 * Add an item to the syncer work queue.
1099 vn_syncer_add_to_worklist(struct vnode *vp, int delay)
1105 if (vp->v_flag & VONWORKLST) {
1106 LIST_REMOVE(vp, v_synclist);
1109 if (delay > syncer_maxdelay - 2)
1110 delay = syncer_maxdelay - 2;
1111 slot = (syncer_delayno + delay) & syncer_mask;
1113 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], vp, v_synclist);
1114 vp->v_flag |= VONWORKLST;
1118 struct proc *updateproc;
1119 static void sched_sync __P((void));
1120 static struct kproc_desc up_kp = {
1125 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp)
1128 * System filesystem synchronizer daemon.
1133 struct synclist *slp;
1137 struct proc *p = updateproc;
1139 EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc, p,
1143 kproc_suspend_loop(p);
1145 starttime = time_second;
1148 * Push files whose dirty time has expired. Be careful
1149 * of interrupt race on slp queue.
1152 slp = &syncer_workitem_pending[syncer_delayno];
1153 syncer_delayno += 1;
1154 if (syncer_delayno == syncer_maxdelay)
1158 while ((vp = LIST_FIRST(slp)) != NULL) {
1159 if (VOP_ISLOCKED(vp, NULL) == 0) {
1160 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1161 (void) VOP_FSYNC(vp, p->p_ucred, MNT_LAZY, p);
1162 VOP_UNLOCK(vp, 0, p);
1165 if (LIST_FIRST(slp) == vp) {
1167 * Note: v_tag VT_VFS vps can remain on the
1168 * worklist too with no dirty blocks, but
1169 * since sync_fsync() moves it to a different
1172 if (TAILQ_EMPTY(&vp->v_dirtyblkhd) &&
1173 !vn_isdisk(vp, NULL))
1174 panic("sched_sync: fsync failed vp %p tag %d", vp, vp->v_tag);
1176 * Put us back on the worklist. The worklist
1177 * routine will remove us from our current
1178 * position and then add us back in at a later
1181 vn_syncer_add_to_worklist(vp, syncdelay);
1187 * Do soft update processing.
1190 (*bioops.io_sync)(NULL);
1193 * The variable rushjob allows the kernel to speed up the
1194 * processing of the filesystem syncer process. A rushjob
1195 * value of N tells the filesystem syncer to process the next
1196 * N seconds worth of work on its queue ASAP. Currently rushjob
1197 * is used by the soft update code to speed up the filesystem
1198 * syncer process when the incore state is getting so far
1199 * ahead of the disk that the kernel memory pool is being
1200 * threatened with exhaustion.
1207 * If it has taken us less than a second to process the
1208 * current work, then wait. Otherwise start right over
1209 * again. We can still lose time if any single round
1210 * takes more than two seconds, but it does not really
1211 * matter as we are just trying to generally pace the
1212 * filesystem activity.
1214 if (time_second == starttime)
1215 tsleep(&lbolt, PPAUSE, "syncer", 0);
1220 * Request the syncer daemon to speed up its work.
1221 * We never push it to speed up more than half of its
1222 * normal turn time, otherwise it could take over the cpu.
1230 if (updateproc->p_wchan == &lbolt)
1231 setrunnable(updateproc);
1233 if (rushjob < syncdelay / 2) {
1235 stat_rush_requests += 1;
1242 * Associate a p-buffer with a vnode.
1244 * Also sets B_PAGING flag to indicate that vnode is not fully associated
1245 * with the buffer. i.e. the bp has not been linked into the vnode or
1250 register struct vnode *vp;
1251 register struct buf *bp;
1254 KASSERT(bp->b_vp == NULL, ("pbgetvp: not free"));
1257 bp->b_flags |= B_PAGING;
1258 bp->b_dev = vn_todev(vp);
1262 * Disassociate a p-buffer from a vnode.
1266 register struct buf *bp;
1269 KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL"));
1272 if (TAILQ_NEXT(bp, b_vnbufs) != NULL) {
1274 "relpbuf(): b_vp was probably reassignbuf()d %p %x",
1279 bp->b_vp = (struct vnode *) 0;
1280 bp->b_flags &= ~B_PAGING;
1284 pbreassignbuf(bp, newvp)
1286 struct vnode *newvp;
1288 if ((bp->b_flags & B_PAGING) == 0) {
1290 "pbreassignbuf() on non phys bp %p",
1298 * Reassign a buffer from one vnode to another.
1299 * Used to assign file specific control information
1300 * (indirect blocks) to the vnode to which they belong.
1303 reassignbuf(bp, newvp)
1304 register struct buf *bp;
1305 register struct vnode *newvp;
1307 struct buflists *listheadp;
1311 if (newvp == NULL) {
1312 printf("reassignbuf: NULL");
1318 * B_PAGING flagged buffers cannot be reassigned because their vp
1319 * is not fully linked in.
1321 if (bp->b_flags & B_PAGING)
1322 panic("cannot reassign paging buffer");
1326 * Delete from old vnode list, if on one.
1328 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) {
1329 if (bp->b_xflags & BX_VNDIRTY)
1330 listheadp = &bp->b_vp->v_dirtyblkhd;
1332 listheadp = &bp->b_vp->v_cleanblkhd;
1333 TAILQ_REMOVE(listheadp, bp, b_vnbufs);
1334 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1335 if (bp->b_vp != newvp) {
1337 bp->b_vp = NULL; /* for clarification */
1341 * If dirty, put on list of dirty buffers; otherwise insert onto list
1344 if (bp->b_flags & B_DELWRI) {
1347 listheadp = &newvp->v_dirtyblkhd;
1348 if ((newvp->v_flag & VONWORKLST) == 0) {
1349 switch (newvp->v_type) {
1355 if (newvp->v_specmountpoint != NULL) {
1363 vn_syncer_add_to_worklist(newvp, delay);
1365 bp->b_xflags |= BX_VNDIRTY;
1366 tbp = TAILQ_FIRST(listheadp);
1368 bp->b_lblkno == 0 ||
1369 (bp->b_lblkno > 0 && tbp->b_lblkno < 0) ||
1370 (bp->b_lblkno > 0 && bp->b_lblkno < tbp->b_lblkno)) {
1371 TAILQ_INSERT_HEAD(listheadp, bp, b_vnbufs);
1372 ++reassignbufsortgood;
1373 } else if (bp->b_lblkno < 0) {
1374 TAILQ_INSERT_TAIL(listheadp, bp, b_vnbufs);
1375 ++reassignbufsortgood;
1376 } else if (reassignbufmethod == 1) {
1378 * New sorting algorithm, only handle sequential case,
1379 * otherwise append to end (but before metadata)
1381 if ((tbp = gbincore(newvp, bp->b_lblkno - 1)) != NULL &&
1382 (tbp->b_xflags & BX_VNDIRTY)) {
1384 * Found the best place to insert the buffer
1386 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1387 ++reassignbufsortgood;
1390 * Missed, append to end, but before meta-data.
1391 * We know that the head buffer in the list is
1392 * not meta-data due to prior conditionals.
1394 * Indirect effects: NFS second stage write
1395 * tends to wind up here, giving maximum
1396 * distance between the unstable write and the
1399 tbp = TAILQ_LAST(listheadp, buflists);
1400 while (tbp && tbp->b_lblkno < 0)
1401 tbp = TAILQ_PREV(tbp, buflists, b_vnbufs);
1402 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1403 ++reassignbufsortbad;
1407 * Old sorting algorithm, scan queue and insert
1410 while ((ttbp = TAILQ_NEXT(tbp, b_vnbufs)) &&
1411 (ttbp->b_lblkno < bp->b_lblkno)) {
1415 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1418 bp->b_xflags |= BX_VNCLEAN;
1419 TAILQ_INSERT_TAIL(&newvp->v_cleanblkhd, bp, b_vnbufs);
1420 if ((newvp->v_flag & VONWORKLST) &&
1421 TAILQ_EMPTY(&newvp->v_dirtyblkhd)) {
1422 newvp->v_flag &= ~VONWORKLST;
1423 LIST_REMOVE(newvp, v_synclist);
1426 if (bp->b_vp != newvp) {
1434 * Create a vnode for a block device.
1435 * Used for mounting the root file system.
1442 register struct vnode *vp;
1450 error = getnewvnode(VT_NON, (struct mount *)0, spec_vnodeop_p, &nvp);
1463 * Add vnode to the alias list hung off the dev_t.
1465 * The reason for this gunk is that multiple vnodes can reference
1466 * the same physical device, so checking vp->v_usecount to see
1467 * how many users there are is inadequate; the v_usecount for
1468 * the vnodes need to be accumulated. vcount() does that.
1471 addaliasu(nvp, nvp_rdev)
1476 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1477 panic("addaliasu on non-special vnode");
1478 addalias(nvp, udev2dev(nvp_rdev, nvp->v_type == VBLK ? 1 : 0));
1487 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1488 panic("addalias on non-special vnode");
1491 simple_lock(&spechash_slock);
1492 SLIST_INSERT_HEAD(&dev->si_hlist, nvp, v_specnext);
1493 simple_unlock(&spechash_slock);
1497 * Grab a particular vnode from the free list, increment its
1498 * reference count and lock it. The vnode lock bit is set if the
1499 * vnode is being eliminated in vgone. The process is awakened
1500 * when the transition is completed, and an error returned to
1501 * indicate that the vnode is no longer usable (possibly having
1502 * been changed to a new file system type).
1506 register struct vnode *vp;
1513 * If the vnode is in the process of being cleaned out for
1514 * another use, we wait for the cleaning to finish and then
1515 * return failure. Cleaning is determined by checking that
1516 * the VXLOCK flag is set.
1518 if ((flags & LK_INTERLOCK) == 0) {
1519 simple_lock(&vp->v_interlock);
1521 if (vp->v_flag & VXLOCK) {
1522 if (vp->v_vxproc == curproc) {
1524 /* this can now occur in normal operation */
1525 log(LOG_INFO, "VXLOCK interlock avoided\n");
1528 vp->v_flag |= VXWANT;
1529 simple_unlock(&vp->v_interlock);
1530 tsleep((caddr_t)vp, PINOD, "vget", 0);
1537 if (VSHOULDBUSY(vp))
1539 if (flags & LK_TYPE_MASK) {
1540 if ((error = vn_lock(vp, flags | LK_INTERLOCK, p)) != 0) {
1542 * must expand vrele here because we do not want
1543 * to call VOP_INACTIVE if the reference count
1544 * drops back to zero since it was never really
1545 * active. We must remove it from the free list
1546 * before sleeping so that multiple processes do
1547 * not try to recycle it.
1549 simple_lock(&vp->v_interlock);
1551 if (VSHOULDFREE(vp))
1555 simple_unlock(&vp->v_interlock);
1559 simple_unlock(&vp->v_interlock);
1564 vref(struct vnode *vp)
1566 simple_lock(&vp->v_interlock);
1568 simple_unlock(&vp->v_interlock);
1572 * Vnode put/release.
1573 * If count drops to zero, call inactive routine and return to freelist.
1579 struct proc *p = curproc; /* XXX */
1581 KASSERT(vp != NULL, ("vrele: null vp"));
1583 simple_lock(&vp->v_interlock);
1585 if (vp->v_usecount > 1) {
1588 simple_unlock(&vp->v_interlock);
1593 if (vp->v_usecount == 1) {
1596 * We must call VOP_INACTIVE with the node locked.
1597 * If we are doing a vpu, the node is already locked,
1598 * but, in the case of vrele, we must explicitly lock
1599 * the vnode before calling VOP_INACTIVE
1602 if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK, p) == 0)
1603 VOP_INACTIVE(vp, p);
1604 if (VSHOULDFREE(vp))
1610 vprint("vrele: negative ref count", vp);
1611 simple_unlock(&vp->v_interlock);
1613 panic("vrele: negative ref cnt");
1621 struct proc *p = curproc; /* XXX */
1623 KASSERT(vp != NULL, ("vput: null vp"));
1625 simple_lock(&vp->v_interlock);
1627 if (vp->v_usecount > 1) {
1629 VOP_UNLOCK(vp, LK_INTERLOCK, p);
1633 if (vp->v_usecount == 1) {
1636 * We must call VOP_INACTIVE with the node locked.
1637 * If we are doing a vpu, the node is already locked,
1638 * so we just need to release the vnode mutex.
1640 simple_unlock(&vp->v_interlock);
1641 VOP_INACTIVE(vp, p);
1642 if (VSHOULDFREE(vp))
1648 vprint("vput: negative ref count", vp);
1650 panic("vput: negative ref cnt");
1655 * Somebody doesn't want the vnode recycled.
1659 register struct vnode *vp;
1665 if (VSHOULDBUSY(vp))
1671 * One less who cares about this vnode.
1675 register struct vnode *vp;
1680 if (vp->v_holdcnt <= 0)
1681 panic("vdrop: holdcnt");
1683 if (VSHOULDFREE(vp))
1689 * Remove any vnodes in the vnode table belonging to mount point mp.
1691 * If FORCECLOSE is not specified, there should not be any active ones,
1692 * return error if any are found (nb: this is a user error, not a
1693 * system error). If FORCECLOSE is specified, detach any active vnodes
1696 * If WRITECLOSE is set, only flush out regular file vnodes open for
1699 * SKIPSYSTEM causes any vnodes marked VSYSTEM to be skipped.
1701 * `rootrefs' specifies the base reference count for the root vnode
1702 * of this filesystem. The root vnode is considered busy if its
1703 * v_usecount exceeds this value. On a successful return, vflush()
1704 * will call vrele() on the root vnode exactly rootrefs times.
1705 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
1709 static int busyprt = 0; /* print out busy vnodes */
1710 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "");
1714 vflush(mp, rootrefs, flags)
1719 struct proc *p = curproc; /* XXX */
1720 struct vnode *vp, *nvp, *rootvp = NULL;
1722 int busy = 0, error;
1725 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
1726 ("vflush: bad args"));
1728 * Get the filesystem root vnode. We can vput() it
1729 * immediately, since with rootrefs > 0, it won't go away.
1731 if ((error = VFS_ROOT(mp, &rootvp)) != 0)
1735 simple_lock(&mntvnode_slock);
1737 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp; vp = nvp) {
1739 * Make sure this vnode wasn't reclaimed in getnewvnode().
1740 * Start over if it has (it won't be on the list anymore).
1742 if (vp->v_mount != mp)
1744 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
1746 simple_lock(&vp->v_interlock);
1748 * Skip over a vnodes marked VSYSTEM.
1750 if ((flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM)) {
1751 simple_unlock(&vp->v_interlock);
1755 * If WRITECLOSE is set, flush out unlinked but still open
1756 * files (even if open only for reading) and regular file
1757 * vnodes open for writing.
1759 if ((flags & WRITECLOSE) &&
1760 (vp->v_type == VNON ||
1761 (VOP_GETATTR(vp, &vattr, p->p_ucred, p) == 0 &&
1762 vattr.va_nlink > 0)) &&
1763 (vp->v_writecount == 0 || vp->v_type != VREG)) {
1764 simple_unlock(&vp->v_interlock);
1769 * With v_usecount == 0, all we need to do is clear out the
1770 * vnode data structures and we are done.
1772 if (vp->v_usecount == 0) {
1773 simple_unlock(&mntvnode_slock);
1775 simple_lock(&mntvnode_slock);
1780 * If FORCECLOSE is set, forcibly close the vnode. For block
1781 * or character devices, revert to an anonymous device. For
1782 * all other files, just kill them.
1784 if (flags & FORCECLOSE) {
1785 simple_unlock(&mntvnode_slock);
1786 if (vp->v_type != VBLK && vp->v_type != VCHR) {
1790 vp->v_op = spec_vnodeop_p;
1791 insmntque(vp, (struct mount *) 0);
1793 simple_lock(&mntvnode_slock);
1798 vprint("vflush: busy vnode", vp);
1800 simple_unlock(&vp->v_interlock);
1803 simple_unlock(&mntvnode_slock);
1804 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
1806 * If just the root vnode is busy, and if its refcount
1807 * is equal to `rootrefs', then go ahead and kill it.
1809 simple_lock(&rootvp->v_interlock);
1810 KASSERT(busy > 0, ("vflush: not busy"));
1811 KASSERT(rootvp->v_usecount >= rootrefs, ("vflush: rootrefs"));
1812 if (busy == 1 && rootvp->v_usecount == rootrefs) {
1816 simple_unlock(&rootvp->v_interlock);
1820 for (; rootrefs > 0; rootrefs--)
1826 * We do not want to recycle the vnode too quickly.
1828 * XXX we can't move vp's around the nvnodelist without really screwing
1829 * up the efficiency of filesystem SYNC and friends. This code is
1830 * disabled until we fix the syncing code's scanning algorithm.
1833 vlruvp(struct vnode *vp)
1838 if ((mp = vp->v_mount) != NULL) {
1839 simple_lock(&mntvnode_slock);
1840 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1841 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1842 simple_unlock(&mntvnode_slock);
1848 * Disassociate the underlying file system from a vnode.
1851 vclean(vp, flags, p)
1859 * Check to see if the vnode is in use. If so we have to reference it
1860 * before we clean it out so that its count cannot fall to zero and
1861 * generate a race against ourselves to recycle it.
1863 if ((active = vp->v_usecount))
1867 * Prevent the vnode from being recycled or brought into use while we
1870 if (vp->v_flag & VXLOCK)
1871 panic("vclean: deadlock");
1872 vp->v_flag |= VXLOCK;
1873 vp->v_vxproc = curproc;
1875 * Even if the count is zero, the VOP_INACTIVE routine may still
1876 * have the object locked while it cleans it out. The VOP_LOCK
1877 * ensures that the VOP_INACTIVE routine is done with its work.
1878 * For active vnodes, it ensures that no other activity can
1879 * occur while the underlying object is being cleaned out.
1881 VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK, p);
1884 * Clean out any buffers associated with the vnode.
1886 vinvalbuf(vp, V_SAVE, NOCRED, p, 0, 0);
1888 VOP_DESTROYVOBJECT(vp);
1891 * If purging an active vnode, it must be closed and
1892 * deactivated before being reclaimed. Note that the
1893 * VOP_INACTIVE will unlock the vnode.
1896 if (flags & DOCLOSE)
1897 VOP_CLOSE(vp, FNONBLOCK, NOCRED, p);
1898 VOP_INACTIVE(vp, p);
1901 * Any other processes trying to obtain this lock must first
1902 * wait for VXLOCK to clear, then call the new lock operation.
1904 VOP_UNLOCK(vp, 0, p);
1907 * Reclaim the vnode.
1909 if (VOP_RECLAIM(vp, p))
1910 panic("vclean: cannot reclaim");
1914 * Inline copy of vrele() since VOP_INACTIVE
1915 * has already been called.
1917 simple_lock(&vp->v_interlock);
1918 if (--vp->v_usecount <= 0) {
1920 if (vp->v_usecount < 0 || vp->v_writecount != 0) {
1921 vprint("vclean: bad ref count", vp);
1922 panic("vclean: ref cnt");
1927 simple_unlock(&vp->v_interlock);
1931 vp->v_vnlock = NULL;
1933 if (VSHOULDFREE(vp))
1937 * Done with purge, notify sleepers of the grim news.
1939 vp->v_op = dead_vnodeop_p;
1942 vp->v_flag &= ~VXLOCK;
1943 vp->v_vxproc = NULL;
1944 if (vp->v_flag & VXWANT) {
1945 vp->v_flag &= ~VXWANT;
1946 wakeup((caddr_t) vp);
1951 * Eliminate all activity associated with the requested vnode
1952 * and with all vnodes aliased to the requested vnode.
1956 struct vop_revoke_args /* {
1961 struct vnode *vp, *vq;
1964 KASSERT((ap->a_flags & REVOKEALL) != 0, ("vop_revoke"));
1968 * If a vgone (or vclean) is already in progress,
1969 * wait until it is done and return.
1971 if (vp->v_flag & VXLOCK) {
1972 vp->v_flag |= VXWANT;
1973 simple_unlock(&vp->v_interlock);
1974 tsleep((caddr_t)vp, PINOD, "vop_revokeall", 0);
1979 simple_lock(&spechash_slock);
1980 vq = SLIST_FIRST(&dev->si_hlist);
1981 simple_unlock(&spechash_slock);
1990 * Recycle an unused vnode to the front of the free list.
1991 * Release the passed interlock if the vnode will be recycled.
1994 vrecycle(vp, inter_lkp, p)
1996 struct simplelock *inter_lkp;
2000 simple_lock(&vp->v_interlock);
2001 if (vp->v_usecount == 0) {
2003 simple_unlock(inter_lkp);
2008 simple_unlock(&vp->v_interlock);
2013 * Eliminate all activity associated with a vnode
2014 * in preparation for reuse.
2018 register struct vnode *vp;
2020 struct proc *p = curproc; /* XXX */
2022 simple_lock(&vp->v_interlock);
2027 * vgone, with the vp interlock held.
2037 * If a vgone (or vclean) is already in progress,
2038 * wait until it is done and return.
2040 if (vp->v_flag & VXLOCK) {
2041 vp->v_flag |= VXWANT;
2042 simple_unlock(&vp->v_interlock);
2043 tsleep((caddr_t)vp, PINOD, "vgone", 0);
2048 * Clean out the filesystem specific data.
2050 vclean(vp, DOCLOSE, p);
2051 simple_lock(&vp->v_interlock);
2054 * Delete from old mount point vnode list, if on one.
2056 if (vp->v_mount != NULL)
2057 insmntque(vp, (struct mount *)0);
2059 * If special device, remove it from special device alias list
2062 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
2063 simple_lock(&spechash_slock);
2064 SLIST_REMOVE(&vp->v_hashchain, vp, vnode, v_specnext);
2065 freedev(vp->v_rdev);
2066 simple_unlock(&spechash_slock);
2071 * If it is on the freelist and not already at the head,
2072 * move it to the head of the list. The test of the
2073 * VDOOMED flag and the reference count of zero is because
2074 * it will be removed from the free list by getnewvnode,
2075 * but will not have its reference count incremented until
2076 * after calling vgone. If the reference count were
2077 * incremented first, vgone would (incorrectly) try to
2078 * close the previous instance of the underlying object.
2080 if (vp->v_usecount == 0 && !(vp->v_flag & VDOOMED)) {
2082 simple_lock(&vnode_free_list_slock);
2083 if (vp->v_flag & VFREE)
2084 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
2087 vp->v_flag |= VFREE;
2088 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2089 simple_unlock(&vnode_free_list_slock);
2094 simple_unlock(&vp->v_interlock);
2098 * Lookup a vnode by device number.
2101 vfinddev(dev, type, vpp)
2108 simple_lock(&spechash_slock);
2109 SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) {
2110 if (type == vp->v_type) {
2112 simple_unlock(&spechash_slock);
2116 simple_unlock(&spechash_slock);
2121 * Calculate the total number of references to a special device.
2131 simple_lock(&spechash_slock);
2132 SLIST_FOREACH(vq, &vp->v_hashchain, v_specnext)
2133 count += vq->v_usecount;
2134 simple_unlock(&spechash_slock);
2139 * Same as above, but using the dev_t as argument
2148 vp = SLIST_FIRST(&dev->si_hlist);
2155 * Print out a description of a vnode.
2157 static char *typename[] =
2158 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
2168 printf("%s: %p: ", label, (void *)vp);
2170 printf("%p: ", (void *)vp);
2171 printf("type %s, usecount %d, writecount %d, refcount %d,",
2172 typename[vp->v_type], vp->v_usecount, vp->v_writecount,
2175 if (vp->v_flag & VROOT)
2176 strcat(buf, "|VROOT");
2177 if (vp->v_flag & VTEXT)
2178 strcat(buf, "|VTEXT");
2179 if (vp->v_flag & VSYSTEM)
2180 strcat(buf, "|VSYSTEM");
2181 if (vp->v_flag & VXLOCK)
2182 strcat(buf, "|VXLOCK");
2183 if (vp->v_flag & VXWANT)
2184 strcat(buf, "|VXWANT");
2185 if (vp->v_flag & VBWAIT)
2186 strcat(buf, "|VBWAIT");
2187 if (vp->v_flag & VDOOMED)
2188 strcat(buf, "|VDOOMED");
2189 if (vp->v_flag & VFREE)
2190 strcat(buf, "|VFREE");
2191 if (vp->v_flag & VOBJBUF)
2192 strcat(buf, "|VOBJBUF");
2194 printf(" flags (%s)", &buf[1]);
2195 if (vp->v_data == NULL) {
2204 #include <ddb/ddb.h>
2206 * List all of the locked vnodes in the system.
2207 * Called when debugging the kernel.
2209 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
2211 struct proc *p = curproc; /* XXX */
2212 struct mount *mp, *nmp;
2215 printf("Locked vnodes\n");
2216 simple_lock(&mountlist_slock);
2217 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2218 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
2219 nmp = TAILQ_NEXT(mp, mnt_list);
2222 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
2223 if (VOP_ISLOCKED(vp, NULL))
2224 vprint((char *)0, vp);
2226 simple_lock(&mountlist_slock);
2227 nmp = TAILQ_NEXT(mp, mnt_list);
2230 simple_unlock(&mountlist_slock);
2235 * Top level filesystem related information gathering.
2237 static int sysctl_ovfs_conf __P((SYSCTL_HANDLER_ARGS));
2240 vfs_sysctl(SYSCTL_HANDLER_ARGS)
2242 int *name = (int *)arg1 - 1; /* XXX */
2243 u_int namelen = arg2 + 1; /* XXX */
2244 struct vfsconf *vfsp;
2246 #if 1 || defined(COMPAT_PRELITE2)
2247 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
2249 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
2253 /* all sysctl names at this level are at least name and field */
2255 return (ENOTDIR); /* overloaded */
2256 if (name[0] != VFS_GENERIC) {
2257 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2258 if (vfsp->vfc_typenum == name[0])
2261 return (EOPNOTSUPP);
2262 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
2263 oldp, oldlenp, newp, newlen, p));
2267 case VFS_MAXTYPENUM:
2270 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
2273 return (ENOTDIR); /* overloaded */
2274 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2275 if (vfsp->vfc_typenum == name[2])
2278 return (EOPNOTSUPP);
2279 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
2281 return (EOPNOTSUPP);
2284 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
2285 "Generic filesystem");
2287 #if 1 || defined(COMPAT_PRELITE2)
2290 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
2293 struct vfsconf *vfsp;
2294 struct ovfsconf ovfs;
2296 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
2297 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
2298 strcpy(ovfs.vfc_name, vfsp->vfc_name);
2299 ovfs.vfc_index = vfsp->vfc_typenum;
2300 ovfs.vfc_refcount = vfsp->vfc_refcount;
2301 ovfs.vfc_flags = vfsp->vfc_flags;
2302 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
2309 #endif /* 1 || COMPAT_PRELITE2 */
2312 #define KINFO_VNODESLOP 10
2314 * Dump vnode list (via sysctl).
2315 * Copyout address of vnode followed by vnode.
2319 sysctl_vnode(SYSCTL_HANDLER_ARGS)
2321 struct proc *p = curproc; /* XXX */
2322 struct mount *mp, *nmp;
2323 struct vnode *nvp, *vp;
2326 #define VPTRSZ sizeof (struct vnode *)
2327 #define VNODESZ sizeof (struct vnode)
2330 if (!req->oldptr) /* Make an estimate */
2331 return (SYSCTL_OUT(req, 0,
2332 (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ)));
2334 simple_lock(&mountlist_slock);
2335 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2336 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
2337 nmp = TAILQ_NEXT(mp, mnt_list);
2341 simple_lock(&mntvnode_slock);
2342 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
2346 * Check that the vp is still associated with
2347 * this filesystem. RACE: could have been
2348 * recycled onto the same filesystem.
2350 if (vp->v_mount != mp) {
2351 simple_unlock(&mntvnode_slock);
2354 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
2355 simple_unlock(&mntvnode_slock);
2356 if ((error = SYSCTL_OUT(req, &vp, VPTRSZ)) ||
2357 (error = SYSCTL_OUT(req, vp, VNODESZ)))
2359 simple_lock(&mntvnode_slock);
2361 simple_unlock(&mntvnode_slock);
2362 simple_lock(&mountlist_slock);
2363 nmp = TAILQ_NEXT(mp, mnt_list);
2366 simple_unlock(&mountlist_slock);
2374 * Exporting the vnode list on large systems causes them to crash.
2375 * Exporting the vnode list on medium systems causes sysctl to coredump.
2378 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD,
2379 0, 0, sysctl_vnode, "S,vnode", "");
2383 * Check to see if a filesystem is mounted on a block device.
2390 if (vp->v_specmountpoint != NULL)
2396 * Unmount all filesystems. The list is traversed in reverse order
2397 * of mounting to avoid dependencies.
2406 if (curproc != NULL)
2409 p = initproc; /* XXX XXX should this be proc0? */
2411 * Since this only runs when rebooting, it is not interlocked.
2413 while(!TAILQ_EMPTY(&mountlist)) {
2414 mp = TAILQ_LAST(&mountlist, mntlist);
2415 error = dounmount(mp, MNT_FORCE, p);
2417 TAILQ_REMOVE(&mountlist, mp, mnt_list);
2418 printf("unmount of %s failed (",
2419 mp->mnt_stat.f_mntonname);
2423 printf("%d)\n", error);
2425 /* The unmount has removed mp from the mountlist */
2431 * Build hash lists of net addresses and hang them off the mount point.
2432 * Called by ufs_mount() to set up the lists of export addresses.
2435 vfs_hang_addrlist(mp, nep, argp)
2437 struct netexport *nep;
2438 struct export_args *argp;
2440 register struct netcred *np;
2441 register struct radix_node_head *rnh;
2443 struct radix_node *rn;
2444 struct sockaddr *saddr, *smask = 0;
2448 if (argp->ex_addrlen == 0) {
2449 if (mp->mnt_flag & MNT_DEFEXPORTED)
2451 np = &nep->ne_defexported;
2452 np->netc_exflags = argp->ex_flags;
2453 np->netc_anon = argp->ex_anon;
2454 np->netc_anon.cr_ref = 1;
2455 mp->mnt_flag |= MNT_DEFEXPORTED;
2459 if (argp->ex_addrlen > MLEN)
2462 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
2463 np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK);
2464 bzero((caddr_t) np, i);
2465 saddr = (struct sockaddr *) (np + 1);
2466 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
2468 if (saddr->sa_len > argp->ex_addrlen)
2469 saddr->sa_len = argp->ex_addrlen;
2470 if (argp->ex_masklen) {
2471 smask = (struct sockaddr *) ((caddr_t) saddr + argp->ex_addrlen);
2472 error = copyin(argp->ex_mask, (caddr_t) smask, argp->ex_masklen);
2475 if (smask->sa_len > argp->ex_masklen)
2476 smask->sa_len = argp->ex_masklen;
2478 i = saddr->sa_family;
2479 if ((rnh = nep->ne_rtable[i]) == 0) {
2481 * Seems silly to initialize every AF when most are not used,
2482 * do so on demand here
2484 for (dom = domains; dom; dom = dom->dom_next)
2485 if (dom->dom_family == i && dom->dom_rtattach) {
2486 dom->dom_rtattach((void **) &nep->ne_rtable[i],
2490 if ((rnh = nep->ne_rtable[i]) == 0) {
2495 rn = (*rnh->rnh_addaddr) ((caddr_t) saddr, (caddr_t) smask, rnh,
2497 if (rn == 0 || np != (struct netcred *) rn) { /* already exists */
2501 np->netc_exflags = argp->ex_flags;
2502 np->netc_anon = argp->ex_anon;
2503 np->netc_anon.cr_ref = 1;
2506 free(np, M_NETADDR);
2512 vfs_free_netcred(rn, w)
2513 struct radix_node *rn;
2516 register struct radix_node_head *rnh = (struct radix_node_head *) w;
2518 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
2519 free((caddr_t) rn, M_NETADDR);
2524 * Free the net address hash lists that are hanging off the mount points.
2527 vfs_free_addrlist(nep)
2528 struct netexport *nep;
2531 register struct radix_node_head *rnh;
2533 for (i = 0; i <= AF_MAX; i++)
2534 if ((rnh = nep->ne_rtable[i])) {
2535 (*rnh->rnh_walktree) (rnh, vfs_free_netcred,
2537 free((caddr_t) rnh, M_RTABLE);
2538 nep->ne_rtable[i] = 0;
2543 vfs_export(mp, nep, argp)
2545 struct netexport *nep;
2546 struct export_args *argp;
2550 if (argp->ex_flags & MNT_DELEXPORT) {
2551 if (mp->mnt_flag & MNT_EXPUBLIC) {
2552 vfs_setpublicfs(NULL, NULL, NULL);
2553 mp->mnt_flag &= ~MNT_EXPUBLIC;
2555 vfs_free_addrlist(nep);
2556 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
2558 if (argp->ex_flags & MNT_EXPORTED) {
2559 if (argp->ex_flags & MNT_EXPUBLIC) {
2560 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
2562 mp->mnt_flag |= MNT_EXPUBLIC;
2564 if ((error = vfs_hang_addrlist(mp, nep, argp)))
2566 mp->mnt_flag |= MNT_EXPORTED;
2573 * Set the publicly exported filesystem (WebNFS). Currently, only
2574 * one public filesystem is possible in the spec (RFC 2054 and 2055)
2577 vfs_setpublicfs(mp, nep, argp)
2579 struct netexport *nep;
2580 struct export_args *argp;
2587 * mp == NULL -> invalidate the current info, the FS is
2588 * no longer exported. May be called from either vfs_export
2589 * or unmount, so check if it hasn't already been done.
2592 if (nfs_pub.np_valid) {
2593 nfs_pub.np_valid = 0;
2594 if (nfs_pub.np_index != NULL) {
2595 FREE(nfs_pub.np_index, M_TEMP);
2596 nfs_pub.np_index = NULL;
2603 * Only one allowed at a time.
2605 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
2609 * Get real filehandle for root of exported FS.
2611 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
2612 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
2614 if ((error = VFS_ROOT(mp, &rvp)))
2617 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
2623 * If an indexfile was specified, pull it in.
2625 if (argp->ex_indexfile != NULL) {
2626 MALLOC(nfs_pub.np_index, char *, MAXNAMLEN + 1, M_TEMP,
2628 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
2629 MAXNAMLEN, (size_t *)0);
2632 * Check for illegal filenames.
2634 for (cp = nfs_pub.np_index; *cp; cp++) {
2642 FREE(nfs_pub.np_index, M_TEMP);
2647 nfs_pub.np_mount = mp;
2648 nfs_pub.np_valid = 1;
2653 vfs_export_lookup(mp, nep, nam)
2654 register struct mount *mp;
2655 struct netexport *nep;
2656 struct sockaddr *nam;
2658 register struct netcred *np;
2659 register struct radix_node_head *rnh;
2660 struct sockaddr *saddr;
2663 if (mp->mnt_flag & MNT_EXPORTED) {
2665 * Lookup in the export list first.
2669 rnh = nep->ne_rtable[saddr->sa_family];
2671 np = (struct netcred *)
2672 (*rnh->rnh_matchaddr)((caddr_t)saddr,
2674 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
2679 * If no address match, use the default if it exists.
2681 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
2682 np = &nep->ne_defexported;
2688 * perform msync on all vnodes under a mount point
2689 * the mount point must be locked.
2692 vfs_msync(struct mount *mp, int flags)
2694 struct vnode *vp, *nvp;
2695 struct vm_object *obj;
2699 simple_lock(&mntvnode_slock);
2701 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nvp) {
2702 if (vp->v_mount != mp) {
2707 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
2709 if (vp->v_flag & VXLOCK) /* XXX: what if MNT_WAIT? */
2713 * There could be hundreds of thousands of vnodes, we cannot
2714 * afford to do anything heavy-weight until we have a fairly
2715 * good indication that there is something to do.
2717 if ((vp->v_flag & VOBJDIRTY) &&
2718 (flags == MNT_WAIT || VOP_ISLOCKED(vp, NULL) == 0)) {
2719 simple_unlock(&mntvnode_slock);
2721 LK_EXCLUSIVE | LK_RETRY | LK_NOOBJ, curproc)) {
2722 if (VOP_GETVOBJECT(vp, &obj) == 0) {
2723 vm_object_page_clean(obj, 0, 0, flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
2727 simple_lock(&mntvnode_slock);
2728 if (TAILQ_NEXT(vp, v_nmntvnodes) != nvp) {
2735 simple_unlock(&mntvnode_slock);
2739 * Create the VM object needed for VMIO and mmap support. This
2740 * is done for all VREG files in the system. Some filesystems might
2741 * afford the additional metadata buffering capability of the
2742 * VMIO code by making the device node be VMIO mode also.
2744 * vp must be locked when vfs_object_create is called.
2747 vfs_object_create(vp, p, cred)
2752 return (VOP_CREATEVOBJECT(vp, cred, p));
2762 simple_lock(&vnode_free_list_slock);
2763 KASSERT((vp->v_flag & VFREE) == 0, ("vnode already free"));
2764 if (vp->v_flag & VAGE) {
2765 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2767 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
2770 simple_unlock(&vnode_free_list_slock);
2771 vp->v_flag &= ~VAGE;
2772 vp->v_flag |= VFREE;
2783 simple_lock(&vnode_free_list_slock);
2784 KASSERT((vp->v_flag & VFREE) != 0, ("vnode not free"));
2785 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
2787 simple_unlock(&vnode_free_list_slock);
2788 vp->v_flag &= ~(VFREE|VAGE);
2793 * Record a process's interest in events which might happen to
2794 * a vnode. Because poll uses the historic select-style interface
2795 * internally, this routine serves as both the ``check for any
2796 * pending events'' and the ``record my interest in future events''
2797 * functions. (These are done together, while the lock is held,
2798 * to avoid race conditions.)
2801 vn_pollrecord(vp, p, events)
2806 simple_lock(&vp->v_pollinfo.vpi_lock);
2807 if (vp->v_pollinfo.vpi_revents & events) {
2809 * This leaves events we are not interested
2810 * in available for the other process which
2811 * which presumably had requested them
2812 * (otherwise they would never have been
2815 events &= vp->v_pollinfo.vpi_revents;
2816 vp->v_pollinfo.vpi_revents &= ~events;
2818 simple_unlock(&vp->v_pollinfo.vpi_lock);
2821 vp->v_pollinfo.vpi_events |= events;
2822 selrecord(p, &vp->v_pollinfo.vpi_selinfo);
2823 simple_unlock(&vp->v_pollinfo.vpi_lock);
2828 * Note the occurrence of an event. If the VN_POLLEVENT macro is used,
2829 * it is possible for us to miss an event due to race conditions, but
2830 * that condition is expected to be rare, so for the moment it is the
2831 * preferred interface.
2834 vn_pollevent(vp, events)
2838 simple_lock(&vp->v_pollinfo.vpi_lock);
2839 if (vp->v_pollinfo.vpi_events & events) {
2841 * We clear vpi_events so that we don't
2842 * call selwakeup() twice if two events are
2843 * posted before the polling process(es) is
2844 * awakened. This also ensures that we take at
2845 * most one selwakeup() if the polling process
2846 * is no longer interested. However, it does
2847 * mean that only one event can be noticed at
2848 * a time. (Perhaps we should only clear those
2849 * event bits which we note?) XXX
2851 vp->v_pollinfo.vpi_events = 0; /* &= ~events ??? */
2852 vp->v_pollinfo.vpi_revents |= events;
2853 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2855 simple_unlock(&vp->v_pollinfo.vpi_lock);
2859 * Wake up anyone polling on vp because it is being revoked.
2860 * This depends on dead_poll() returning POLLHUP for correct
2867 simple_lock(&vp->v_pollinfo.vpi_lock);
2868 if (vp->v_pollinfo.vpi_events) {
2869 vp->v_pollinfo.vpi_events = 0;
2870 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2872 simple_unlock(&vp->v_pollinfo.vpi_lock);
2878 * Routine to create and manage a filesystem syncer vnode.
2880 #define sync_close ((int (*) __P((struct vop_close_args *)))nullop)
2881 static int sync_fsync __P((struct vop_fsync_args *));
2882 static int sync_inactive __P((struct vop_inactive_args *));
2883 static int sync_reclaim __P((struct vop_reclaim_args *));
2884 #define sync_lock ((int (*) __P((struct vop_lock_args *)))vop_nolock)
2885 #define sync_unlock ((int (*) __P((struct vop_unlock_args *)))vop_nounlock)
2886 static int sync_print __P((struct vop_print_args *));
2887 #define sync_islocked ((int(*) __P((struct vop_islocked_args *)))vop_noislocked)
2889 static vop_t **sync_vnodeop_p;
2890 static struct vnodeopv_entry_desc sync_vnodeop_entries[] = {
2891 { &vop_default_desc, (vop_t *) vop_eopnotsupp },
2892 { &vop_close_desc, (vop_t *) sync_close }, /* close */
2893 { &vop_fsync_desc, (vop_t *) sync_fsync }, /* fsync */
2894 { &vop_inactive_desc, (vop_t *) sync_inactive }, /* inactive */
2895 { &vop_reclaim_desc, (vop_t *) sync_reclaim }, /* reclaim */
2896 { &vop_lock_desc, (vop_t *) sync_lock }, /* lock */
2897 { &vop_unlock_desc, (vop_t *) sync_unlock }, /* unlock */
2898 { &vop_print_desc, (vop_t *) sync_print }, /* print */
2899 { &vop_islocked_desc, (vop_t *) sync_islocked }, /* islocked */
2902 static struct vnodeopv_desc sync_vnodeop_opv_desc =
2903 { &sync_vnodeop_p, sync_vnodeop_entries };
2905 VNODEOP_SET(sync_vnodeop_opv_desc);
2908 * Create a new filesystem syncer vnode for the specified mount point.
2911 vfs_allocate_syncvnode(mp)
2915 static long start, incr, next;
2918 /* Allocate a new vnode */
2919 if ((error = getnewvnode(VT_VFS, mp, sync_vnodeop_p, &vp)) != 0) {
2920 mp->mnt_syncer = NULL;
2925 * Place the vnode onto the syncer worklist. We attempt to
2926 * scatter them about on the list so that they will go off
2927 * at evenly distributed times even if all the filesystems
2928 * are mounted at once.
2931 if (next == 0 || next > syncer_maxdelay) {
2935 start = syncer_maxdelay / 2;
2936 incr = syncer_maxdelay;
2940 vn_syncer_add_to_worklist(vp, syncdelay > 0 ? next % syncdelay : 0);
2941 mp->mnt_syncer = vp;
2946 * Do a lazy sync of the filesystem.
2950 struct vop_fsync_args /* {
2952 struct ucred *a_cred;
2957 struct vnode *syncvp = ap->a_vp;
2958 struct mount *mp = syncvp->v_mount;
2959 struct proc *p = ap->a_p;
2963 * We only need to do something if this is a lazy evaluation.
2965 if (ap->a_waitfor != MNT_LAZY)
2969 * Move ourselves to the back of the sync list.
2971 vn_syncer_add_to_worklist(syncvp, syncdelay);
2974 * Walk the list of vnodes pushing all that are dirty and
2975 * not already on the sync list.
2977 simple_lock(&mountlist_slock);
2978 if (vfs_busy(mp, LK_EXCLUSIVE | LK_NOWAIT, &mountlist_slock, p) != 0) {
2979 simple_unlock(&mountlist_slock);
2982 asyncflag = mp->mnt_flag & MNT_ASYNC;
2983 mp->mnt_flag &= ~MNT_ASYNC;
2984 vfs_msync(mp, MNT_NOWAIT);
2985 VFS_SYNC(mp, MNT_LAZY, ap->a_cred, p);
2987 mp->mnt_flag |= MNT_ASYNC;
2993 * The syncer vnode is no referenced.
2997 struct vop_inactive_args /* {
3008 * The syncer vnode is no longer needed and is being decommissioned.
3010 * Modifications to the worklist must be protected at splbio().
3014 struct vop_reclaim_args /* {
3018 struct vnode *vp = ap->a_vp;
3022 vp->v_mount->mnt_syncer = NULL;
3023 if (vp->v_flag & VONWORKLST) {
3024 LIST_REMOVE(vp, v_synclist);
3025 vp->v_flag &= ~VONWORKLST;
3033 * Print out a syncer vnode.
3037 struct vop_print_args /* {
3041 struct vnode *vp = ap->a_vp;
3043 printf("syncer vnode");
3044 if (vp->v_vnlock != NULL)
3045 lockmgr_printinfo(vp->v_vnlock);
3051 * extract the dev_t from a VBLK or VCHR
3057 if (vp->v_type != VBLK && vp->v_type != VCHR)
3059 return (vp->v_rdev);
3063 * Check if vnode represents a disk device
3070 if (vp->v_type != VBLK && vp->v_type != VCHR) {
3075 if (vp->v_rdev == NULL) {
3080 if (!devsw(vp->v_rdev)) {
3085 if (!(devsw(vp->v_rdev)->d_flags & D_DISK)) {
3097 struct nameidata *ndp;
3100 if (!(flags & NDF_NO_FREE_PNBUF) &&
3101 (ndp->ni_cnd.cn_flags & HASBUF)) {
3102 zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
3103 ndp->ni_cnd.cn_flags &= ~HASBUF;
3105 if (!(flags & NDF_NO_DVP_UNLOCK) &&
3106 (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
3107 ndp->ni_dvp != ndp->ni_vp)
3108 VOP_UNLOCK(ndp->ni_dvp, 0, ndp->ni_cnd.cn_proc);
3109 if (!(flags & NDF_NO_DVP_RELE) &&
3110 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
3114 if (!(flags & NDF_NO_VP_UNLOCK) &&
3115 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
3116 VOP_UNLOCK(ndp->ni_vp, 0, ndp->ni_cnd.cn_proc);
3117 if (!(flags & NDF_NO_VP_RELE) &&
3122 if (!(flags & NDF_NO_STARTDIR_RELE) &&
3123 (ndp->ni_cnd.cn_flags & SAVESTART)) {
3124 vrele(ndp->ni_startdir);
3125 ndp->ni_startdir = NULL;