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.6 2003/06/25 03:55:57 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>
62 #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 thread *td));
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(struct mount *mp, int flags, struct simplelock *interlkp,
208 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
209 if (flags & LK_NOWAIT)
211 mp->mnt_kern_flag |= MNTK_MWAIT;
213 simple_unlock(interlkp);
216 * Since all busy locks are shared except the exclusive
217 * lock granted when unmounting, the only place that a
218 * wakeup needs to be done is at the release of the
219 * exclusive lock at the end of dounmount.
221 tsleep((caddr_t)mp, PVFS, "vfs_busy", 0);
223 simple_lock(interlkp);
227 lkflags = LK_SHARED | LK_NOPAUSE;
229 lkflags |= LK_INTERLOCK;
230 if (lockmgr(&mp->mnt_lock, lkflags, interlkp, td))
231 panic("vfs_busy: unexpected lock failure");
236 * Free a busy filesystem.
239 vfs_unbusy(struct mount *mp, struct thread *td)
241 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td);
245 * Lookup a filesystem type, and if found allocate and initialize
246 * a mount structure for it.
248 * Devname is usually updated by mount(8) after booting.
251 vfs_rootmountalloc(char *fstypename, char *devname, struct mount **mpp)
253 struct thread *td = curthread; /* XXX */
254 struct vfsconf *vfsp;
257 if (fstypename == NULL)
259 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
260 if (!strcmp(vfsp->vfc_name, fstypename))
264 mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
265 bzero((char *)mp, (u_long)sizeof(struct mount));
266 lockinit(&mp->mnt_lock, PVFS, "vfslock", VLKTIMEOUT, LK_NOPAUSE);
267 (void)vfs_busy(mp, LK_NOWAIT, 0, td);
268 TAILQ_INIT(&mp->mnt_nvnodelist);
269 TAILQ_INIT(&mp->mnt_reservedvnlist);
270 mp->mnt_nvnodelistsize = 0;
272 mp->mnt_op = vfsp->vfc_vfsops;
273 mp->mnt_flag = MNT_RDONLY;
274 mp->mnt_vnodecovered = NULLVP;
275 vfsp->vfc_refcount++;
276 mp->mnt_iosize_max = DFLTPHYS;
277 mp->mnt_stat.f_type = vfsp->vfc_typenum;
278 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
279 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
280 mp->mnt_stat.f_mntonname[0] = '/';
281 mp->mnt_stat.f_mntonname[1] = 0;
282 (void) copystr(devname, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 0);
288 * Find an appropriate filesystem to use for the root. If a filesystem
289 * has not been preselected, walk through the list of known filesystems
290 * trying those that have mountroot routines, and try them until one
291 * works or we have tried them all.
293 #ifdef notdef /* XXX JH */
295 lite2_vfs_mountroot()
297 struct vfsconf *vfsp;
298 extern int (*lite2_mountroot) __P((void));
301 if (lite2_mountroot != NULL)
302 return ((*lite2_mountroot)());
303 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
304 if (vfsp->vfc_mountroot == NULL)
306 if ((error = (*vfsp->vfc_mountroot)()) == 0)
308 printf("%s_mountroot failed: %d\n", vfsp->vfc_name, error);
315 * Lookup a mount point by filesystem identifier.
321 register struct mount *mp;
323 simple_lock(&mountlist_slock);
324 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
325 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
326 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
327 simple_unlock(&mountlist_slock);
331 simple_unlock(&mountlist_slock);
332 return ((struct mount *) 0);
336 * Get a new unique fsid. Try to make its val[0] unique, since this value
337 * will be used to create fake device numbers for stat(). Also try (but
338 * not so hard) make its val[0] unique mod 2^16, since some emulators only
339 * support 16-bit device numbers. We end up with unique val[0]'s for the
340 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
342 * Keep in mind that several mounts may be running in parallel. Starting
343 * the search one past where the previous search terminated is both a
344 * micro-optimization and a defense against returning the same fsid to
351 static u_int16_t mntid_base;
355 simple_lock(&mntid_slock);
356 mtype = mp->mnt_vfc->vfc_typenum;
357 tfsid.val[1] = mtype;
358 mtype = (mtype & 0xFF) << 24;
360 tfsid.val[0] = makeudev(255,
361 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
363 if (vfs_getvfs(&tfsid) == NULL)
366 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
367 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
368 simple_unlock(&mntid_slock);
372 * Knob to control the precision of file timestamps:
374 * 0 = seconds only; nanoseconds zeroed.
375 * 1 = seconds and nanoseconds, accurate within 1/HZ.
376 * 2 = seconds and nanoseconds, truncated to microseconds.
377 * >=3 = seconds and nanoseconds, maximum precision.
379 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
381 static int timestamp_precision = TSP_SEC;
382 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
383 ×tamp_precision, 0, "");
386 * Get a current timestamp.
390 struct timespec *tsp;
394 switch (timestamp_precision) {
396 tsp->tv_sec = time_second;
404 TIMEVAL_TO_TIMESPEC(&tv, tsp);
414 * Set vnode attributes to VNOVAL
418 register struct vattr *vap;
422 vap->va_size = VNOVAL;
423 vap->va_bytes = VNOVAL;
424 vap->va_mode = VNOVAL;
425 vap->va_nlink = VNOVAL;
426 vap->va_uid = VNOVAL;
427 vap->va_gid = VNOVAL;
428 vap->va_fsid = VNOVAL;
429 vap->va_fileid = VNOVAL;
430 vap->va_blocksize = VNOVAL;
431 vap->va_rdev = VNOVAL;
432 vap->va_atime.tv_sec = VNOVAL;
433 vap->va_atime.tv_nsec = VNOVAL;
434 vap->va_mtime.tv_sec = VNOVAL;
435 vap->va_mtime.tv_nsec = VNOVAL;
436 vap->va_ctime.tv_sec = VNOVAL;
437 vap->va_ctime.tv_nsec = VNOVAL;
438 vap->va_flags = VNOVAL;
439 vap->va_gen = VNOVAL;
444 * This routine is called when we have too many vnodes. It attempts
445 * to free <count> vnodes and will potentially free vnodes that still
446 * have VM backing store (VM backing store is typically the cause
447 * of a vnode blowout so we want to do this). Therefore, this operation
448 * is not considered cheap.
450 * A number of conditions may prevent a vnode from being reclaimed.
451 * the buffer cache may have references on the vnode, a directory
452 * vnode may still have references due to the namei cache representing
453 * underlying files, or the vnode may be in active use. It is not
454 * desireable to reuse such vnodes. These conditions may cause the
455 * number of vnodes to reach some minimum value regardless of what
456 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
459 vlrureclaim(struct mount *mp)
468 * Calculate the trigger point, don't allow user
469 * screwups to blow us up. This prevents us from
470 * recycling vnodes with lots of resident pages. We
471 * aren't trying to free memory, we are trying to
474 usevnodes = desiredvnodes;
477 trigger = cnt.v_page_count * 2 / usevnodes;
480 simple_lock(&mntvnode_slock);
481 count = mp->mnt_nvnodelistsize / 10 + 1;
482 while (count && (vp = TAILQ_FIRST(&mp->mnt_nvnodelist)) != NULL) {
483 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
484 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
486 if (vp->v_type != VNON &&
487 vp->v_type != VBAD &&
488 VMIGHTFREE(vp) && /* critical path opt */
489 (vp->v_object == NULL || vp->v_object->resident_page_count < trigger) &&
490 simple_lock_try(&vp->v_interlock)
492 simple_unlock(&mntvnode_slock);
493 if (VMIGHTFREE(vp)) {
494 vgonel(vp, curthread);
497 simple_unlock(&vp->v_interlock);
499 simple_lock(&mntvnode_slock);
503 simple_unlock(&mntvnode_slock);
508 * Attempt to recycle vnodes in a context that is always safe to block.
509 * Calling vlrurecycle() from the bowels of file system code has some
510 * interesting deadlock problems.
512 static struct thread *vnlruthread;
513 static int vnlruproc_sig;
518 struct mount *mp, *nmp;
521 struct thread *td = vnlruthread;
522 struct proc *p = td->td_proc;
524 EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc, td,
529 kproc_suspend_loop(td);
530 if (numvnodes - freevnodes <= desiredvnodes * 9 / 10) {
532 wakeup(&vnlruproc_sig);
533 tsleep(p, PVFS, "vlruwt", hz);
537 simple_lock(&mountlist_slock);
538 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
539 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, td)) {
540 nmp = TAILQ_NEXT(mp, mnt_list);
543 done += vlrureclaim(mp);
544 simple_lock(&mountlist_slock);
545 nmp = TAILQ_NEXT(mp, mnt_list);
548 simple_unlock(&mountlist_slock);
551 tsleep(p, PPAUSE, "vlrup", hz * 3);
557 static struct kproc_desc vnlru_kp = {
562 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &vnlru_kp)
565 * Routines having to do with the management of the vnode table.
567 extern vop_t **dead_vnodeop_p;
570 * Return the next vnode from the free list.
573 getnewvnode(tag, mp, vops, vpp)
580 struct thread *td = curthread; /* XXX */
581 struct proc *p = td->td_proc;
582 struct vnode *vp = NULL;
588 * Try to reuse vnodes if we hit the max. This situation only
589 * occurs in certain large-memory (2G+) situations. We cannot
590 * attempt to directly reclaim vnodes due to nasty recursion
593 while (numvnodes - freevnodes > desiredvnodes) {
594 if (vnlruproc_sig == 0) {
595 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
598 tsleep(&vnlruproc_sig, PVFS, "vlruwk", hz);
603 * Attempt to reuse a vnode already on the free list, allocating
604 * a new vnode if we can't find one or if we have not reached a
605 * good minimum for good LRU performance.
607 simple_lock(&vnode_free_list_slock);
608 if (freevnodes >= wantfreevnodes && numvnodes >= minvnodes) {
611 for (count = 0; count < freevnodes; count++) {
612 vp = TAILQ_FIRST(&vnode_free_list);
613 if (vp == NULL || vp->v_usecount)
614 panic("getnewvnode: free vnode isn't");
616 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
617 if ((VOP_GETVOBJECT(vp, &object) == 0 &&
618 (object->resident_page_count || object->ref_count)) ||
619 !simple_lock_try(&vp->v_interlock)) {
620 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
624 if (LIST_FIRST(&vp->v_cache_src)) {
626 * note: nameileafonly sysctl is temporary,
627 * for debugging only, and will eventually be
630 if (nameileafonly > 0) {
632 * Do not reuse namei-cached directory
633 * vnodes that have cached
636 if (cache_leaf_test(vp) < 0) {
637 simple_unlock(&vp->v_interlock);
638 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
642 } else if (nameileafonly < 0 ||
643 vmiodirenable == 0) {
645 * Do not reuse namei-cached directory
646 * vnodes if nameileafonly is -1 or
647 * if VMIO backing for directories is
648 * turned off (otherwise we reuse them
651 simple_unlock(&vp->v_interlock);
652 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
662 vp->v_flag |= VDOOMED;
663 vp->v_flag &= ~VFREE;
665 simple_unlock(&vnode_free_list_slock);
668 if (vp->v_type != VBAD) {
671 simple_unlock(&vp->v_interlock);
679 panic("cleaned vnode isn't");
682 panic("Clean vnode has pending I/O's");
692 vp->v_writecount = 0; /* XXX */
694 simple_unlock(&vnode_free_list_slock);
695 vp = (struct vnode *) zalloc(vnode_zone);
696 bzero((char *) vp, sizeof *vp);
697 simple_lock_init(&vp->v_interlock);
700 LIST_INIT(&vp->v_cache_src);
701 TAILQ_INIT(&vp->v_cache_dst);
705 TAILQ_INIT(&vp->v_cleanblkhd);
706 TAILQ_INIT(&vp->v_dirtyblkhd);
716 vfs_object_create(vp, td, p->p_ucred);
721 * Move a vnode from one mount queue to another.
725 register struct vnode *vp;
726 register struct mount *mp;
729 simple_lock(&mntvnode_slock);
731 * Delete from old mount point vnode list, if on one.
733 if (vp->v_mount != NULL) {
734 KASSERT(vp->v_mount->mnt_nvnodelistsize > 0,
735 ("bad mount point vnode list size"));
736 TAILQ_REMOVE(&vp->v_mount->mnt_nvnodelist, vp, v_nmntvnodes);
737 vp->v_mount->mnt_nvnodelistsize--;
740 * Insert into list of vnodes for the new mount point, if available.
742 if ((vp->v_mount = mp) == NULL) {
743 simple_unlock(&mntvnode_slock);
746 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
747 mp->mnt_nvnodelistsize++;
748 simple_unlock(&mntvnode_slock);
752 * Update outstanding I/O count and do wakeup if requested.
756 register struct buf *bp;
758 register struct vnode *vp;
760 bp->b_flags &= ~B_WRITEINPROG;
761 if ((vp = bp->b_vp)) {
763 if (vp->v_numoutput < 0)
764 panic("vwakeup: neg numoutput");
765 if ((vp->v_numoutput == 0) && (vp->v_flag & VBWAIT)) {
766 vp->v_flag &= ~VBWAIT;
767 wakeup((caddr_t) &vp->v_numoutput);
773 * Flush out and invalidate all buffers associated with a vnode.
774 * Called with the underlying object locked.
777 vinvalbuf(struct vnode *vp, int flags, struct ucred *cred,
778 struct thread *td, int slpflag, int slptimeo)
780 register struct buf *bp;
781 struct buf *nbp, *blist;
785 if (flags & V_SAVE) {
787 while (vp->v_numoutput) {
788 vp->v_flag |= VBWAIT;
789 error = tsleep((caddr_t)&vp->v_numoutput,
790 slpflag | (PRIBIO + 1), "vinvlbuf", slptimeo);
796 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
798 if ((error = VOP_FSYNC(vp, cred, MNT_WAIT, td)) != 0)
801 if (vp->v_numoutput > 0 ||
802 !TAILQ_EMPTY(&vp->v_dirtyblkhd))
803 panic("vinvalbuf: dirty bufs");
809 blist = TAILQ_FIRST(&vp->v_cleanblkhd);
811 blist = TAILQ_FIRST(&vp->v_dirtyblkhd);
815 for (bp = blist; bp; bp = nbp) {
816 nbp = TAILQ_NEXT(bp, b_vnbufs);
817 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
818 error = BUF_TIMELOCK(bp,
819 LK_EXCLUSIVE | LK_SLEEPFAIL,
820 "vinvalbuf", slpflag, slptimeo);
827 * XXX Since there are no node locks for NFS, I
828 * believe there is a slight chance that a delayed
829 * write will occur while sleeping just above, so
830 * check for it. Note that vfs_bio_awrite expects
831 * buffers to reside on a queue, while VOP_BWRITE and
834 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
837 if (bp->b_vp == vp) {
838 if (bp->b_flags & B_CLUSTEROK) {
843 bp->b_flags |= B_ASYNC;
844 VOP_BWRITE(bp->b_vp, bp);
848 (void) VOP_BWRITE(bp->b_vp, bp);
853 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
854 bp->b_flags &= ~B_ASYNC;
860 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
861 * have write I/O in-progress but if there is a VM object then the
862 * VM object can also have read-I/O in-progress.
865 while (vp->v_numoutput > 0) {
866 vp->v_flag |= VBWAIT;
867 tsleep(&vp->v_numoutput, PVM, "vnvlbv", 0);
869 if (VOP_GETVOBJECT(vp, &object) == 0) {
870 while (object->paging_in_progress)
871 vm_object_pip_sleep(object, "vnvlbx");
873 } while (vp->v_numoutput > 0);
878 * Destroy the copy in the VM cache, too.
880 simple_lock(&vp->v_interlock);
881 if (VOP_GETVOBJECT(vp, &object) == 0) {
882 vm_object_page_remove(object, 0, 0,
883 (flags & V_SAVE) ? TRUE : FALSE);
885 simple_unlock(&vp->v_interlock);
887 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd) || !TAILQ_EMPTY(&vp->v_cleanblkhd))
888 panic("vinvalbuf: flush failed");
893 * Truncate a file's buffer and pages to a specified length. This
894 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
898 vtruncbuf(struct vnode *vp, struct ucred *cred, struct thread *td,
899 off_t length, int blksize)
907 * Round up to the *next* lbn.
909 trunclbn = (length + blksize - 1) / blksize;
916 for (bp = TAILQ_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
917 nbp = TAILQ_NEXT(bp, b_vnbufs);
918 if (bp->b_lblkno >= trunclbn) {
919 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
920 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
924 bp->b_flags |= (B_INVAL | B_RELBUF);
925 bp->b_flags &= ~B_ASYNC;
930 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
932 (nbp->b_flags & B_DELWRI))) {
938 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
939 nbp = TAILQ_NEXT(bp, b_vnbufs);
940 if (bp->b_lblkno >= trunclbn) {
941 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
942 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
946 bp->b_flags |= (B_INVAL | B_RELBUF);
947 bp->b_flags &= ~B_ASYNC;
952 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
954 (nbp->b_flags & B_DELWRI) == 0)) {
963 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
964 nbp = TAILQ_NEXT(bp, b_vnbufs);
965 if ((bp->b_flags & B_DELWRI) && (bp->b_lblkno < 0)) {
966 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
967 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
971 if (bp->b_vp == vp) {
972 bp->b_flags |= B_ASYNC;
974 bp->b_flags &= ~B_ASYNC;
976 VOP_BWRITE(bp->b_vp, bp);
984 while (vp->v_numoutput > 0) {
985 vp->v_flag |= VBWAIT;
986 tsleep(&vp->v_numoutput, PVM, "vbtrunc", 0);
991 vnode_pager_setsize(vp, length);
997 * Associate a buffer with a vnode.
1001 register struct vnode *vp;
1002 register struct buf *bp;
1006 KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
1010 bp->b_dev = vn_todev(vp);
1012 * Insert onto list for new vnode.
1015 bp->b_xflags |= BX_VNCLEAN;
1016 bp->b_xflags &= ~BX_VNDIRTY;
1017 TAILQ_INSERT_TAIL(&vp->v_cleanblkhd, bp, b_vnbufs);
1022 * Disassociate a buffer from a vnode.
1026 register struct buf *bp;
1029 struct buflists *listheadp;
1032 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1035 * Delete from old vnode list, if on one.
1039 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) {
1040 if (bp->b_xflags & BX_VNDIRTY)
1041 listheadp = &vp->v_dirtyblkhd;
1043 listheadp = &vp->v_cleanblkhd;
1044 TAILQ_REMOVE(listheadp, bp, b_vnbufs);
1045 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1047 if ((vp->v_flag & VONWORKLST) && TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
1048 vp->v_flag &= ~VONWORKLST;
1049 LIST_REMOVE(vp, v_synclist);
1052 bp->b_vp = (struct vnode *) 0;
1057 * The workitem queue.
1059 * It is useful to delay writes of file data and filesystem metadata
1060 * for tens of seconds so that quickly created and deleted files need
1061 * not waste disk bandwidth being created and removed. To realize this,
1062 * we append vnodes to a "workitem" queue. When running with a soft
1063 * updates implementation, most pending metadata dependencies should
1064 * not wait for more than a few seconds. Thus, mounted on block devices
1065 * are delayed only about a half the time that file data is delayed.
1066 * Similarly, directory updates are more critical, so are only delayed
1067 * about a third the time that file data is delayed. Thus, there are
1068 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
1069 * one each second (driven off the filesystem syncer process). The
1070 * syncer_delayno variable indicates the next queue that is to be processed.
1071 * Items that need to be processed soon are placed in this queue:
1073 * syncer_workitem_pending[syncer_delayno]
1075 * A delay of fifteen seconds is done by placing the request fifteen
1076 * entries later in the queue:
1078 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
1083 * Add an item to the syncer work queue.
1086 vn_syncer_add_to_worklist(struct vnode *vp, int delay)
1092 if (vp->v_flag & VONWORKLST) {
1093 LIST_REMOVE(vp, v_synclist);
1096 if (delay > syncer_maxdelay - 2)
1097 delay = syncer_maxdelay - 2;
1098 slot = (syncer_delayno + delay) & syncer_mask;
1100 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], vp, v_synclist);
1101 vp->v_flag |= VONWORKLST;
1105 struct thread *updatethread;
1106 static void sched_sync __P((void));
1107 static struct kproc_desc up_kp = {
1112 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp)
1115 * System filesystem synchronizer daemon.
1120 struct synclist *slp;
1124 struct thread *td = updatethread;
1125 struct proc *p = td->td_proc;
1127 EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc, td,
1131 kproc_suspend_loop(td);
1133 starttime = time_second;
1136 * Push files whose dirty time has expired. Be careful
1137 * of interrupt race on slp queue.
1140 slp = &syncer_workitem_pending[syncer_delayno];
1141 syncer_delayno += 1;
1142 if (syncer_delayno == syncer_maxdelay)
1146 while ((vp = LIST_FIRST(slp)) != NULL) {
1147 if (VOP_ISLOCKED(vp, NULL) == 0) {
1148 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1149 (void) VOP_FSYNC(vp, p->p_ucred, MNT_LAZY, td);
1150 VOP_UNLOCK(vp, 0, td);
1153 if (LIST_FIRST(slp) == vp) {
1155 * Note: v_tag VT_VFS vps can remain on the
1156 * worklist too with no dirty blocks, but
1157 * since sync_fsync() moves it to a different
1160 if (TAILQ_EMPTY(&vp->v_dirtyblkhd) &&
1161 !vn_isdisk(vp, NULL))
1162 panic("sched_sync: fsync failed vp %p tag %d", vp, vp->v_tag);
1164 * Put us back on the worklist. The worklist
1165 * routine will remove us from our current
1166 * position and then add us back in at a later
1169 vn_syncer_add_to_worklist(vp, syncdelay);
1175 * Do soft update processing.
1178 (*bioops.io_sync)(NULL);
1181 * The variable rushjob allows the kernel to speed up the
1182 * processing of the filesystem syncer process. A rushjob
1183 * value of N tells the filesystem syncer to process the next
1184 * N seconds worth of work on its queue ASAP. Currently rushjob
1185 * is used by the soft update code to speed up the filesystem
1186 * syncer process when the incore state is getting so far
1187 * ahead of the disk that the kernel memory pool is being
1188 * threatened with exhaustion.
1195 * If it has taken us less than a second to process the
1196 * current work, then wait. Otherwise start right over
1197 * again. We can still lose time if any single round
1198 * takes more than two seconds, but it does not really
1199 * matter as we are just trying to generally pace the
1200 * filesystem activity.
1202 if (time_second == starttime)
1203 tsleep(&lbolt, PPAUSE, "syncer", 0);
1208 * Request the syncer daemon to speed up its work.
1209 * We never push it to speed up more than half of its
1210 * normal turn time, otherwise it could take over the cpu.
1218 if (updatethread->td_proc->p_wchan == &lbolt) /* YYY */
1219 setrunnable(updatethread->td_proc);
1221 if (rushjob < syncdelay / 2) {
1223 stat_rush_requests += 1;
1230 * Associate a p-buffer with a vnode.
1232 * Also sets B_PAGING flag to indicate that vnode is not fully associated
1233 * with the buffer. i.e. the bp has not been linked into the vnode or
1238 register struct vnode *vp;
1239 register struct buf *bp;
1242 KASSERT(bp->b_vp == NULL, ("pbgetvp: not free"));
1245 bp->b_flags |= B_PAGING;
1246 bp->b_dev = vn_todev(vp);
1250 * Disassociate a p-buffer from a vnode.
1254 register struct buf *bp;
1257 KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL"));
1260 if (TAILQ_NEXT(bp, b_vnbufs) != NULL) {
1262 "relpbuf(): b_vp was probably reassignbuf()d %p %x",
1267 bp->b_vp = (struct vnode *) 0;
1268 bp->b_flags &= ~B_PAGING;
1272 pbreassignbuf(bp, newvp)
1274 struct vnode *newvp;
1276 if ((bp->b_flags & B_PAGING) == 0) {
1278 "pbreassignbuf() on non phys bp %p",
1286 * Reassign a buffer from one vnode to another.
1287 * Used to assign file specific control information
1288 * (indirect blocks) to the vnode to which they belong.
1291 reassignbuf(bp, newvp)
1292 register struct buf *bp;
1293 register struct vnode *newvp;
1295 struct buflists *listheadp;
1299 if (newvp == NULL) {
1300 printf("reassignbuf: NULL");
1306 * B_PAGING flagged buffers cannot be reassigned because their vp
1307 * is not fully linked in.
1309 if (bp->b_flags & B_PAGING)
1310 panic("cannot reassign paging buffer");
1314 * Delete from old vnode list, if on one.
1316 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) {
1317 if (bp->b_xflags & BX_VNDIRTY)
1318 listheadp = &bp->b_vp->v_dirtyblkhd;
1320 listheadp = &bp->b_vp->v_cleanblkhd;
1321 TAILQ_REMOVE(listheadp, bp, b_vnbufs);
1322 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1323 if (bp->b_vp != newvp) {
1325 bp->b_vp = NULL; /* for clarification */
1329 * If dirty, put on list of dirty buffers; otherwise insert onto list
1332 if (bp->b_flags & B_DELWRI) {
1335 listheadp = &newvp->v_dirtyblkhd;
1336 if ((newvp->v_flag & VONWORKLST) == 0) {
1337 switch (newvp->v_type) {
1343 if (newvp->v_specmountpoint != NULL) {
1351 vn_syncer_add_to_worklist(newvp, delay);
1353 bp->b_xflags |= BX_VNDIRTY;
1354 tbp = TAILQ_FIRST(listheadp);
1356 bp->b_lblkno == 0 ||
1357 (bp->b_lblkno > 0 && tbp->b_lblkno < 0) ||
1358 (bp->b_lblkno > 0 && bp->b_lblkno < tbp->b_lblkno)) {
1359 TAILQ_INSERT_HEAD(listheadp, bp, b_vnbufs);
1360 ++reassignbufsortgood;
1361 } else if (bp->b_lblkno < 0) {
1362 TAILQ_INSERT_TAIL(listheadp, bp, b_vnbufs);
1363 ++reassignbufsortgood;
1364 } else if (reassignbufmethod == 1) {
1366 * New sorting algorithm, only handle sequential case,
1367 * otherwise append to end (but before metadata)
1369 if ((tbp = gbincore(newvp, bp->b_lblkno - 1)) != NULL &&
1370 (tbp->b_xflags & BX_VNDIRTY)) {
1372 * Found the best place to insert the buffer
1374 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1375 ++reassignbufsortgood;
1378 * Missed, append to end, but before meta-data.
1379 * We know that the head buffer in the list is
1380 * not meta-data due to prior conditionals.
1382 * Indirect effects: NFS second stage write
1383 * tends to wind up here, giving maximum
1384 * distance between the unstable write and the
1387 tbp = TAILQ_LAST(listheadp, buflists);
1388 while (tbp && tbp->b_lblkno < 0)
1389 tbp = TAILQ_PREV(tbp, buflists, b_vnbufs);
1390 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1391 ++reassignbufsortbad;
1395 * Old sorting algorithm, scan queue and insert
1398 while ((ttbp = TAILQ_NEXT(tbp, b_vnbufs)) &&
1399 (ttbp->b_lblkno < bp->b_lblkno)) {
1403 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1406 bp->b_xflags |= BX_VNCLEAN;
1407 TAILQ_INSERT_TAIL(&newvp->v_cleanblkhd, bp, b_vnbufs);
1408 if ((newvp->v_flag & VONWORKLST) &&
1409 TAILQ_EMPTY(&newvp->v_dirtyblkhd)) {
1410 newvp->v_flag &= ~VONWORKLST;
1411 LIST_REMOVE(newvp, v_synclist);
1414 if (bp->b_vp != newvp) {
1422 * Create a vnode for a block device.
1423 * Used for mounting the root file system.
1430 register struct vnode *vp;
1438 error = getnewvnode(VT_NON, (struct mount *)0, spec_vnodeop_p, &nvp);
1451 * Add vnode to the alias list hung off the dev_t.
1453 * The reason for this gunk is that multiple vnodes can reference
1454 * the same physical device, so checking vp->v_usecount to see
1455 * how many users there are is inadequate; the v_usecount for
1456 * the vnodes need to be accumulated. vcount() does that.
1459 addaliasu(nvp, nvp_rdev)
1464 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1465 panic("addaliasu on non-special vnode");
1466 addalias(nvp, udev2dev(nvp_rdev, nvp->v_type == VBLK ? 1 : 0));
1475 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1476 panic("addalias on non-special vnode");
1479 simple_lock(&spechash_slock);
1480 SLIST_INSERT_HEAD(&dev->si_hlist, nvp, v_specnext);
1481 simple_unlock(&spechash_slock);
1485 * Grab a particular vnode from the free list, increment its
1486 * reference count and lock it. The vnode lock bit is set if the
1487 * vnode is being eliminated in vgone. The process is awakened
1488 * when the transition is completed, and an error returned to
1489 * indicate that the vnode is no longer usable (possibly having
1490 * been changed to a new file system type).
1501 * If the vnode is in the process of being cleaned out for
1502 * another use, we wait for the cleaning to finish and then
1503 * return failure. Cleaning is determined by checking that
1504 * the VXLOCK flag is set.
1506 if ((flags & LK_INTERLOCK) == 0) {
1507 simple_lock(&vp->v_interlock);
1509 if (vp->v_flag & VXLOCK) {
1510 if (vp->v_vxproc == curproc) {
1512 /* this can now occur in normal operation */
1513 log(LOG_INFO, "VXLOCK interlock avoided\n");
1516 vp->v_flag |= VXWANT;
1517 simple_unlock(&vp->v_interlock);
1518 tsleep((caddr_t)vp, PINOD, "vget", 0);
1525 if (VSHOULDBUSY(vp))
1527 if (flags & LK_TYPE_MASK) {
1528 if ((error = vn_lock(vp, flags | LK_INTERLOCK, td)) != 0) {
1530 * must expand vrele here because we do not want
1531 * to call VOP_INACTIVE if the reference count
1532 * drops back to zero since it was never really
1533 * active. We must remove it from the free list
1534 * before sleeping so that multiple processes do
1535 * not try to recycle it.
1537 simple_lock(&vp->v_interlock);
1539 if (VSHOULDFREE(vp))
1543 simple_unlock(&vp->v_interlock);
1547 simple_unlock(&vp->v_interlock);
1552 vref(struct vnode *vp)
1554 simple_lock(&vp->v_interlock);
1556 simple_unlock(&vp->v_interlock);
1560 * Vnode put/release.
1561 * If count drops to zero, call inactive routine and return to freelist.
1564 vrele(struct vnode *vp)
1566 struct thread *td = curthread; /* XXX */
1568 KASSERT(vp != NULL, ("vrele: null vp"));
1570 simple_lock(&vp->v_interlock);
1572 if (vp->v_usecount > 1) {
1575 simple_unlock(&vp->v_interlock);
1580 if (vp->v_usecount == 1) {
1583 * We must call VOP_INACTIVE with the node locked.
1584 * If we are doing a vpu, the node is already locked,
1585 * but, in the case of vrele, we must explicitly lock
1586 * the vnode before calling VOP_INACTIVE
1589 if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK, td) == 0)
1590 VOP_INACTIVE(vp, td);
1591 if (VSHOULDFREE(vp))
1597 vprint("vrele: negative ref count", vp);
1598 simple_unlock(&vp->v_interlock);
1600 panic("vrele: negative ref cnt");
1605 vput(struct vnode *vp)
1607 struct thread *td = curthread; /* XXX */
1609 KASSERT(vp != NULL, ("vput: null vp"));
1611 simple_lock(&vp->v_interlock);
1613 if (vp->v_usecount > 1) {
1615 VOP_UNLOCK(vp, LK_INTERLOCK, td);
1619 if (vp->v_usecount == 1) {
1622 * We must call VOP_INACTIVE with the node locked.
1623 * If we are doing a vpu, the node is already locked,
1624 * so we just need to release the vnode mutex.
1626 simple_unlock(&vp->v_interlock);
1627 VOP_INACTIVE(vp, td);
1628 if (VSHOULDFREE(vp))
1634 vprint("vput: negative ref count", vp);
1636 panic("vput: negative ref cnt");
1641 * Somebody doesn't want the vnode recycled.
1645 register struct vnode *vp;
1651 if (VSHOULDBUSY(vp))
1657 * One less who cares about this vnode.
1661 register struct vnode *vp;
1666 if (vp->v_holdcnt <= 0)
1667 panic("vdrop: holdcnt");
1669 if (VSHOULDFREE(vp))
1675 * Remove any vnodes in the vnode table belonging to mount point mp.
1677 * If FORCECLOSE is not specified, there should not be any active ones,
1678 * return error if any are found (nb: this is a user error, not a
1679 * system error). If FORCECLOSE is specified, detach any active vnodes
1682 * If WRITECLOSE is set, only flush out regular file vnodes open for
1685 * SKIPSYSTEM causes any vnodes marked VSYSTEM to be skipped.
1687 * `rootrefs' specifies the base reference count for the root vnode
1688 * of this filesystem. The root vnode is considered busy if its
1689 * v_usecount exceeds this value. On a successful return, vflush()
1690 * will call vrele() on the root vnode exactly rootrefs times.
1691 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
1695 static int busyprt = 0; /* print out busy vnodes */
1696 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "");
1700 vflush(mp, rootrefs, flags)
1705 struct thread *td = curthread; /* XXX */
1706 struct proc *p = td->td_proc;
1707 struct vnode *vp, *nvp, *rootvp = NULL;
1709 int busy = 0, error;
1712 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
1713 ("vflush: bad args"));
1715 * Get the filesystem root vnode. We can vput() it
1716 * immediately, since with rootrefs > 0, it won't go away.
1718 if ((error = VFS_ROOT(mp, &rootvp)) != 0)
1722 simple_lock(&mntvnode_slock);
1724 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp; vp = nvp) {
1726 * Make sure this vnode wasn't reclaimed in getnewvnode().
1727 * Start over if it has (it won't be on the list anymore).
1729 if (vp->v_mount != mp)
1731 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
1733 simple_lock(&vp->v_interlock);
1735 * Skip over a vnodes marked VSYSTEM.
1737 if ((flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM)) {
1738 simple_unlock(&vp->v_interlock);
1742 * If WRITECLOSE is set, flush out unlinked but still open
1743 * files (even if open only for reading) and regular file
1744 * vnodes open for writing.
1746 if ((flags & WRITECLOSE) &&
1747 (vp->v_type == VNON ||
1748 (VOP_GETATTR(vp, &vattr, p->p_ucred, td) == 0 &&
1749 vattr.va_nlink > 0)) &&
1750 (vp->v_writecount == 0 || vp->v_type != VREG)) {
1751 simple_unlock(&vp->v_interlock);
1756 * With v_usecount == 0, all we need to do is clear out the
1757 * vnode data structures and we are done.
1759 if (vp->v_usecount == 0) {
1760 simple_unlock(&mntvnode_slock);
1762 simple_lock(&mntvnode_slock);
1767 * If FORCECLOSE is set, forcibly close the vnode. For block
1768 * or character devices, revert to an anonymous device. For
1769 * all other files, just kill them.
1771 if (flags & FORCECLOSE) {
1772 simple_unlock(&mntvnode_slock);
1773 if (vp->v_type != VBLK && vp->v_type != VCHR) {
1777 vp->v_op = spec_vnodeop_p;
1778 insmntque(vp, (struct mount *) 0);
1780 simple_lock(&mntvnode_slock);
1785 vprint("vflush: busy vnode", vp);
1787 simple_unlock(&vp->v_interlock);
1790 simple_unlock(&mntvnode_slock);
1791 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
1793 * If just the root vnode is busy, and if its refcount
1794 * is equal to `rootrefs', then go ahead and kill it.
1796 simple_lock(&rootvp->v_interlock);
1797 KASSERT(busy > 0, ("vflush: not busy"));
1798 KASSERT(rootvp->v_usecount >= rootrefs, ("vflush: rootrefs"));
1799 if (busy == 1 && rootvp->v_usecount == rootrefs) {
1803 simple_unlock(&rootvp->v_interlock);
1807 for (; rootrefs > 0; rootrefs--)
1813 * We do not want to recycle the vnode too quickly.
1815 * XXX we can't move vp's around the nvnodelist without really screwing
1816 * up the efficiency of filesystem SYNC and friends. This code is
1817 * disabled until we fix the syncing code's scanning algorithm.
1820 vlruvp(struct vnode *vp)
1825 if ((mp = vp->v_mount) != NULL) {
1826 simple_lock(&mntvnode_slock);
1827 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1828 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1829 simple_unlock(&mntvnode_slock);
1835 * Disassociate the underlying file system from a vnode.
1838 vclean(struct vnode *vp, int flags, struct thread *td)
1843 * Check to see if the vnode is in use. If so we have to reference it
1844 * before we clean it out so that its count cannot fall to zero and
1845 * generate a race against ourselves to recycle it.
1847 if ((active = vp->v_usecount))
1851 * Prevent the vnode from being recycled or brought into use while we
1854 if (vp->v_flag & VXLOCK)
1855 panic("vclean: deadlock");
1856 vp->v_flag |= VXLOCK;
1857 vp->v_vxproc = curproc;
1859 * Even if the count is zero, the VOP_INACTIVE routine may still
1860 * have the object locked while it cleans it out. The VOP_LOCK
1861 * ensures that the VOP_INACTIVE routine is done with its work.
1862 * For active vnodes, it ensures that no other activity can
1863 * occur while the underlying object is being cleaned out.
1865 VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK, td);
1868 * Clean out any buffers associated with the vnode.
1870 vinvalbuf(vp, V_SAVE, NOCRED, td, 0, 0);
1872 VOP_DESTROYVOBJECT(vp);
1875 * If purging an active vnode, it must be closed and
1876 * deactivated before being reclaimed. Note that the
1877 * VOP_INACTIVE will unlock the vnode.
1880 if (flags & DOCLOSE)
1881 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
1882 VOP_INACTIVE(vp, td);
1885 * Any other processes trying to obtain this lock must first
1886 * wait for VXLOCK to clear, then call the new lock operation.
1888 VOP_UNLOCK(vp, 0, td);
1891 * Reclaim the vnode.
1893 if (VOP_RECLAIM(vp, td))
1894 panic("vclean: cannot reclaim");
1898 * Inline copy of vrele() since VOP_INACTIVE
1899 * has already been called.
1901 simple_lock(&vp->v_interlock);
1902 if (--vp->v_usecount <= 0) {
1904 if (vp->v_usecount < 0 || vp->v_writecount != 0) {
1905 vprint("vclean: bad ref count", vp);
1906 panic("vclean: ref cnt");
1911 simple_unlock(&vp->v_interlock);
1915 vp->v_vnlock = NULL;
1917 if (VSHOULDFREE(vp))
1921 * Done with purge, notify sleepers of the grim news.
1923 vp->v_op = dead_vnodeop_p;
1926 vp->v_flag &= ~VXLOCK;
1927 vp->v_vxproc = NULL;
1928 if (vp->v_flag & VXWANT) {
1929 vp->v_flag &= ~VXWANT;
1930 wakeup((caddr_t) vp);
1935 * Eliminate all activity associated with the requested vnode
1936 * and with all vnodes aliased to the requested vnode.
1940 struct vop_revoke_args /* {
1945 struct vnode *vp, *vq;
1948 KASSERT((ap->a_flags & REVOKEALL) != 0, ("vop_revoke"));
1952 * If a vgone (or vclean) is already in progress,
1953 * wait until it is done and return.
1955 if (vp->v_flag & VXLOCK) {
1956 vp->v_flag |= VXWANT;
1957 simple_unlock(&vp->v_interlock);
1958 tsleep((caddr_t)vp, PINOD, "vop_revokeall", 0);
1963 simple_lock(&spechash_slock);
1964 vq = SLIST_FIRST(&dev->si_hlist);
1965 simple_unlock(&spechash_slock);
1974 * Recycle an unused vnode to the front of the free list.
1975 * Release the passed interlock if the vnode will be recycled.
1978 vrecycle(struct vnode *vp, struct simplelock *inter_lkp, struct thread *td)
1980 simple_lock(&vp->v_interlock);
1981 if (vp->v_usecount == 0) {
1983 simple_unlock(inter_lkp);
1988 simple_unlock(&vp->v_interlock);
1993 * Eliminate all activity associated with a vnode
1994 * in preparation for reuse.
1997 vgone(struct vnode *vp)
1999 struct thread *td = curthread; /* XXX */
2001 simple_lock(&vp->v_interlock);
2006 * vgone, with the vp interlock held.
2009 vgonel(struct vnode *vp, struct thread *td)
2014 * If a vgone (or vclean) is already in progress,
2015 * wait until it is done and return.
2017 if (vp->v_flag & VXLOCK) {
2018 vp->v_flag |= VXWANT;
2019 simple_unlock(&vp->v_interlock);
2020 tsleep((caddr_t)vp, PINOD, "vgone", 0);
2025 * Clean out the filesystem specific data.
2027 vclean(vp, DOCLOSE, td);
2028 simple_lock(&vp->v_interlock);
2031 * Delete from old mount point vnode list, if on one.
2033 if (vp->v_mount != NULL)
2034 insmntque(vp, (struct mount *)0);
2036 * If special device, remove it from special device alias list
2039 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
2040 simple_lock(&spechash_slock);
2041 SLIST_REMOVE(&vp->v_hashchain, vp, vnode, v_specnext);
2042 freedev(vp->v_rdev);
2043 simple_unlock(&spechash_slock);
2048 * If it is on the freelist and not already at the head,
2049 * move it to the head of the list. The test of the
2050 * VDOOMED flag and the reference count of zero is because
2051 * it will be removed from the free list by getnewvnode,
2052 * but will not have its reference count incremented until
2053 * after calling vgone. If the reference count were
2054 * incremented first, vgone would (incorrectly) try to
2055 * close the previous instance of the underlying object.
2057 if (vp->v_usecount == 0 && !(vp->v_flag & VDOOMED)) {
2059 simple_lock(&vnode_free_list_slock);
2060 if (vp->v_flag & VFREE)
2061 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
2064 vp->v_flag |= VFREE;
2065 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2066 simple_unlock(&vnode_free_list_slock);
2071 simple_unlock(&vp->v_interlock);
2075 * Lookup a vnode by device number.
2078 vfinddev(dev, type, vpp)
2085 simple_lock(&spechash_slock);
2086 SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) {
2087 if (type == vp->v_type) {
2089 simple_unlock(&spechash_slock);
2093 simple_unlock(&spechash_slock);
2098 * Calculate the total number of references to a special device.
2108 simple_lock(&spechash_slock);
2109 SLIST_FOREACH(vq, &vp->v_hashchain, v_specnext)
2110 count += vq->v_usecount;
2111 simple_unlock(&spechash_slock);
2116 * Same as above, but using the dev_t as argument
2125 vp = SLIST_FIRST(&dev->si_hlist);
2132 * Print out a description of a vnode.
2134 static char *typename[] =
2135 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
2145 printf("%s: %p: ", label, (void *)vp);
2147 printf("%p: ", (void *)vp);
2148 printf("type %s, usecount %d, writecount %d, refcount %d,",
2149 typename[vp->v_type], vp->v_usecount, vp->v_writecount,
2152 if (vp->v_flag & VROOT)
2153 strcat(buf, "|VROOT");
2154 if (vp->v_flag & VTEXT)
2155 strcat(buf, "|VTEXT");
2156 if (vp->v_flag & VSYSTEM)
2157 strcat(buf, "|VSYSTEM");
2158 if (vp->v_flag & VXLOCK)
2159 strcat(buf, "|VXLOCK");
2160 if (vp->v_flag & VXWANT)
2161 strcat(buf, "|VXWANT");
2162 if (vp->v_flag & VBWAIT)
2163 strcat(buf, "|VBWAIT");
2164 if (vp->v_flag & VDOOMED)
2165 strcat(buf, "|VDOOMED");
2166 if (vp->v_flag & VFREE)
2167 strcat(buf, "|VFREE");
2168 if (vp->v_flag & VOBJBUF)
2169 strcat(buf, "|VOBJBUF");
2171 printf(" flags (%s)", &buf[1]);
2172 if (vp->v_data == NULL) {
2181 #include <ddb/ddb.h>
2183 * List all of the locked vnodes in the system.
2184 * Called when debugging the kernel.
2186 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
2188 struct thread *td = curthread; /* XXX */
2189 struct mount *mp, *nmp;
2192 printf("Locked vnodes\n");
2193 simple_lock(&mountlist_slock);
2194 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2195 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, td)) {
2196 nmp = TAILQ_NEXT(mp, mnt_list);
2199 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
2200 if (VOP_ISLOCKED(vp, NULL))
2201 vprint((char *)0, vp);
2203 simple_lock(&mountlist_slock);
2204 nmp = TAILQ_NEXT(mp, mnt_list);
2207 simple_unlock(&mountlist_slock);
2212 * Top level filesystem related information gathering.
2214 static int sysctl_ovfs_conf __P((SYSCTL_HANDLER_ARGS));
2217 vfs_sysctl(SYSCTL_HANDLER_ARGS)
2219 int *name = (int *)arg1 - 1; /* XXX */
2220 u_int namelen = arg2 + 1; /* XXX */
2221 struct vfsconf *vfsp;
2223 #if 1 || defined(COMPAT_PRELITE2)
2224 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
2226 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
2230 /* all sysctl names at this level are at least name and field */
2232 return (ENOTDIR); /* overloaded */
2233 if (name[0] != VFS_GENERIC) {
2234 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2235 if (vfsp->vfc_typenum == name[0])
2238 return (EOPNOTSUPP);
2239 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
2240 oldp, oldlenp, newp, newlen, p));
2244 case VFS_MAXTYPENUM:
2247 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
2250 return (ENOTDIR); /* overloaded */
2251 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2252 if (vfsp->vfc_typenum == name[2])
2255 return (EOPNOTSUPP);
2256 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
2258 return (EOPNOTSUPP);
2261 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
2262 "Generic filesystem");
2264 #if 1 || defined(COMPAT_PRELITE2)
2267 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
2270 struct vfsconf *vfsp;
2271 struct ovfsconf ovfs;
2273 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
2274 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
2275 strcpy(ovfs.vfc_name, vfsp->vfc_name);
2276 ovfs.vfc_index = vfsp->vfc_typenum;
2277 ovfs.vfc_refcount = vfsp->vfc_refcount;
2278 ovfs.vfc_flags = vfsp->vfc_flags;
2279 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
2286 #endif /* 1 || COMPAT_PRELITE2 */
2289 #define KINFO_VNODESLOP 10
2291 * Dump vnode list (via sysctl).
2292 * Copyout address of vnode followed by vnode.
2296 sysctl_vnode(SYSCTL_HANDLER_ARGS)
2298 struct proc *p = curproc; /* XXX */
2299 struct mount *mp, *nmp;
2300 struct vnode *nvp, *vp;
2303 #define VPTRSZ sizeof (struct vnode *)
2304 #define VNODESZ sizeof (struct vnode)
2307 if (!req->oldptr) /* Make an estimate */
2308 return (SYSCTL_OUT(req, 0,
2309 (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ)));
2311 simple_lock(&mountlist_slock);
2312 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2313 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
2314 nmp = TAILQ_NEXT(mp, mnt_list);
2318 simple_lock(&mntvnode_slock);
2319 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
2323 * Check that the vp is still associated with
2324 * this filesystem. RACE: could have been
2325 * recycled onto the same filesystem.
2327 if (vp->v_mount != mp) {
2328 simple_unlock(&mntvnode_slock);
2331 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
2332 simple_unlock(&mntvnode_slock);
2333 if ((error = SYSCTL_OUT(req, &vp, VPTRSZ)) ||
2334 (error = SYSCTL_OUT(req, vp, VNODESZ)))
2336 simple_lock(&mntvnode_slock);
2338 simple_unlock(&mntvnode_slock);
2339 simple_lock(&mountlist_slock);
2340 nmp = TAILQ_NEXT(mp, mnt_list);
2343 simple_unlock(&mountlist_slock);
2351 * Exporting the vnode list on large systems causes them to crash.
2352 * Exporting the vnode list on medium systems causes sysctl to coredump.
2355 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD,
2356 0, 0, sysctl_vnode, "S,vnode", "");
2360 * Check to see if a filesystem is mounted on a block device.
2367 if (vp->v_specmountpoint != NULL)
2373 * Unmount all filesystems. The list is traversed in reverse order
2374 * of mounting to avoid dependencies.
2380 struct thread *td = curthread;
2383 if (td->td_proc == NULL)
2384 td = initproc->p_thread; /* XXX XXX use proc0 instead? */
2387 * Since this only runs when rebooting, it is not interlocked.
2389 while(!TAILQ_EMPTY(&mountlist)) {
2390 mp = TAILQ_LAST(&mountlist, mntlist);
2391 error = dounmount(mp, MNT_FORCE, td);
2393 TAILQ_REMOVE(&mountlist, mp, mnt_list);
2394 printf("unmount of %s failed (",
2395 mp->mnt_stat.f_mntonname);
2399 printf("%d)\n", error);
2401 /* The unmount has removed mp from the mountlist */
2407 * Build hash lists of net addresses and hang them off the mount point.
2408 * Called by ufs_mount() to set up the lists of export addresses.
2411 vfs_hang_addrlist(mp, nep, argp)
2413 struct netexport *nep;
2414 struct export_args *argp;
2416 register struct netcred *np;
2417 register struct radix_node_head *rnh;
2419 struct radix_node *rn;
2420 struct sockaddr *saddr, *smask = 0;
2424 if (argp->ex_addrlen == 0) {
2425 if (mp->mnt_flag & MNT_DEFEXPORTED)
2427 np = &nep->ne_defexported;
2428 np->netc_exflags = argp->ex_flags;
2429 np->netc_anon = argp->ex_anon;
2430 np->netc_anon.cr_ref = 1;
2431 mp->mnt_flag |= MNT_DEFEXPORTED;
2435 if (argp->ex_addrlen > MLEN)
2438 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
2439 np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK);
2440 bzero((caddr_t) np, i);
2441 saddr = (struct sockaddr *) (np + 1);
2442 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
2444 if (saddr->sa_len > argp->ex_addrlen)
2445 saddr->sa_len = argp->ex_addrlen;
2446 if (argp->ex_masklen) {
2447 smask = (struct sockaddr *) ((caddr_t) saddr + argp->ex_addrlen);
2448 error = copyin(argp->ex_mask, (caddr_t) smask, argp->ex_masklen);
2451 if (smask->sa_len > argp->ex_masklen)
2452 smask->sa_len = argp->ex_masklen;
2454 i = saddr->sa_family;
2455 if ((rnh = nep->ne_rtable[i]) == 0) {
2457 * Seems silly to initialize every AF when most are not used,
2458 * do so on demand here
2460 for (dom = domains; dom; dom = dom->dom_next)
2461 if (dom->dom_family == i && dom->dom_rtattach) {
2462 dom->dom_rtattach((void **) &nep->ne_rtable[i],
2466 if ((rnh = nep->ne_rtable[i]) == 0) {
2471 rn = (*rnh->rnh_addaddr) ((caddr_t) saddr, (caddr_t) smask, rnh,
2473 if (rn == 0 || np != (struct netcred *) rn) { /* already exists */
2477 np->netc_exflags = argp->ex_flags;
2478 np->netc_anon = argp->ex_anon;
2479 np->netc_anon.cr_ref = 1;
2482 free(np, M_NETADDR);
2488 vfs_free_netcred(rn, w)
2489 struct radix_node *rn;
2492 register struct radix_node_head *rnh = (struct radix_node_head *) w;
2494 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
2495 free((caddr_t) rn, M_NETADDR);
2500 * Free the net address hash lists that are hanging off the mount points.
2503 vfs_free_addrlist(nep)
2504 struct netexport *nep;
2507 register struct radix_node_head *rnh;
2509 for (i = 0; i <= AF_MAX; i++)
2510 if ((rnh = nep->ne_rtable[i])) {
2511 (*rnh->rnh_walktree) (rnh, vfs_free_netcred,
2513 free((caddr_t) rnh, M_RTABLE);
2514 nep->ne_rtable[i] = 0;
2519 vfs_export(mp, nep, argp)
2521 struct netexport *nep;
2522 struct export_args *argp;
2526 if (argp->ex_flags & MNT_DELEXPORT) {
2527 if (mp->mnt_flag & MNT_EXPUBLIC) {
2528 vfs_setpublicfs(NULL, NULL, NULL);
2529 mp->mnt_flag &= ~MNT_EXPUBLIC;
2531 vfs_free_addrlist(nep);
2532 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
2534 if (argp->ex_flags & MNT_EXPORTED) {
2535 if (argp->ex_flags & MNT_EXPUBLIC) {
2536 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
2538 mp->mnt_flag |= MNT_EXPUBLIC;
2540 if ((error = vfs_hang_addrlist(mp, nep, argp)))
2542 mp->mnt_flag |= MNT_EXPORTED;
2549 * Set the publicly exported filesystem (WebNFS). Currently, only
2550 * one public filesystem is possible in the spec (RFC 2054 and 2055)
2553 vfs_setpublicfs(mp, nep, argp)
2555 struct netexport *nep;
2556 struct export_args *argp;
2563 * mp == NULL -> invalidate the current info, the FS is
2564 * no longer exported. May be called from either vfs_export
2565 * or unmount, so check if it hasn't already been done.
2568 if (nfs_pub.np_valid) {
2569 nfs_pub.np_valid = 0;
2570 if (nfs_pub.np_index != NULL) {
2571 FREE(nfs_pub.np_index, M_TEMP);
2572 nfs_pub.np_index = NULL;
2579 * Only one allowed at a time.
2581 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
2585 * Get real filehandle for root of exported FS.
2587 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
2588 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
2590 if ((error = VFS_ROOT(mp, &rvp)))
2593 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
2599 * If an indexfile was specified, pull it in.
2601 if (argp->ex_indexfile != NULL) {
2602 MALLOC(nfs_pub.np_index, char *, MAXNAMLEN + 1, M_TEMP,
2604 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
2605 MAXNAMLEN, (size_t *)0);
2608 * Check for illegal filenames.
2610 for (cp = nfs_pub.np_index; *cp; cp++) {
2618 FREE(nfs_pub.np_index, M_TEMP);
2623 nfs_pub.np_mount = mp;
2624 nfs_pub.np_valid = 1;
2629 vfs_export_lookup(mp, nep, nam)
2630 register struct mount *mp;
2631 struct netexport *nep;
2632 struct sockaddr *nam;
2634 register struct netcred *np;
2635 register struct radix_node_head *rnh;
2636 struct sockaddr *saddr;
2639 if (mp->mnt_flag & MNT_EXPORTED) {
2641 * Lookup in the export list first.
2645 rnh = nep->ne_rtable[saddr->sa_family];
2647 np = (struct netcred *)
2648 (*rnh->rnh_matchaddr)((caddr_t)saddr,
2650 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
2655 * If no address match, use the default if it exists.
2657 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
2658 np = &nep->ne_defexported;
2664 * perform msync on all vnodes under a mount point
2665 * the mount point must be locked.
2668 vfs_msync(struct mount *mp, int flags)
2670 struct thread *td = curthread; /* XXX */
2671 struct vnode *vp, *nvp;
2672 struct vm_object *obj;
2676 simple_lock(&mntvnode_slock);
2678 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nvp) {
2679 if (vp->v_mount != mp) {
2684 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
2686 if (vp->v_flag & VXLOCK) /* XXX: what if MNT_WAIT? */
2690 * There could be hundreds of thousands of vnodes, we cannot
2691 * afford to do anything heavy-weight until we have a fairly
2692 * good indication that there is something to do.
2694 if ((vp->v_flag & VOBJDIRTY) &&
2695 (flags == MNT_WAIT || VOP_ISLOCKED(vp, NULL) == 0)) {
2696 simple_unlock(&mntvnode_slock);
2698 LK_EXCLUSIVE | LK_RETRY | LK_NOOBJ, td)) {
2699 if (VOP_GETVOBJECT(vp, &obj) == 0) {
2700 vm_object_page_clean(obj, 0, 0, flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
2704 simple_lock(&mntvnode_slock);
2705 if (TAILQ_NEXT(vp, v_nmntvnodes) != nvp) {
2712 simple_unlock(&mntvnode_slock);
2716 * Create the VM object needed for VMIO and mmap support. This
2717 * is done for all VREG files in the system. Some filesystems might
2718 * afford the additional metadata buffering capability of the
2719 * VMIO code by making the device node be VMIO mode also.
2721 * vp must be locked when vfs_object_create is called.
2724 vfs_object_create(struct vnode *vp, struct thread *td, struct ucred *cred)
2726 return (VOP_CREATEVOBJECT(vp, cred, td));
2736 simple_lock(&vnode_free_list_slock);
2737 KASSERT((vp->v_flag & VFREE) == 0, ("vnode already free"));
2738 if (vp->v_flag & VAGE) {
2739 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2741 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
2744 simple_unlock(&vnode_free_list_slock);
2745 vp->v_flag &= ~VAGE;
2746 vp->v_flag |= VFREE;
2757 simple_lock(&vnode_free_list_slock);
2758 KASSERT((vp->v_flag & VFREE) != 0, ("vnode not free"));
2759 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
2761 simple_unlock(&vnode_free_list_slock);
2762 vp->v_flag &= ~(VFREE|VAGE);
2767 * Record a process's interest in events which might happen to
2768 * a vnode. Because poll uses the historic select-style interface
2769 * internally, this routine serves as both the ``check for any
2770 * pending events'' and the ``record my interest in future events''
2771 * functions. (These are done together, while the lock is held,
2772 * to avoid race conditions.)
2775 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
2777 simple_lock(&vp->v_pollinfo.vpi_lock);
2778 if (vp->v_pollinfo.vpi_revents & events) {
2780 * This leaves events we are not interested
2781 * in available for the other process which
2782 * which presumably had requested them
2783 * (otherwise they would never have been
2786 events &= vp->v_pollinfo.vpi_revents;
2787 vp->v_pollinfo.vpi_revents &= ~events;
2789 simple_unlock(&vp->v_pollinfo.vpi_lock);
2792 vp->v_pollinfo.vpi_events |= events;
2793 selrecord(td, &vp->v_pollinfo.vpi_selinfo);
2794 simple_unlock(&vp->v_pollinfo.vpi_lock);
2799 * Note the occurrence of an event. If the VN_POLLEVENT macro is used,
2800 * it is possible for us to miss an event due to race conditions, but
2801 * that condition is expected to be rare, so for the moment it is the
2802 * preferred interface.
2805 vn_pollevent(vp, events)
2809 simple_lock(&vp->v_pollinfo.vpi_lock);
2810 if (vp->v_pollinfo.vpi_events & events) {
2812 * We clear vpi_events so that we don't
2813 * call selwakeup() twice if two events are
2814 * posted before the polling process(es) is
2815 * awakened. This also ensures that we take at
2816 * most one selwakeup() if the polling process
2817 * is no longer interested. However, it does
2818 * mean that only one event can be noticed at
2819 * a time. (Perhaps we should only clear those
2820 * event bits which we note?) XXX
2822 vp->v_pollinfo.vpi_events = 0; /* &= ~events ??? */
2823 vp->v_pollinfo.vpi_revents |= events;
2824 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2826 simple_unlock(&vp->v_pollinfo.vpi_lock);
2830 * Wake up anyone polling on vp because it is being revoked.
2831 * This depends on dead_poll() returning POLLHUP for correct
2838 simple_lock(&vp->v_pollinfo.vpi_lock);
2839 if (vp->v_pollinfo.vpi_events) {
2840 vp->v_pollinfo.vpi_events = 0;
2841 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2843 simple_unlock(&vp->v_pollinfo.vpi_lock);
2849 * Routine to create and manage a filesystem syncer vnode.
2851 #define sync_close ((int (*) __P((struct vop_close_args *)))nullop)
2852 static int sync_fsync __P((struct vop_fsync_args *));
2853 static int sync_inactive __P((struct vop_inactive_args *));
2854 static int sync_reclaim __P((struct vop_reclaim_args *));
2855 #define sync_lock ((int (*) __P((struct vop_lock_args *)))vop_nolock)
2856 #define sync_unlock ((int (*) __P((struct vop_unlock_args *)))vop_nounlock)
2857 static int sync_print __P((struct vop_print_args *));
2858 #define sync_islocked ((int(*) __P((struct vop_islocked_args *)))vop_noislocked)
2860 static vop_t **sync_vnodeop_p;
2861 static struct vnodeopv_entry_desc sync_vnodeop_entries[] = {
2862 { &vop_default_desc, (vop_t *) vop_eopnotsupp },
2863 { &vop_close_desc, (vop_t *) sync_close }, /* close */
2864 { &vop_fsync_desc, (vop_t *) sync_fsync }, /* fsync */
2865 { &vop_inactive_desc, (vop_t *) sync_inactive }, /* inactive */
2866 { &vop_reclaim_desc, (vop_t *) sync_reclaim }, /* reclaim */
2867 { &vop_lock_desc, (vop_t *) sync_lock }, /* lock */
2868 { &vop_unlock_desc, (vop_t *) sync_unlock }, /* unlock */
2869 { &vop_print_desc, (vop_t *) sync_print }, /* print */
2870 { &vop_islocked_desc, (vop_t *) sync_islocked }, /* islocked */
2873 static struct vnodeopv_desc sync_vnodeop_opv_desc =
2874 { &sync_vnodeop_p, sync_vnodeop_entries };
2876 VNODEOP_SET(sync_vnodeop_opv_desc);
2879 * Create a new filesystem syncer vnode for the specified mount point.
2882 vfs_allocate_syncvnode(mp)
2886 static long start, incr, next;
2889 /* Allocate a new vnode */
2890 if ((error = getnewvnode(VT_VFS, mp, sync_vnodeop_p, &vp)) != 0) {
2891 mp->mnt_syncer = NULL;
2896 * Place the vnode onto the syncer worklist. We attempt to
2897 * scatter them about on the list so that they will go off
2898 * at evenly distributed times even if all the filesystems
2899 * are mounted at once.
2902 if (next == 0 || next > syncer_maxdelay) {
2906 start = syncer_maxdelay / 2;
2907 incr = syncer_maxdelay;
2911 vn_syncer_add_to_worklist(vp, syncdelay > 0 ? next % syncdelay : 0);
2912 mp->mnt_syncer = vp;
2917 * Do a lazy sync of the filesystem.
2921 struct vop_fsync_args /* {
2923 struct ucred *a_cred;
2925 struct thread *a_td;
2928 struct vnode *syncvp = ap->a_vp;
2929 struct mount *mp = syncvp->v_mount;
2930 struct thread *td = ap->a_td;
2934 * We only need to do something if this is a lazy evaluation.
2936 if (ap->a_waitfor != MNT_LAZY)
2940 * Move ourselves to the back of the sync list.
2942 vn_syncer_add_to_worklist(syncvp, syncdelay);
2945 * Walk the list of vnodes pushing all that are dirty and
2946 * not already on the sync list.
2948 simple_lock(&mountlist_slock);
2949 if (vfs_busy(mp, LK_EXCLUSIVE | LK_NOWAIT, &mountlist_slock, td) != 0) {
2950 simple_unlock(&mountlist_slock);
2953 asyncflag = mp->mnt_flag & MNT_ASYNC;
2954 mp->mnt_flag &= ~MNT_ASYNC;
2955 vfs_msync(mp, MNT_NOWAIT);
2956 VFS_SYNC(mp, MNT_LAZY, ap->a_cred, td);
2958 mp->mnt_flag |= MNT_ASYNC;
2964 * The syncer vnode is no referenced.
2968 struct vop_inactive_args /* {
2979 * The syncer vnode is no longer needed and is being decommissioned.
2981 * Modifications to the worklist must be protected at splbio().
2985 struct vop_reclaim_args /* {
2989 struct vnode *vp = ap->a_vp;
2993 vp->v_mount->mnt_syncer = NULL;
2994 if (vp->v_flag & VONWORKLST) {
2995 LIST_REMOVE(vp, v_synclist);
2996 vp->v_flag &= ~VONWORKLST;
3004 * Print out a syncer vnode.
3008 struct vop_print_args /* {
3012 struct vnode *vp = ap->a_vp;
3014 printf("syncer vnode");
3015 if (vp->v_vnlock != NULL)
3016 lockmgr_printinfo(vp->v_vnlock);
3022 * extract the dev_t from a VBLK or VCHR
3028 if (vp->v_type != VBLK && vp->v_type != VCHR)
3030 return (vp->v_rdev);
3034 * Check if vnode represents a disk device
3041 if (vp->v_type != VBLK && vp->v_type != VCHR) {
3046 if (vp->v_rdev == NULL) {
3051 if (!devsw(vp->v_rdev)) {
3056 if (!(devsw(vp->v_rdev)->d_flags & D_DISK)) {
3068 struct nameidata *ndp;
3071 if (!(flags & NDF_NO_FREE_PNBUF) &&
3072 (ndp->ni_cnd.cn_flags & HASBUF)) {
3073 zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
3074 ndp->ni_cnd.cn_flags &= ~HASBUF;
3076 if (!(flags & NDF_NO_DVP_UNLOCK) &&
3077 (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
3078 ndp->ni_dvp != ndp->ni_vp)
3079 VOP_UNLOCK(ndp->ni_dvp, 0, ndp->ni_cnd.cn_td);
3080 if (!(flags & NDF_NO_DVP_RELE) &&
3081 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
3085 if (!(flags & NDF_NO_VP_UNLOCK) &&
3086 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
3087 VOP_UNLOCK(ndp->ni_vp, 0, ndp->ni_cnd.cn_td);
3088 if (!(flags & NDF_NO_VP_RELE) &&
3093 if (!(flags & NDF_NO_STARTDIR_RELE) &&
3094 (ndp->ni_cnd.cn_flags & SAVESTART)) {
3095 vrele(ndp->ni_startdir);
3096 ndp->ni_startdir = NULL;