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.8 2003/06/27 01:53:25 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 = curthread;
523 EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc, td,
528 kproc_suspend_loop();
529 if (numvnodes - freevnodes <= desiredvnodes * 9 / 10) {
531 wakeup(&vnlruproc_sig);
532 tsleep(td, PVFS, "vlruwt", hz);
536 simple_lock(&mountlist_slock);
537 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
538 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, td)) {
539 nmp = TAILQ_NEXT(mp, mnt_list);
542 done += vlrureclaim(mp);
543 simple_lock(&mountlist_slock);
544 nmp = TAILQ_NEXT(mp, mnt_list);
547 simple_unlock(&mountlist_slock);
550 tsleep(td, PPAUSE, "vlrup", hz * 3);
556 static struct kproc_desc vnlru_kp = {
561 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &vnlru_kp)
564 * Routines having to do with the management of the vnode table.
566 extern vop_t **dead_vnodeop_p;
569 * Return the next vnode from the free list.
572 getnewvnode(tag, mp, vops, vpp)
579 struct thread *td = curthread; /* XXX */
580 struct vnode *vp = NULL;
586 * Try to reuse vnodes if we hit the max. This situation only
587 * occurs in certain large-memory (2G+) situations. We cannot
588 * attempt to directly reclaim vnodes due to nasty recursion
591 while (numvnodes - freevnodes > desiredvnodes) {
592 if (vnlruproc_sig == 0) {
593 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
596 tsleep(&vnlruproc_sig, PVFS, "vlruwk", hz);
601 * Attempt to reuse a vnode already on the free list, allocating
602 * a new vnode if we can't find one or if we have not reached a
603 * good minimum for good LRU performance.
605 simple_lock(&vnode_free_list_slock);
606 if (freevnodes >= wantfreevnodes && numvnodes >= minvnodes) {
609 for (count = 0; count < freevnodes; count++) {
610 vp = TAILQ_FIRST(&vnode_free_list);
611 if (vp == NULL || vp->v_usecount)
612 panic("getnewvnode: free vnode isn't");
614 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
615 if ((VOP_GETVOBJECT(vp, &object) == 0 &&
616 (object->resident_page_count || object->ref_count)) ||
617 !simple_lock_try(&vp->v_interlock)) {
618 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
622 if (LIST_FIRST(&vp->v_cache_src)) {
624 * note: nameileafonly sysctl is temporary,
625 * for debugging only, and will eventually be
628 if (nameileafonly > 0) {
630 * Do not reuse namei-cached directory
631 * vnodes that have cached
634 if (cache_leaf_test(vp) < 0) {
635 simple_unlock(&vp->v_interlock);
636 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
640 } else if (nameileafonly < 0 ||
641 vmiodirenable == 0) {
643 * Do not reuse namei-cached directory
644 * vnodes if nameileafonly is -1 or
645 * if VMIO backing for directories is
646 * turned off (otherwise we reuse them
649 simple_unlock(&vp->v_interlock);
650 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
660 vp->v_flag |= VDOOMED;
661 vp->v_flag &= ~VFREE;
663 simple_unlock(&vnode_free_list_slock);
666 if (vp->v_type != VBAD) {
669 simple_unlock(&vp->v_interlock);
677 panic("cleaned vnode isn't");
680 panic("Clean vnode has pending I/O's");
690 vp->v_writecount = 0; /* XXX */
692 simple_unlock(&vnode_free_list_slock);
693 vp = (struct vnode *) zalloc(vnode_zone);
694 bzero((char *) vp, sizeof *vp);
695 simple_lock_init(&vp->v_interlock);
698 LIST_INIT(&vp->v_cache_src);
699 TAILQ_INIT(&vp->v_cache_dst);
703 TAILQ_INIT(&vp->v_cleanblkhd);
704 TAILQ_INIT(&vp->v_dirtyblkhd);
714 vfs_object_create(vp, td);
719 * Move a vnode from one mount queue to another.
723 register struct vnode *vp;
724 register struct mount *mp;
727 simple_lock(&mntvnode_slock);
729 * Delete from old mount point vnode list, if on one.
731 if (vp->v_mount != NULL) {
732 KASSERT(vp->v_mount->mnt_nvnodelistsize > 0,
733 ("bad mount point vnode list size"));
734 TAILQ_REMOVE(&vp->v_mount->mnt_nvnodelist, vp, v_nmntvnodes);
735 vp->v_mount->mnt_nvnodelistsize--;
738 * Insert into list of vnodes for the new mount point, if available.
740 if ((vp->v_mount = mp) == NULL) {
741 simple_unlock(&mntvnode_slock);
744 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
745 mp->mnt_nvnodelistsize++;
746 simple_unlock(&mntvnode_slock);
750 * Update outstanding I/O count and do wakeup if requested.
754 register struct buf *bp;
756 register struct vnode *vp;
758 bp->b_flags &= ~B_WRITEINPROG;
759 if ((vp = bp->b_vp)) {
761 if (vp->v_numoutput < 0)
762 panic("vwakeup: neg numoutput");
763 if ((vp->v_numoutput == 0) && (vp->v_flag & VBWAIT)) {
764 vp->v_flag &= ~VBWAIT;
765 wakeup((caddr_t) &vp->v_numoutput);
771 * Flush out and invalidate all buffers associated with a vnode.
772 * Called with the underlying object locked.
775 vinvalbuf(struct vnode *vp, int flags, struct thread *td,
776 int slpflag, int slptimeo)
778 register struct buf *bp;
779 struct buf *nbp, *blist;
783 if (flags & V_SAVE) {
785 while (vp->v_numoutput) {
786 vp->v_flag |= VBWAIT;
787 error = tsleep((caddr_t)&vp->v_numoutput,
788 slpflag | (PRIBIO + 1), "vinvlbuf", slptimeo);
794 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
796 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) != 0)
799 if (vp->v_numoutput > 0 ||
800 !TAILQ_EMPTY(&vp->v_dirtyblkhd))
801 panic("vinvalbuf: dirty bufs");
807 blist = TAILQ_FIRST(&vp->v_cleanblkhd);
809 blist = TAILQ_FIRST(&vp->v_dirtyblkhd);
813 for (bp = blist; bp; bp = nbp) {
814 nbp = TAILQ_NEXT(bp, b_vnbufs);
815 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
816 error = BUF_TIMELOCK(bp,
817 LK_EXCLUSIVE | LK_SLEEPFAIL,
818 "vinvalbuf", slpflag, slptimeo);
825 * XXX Since there are no node locks for NFS, I
826 * believe there is a slight chance that a delayed
827 * write will occur while sleeping just above, so
828 * check for it. Note that vfs_bio_awrite expects
829 * buffers to reside on a queue, while VOP_BWRITE and
832 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
835 if (bp->b_vp == vp) {
836 if (bp->b_flags & B_CLUSTEROK) {
841 bp->b_flags |= B_ASYNC;
842 VOP_BWRITE(bp->b_vp, bp);
846 (void) VOP_BWRITE(bp->b_vp, bp);
851 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
852 bp->b_flags &= ~B_ASYNC;
858 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
859 * have write I/O in-progress but if there is a VM object then the
860 * VM object can also have read-I/O in-progress.
863 while (vp->v_numoutput > 0) {
864 vp->v_flag |= VBWAIT;
865 tsleep(&vp->v_numoutput, PVM, "vnvlbv", 0);
867 if (VOP_GETVOBJECT(vp, &object) == 0) {
868 while (object->paging_in_progress)
869 vm_object_pip_sleep(object, "vnvlbx");
871 } while (vp->v_numoutput > 0);
876 * Destroy the copy in the VM cache, too.
878 simple_lock(&vp->v_interlock);
879 if (VOP_GETVOBJECT(vp, &object) == 0) {
880 vm_object_page_remove(object, 0, 0,
881 (flags & V_SAVE) ? TRUE : FALSE);
883 simple_unlock(&vp->v_interlock);
885 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd) || !TAILQ_EMPTY(&vp->v_cleanblkhd))
886 panic("vinvalbuf: flush failed");
891 * Truncate a file's buffer and pages to a specified length. This
892 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
896 vtruncbuf(struct vnode *vp, struct thread *td, off_t length, int blksize)
904 * Round up to the *next* lbn.
906 trunclbn = (length + blksize - 1) / blksize;
913 for (bp = TAILQ_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
914 nbp = TAILQ_NEXT(bp, b_vnbufs);
915 if (bp->b_lblkno >= trunclbn) {
916 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
917 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
921 bp->b_flags |= (B_INVAL | B_RELBUF);
922 bp->b_flags &= ~B_ASYNC;
927 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
929 (nbp->b_flags & B_DELWRI))) {
935 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
936 nbp = TAILQ_NEXT(bp, b_vnbufs);
937 if (bp->b_lblkno >= trunclbn) {
938 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
939 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
943 bp->b_flags |= (B_INVAL | B_RELBUF);
944 bp->b_flags &= ~B_ASYNC;
949 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
951 (nbp->b_flags & B_DELWRI) == 0)) {
960 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
961 nbp = TAILQ_NEXT(bp, b_vnbufs);
962 if ((bp->b_flags & B_DELWRI) && (bp->b_lblkno < 0)) {
963 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
964 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
968 if (bp->b_vp == vp) {
969 bp->b_flags |= B_ASYNC;
971 bp->b_flags &= ~B_ASYNC;
973 VOP_BWRITE(bp->b_vp, bp);
981 while (vp->v_numoutput > 0) {
982 vp->v_flag |= VBWAIT;
983 tsleep(&vp->v_numoutput, PVM, "vbtrunc", 0);
988 vnode_pager_setsize(vp, length);
994 * Associate a buffer with a vnode.
998 register struct vnode *vp;
999 register struct buf *bp;
1003 KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
1007 bp->b_dev = vn_todev(vp);
1009 * Insert onto list for new vnode.
1012 bp->b_xflags |= BX_VNCLEAN;
1013 bp->b_xflags &= ~BX_VNDIRTY;
1014 TAILQ_INSERT_TAIL(&vp->v_cleanblkhd, bp, b_vnbufs);
1019 * Disassociate a buffer from a vnode.
1023 register struct buf *bp;
1026 struct buflists *listheadp;
1029 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1032 * Delete from old vnode list, if on one.
1036 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) {
1037 if (bp->b_xflags & BX_VNDIRTY)
1038 listheadp = &vp->v_dirtyblkhd;
1040 listheadp = &vp->v_cleanblkhd;
1041 TAILQ_REMOVE(listheadp, bp, b_vnbufs);
1042 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1044 if ((vp->v_flag & VONWORKLST) && TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
1045 vp->v_flag &= ~VONWORKLST;
1046 LIST_REMOVE(vp, v_synclist);
1049 bp->b_vp = (struct vnode *) 0;
1054 * The workitem queue.
1056 * It is useful to delay writes of file data and filesystem metadata
1057 * for tens of seconds so that quickly created and deleted files need
1058 * not waste disk bandwidth being created and removed. To realize this,
1059 * we append vnodes to a "workitem" queue. When running with a soft
1060 * updates implementation, most pending metadata dependencies should
1061 * not wait for more than a few seconds. Thus, mounted on block devices
1062 * are delayed only about a half the time that file data is delayed.
1063 * Similarly, directory updates are more critical, so are only delayed
1064 * about a third the time that file data is delayed. Thus, there are
1065 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
1066 * one each second (driven off the filesystem syncer process). The
1067 * syncer_delayno variable indicates the next queue that is to be processed.
1068 * Items that need to be processed soon are placed in this queue:
1070 * syncer_workitem_pending[syncer_delayno]
1072 * A delay of fifteen seconds is done by placing the request fifteen
1073 * entries later in the queue:
1075 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
1080 * Add an item to the syncer work queue.
1083 vn_syncer_add_to_worklist(struct vnode *vp, int delay)
1089 if (vp->v_flag & VONWORKLST) {
1090 LIST_REMOVE(vp, v_synclist);
1093 if (delay > syncer_maxdelay - 2)
1094 delay = syncer_maxdelay - 2;
1095 slot = (syncer_delayno + delay) & syncer_mask;
1097 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], vp, v_synclist);
1098 vp->v_flag |= VONWORKLST;
1102 struct thread *updatethread;
1103 static void sched_sync __P((void));
1104 static struct kproc_desc up_kp = {
1109 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp)
1112 * System filesystem synchronizer daemon.
1117 struct synclist *slp;
1121 struct thread *td = curthread;
1123 EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc, td,
1127 kproc_suspend_loop();
1129 starttime = time_second;
1132 * Push files whose dirty time has expired. Be careful
1133 * of interrupt race on slp queue.
1136 slp = &syncer_workitem_pending[syncer_delayno];
1137 syncer_delayno += 1;
1138 if (syncer_delayno == syncer_maxdelay)
1142 while ((vp = LIST_FIRST(slp)) != NULL) {
1143 if (VOP_ISLOCKED(vp, NULL) == 0) {
1144 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1145 (void) VOP_FSYNC(vp, MNT_LAZY, td);
1146 VOP_UNLOCK(vp, 0, td);
1149 if (LIST_FIRST(slp) == vp) {
1151 * Note: v_tag VT_VFS vps can remain on the
1152 * worklist too with no dirty blocks, but
1153 * since sync_fsync() moves it to a different
1156 if (TAILQ_EMPTY(&vp->v_dirtyblkhd) &&
1157 !vn_isdisk(vp, NULL))
1158 panic("sched_sync: fsync failed vp %p tag %d", vp, vp->v_tag);
1160 * Put us back on the worklist. The worklist
1161 * routine will remove us from our current
1162 * position and then add us back in at a later
1165 vn_syncer_add_to_worklist(vp, syncdelay);
1171 * Do soft update processing.
1174 (*bioops.io_sync)(NULL);
1177 * The variable rushjob allows the kernel to speed up the
1178 * processing of the filesystem syncer process. A rushjob
1179 * value of N tells the filesystem syncer to process the next
1180 * N seconds worth of work on its queue ASAP. Currently rushjob
1181 * is used by the soft update code to speed up the filesystem
1182 * syncer process when the incore state is getting so far
1183 * ahead of the disk that the kernel memory pool is being
1184 * threatened with exhaustion.
1191 * If it has taken us less than a second to process the
1192 * current work, then wait. Otherwise start right over
1193 * again. We can still lose time if any single round
1194 * takes more than two seconds, but it does not really
1195 * matter as we are just trying to generally pace the
1196 * filesystem activity.
1198 if (time_second == starttime)
1199 tsleep(&lbolt, PPAUSE, "syncer", 0);
1204 * Request the syncer daemon to speed up its work.
1205 * We never push it to speed up more than half of its
1206 * normal turn time, otherwise it could take over the cpu.
1214 if (updatethread->td_proc->p_wchan == &lbolt) /* YYY */
1215 setrunnable(updatethread->td_proc);
1217 if (rushjob < syncdelay / 2) {
1219 stat_rush_requests += 1;
1226 * Associate a p-buffer with a vnode.
1228 * Also sets B_PAGING flag to indicate that vnode is not fully associated
1229 * with the buffer. i.e. the bp has not been linked into the vnode or
1234 register struct vnode *vp;
1235 register struct buf *bp;
1238 KASSERT(bp->b_vp == NULL, ("pbgetvp: not free"));
1241 bp->b_flags |= B_PAGING;
1242 bp->b_dev = vn_todev(vp);
1246 * Disassociate a p-buffer from a vnode.
1250 register struct buf *bp;
1253 KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL"));
1256 if (TAILQ_NEXT(bp, b_vnbufs) != NULL) {
1258 "relpbuf(): b_vp was probably reassignbuf()d %p %x",
1263 bp->b_vp = (struct vnode *) 0;
1264 bp->b_flags &= ~B_PAGING;
1268 pbreassignbuf(bp, newvp)
1270 struct vnode *newvp;
1272 if ((bp->b_flags & B_PAGING) == 0) {
1274 "pbreassignbuf() on non phys bp %p",
1282 * Reassign a buffer from one vnode to another.
1283 * Used to assign file specific control information
1284 * (indirect blocks) to the vnode to which they belong.
1287 reassignbuf(bp, newvp)
1288 register struct buf *bp;
1289 register struct vnode *newvp;
1291 struct buflists *listheadp;
1295 if (newvp == NULL) {
1296 printf("reassignbuf: NULL");
1302 * B_PAGING flagged buffers cannot be reassigned because their vp
1303 * is not fully linked in.
1305 if (bp->b_flags & B_PAGING)
1306 panic("cannot reassign paging buffer");
1310 * Delete from old vnode list, if on one.
1312 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) {
1313 if (bp->b_xflags & BX_VNDIRTY)
1314 listheadp = &bp->b_vp->v_dirtyblkhd;
1316 listheadp = &bp->b_vp->v_cleanblkhd;
1317 TAILQ_REMOVE(listheadp, bp, b_vnbufs);
1318 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1319 if (bp->b_vp != newvp) {
1321 bp->b_vp = NULL; /* for clarification */
1325 * If dirty, put on list of dirty buffers; otherwise insert onto list
1328 if (bp->b_flags & B_DELWRI) {
1331 listheadp = &newvp->v_dirtyblkhd;
1332 if ((newvp->v_flag & VONWORKLST) == 0) {
1333 switch (newvp->v_type) {
1339 if (newvp->v_specmountpoint != NULL) {
1347 vn_syncer_add_to_worklist(newvp, delay);
1349 bp->b_xflags |= BX_VNDIRTY;
1350 tbp = TAILQ_FIRST(listheadp);
1352 bp->b_lblkno == 0 ||
1353 (bp->b_lblkno > 0 && tbp->b_lblkno < 0) ||
1354 (bp->b_lblkno > 0 && bp->b_lblkno < tbp->b_lblkno)) {
1355 TAILQ_INSERT_HEAD(listheadp, bp, b_vnbufs);
1356 ++reassignbufsortgood;
1357 } else if (bp->b_lblkno < 0) {
1358 TAILQ_INSERT_TAIL(listheadp, bp, b_vnbufs);
1359 ++reassignbufsortgood;
1360 } else if (reassignbufmethod == 1) {
1362 * New sorting algorithm, only handle sequential case,
1363 * otherwise append to end (but before metadata)
1365 if ((tbp = gbincore(newvp, bp->b_lblkno - 1)) != NULL &&
1366 (tbp->b_xflags & BX_VNDIRTY)) {
1368 * Found the best place to insert the buffer
1370 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1371 ++reassignbufsortgood;
1374 * Missed, append to end, but before meta-data.
1375 * We know that the head buffer in the list is
1376 * not meta-data due to prior conditionals.
1378 * Indirect effects: NFS second stage write
1379 * tends to wind up here, giving maximum
1380 * distance between the unstable write and the
1383 tbp = TAILQ_LAST(listheadp, buflists);
1384 while (tbp && tbp->b_lblkno < 0)
1385 tbp = TAILQ_PREV(tbp, buflists, b_vnbufs);
1386 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1387 ++reassignbufsortbad;
1391 * Old sorting algorithm, scan queue and insert
1394 while ((ttbp = TAILQ_NEXT(tbp, b_vnbufs)) &&
1395 (ttbp->b_lblkno < bp->b_lblkno)) {
1399 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1402 bp->b_xflags |= BX_VNCLEAN;
1403 TAILQ_INSERT_TAIL(&newvp->v_cleanblkhd, bp, b_vnbufs);
1404 if ((newvp->v_flag & VONWORKLST) &&
1405 TAILQ_EMPTY(&newvp->v_dirtyblkhd)) {
1406 newvp->v_flag &= ~VONWORKLST;
1407 LIST_REMOVE(newvp, v_synclist);
1410 if (bp->b_vp != newvp) {
1418 * Create a vnode for a block device.
1419 * Used for mounting the root file system.
1426 register struct vnode *vp;
1434 error = getnewvnode(VT_NON, (struct mount *)0, spec_vnodeop_p, &nvp);
1447 * Add vnode to the alias list hung off the dev_t.
1449 * The reason for this gunk is that multiple vnodes can reference
1450 * the same physical device, so checking vp->v_usecount to see
1451 * how many users there are is inadequate; the v_usecount for
1452 * the vnodes need to be accumulated. vcount() does that.
1455 addaliasu(nvp, nvp_rdev)
1460 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1461 panic("addaliasu on non-special vnode");
1462 addalias(nvp, udev2dev(nvp_rdev, nvp->v_type == VBLK ? 1 : 0));
1471 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1472 panic("addalias on non-special vnode");
1475 simple_lock(&spechash_slock);
1476 SLIST_INSERT_HEAD(&dev->si_hlist, nvp, v_specnext);
1477 simple_unlock(&spechash_slock);
1481 * Grab a particular vnode from the free list, increment its
1482 * reference count and lock it. The vnode lock bit is set if the
1483 * vnode is being eliminated in vgone. The process is awakened
1484 * when the transition is completed, and an error returned to
1485 * indicate that the vnode is no longer usable (possibly having
1486 * been changed to a new file system type).
1497 * If the vnode is in the process of being cleaned out for
1498 * another use, we wait for the cleaning to finish and then
1499 * return failure. Cleaning is determined by checking that
1500 * the VXLOCK flag is set.
1502 if ((flags & LK_INTERLOCK) == 0) {
1503 simple_lock(&vp->v_interlock);
1505 if (vp->v_flag & VXLOCK) {
1506 if (vp->v_vxproc == curproc) {
1508 /* this can now occur in normal operation */
1509 log(LOG_INFO, "VXLOCK interlock avoided\n");
1512 vp->v_flag |= VXWANT;
1513 simple_unlock(&vp->v_interlock);
1514 tsleep((caddr_t)vp, PINOD, "vget", 0);
1521 if (VSHOULDBUSY(vp))
1523 if (flags & LK_TYPE_MASK) {
1524 if ((error = vn_lock(vp, flags | LK_INTERLOCK, td)) != 0) {
1526 * must expand vrele here because we do not want
1527 * to call VOP_INACTIVE if the reference count
1528 * drops back to zero since it was never really
1529 * active. We must remove it from the free list
1530 * before sleeping so that multiple processes do
1531 * not try to recycle it.
1533 simple_lock(&vp->v_interlock);
1535 if (VSHOULDFREE(vp))
1539 simple_unlock(&vp->v_interlock);
1543 simple_unlock(&vp->v_interlock);
1548 vref(struct vnode *vp)
1550 simple_lock(&vp->v_interlock);
1552 simple_unlock(&vp->v_interlock);
1556 * Vnode put/release.
1557 * If count drops to zero, call inactive routine and return to freelist.
1560 vrele(struct vnode *vp)
1562 struct thread *td = curthread; /* XXX */
1564 KASSERT(vp != NULL, ("vrele: null vp"));
1566 simple_lock(&vp->v_interlock);
1568 if (vp->v_usecount > 1) {
1571 simple_unlock(&vp->v_interlock);
1576 if (vp->v_usecount == 1) {
1579 * We must call VOP_INACTIVE with the node locked.
1580 * If we are doing a vpu, the node is already locked,
1581 * but, in the case of vrele, we must explicitly lock
1582 * the vnode before calling VOP_INACTIVE
1585 if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK, td) == 0)
1586 VOP_INACTIVE(vp, td);
1587 if (VSHOULDFREE(vp))
1593 vprint("vrele: negative ref count", vp);
1594 simple_unlock(&vp->v_interlock);
1596 panic("vrele: negative ref cnt");
1601 vput(struct vnode *vp)
1603 struct thread *td = curthread; /* XXX */
1605 KASSERT(vp != NULL, ("vput: null vp"));
1607 simple_lock(&vp->v_interlock);
1609 if (vp->v_usecount > 1) {
1611 VOP_UNLOCK(vp, LK_INTERLOCK, td);
1615 if (vp->v_usecount == 1) {
1618 * We must call VOP_INACTIVE with the node locked.
1619 * If we are doing a vpu, the node is already locked,
1620 * so we just need to release the vnode mutex.
1622 simple_unlock(&vp->v_interlock);
1623 VOP_INACTIVE(vp, td);
1624 if (VSHOULDFREE(vp))
1630 vprint("vput: negative ref count", vp);
1632 panic("vput: negative ref cnt");
1637 * Somebody doesn't want the vnode recycled.
1641 register struct vnode *vp;
1647 if (VSHOULDBUSY(vp))
1653 * One less who cares about this vnode.
1657 register struct vnode *vp;
1662 if (vp->v_holdcnt <= 0)
1663 panic("vdrop: holdcnt");
1665 if (VSHOULDFREE(vp))
1671 * Remove any vnodes in the vnode table belonging to mount point mp.
1673 * If FORCECLOSE is not specified, there should not be any active ones,
1674 * return error if any are found (nb: this is a user error, not a
1675 * system error). If FORCECLOSE is specified, detach any active vnodes
1678 * If WRITECLOSE is set, only flush out regular file vnodes open for
1681 * SKIPSYSTEM causes any vnodes marked VSYSTEM to be skipped.
1683 * `rootrefs' specifies the base reference count for the root vnode
1684 * of this filesystem. The root vnode is considered busy if its
1685 * v_usecount exceeds this value. On a successful return, vflush()
1686 * will call vrele() on the root vnode exactly rootrefs times.
1687 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
1691 static int busyprt = 0; /* print out busy vnodes */
1692 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "");
1696 vflush(mp, rootrefs, flags)
1701 struct thread *td = curthread; /* XXX */
1702 struct vnode *vp, *nvp, *rootvp = NULL;
1704 int busy = 0, error;
1707 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
1708 ("vflush: bad args"));
1710 * Get the filesystem root vnode. We can vput() it
1711 * immediately, since with rootrefs > 0, it won't go away.
1713 if ((error = VFS_ROOT(mp, &rootvp)) != 0)
1717 simple_lock(&mntvnode_slock);
1719 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp; vp = nvp) {
1721 * Make sure this vnode wasn't reclaimed in getnewvnode().
1722 * Start over if it has (it won't be on the list anymore).
1724 if (vp->v_mount != mp)
1726 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
1728 simple_lock(&vp->v_interlock);
1730 * Skip over a vnodes marked VSYSTEM.
1732 if ((flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM)) {
1733 simple_unlock(&vp->v_interlock);
1737 * If WRITECLOSE is set, flush out unlinked but still open
1738 * files (even if open only for reading) and regular file
1739 * vnodes open for writing.
1741 if ((flags & WRITECLOSE) &&
1742 (vp->v_type == VNON ||
1743 (VOP_GETATTR(vp, &vattr, td) == 0 &&
1744 vattr.va_nlink > 0)) &&
1745 (vp->v_writecount == 0 || vp->v_type != VREG)) {
1746 simple_unlock(&vp->v_interlock);
1751 * With v_usecount == 0, all we need to do is clear out the
1752 * vnode data structures and we are done.
1754 if (vp->v_usecount == 0) {
1755 simple_unlock(&mntvnode_slock);
1757 simple_lock(&mntvnode_slock);
1762 * If FORCECLOSE is set, forcibly close the vnode. For block
1763 * or character devices, revert to an anonymous device. For
1764 * all other files, just kill them.
1766 if (flags & FORCECLOSE) {
1767 simple_unlock(&mntvnode_slock);
1768 if (vp->v_type != VBLK && vp->v_type != VCHR) {
1772 vp->v_op = spec_vnodeop_p;
1773 insmntque(vp, (struct mount *) 0);
1775 simple_lock(&mntvnode_slock);
1780 vprint("vflush: busy vnode", vp);
1782 simple_unlock(&vp->v_interlock);
1785 simple_unlock(&mntvnode_slock);
1786 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
1788 * If just the root vnode is busy, and if its refcount
1789 * is equal to `rootrefs', then go ahead and kill it.
1791 simple_lock(&rootvp->v_interlock);
1792 KASSERT(busy > 0, ("vflush: not busy"));
1793 KASSERT(rootvp->v_usecount >= rootrefs, ("vflush: rootrefs"));
1794 if (busy == 1 && rootvp->v_usecount == rootrefs) {
1798 simple_unlock(&rootvp->v_interlock);
1802 for (; rootrefs > 0; rootrefs--)
1808 * We do not want to recycle the vnode too quickly.
1810 * XXX we can't move vp's around the nvnodelist without really screwing
1811 * up the efficiency of filesystem SYNC and friends. This code is
1812 * disabled until we fix the syncing code's scanning algorithm.
1815 vlruvp(struct vnode *vp)
1820 if ((mp = vp->v_mount) != NULL) {
1821 simple_lock(&mntvnode_slock);
1822 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1823 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1824 simple_unlock(&mntvnode_slock);
1830 * Disassociate the underlying file system from a vnode.
1833 vclean(struct vnode *vp, int flags, struct thread *td)
1838 * Check to see if the vnode is in use. If so we have to reference it
1839 * before we clean it out so that its count cannot fall to zero and
1840 * generate a race against ourselves to recycle it.
1842 if ((active = vp->v_usecount))
1846 * Prevent the vnode from being recycled or brought into use while we
1849 if (vp->v_flag & VXLOCK)
1850 panic("vclean: deadlock");
1851 vp->v_flag |= VXLOCK;
1852 vp->v_vxproc = curproc;
1854 * Even if the count is zero, the VOP_INACTIVE routine may still
1855 * have the object locked while it cleans it out. The VOP_LOCK
1856 * ensures that the VOP_INACTIVE routine is done with its work.
1857 * For active vnodes, it ensures that no other activity can
1858 * occur while the underlying object is being cleaned out.
1860 VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK, td);
1863 * Clean out any buffers associated with the vnode.
1865 vinvalbuf(vp, V_SAVE, td, 0, 0);
1867 VOP_DESTROYVOBJECT(vp);
1870 * If purging an active vnode, it must be closed and
1871 * deactivated before being reclaimed. Note that the
1872 * VOP_INACTIVE will unlock the vnode.
1875 if (flags & DOCLOSE)
1876 VOP_CLOSE(vp, FNONBLOCK, td);
1877 VOP_INACTIVE(vp, td);
1880 * Any other processes trying to obtain this lock must first
1881 * wait for VXLOCK to clear, then call the new lock operation.
1883 VOP_UNLOCK(vp, 0, td);
1886 * Reclaim the vnode.
1888 if (VOP_RECLAIM(vp, td))
1889 panic("vclean: cannot reclaim");
1893 * Inline copy of vrele() since VOP_INACTIVE
1894 * has already been called.
1896 simple_lock(&vp->v_interlock);
1897 if (--vp->v_usecount <= 0) {
1899 if (vp->v_usecount < 0 || vp->v_writecount != 0) {
1900 vprint("vclean: bad ref count", vp);
1901 panic("vclean: ref cnt");
1906 simple_unlock(&vp->v_interlock);
1910 vp->v_vnlock = NULL;
1912 if (VSHOULDFREE(vp))
1916 * Done with purge, notify sleepers of the grim news.
1918 vp->v_op = dead_vnodeop_p;
1921 vp->v_flag &= ~VXLOCK;
1922 vp->v_vxproc = NULL;
1923 if (vp->v_flag & VXWANT) {
1924 vp->v_flag &= ~VXWANT;
1925 wakeup((caddr_t) vp);
1930 * Eliminate all activity associated with the requested vnode
1931 * and with all vnodes aliased to the requested vnode.
1935 struct vop_revoke_args /* {
1940 struct vnode *vp, *vq;
1943 KASSERT((ap->a_flags & REVOKEALL) != 0, ("vop_revoke"));
1947 * If a vgone (or vclean) is already in progress,
1948 * wait until it is done and return.
1950 if (vp->v_flag & VXLOCK) {
1951 vp->v_flag |= VXWANT;
1952 simple_unlock(&vp->v_interlock);
1953 tsleep((caddr_t)vp, PINOD, "vop_revokeall", 0);
1958 simple_lock(&spechash_slock);
1959 vq = SLIST_FIRST(&dev->si_hlist);
1960 simple_unlock(&spechash_slock);
1969 * Recycle an unused vnode to the front of the free list.
1970 * Release the passed interlock if the vnode will be recycled.
1973 vrecycle(struct vnode *vp, struct simplelock *inter_lkp, struct thread *td)
1975 simple_lock(&vp->v_interlock);
1976 if (vp->v_usecount == 0) {
1978 simple_unlock(inter_lkp);
1983 simple_unlock(&vp->v_interlock);
1988 * Eliminate all activity associated with a vnode
1989 * in preparation for reuse.
1992 vgone(struct vnode *vp)
1994 struct thread *td = curthread; /* XXX */
1996 simple_lock(&vp->v_interlock);
2001 * vgone, with the vp interlock held.
2004 vgonel(struct vnode *vp, struct thread *td)
2009 * If a vgone (or vclean) is already in progress,
2010 * wait until it is done and return.
2012 if (vp->v_flag & VXLOCK) {
2013 vp->v_flag |= VXWANT;
2014 simple_unlock(&vp->v_interlock);
2015 tsleep((caddr_t)vp, PINOD, "vgone", 0);
2020 * Clean out the filesystem specific data.
2022 vclean(vp, DOCLOSE, td);
2023 simple_lock(&vp->v_interlock);
2026 * Delete from old mount point vnode list, if on one.
2028 if (vp->v_mount != NULL)
2029 insmntque(vp, (struct mount *)0);
2031 * If special device, remove it from special device alias list
2034 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
2035 simple_lock(&spechash_slock);
2036 SLIST_REMOVE(&vp->v_hashchain, vp, vnode, v_specnext);
2037 freedev(vp->v_rdev);
2038 simple_unlock(&spechash_slock);
2043 * If it is on the freelist and not already at the head,
2044 * move it to the head of the list. The test of the
2045 * VDOOMED flag and the reference count of zero is because
2046 * it will be removed from the free list by getnewvnode,
2047 * but will not have its reference count incremented until
2048 * after calling vgone. If the reference count were
2049 * incremented first, vgone would (incorrectly) try to
2050 * close the previous instance of the underlying object.
2052 if (vp->v_usecount == 0 && !(vp->v_flag & VDOOMED)) {
2054 simple_lock(&vnode_free_list_slock);
2055 if (vp->v_flag & VFREE)
2056 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
2059 vp->v_flag |= VFREE;
2060 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2061 simple_unlock(&vnode_free_list_slock);
2066 simple_unlock(&vp->v_interlock);
2070 * Lookup a vnode by device number.
2073 vfinddev(dev, type, vpp)
2080 simple_lock(&spechash_slock);
2081 SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) {
2082 if (type == vp->v_type) {
2084 simple_unlock(&spechash_slock);
2088 simple_unlock(&spechash_slock);
2093 * Calculate the total number of references to a special device.
2103 simple_lock(&spechash_slock);
2104 SLIST_FOREACH(vq, &vp->v_hashchain, v_specnext)
2105 count += vq->v_usecount;
2106 simple_unlock(&spechash_slock);
2111 * Same as above, but using the dev_t as argument
2120 vp = SLIST_FIRST(&dev->si_hlist);
2127 * Print out a description of a vnode.
2129 static char *typename[] =
2130 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
2140 printf("%s: %p: ", label, (void *)vp);
2142 printf("%p: ", (void *)vp);
2143 printf("type %s, usecount %d, writecount %d, refcount %d,",
2144 typename[vp->v_type], vp->v_usecount, vp->v_writecount,
2147 if (vp->v_flag & VROOT)
2148 strcat(buf, "|VROOT");
2149 if (vp->v_flag & VTEXT)
2150 strcat(buf, "|VTEXT");
2151 if (vp->v_flag & VSYSTEM)
2152 strcat(buf, "|VSYSTEM");
2153 if (vp->v_flag & VXLOCK)
2154 strcat(buf, "|VXLOCK");
2155 if (vp->v_flag & VXWANT)
2156 strcat(buf, "|VXWANT");
2157 if (vp->v_flag & VBWAIT)
2158 strcat(buf, "|VBWAIT");
2159 if (vp->v_flag & VDOOMED)
2160 strcat(buf, "|VDOOMED");
2161 if (vp->v_flag & VFREE)
2162 strcat(buf, "|VFREE");
2163 if (vp->v_flag & VOBJBUF)
2164 strcat(buf, "|VOBJBUF");
2166 printf(" flags (%s)", &buf[1]);
2167 if (vp->v_data == NULL) {
2176 #include <ddb/ddb.h>
2178 * List all of the locked vnodes in the system.
2179 * Called when debugging the kernel.
2181 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
2183 struct thread *td = curthread; /* XXX */
2184 struct mount *mp, *nmp;
2187 printf("Locked vnodes\n");
2188 simple_lock(&mountlist_slock);
2189 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2190 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, td)) {
2191 nmp = TAILQ_NEXT(mp, mnt_list);
2194 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
2195 if (VOP_ISLOCKED(vp, NULL))
2196 vprint((char *)0, vp);
2198 simple_lock(&mountlist_slock);
2199 nmp = TAILQ_NEXT(mp, mnt_list);
2202 simple_unlock(&mountlist_slock);
2207 * Top level filesystem related information gathering.
2209 static int sysctl_ovfs_conf __P((SYSCTL_HANDLER_ARGS));
2212 vfs_sysctl(SYSCTL_HANDLER_ARGS)
2214 int *name = (int *)arg1 - 1; /* XXX */
2215 u_int namelen = arg2 + 1; /* XXX */
2216 struct vfsconf *vfsp;
2218 #if 1 || defined(COMPAT_PRELITE2)
2219 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
2221 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
2225 /* all sysctl names at this level are at least name and field */
2227 return (ENOTDIR); /* overloaded */
2228 if (name[0] != VFS_GENERIC) {
2229 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2230 if (vfsp->vfc_typenum == name[0])
2233 return (EOPNOTSUPP);
2234 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
2235 oldp, oldlenp, newp, newlen, p));
2239 case VFS_MAXTYPENUM:
2242 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
2245 return (ENOTDIR); /* overloaded */
2246 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2247 if (vfsp->vfc_typenum == name[2])
2250 return (EOPNOTSUPP);
2251 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
2253 return (EOPNOTSUPP);
2256 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
2257 "Generic filesystem");
2259 #if 1 || defined(COMPAT_PRELITE2)
2262 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
2265 struct vfsconf *vfsp;
2266 struct ovfsconf ovfs;
2268 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
2269 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
2270 strcpy(ovfs.vfc_name, vfsp->vfc_name);
2271 ovfs.vfc_index = vfsp->vfc_typenum;
2272 ovfs.vfc_refcount = vfsp->vfc_refcount;
2273 ovfs.vfc_flags = vfsp->vfc_flags;
2274 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
2281 #endif /* 1 || COMPAT_PRELITE2 */
2284 #define KINFO_VNODESLOP 10
2286 * Dump vnode list (via sysctl).
2287 * Copyout address of vnode followed by vnode.
2291 sysctl_vnode(SYSCTL_HANDLER_ARGS)
2293 struct proc *p = curproc; /* XXX */
2294 struct mount *mp, *nmp;
2295 struct vnode *nvp, *vp;
2298 #define VPTRSZ sizeof (struct vnode *)
2299 #define VNODESZ sizeof (struct vnode)
2302 if (!req->oldptr) /* Make an estimate */
2303 return (SYSCTL_OUT(req, 0,
2304 (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ)));
2306 simple_lock(&mountlist_slock);
2307 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2308 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
2309 nmp = TAILQ_NEXT(mp, mnt_list);
2313 simple_lock(&mntvnode_slock);
2314 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
2318 * Check that the vp is still associated with
2319 * this filesystem. RACE: could have been
2320 * recycled onto the same filesystem.
2322 if (vp->v_mount != mp) {
2323 simple_unlock(&mntvnode_slock);
2326 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
2327 simple_unlock(&mntvnode_slock);
2328 if ((error = SYSCTL_OUT(req, &vp, VPTRSZ)) ||
2329 (error = SYSCTL_OUT(req, vp, VNODESZ)))
2331 simple_lock(&mntvnode_slock);
2333 simple_unlock(&mntvnode_slock);
2334 simple_lock(&mountlist_slock);
2335 nmp = TAILQ_NEXT(mp, mnt_list);
2338 simple_unlock(&mountlist_slock);
2346 * Exporting the vnode list on large systems causes them to crash.
2347 * Exporting the vnode list on medium systems causes sysctl to coredump.
2350 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD,
2351 0, 0, sysctl_vnode, "S,vnode", "");
2355 * Check to see if a filesystem is mounted on a block device.
2362 if (vp->v_specmountpoint != NULL)
2368 * Unmount all filesystems. The list is traversed in reverse order
2369 * of mounting to avoid dependencies.
2375 struct thread *td = curthread;
2378 if (td->td_proc == NULL)
2379 td = initproc->p_thread; /* XXX XXX use proc0 instead? */
2382 * Since this only runs when rebooting, it is not interlocked.
2384 while(!TAILQ_EMPTY(&mountlist)) {
2385 mp = TAILQ_LAST(&mountlist, mntlist);
2386 error = dounmount(mp, MNT_FORCE, td);
2388 TAILQ_REMOVE(&mountlist, mp, mnt_list);
2389 printf("unmount of %s failed (",
2390 mp->mnt_stat.f_mntonname);
2394 printf("%d)\n", error);
2396 /* The unmount has removed mp from the mountlist */
2402 * Build hash lists of net addresses and hang them off the mount point.
2403 * Called by ufs_mount() to set up the lists of export addresses.
2406 vfs_hang_addrlist(mp, nep, argp)
2408 struct netexport *nep;
2409 struct export_args *argp;
2411 register struct netcred *np;
2412 register struct radix_node_head *rnh;
2414 struct radix_node *rn;
2415 struct sockaddr *saddr, *smask = 0;
2419 if (argp->ex_addrlen == 0) {
2420 if (mp->mnt_flag & MNT_DEFEXPORTED)
2422 np = &nep->ne_defexported;
2423 np->netc_exflags = argp->ex_flags;
2424 np->netc_anon = argp->ex_anon;
2425 np->netc_anon.cr_ref = 1;
2426 mp->mnt_flag |= MNT_DEFEXPORTED;
2430 if (argp->ex_addrlen > MLEN)
2433 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
2434 np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK);
2435 bzero((caddr_t) np, i);
2436 saddr = (struct sockaddr *) (np + 1);
2437 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
2439 if (saddr->sa_len > argp->ex_addrlen)
2440 saddr->sa_len = argp->ex_addrlen;
2441 if (argp->ex_masklen) {
2442 smask = (struct sockaddr *) ((caddr_t) saddr + argp->ex_addrlen);
2443 error = copyin(argp->ex_mask, (caddr_t) smask, argp->ex_masklen);
2446 if (smask->sa_len > argp->ex_masklen)
2447 smask->sa_len = argp->ex_masklen;
2449 i = saddr->sa_family;
2450 if ((rnh = nep->ne_rtable[i]) == 0) {
2452 * Seems silly to initialize every AF when most are not used,
2453 * do so on demand here
2455 for (dom = domains; dom; dom = dom->dom_next)
2456 if (dom->dom_family == i && dom->dom_rtattach) {
2457 dom->dom_rtattach((void **) &nep->ne_rtable[i],
2461 if ((rnh = nep->ne_rtable[i]) == 0) {
2466 rn = (*rnh->rnh_addaddr) ((caddr_t) saddr, (caddr_t) smask, rnh,
2468 if (rn == 0 || np != (struct netcred *) rn) { /* already exists */
2472 np->netc_exflags = argp->ex_flags;
2473 np->netc_anon = argp->ex_anon;
2474 np->netc_anon.cr_ref = 1;
2477 free(np, M_NETADDR);
2483 vfs_free_netcred(rn, w)
2484 struct radix_node *rn;
2487 register struct radix_node_head *rnh = (struct radix_node_head *) w;
2489 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
2490 free((caddr_t) rn, M_NETADDR);
2495 * Free the net address hash lists that are hanging off the mount points.
2498 vfs_free_addrlist(nep)
2499 struct netexport *nep;
2502 register struct radix_node_head *rnh;
2504 for (i = 0; i <= AF_MAX; i++)
2505 if ((rnh = nep->ne_rtable[i])) {
2506 (*rnh->rnh_walktree) (rnh, vfs_free_netcred,
2508 free((caddr_t) rnh, M_RTABLE);
2509 nep->ne_rtable[i] = 0;
2514 vfs_export(mp, nep, argp)
2516 struct netexport *nep;
2517 struct export_args *argp;
2521 if (argp->ex_flags & MNT_DELEXPORT) {
2522 if (mp->mnt_flag & MNT_EXPUBLIC) {
2523 vfs_setpublicfs(NULL, NULL, NULL);
2524 mp->mnt_flag &= ~MNT_EXPUBLIC;
2526 vfs_free_addrlist(nep);
2527 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
2529 if (argp->ex_flags & MNT_EXPORTED) {
2530 if (argp->ex_flags & MNT_EXPUBLIC) {
2531 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
2533 mp->mnt_flag |= MNT_EXPUBLIC;
2535 if ((error = vfs_hang_addrlist(mp, nep, argp)))
2537 mp->mnt_flag |= MNT_EXPORTED;
2544 * Set the publicly exported filesystem (WebNFS). Currently, only
2545 * one public filesystem is possible in the spec (RFC 2054 and 2055)
2548 vfs_setpublicfs(mp, nep, argp)
2550 struct netexport *nep;
2551 struct export_args *argp;
2558 * mp == NULL -> invalidate the current info, the FS is
2559 * no longer exported. May be called from either vfs_export
2560 * or unmount, so check if it hasn't already been done.
2563 if (nfs_pub.np_valid) {
2564 nfs_pub.np_valid = 0;
2565 if (nfs_pub.np_index != NULL) {
2566 FREE(nfs_pub.np_index, M_TEMP);
2567 nfs_pub.np_index = NULL;
2574 * Only one allowed at a time.
2576 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
2580 * Get real filehandle for root of exported FS.
2582 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
2583 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
2585 if ((error = VFS_ROOT(mp, &rvp)))
2588 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
2594 * If an indexfile was specified, pull it in.
2596 if (argp->ex_indexfile != NULL) {
2597 MALLOC(nfs_pub.np_index, char *, MAXNAMLEN + 1, M_TEMP,
2599 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
2600 MAXNAMLEN, (size_t *)0);
2603 * Check for illegal filenames.
2605 for (cp = nfs_pub.np_index; *cp; cp++) {
2613 FREE(nfs_pub.np_index, M_TEMP);
2618 nfs_pub.np_mount = mp;
2619 nfs_pub.np_valid = 1;
2624 vfs_export_lookup(mp, nep, nam)
2625 register struct mount *mp;
2626 struct netexport *nep;
2627 struct sockaddr *nam;
2629 register struct netcred *np;
2630 register struct radix_node_head *rnh;
2631 struct sockaddr *saddr;
2634 if (mp->mnt_flag & MNT_EXPORTED) {
2636 * Lookup in the export list first.
2640 rnh = nep->ne_rtable[saddr->sa_family];
2642 np = (struct netcred *)
2643 (*rnh->rnh_matchaddr)((caddr_t)saddr,
2645 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
2650 * If no address match, use the default if it exists.
2652 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
2653 np = &nep->ne_defexported;
2659 * perform msync on all vnodes under a mount point
2660 * the mount point must be locked.
2663 vfs_msync(struct mount *mp, int flags)
2665 struct thread *td = curthread; /* XXX */
2666 struct vnode *vp, *nvp;
2667 struct vm_object *obj;
2671 simple_lock(&mntvnode_slock);
2673 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nvp) {
2674 if (vp->v_mount != mp) {
2679 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
2681 if (vp->v_flag & VXLOCK) /* XXX: what if MNT_WAIT? */
2685 * There could be hundreds of thousands of vnodes, we cannot
2686 * afford to do anything heavy-weight until we have a fairly
2687 * good indication that there is something to do.
2689 if ((vp->v_flag & VOBJDIRTY) &&
2690 (flags == MNT_WAIT || VOP_ISLOCKED(vp, NULL) == 0)) {
2691 simple_unlock(&mntvnode_slock);
2693 LK_EXCLUSIVE | LK_RETRY | LK_NOOBJ, td)) {
2694 if (VOP_GETVOBJECT(vp, &obj) == 0) {
2695 vm_object_page_clean(obj, 0, 0, flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
2699 simple_lock(&mntvnode_slock);
2700 if (TAILQ_NEXT(vp, v_nmntvnodes) != nvp) {
2707 simple_unlock(&mntvnode_slock);
2711 * Create the VM object needed for VMIO and mmap support. This
2712 * is done for all VREG files in the system. Some filesystems might
2713 * afford the additional metadata buffering capability of the
2714 * VMIO code by making the device node be VMIO mode also.
2716 * vp must be locked when vfs_object_create is called.
2719 vfs_object_create(struct vnode *vp, struct thread *td)
2721 return (VOP_CREATEVOBJECT(vp, td));
2731 simple_lock(&vnode_free_list_slock);
2732 KASSERT((vp->v_flag & VFREE) == 0, ("vnode already free"));
2733 if (vp->v_flag & VAGE) {
2734 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2736 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
2739 simple_unlock(&vnode_free_list_slock);
2740 vp->v_flag &= ~VAGE;
2741 vp->v_flag |= VFREE;
2752 simple_lock(&vnode_free_list_slock);
2753 KASSERT((vp->v_flag & VFREE) != 0, ("vnode not free"));
2754 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
2756 simple_unlock(&vnode_free_list_slock);
2757 vp->v_flag &= ~(VFREE|VAGE);
2762 * Record a process's interest in events which might happen to
2763 * a vnode. Because poll uses the historic select-style interface
2764 * internally, this routine serves as both the ``check for any
2765 * pending events'' and the ``record my interest in future events''
2766 * functions. (These are done together, while the lock is held,
2767 * to avoid race conditions.)
2770 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
2772 simple_lock(&vp->v_pollinfo.vpi_lock);
2773 if (vp->v_pollinfo.vpi_revents & events) {
2775 * This leaves events we are not interested
2776 * in available for the other process which
2777 * which presumably had requested them
2778 * (otherwise they would never have been
2781 events &= vp->v_pollinfo.vpi_revents;
2782 vp->v_pollinfo.vpi_revents &= ~events;
2784 simple_unlock(&vp->v_pollinfo.vpi_lock);
2787 vp->v_pollinfo.vpi_events |= events;
2788 selrecord(td, &vp->v_pollinfo.vpi_selinfo);
2789 simple_unlock(&vp->v_pollinfo.vpi_lock);
2794 * Note the occurrence of an event. If the VN_POLLEVENT macro is used,
2795 * it is possible for us to miss an event due to race conditions, but
2796 * that condition is expected to be rare, so for the moment it is the
2797 * preferred interface.
2800 vn_pollevent(vp, events)
2804 simple_lock(&vp->v_pollinfo.vpi_lock);
2805 if (vp->v_pollinfo.vpi_events & events) {
2807 * We clear vpi_events so that we don't
2808 * call selwakeup() twice if two events are
2809 * posted before the polling process(es) is
2810 * awakened. This also ensures that we take at
2811 * most one selwakeup() if the polling process
2812 * is no longer interested. However, it does
2813 * mean that only one event can be noticed at
2814 * a time. (Perhaps we should only clear those
2815 * event bits which we note?) XXX
2817 vp->v_pollinfo.vpi_events = 0; /* &= ~events ??? */
2818 vp->v_pollinfo.vpi_revents |= events;
2819 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2821 simple_unlock(&vp->v_pollinfo.vpi_lock);
2825 * Wake up anyone polling on vp because it is being revoked.
2826 * This depends on dead_poll() returning POLLHUP for correct
2833 simple_lock(&vp->v_pollinfo.vpi_lock);
2834 if (vp->v_pollinfo.vpi_events) {
2835 vp->v_pollinfo.vpi_events = 0;
2836 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2838 simple_unlock(&vp->v_pollinfo.vpi_lock);
2844 * Routine to create and manage a filesystem syncer vnode.
2846 #define sync_close ((int (*) __P((struct vop_close_args *)))nullop)
2847 static int sync_fsync __P((struct vop_fsync_args *));
2848 static int sync_inactive __P((struct vop_inactive_args *));
2849 static int sync_reclaim __P((struct vop_reclaim_args *));
2850 #define sync_lock ((int (*) __P((struct vop_lock_args *)))vop_nolock)
2851 #define sync_unlock ((int (*) __P((struct vop_unlock_args *)))vop_nounlock)
2852 static int sync_print __P((struct vop_print_args *));
2853 #define sync_islocked ((int(*) __P((struct vop_islocked_args *)))vop_noislocked)
2855 static vop_t **sync_vnodeop_p;
2856 static struct vnodeopv_entry_desc sync_vnodeop_entries[] = {
2857 { &vop_default_desc, (vop_t *) vop_eopnotsupp },
2858 { &vop_close_desc, (vop_t *) sync_close }, /* close */
2859 { &vop_fsync_desc, (vop_t *) sync_fsync }, /* fsync */
2860 { &vop_inactive_desc, (vop_t *) sync_inactive }, /* inactive */
2861 { &vop_reclaim_desc, (vop_t *) sync_reclaim }, /* reclaim */
2862 { &vop_lock_desc, (vop_t *) sync_lock }, /* lock */
2863 { &vop_unlock_desc, (vop_t *) sync_unlock }, /* unlock */
2864 { &vop_print_desc, (vop_t *) sync_print }, /* print */
2865 { &vop_islocked_desc, (vop_t *) sync_islocked }, /* islocked */
2868 static struct vnodeopv_desc sync_vnodeop_opv_desc =
2869 { &sync_vnodeop_p, sync_vnodeop_entries };
2871 VNODEOP_SET(sync_vnodeop_opv_desc);
2874 * Create a new filesystem syncer vnode for the specified mount point.
2877 vfs_allocate_syncvnode(mp)
2881 static long start, incr, next;
2884 /* Allocate a new vnode */
2885 if ((error = getnewvnode(VT_VFS, mp, sync_vnodeop_p, &vp)) != 0) {
2886 mp->mnt_syncer = NULL;
2891 * Place the vnode onto the syncer worklist. We attempt to
2892 * scatter them about on the list so that they will go off
2893 * at evenly distributed times even if all the filesystems
2894 * are mounted at once.
2897 if (next == 0 || next > syncer_maxdelay) {
2901 start = syncer_maxdelay / 2;
2902 incr = syncer_maxdelay;
2906 vn_syncer_add_to_worklist(vp, syncdelay > 0 ? next % syncdelay : 0);
2907 mp->mnt_syncer = vp;
2912 * Do a lazy sync of the filesystem.
2916 struct vop_fsync_args /* {
2918 struct ucred *a_cred;
2920 struct thread *a_td;
2923 struct vnode *syncvp = ap->a_vp;
2924 struct mount *mp = syncvp->v_mount;
2925 struct thread *td = ap->a_td;
2929 * We only need to do something if this is a lazy evaluation.
2931 if (ap->a_waitfor != MNT_LAZY)
2935 * Move ourselves to the back of the sync list.
2937 vn_syncer_add_to_worklist(syncvp, syncdelay);
2940 * Walk the list of vnodes pushing all that are dirty and
2941 * not already on the sync list.
2943 simple_lock(&mountlist_slock);
2944 if (vfs_busy(mp, LK_EXCLUSIVE | LK_NOWAIT, &mountlist_slock, td) != 0) {
2945 simple_unlock(&mountlist_slock);
2948 asyncflag = mp->mnt_flag & MNT_ASYNC;
2949 mp->mnt_flag &= ~MNT_ASYNC;
2950 vfs_msync(mp, MNT_NOWAIT);
2951 VFS_SYNC(mp, MNT_LAZY, td);
2953 mp->mnt_flag |= MNT_ASYNC;
2959 * The syncer vnode is no referenced.
2963 struct vop_inactive_args /* {
2974 * The syncer vnode is no longer needed and is being decommissioned.
2976 * Modifications to the worklist must be protected at splbio().
2980 struct vop_reclaim_args /* {
2984 struct vnode *vp = ap->a_vp;
2988 vp->v_mount->mnt_syncer = NULL;
2989 if (vp->v_flag & VONWORKLST) {
2990 LIST_REMOVE(vp, v_synclist);
2991 vp->v_flag &= ~VONWORKLST;
2999 * Print out a syncer vnode.
3003 struct vop_print_args /* {
3007 struct vnode *vp = ap->a_vp;
3009 printf("syncer vnode");
3010 if (vp->v_vnlock != NULL)
3011 lockmgr_printinfo(vp->v_vnlock);
3017 * extract the dev_t from a VBLK or VCHR
3023 if (vp->v_type != VBLK && vp->v_type != VCHR)
3025 return (vp->v_rdev);
3029 * Check if vnode represents a disk device
3036 if (vp->v_type != VBLK && vp->v_type != VCHR) {
3041 if (vp->v_rdev == NULL) {
3046 if (!devsw(vp->v_rdev)) {
3051 if (!(devsw(vp->v_rdev)->d_flags & D_DISK)) {
3063 struct nameidata *ndp;
3066 if (!(flags & NDF_NO_FREE_PNBUF) &&
3067 (ndp->ni_cnd.cn_flags & HASBUF)) {
3068 zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
3069 ndp->ni_cnd.cn_flags &= ~HASBUF;
3071 if (!(flags & NDF_NO_DVP_UNLOCK) &&
3072 (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
3073 ndp->ni_dvp != ndp->ni_vp)
3074 VOP_UNLOCK(ndp->ni_dvp, 0, ndp->ni_cnd.cn_td);
3075 if (!(flags & NDF_NO_DVP_RELE) &&
3076 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
3080 if (!(flags & NDF_NO_VP_UNLOCK) &&
3081 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
3082 VOP_UNLOCK(ndp->ni_vp, 0, ndp->ni_cnd.cn_td);
3083 if (!(flags & NDF_NO_VP_RELE) &&
3088 if (!(flags & NDF_NO_STARTDIR_RELE) &&
3089 (ndp->ni_cnd.cn_flags & SAVESTART)) {
3090 vrele(ndp->ni_startdir);
3091 ndp->ni_startdir = NULL;