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.19 2003/09/01 00:35:29 hmp 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>
84 #include <sys/thread2.h>
86 static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
88 static void insmntque (struct vnode *vp, struct mount *mp);
89 static void vclean (struct vnode *vp, int flags, struct thread *td);
90 static unsigned long numvnodes;
91 static void vlruvp(struct vnode *vp);
92 SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, "");
94 enum vtype iftovt_tab[16] = {
95 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
96 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
99 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
100 S_IFSOCK, S_IFIFO, S_IFMT,
103 static TAILQ_HEAD(freelst, vnode) vnode_free_list; /* vnode free list */
105 static u_long wantfreevnodes = 25;
106 SYSCTL_INT(_debug, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, "");
107 static u_long freevnodes = 0;
108 SYSCTL_INT(_debug, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0, "");
110 static int reassignbufcalls;
111 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0, "");
112 static int reassignbufloops;
113 SYSCTL_INT(_vfs, OID_AUTO, reassignbufloops, CTLFLAG_RW, &reassignbufloops, 0, "");
114 static int reassignbufsortgood;
115 SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortgood, CTLFLAG_RW, &reassignbufsortgood, 0, "");
116 static int reassignbufsortbad;
117 SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortbad, CTLFLAG_RW, &reassignbufsortbad, 0, "");
118 static int reassignbufmethod = 1;
119 SYSCTL_INT(_vfs, OID_AUTO, reassignbufmethod, CTLFLAG_RW, &reassignbufmethod, 0, "");
120 static int nameileafonly = 0;
121 SYSCTL_INT(_vfs, OID_AUTO, nameileafonly, CTLFLAG_RW, &nameileafonly, 0, "");
123 #ifdef ENABLE_VFS_IOOPT
125 SYSCTL_INT(_vfs, OID_AUTO, ioopt, CTLFLAG_RW, &vfs_ioopt, 0, "");
128 struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist); /* mounted fs */
129 struct lwkt_token mountlist_token;
130 struct lwkt_token mntvnode_token;
131 int nfs_mount_type = -1;
132 static struct lwkt_token mntid_token;
133 static struct lwkt_token vnode_free_list_token;
134 static struct lwkt_token spechash_token;
135 struct nfs_public nfs_pub; /* publicly exported FS */
136 static vm_zone_t vnode_zone;
139 * The workitem queue.
141 #define SYNCER_MAXDELAY 32
142 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
143 time_t syncdelay = 30; /* max time to delay syncing data */
144 SYSCTL_INT(_kern, OID_AUTO, syncdelay, CTLFLAG_RW, &syncdelay, 0,
145 "VFS data synchronization delay");
146 time_t filedelay = 30; /* time to delay syncing files */
147 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0,
148 "File synchronization delay");
149 time_t dirdelay = 29; /* time to delay syncing directories */
150 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0,
151 "Directory synchronization delay");
152 time_t metadelay = 28; /* time to delay syncing metadata */
153 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0,
154 "VFS metadata synchronization delay");
155 static int rushjob; /* number of slots to run ASAP */
156 static int stat_rush_requests; /* number of times I/O speeded up */
157 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0, "");
159 static int syncer_delayno = 0;
160 static long syncer_mask;
161 LIST_HEAD(synclist, vnode);
162 static struct synclist *syncer_workitem_pending;
165 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
166 &desiredvnodes, 0, "Maximum number of vnodes");
167 static int minvnodes;
168 SYSCTL_INT(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
169 &minvnodes, 0, "Minimum number of vnodes");
170 static int vnlru_nowhere = 0;
171 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW, &vnlru_nowhere, 0,
172 "Number of times the vnlru process ran without success");
174 static void vfs_free_addrlist (struct netexport *nep);
175 static int vfs_free_netcred (struct radix_node *rn, void *w);
176 static int vfs_hang_addrlist (struct mount *mp, struct netexport *nep,
177 struct export_args *argp);
180 * Initialize the vnode management data structures.
186 desiredvnodes = maxproc + vmstats.v_page_count / 4;
187 minvnodes = desiredvnodes / 4;
188 lwkt_inittoken(&mntvnode_token);
189 lwkt_inittoken(&mntid_token);
190 lwkt_inittoken(&spechash_token);
191 TAILQ_INIT(&vnode_free_list);
192 lwkt_inittoken(&vnode_free_list_token);
193 vnode_zone = zinit("VNODE", sizeof (struct vnode), 0, 0, 5);
195 * Initialize the filesystem syncer.
197 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE,
199 syncer_maxdelay = syncer_mask + 1;
203 * Mark a mount point as busy. Used to synchronize access and to delay
204 * unmounting. Interlock is not released on failure.
207 vfs_busy(struct mount *mp, int flags, struct lwkt_token *interlkp,
212 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
213 if (flags & LK_NOWAIT)
215 mp->mnt_kern_flag |= MNTK_MWAIT;
217 lwkt_reltoken(interlkp);
220 * Since all busy locks are shared except the exclusive
221 * lock granted when unmounting, the only place that a
222 * wakeup needs to be done is at the release of the
223 * exclusive lock at the end of dounmount.
225 tsleep((caddr_t)mp, 0, "vfs_busy", 0);
227 lwkt_gettoken(interlkp);
231 lkflags = LK_SHARED | LK_NOPAUSE;
233 lkflags |= LK_INTERLOCK;
234 if (lockmgr(&mp->mnt_lock, lkflags, interlkp, td))
235 panic("vfs_busy: unexpected lock failure");
240 * Free a busy filesystem.
243 vfs_unbusy(struct mount *mp, struct thread *td)
245 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td);
249 * Lookup a filesystem type, and if found allocate and initialize
250 * a mount structure for it.
252 * Devname is usually updated by mount(8) after booting.
255 vfs_rootmountalloc(char *fstypename, char *devname, struct mount **mpp)
257 struct thread *td = curthread; /* XXX */
258 struct vfsconf *vfsp;
261 if (fstypename == NULL)
263 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
264 if (!strcmp(vfsp->vfc_name, fstypename))
268 mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
269 bzero((char *)mp, (u_long)sizeof(struct mount));
270 lockinit(&mp->mnt_lock, 0, "vfslock", VLKTIMEOUT, LK_NOPAUSE);
271 (void)vfs_busy(mp, LK_NOWAIT, 0, td);
272 TAILQ_INIT(&mp->mnt_nvnodelist);
273 TAILQ_INIT(&mp->mnt_reservedvnlist);
274 mp->mnt_nvnodelistsize = 0;
276 mp->mnt_op = vfsp->vfc_vfsops;
277 mp->mnt_flag = MNT_RDONLY;
278 mp->mnt_vnodecovered = NULLVP;
279 vfsp->vfc_refcount++;
280 mp->mnt_iosize_max = DFLTPHYS;
281 mp->mnt_stat.f_type = vfsp->vfc_typenum;
282 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
283 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
284 mp->mnt_stat.f_mntonname[0] = '/';
285 mp->mnt_stat.f_mntonname[1] = 0;
286 (void) copystr(devname, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 0);
292 * Find an appropriate filesystem to use for the root. If a filesystem
293 * has not been preselected, walk through the list of known filesystems
294 * trying those that have mountroot routines, and try them until one
295 * works or we have tried them all.
297 #ifdef notdef /* XXX JH */
299 lite2_vfs_mountroot()
301 struct vfsconf *vfsp;
302 extern int (*lite2_mountroot) (void);
305 if (lite2_mountroot != NULL)
306 return ((*lite2_mountroot)());
307 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
308 if (vfsp->vfc_mountroot == NULL)
310 if ((error = (*vfsp->vfc_mountroot)()) == 0)
312 printf("%s_mountroot failed: %d\n", vfsp->vfc_name, error);
319 * Lookup a mount point by filesystem identifier.
327 lwkt_gettoken(&mountlist_token);
328 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
329 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
330 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
331 lwkt_reltoken(&mountlist_token);
335 lwkt_reltoken(&mountlist_token);
336 return ((struct mount *) 0);
340 * Get a new unique fsid. Try to make its val[0] unique, since this value
341 * will be used to create fake device numbers for stat(). Also try (but
342 * not so hard) make its val[0] unique mod 2^16, since some emulators only
343 * support 16-bit device numbers. We end up with unique val[0]'s for the
344 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
346 * Keep in mind that several mounts may be running in parallel. Starting
347 * the search one past where the previous search terminated is both a
348 * micro-optimization and a defense against returning the same fsid to
355 static u_int16_t mntid_base;
359 lwkt_gettoken(&mntid_token);
360 mtype = mp->mnt_vfc->vfc_typenum;
361 tfsid.val[1] = mtype;
362 mtype = (mtype & 0xFF) << 24;
364 tfsid.val[0] = makeudev(255,
365 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
367 if (vfs_getvfs(&tfsid) == NULL)
370 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
371 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
372 lwkt_reltoken(&mntid_token);
376 * Knob to control the precision of file timestamps:
378 * 0 = seconds only; nanoseconds zeroed.
379 * 1 = seconds and nanoseconds, accurate within 1/HZ.
380 * 2 = seconds and nanoseconds, truncated to microseconds.
381 * >=3 = seconds and nanoseconds, maximum precision.
383 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
385 static int timestamp_precision = TSP_SEC;
386 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
387 ×tamp_precision, 0, "");
390 * Get a current timestamp.
394 struct timespec *tsp;
398 switch (timestamp_precision) {
400 tsp->tv_sec = time_second;
408 TIMEVAL_TO_TIMESPEC(&tv, tsp);
418 * Set vnode attributes to VNOVAL
426 vap->va_size = VNOVAL;
427 vap->va_bytes = VNOVAL;
428 vap->va_mode = VNOVAL;
429 vap->va_nlink = VNOVAL;
430 vap->va_uid = VNOVAL;
431 vap->va_gid = VNOVAL;
432 vap->va_fsid = VNOVAL;
433 vap->va_fileid = VNOVAL;
434 vap->va_blocksize = VNOVAL;
435 vap->va_rdev = VNOVAL;
436 vap->va_atime.tv_sec = VNOVAL;
437 vap->va_atime.tv_nsec = VNOVAL;
438 vap->va_mtime.tv_sec = VNOVAL;
439 vap->va_mtime.tv_nsec = VNOVAL;
440 vap->va_ctime.tv_sec = VNOVAL;
441 vap->va_ctime.tv_nsec = VNOVAL;
442 vap->va_flags = VNOVAL;
443 vap->va_gen = VNOVAL;
448 * This routine is called when we have too many vnodes. It attempts
449 * to free <count> vnodes and will potentially free vnodes that still
450 * have VM backing store (VM backing store is typically the cause
451 * of a vnode blowout so we want to do this). Therefore, this operation
452 * is not considered cheap.
454 * A number of conditions may prevent a vnode from being reclaimed.
455 * the buffer cache may have references on the vnode, a directory
456 * vnode may still have references due to the namei cache representing
457 * underlying files, or the vnode may be in active use. It is not
458 * desireable to reuse such vnodes. These conditions may cause the
459 * number of vnodes to reach some minimum value regardless of what
460 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
463 vlrureclaim(struct mount *mp)
473 * Calculate the trigger point, don't allow user
474 * screwups to blow us up. This prevents us from
475 * recycling vnodes with lots of resident pages. We
476 * aren't trying to free memory, we are trying to
479 usevnodes = desiredvnodes;
482 trigger = vmstats.v_page_count * 2 / usevnodes;
485 gen = lwkt_gettoken(&mntvnode_token);
486 count = mp->mnt_nvnodelistsize / 10 + 1;
487 while (count && (vp = TAILQ_FIRST(&mp->mnt_nvnodelist)) != NULL) {
488 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
489 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
491 if (vp->v_type != VNON &&
492 vp->v_type != VBAD &&
493 VMIGHTFREE(vp) && /* critical path opt */
494 (vp->v_object == NULL || vp->v_object->resident_page_count < trigger)
496 lwkt_gettoken(&vp->v_interlock);
497 if (lwkt_gentoken(&mntvnode_token, &gen) == 0) {
498 if (VMIGHTFREE(vp)) {
499 vgonel(vp, curthread);
502 lwkt_reltoken(&vp->v_interlock);
505 lwkt_reltoken(&vp->v_interlock);
510 lwkt_reltoken(&mntvnode_token);
515 * Attempt to recycle vnodes in a context that is always safe to block.
516 * Calling vlrurecycle() from the bowels of file system code has some
517 * interesting deadlock problems.
519 static struct thread *vnlruthread;
520 static int vnlruproc_sig;
525 struct mount *mp, *nmp;
528 struct thread *td = curthread;
530 EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc, td,
535 kproc_suspend_loop();
536 if (numvnodes - freevnodes <= desiredvnodes * 9 / 10) {
538 wakeup(&vnlruproc_sig);
539 tsleep(td, 0, "vlruwt", hz);
543 lwkt_gettoken(&mountlist_token);
544 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
545 if (vfs_busy(mp, LK_NOWAIT, &mountlist_token, td)) {
546 nmp = TAILQ_NEXT(mp, mnt_list);
549 done += vlrureclaim(mp);
550 lwkt_gettoken(&mountlist_token);
551 nmp = TAILQ_NEXT(mp, mnt_list);
554 lwkt_reltoken(&mountlist_token);
557 tsleep(td, 0, "vlrup", hz * 3);
563 static struct kproc_desc vnlru_kp = {
568 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &vnlru_kp)
571 * Routines having to do with the management of the vnode table.
573 extern vop_t **dead_vnodeop_p;
576 * Return the next vnode from the free list.
579 getnewvnode(tag, mp, vops, vpp)
588 struct thread *td = curthread; /* XXX */
589 struct vnode *vp = NULL;
595 * Try to reuse vnodes if we hit the max. This situation only
596 * occurs in certain large-memory (2G+) situations. We cannot
597 * attempt to directly reclaim vnodes due to nasty recursion
600 while (numvnodes - freevnodes > desiredvnodes) {
601 if (vnlruproc_sig == 0) {
602 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
605 tsleep(&vnlruproc_sig, 0, "vlruwk", hz);
610 * Attempt to reuse a vnode already on the free list, allocating
611 * a new vnode if we can't find one or if we have not reached a
612 * good minimum for good LRU performance.
614 gen = lwkt_gettoken(&vnode_free_list_token);
615 if (freevnodes >= wantfreevnodes && numvnodes >= minvnodes) {
618 for (count = 0; count < freevnodes; count++) {
619 vp = TAILQ_FIRST(&vnode_free_list);
620 if (vp == NULL || vp->v_usecount)
621 panic("getnewvnode: free vnode isn't");
624 * Get the vnode's interlock, then re-obtain
625 * vnode_free_list_token in case we lost it. If we
626 * did lose it while getting the vnode interlock,
627 * even if we got it back again, then retry.
629 vgen = lwkt_gettoken(&vp->v_interlock);
630 if (lwkt_gentoken(&vnode_free_list_token, &gen) != 0) {
632 lwkt_reltoken(&vp->v_interlock);
638 * Whew! We have both tokens. Since we didn't lose
639 * the free list VFREE had better still be set. But
640 * we aren't out of the woods yet. We have to get
641 * the object (may block). If the vnode is not
642 * suitable then move it to the end of the list
643 * if we can. If we can't move it to the end of the
646 if ((VOP_GETVOBJECT(vp, &object) == 0 &&
647 (object->resident_page_count || object->ref_count))
649 if (lwkt_gentoken(&vp->v_interlock, &vgen) == 0 &&
650 lwkt_gentoken(&vnode_free_list_token, &gen) == 0
652 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
653 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
657 lwkt_reltoken(&vp->v_interlock);
663 * Still not out of the woods. VOBJECT might have
664 * blocked, if we did not retain our tokens we have
667 if (lwkt_gentoken(&vp->v_interlock, &vgen) != 0 ||
668 lwkt_gentoken(&vnode_free_list_token, &gen) != 0) {
673 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
674 KKASSERT(vp->v_flag & VFREE);
676 if (LIST_FIRST(&vp->v_cache_src)) {
678 * note: nameileafonly sysctl is temporary,
679 * for debugging only, and will eventually be
682 if (nameileafonly > 0) {
684 * Do not reuse namei-cached directory
685 * vnodes that have cached
688 if (cache_leaf_test(vp) < 0) {
689 lwkt_reltoken(&vp->v_interlock);
690 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
694 } else if (nameileafonly < 0 ||
695 vmiodirenable == 0) {
697 * Do not reuse namei-cached directory
698 * vnodes if nameileafonly is -1 or
699 * if VMIO backing for directories is
700 * turned off (otherwise we reuse them
703 lwkt_reltoken(&vp->v_interlock);
704 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
714 vp->v_flag |= VDOOMED;
715 vp->v_flag &= ~VFREE;
717 lwkt_reltoken(&vnode_free_list_token);
718 cache_purge(vp); /* YYY may block */
720 if (vp->v_type != VBAD) {
723 lwkt_reltoken(&vp->v_interlock);
731 panic("cleaned vnode isn't");
734 panic("Clean vnode has pending I/O's");
744 vp->v_writecount = 0; /* XXX */
746 lwkt_reltoken(&vnode_free_list_token);
747 vp = (struct vnode *) zalloc(vnode_zone);
748 bzero((char *) vp, sizeof *vp);
749 lwkt_inittoken(&vp->v_interlock);
752 LIST_INIT(&vp->v_cache_src);
753 TAILQ_INIT(&vp->v_cache_dst);
757 TAILQ_INIT(&vp->v_cleanblkhd);
758 TAILQ_INIT(&vp->v_dirtyblkhd);
768 vfs_object_create(vp, td);
773 * Move a vnode from one mount queue to another.
781 lwkt_gettoken(&mntvnode_token);
783 * Delete from old mount point vnode list, if on one.
785 if (vp->v_mount != NULL) {
786 KASSERT(vp->v_mount->mnt_nvnodelistsize > 0,
787 ("bad mount point vnode list size"));
788 TAILQ_REMOVE(&vp->v_mount->mnt_nvnodelist, vp, v_nmntvnodes);
789 vp->v_mount->mnt_nvnodelistsize--;
792 * Insert into list of vnodes for the new mount point, if available.
794 if ((vp->v_mount = mp) == NULL) {
795 lwkt_reltoken(&mntvnode_token);
798 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
799 mp->mnt_nvnodelistsize++;
800 lwkt_reltoken(&mntvnode_token);
804 * Update outstanding I/O count and do wakeup if requested.
812 bp->b_flags &= ~B_WRITEINPROG;
813 if ((vp = bp->b_vp)) {
815 if (vp->v_numoutput < 0)
816 panic("vwakeup: neg numoutput");
817 if ((vp->v_numoutput == 0) && (vp->v_flag & VBWAIT)) {
818 vp->v_flag &= ~VBWAIT;
819 wakeup((caddr_t) &vp->v_numoutput);
825 * Flush out and invalidate all buffers associated with a vnode.
826 * Called with the underlying object locked.
829 vinvalbuf(struct vnode *vp, int flags, struct thread *td,
830 int slpflag, int slptimeo)
833 struct buf *nbp, *blist;
837 if (flags & V_SAVE) {
839 while (vp->v_numoutput) {
840 vp->v_flag |= VBWAIT;
841 error = tsleep((caddr_t)&vp->v_numoutput,
842 slpflag, "vinvlbuf", slptimeo);
848 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
850 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) != 0)
853 if (vp->v_numoutput > 0 ||
854 !TAILQ_EMPTY(&vp->v_dirtyblkhd))
855 panic("vinvalbuf: dirty bufs");
861 blist = TAILQ_FIRST(&vp->v_cleanblkhd);
863 blist = TAILQ_FIRST(&vp->v_dirtyblkhd);
867 for (bp = blist; bp; bp = nbp) {
868 nbp = TAILQ_NEXT(bp, b_vnbufs);
869 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
870 error = BUF_TIMELOCK(bp,
871 LK_EXCLUSIVE | LK_SLEEPFAIL,
872 "vinvalbuf", slpflag, slptimeo);
879 * XXX Since there are no node locks for NFS, I
880 * believe there is a slight chance that a delayed
881 * write will occur while sleeping just above, so
882 * check for it. Note that vfs_bio_awrite expects
883 * buffers to reside on a queue, while VOP_BWRITE and
886 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
889 if (bp->b_vp == vp) {
890 if (bp->b_flags & B_CLUSTEROK) {
895 bp->b_flags |= B_ASYNC;
896 VOP_BWRITE(bp->b_vp, bp);
900 (void) VOP_BWRITE(bp->b_vp, bp);
905 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
906 bp->b_flags &= ~B_ASYNC;
912 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
913 * have write I/O in-progress but if there is a VM object then the
914 * VM object can also have read-I/O in-progress.
917 while (vp->v_numoutput > 0) {
918 vp->v_flag |= VBWAIT;
919 tsleep(&vp->v_numoutput, 0, "vnvlbv", 0);
921 if (VOP_GETVOBJECT(vp, &object) == 0) {
922 while (object->paging_in_progress)
923 vm_object_pip_sleep(object, "vnvlbx");
925 } while (vp->v_numoutput > 0);
930 * Destroy the copy in the VM cache, too.
932 lwkt_gettoken(&vp->v_interlock);
933 if (VOP_GETVOBJECT(vp, &object) == 0) {
934 vm_object_page_remove(object, 0, 0,
935 (flags & V_SAVE) ? TRUE : FALSE);
937 lwkt_reltoken(&vp->v_interlock);
939 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd) || !TAILQ_EMPTY(&vp->v_cleanblkhd))
940 panic("vinvalbuf: flush failed");
945 * Truncate a file's buffer and pages to a specified length. This
946 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
950 vtruncbuf(struct vnode *vp, struct thread *td, off_t length, int blksize)
958 * Round up to the *next* lbn.
960 trunclbn = (length + blksize - 1) / blksize;
967 for (bp = TAILQ_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
968 nbp = TAILQ_NEXT(bp, b_vnbufs);
969 if (bp->b_lblkno >= trunclbn) {
970 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
971 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
975 bp->b_flags |= (B_INVAL | B_RELBUF);
976 bp->b_flags &= ~B_ASYNC;
981 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
983 (nbp->b_flags & B_DELWRI))) {
989 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
990 nbp = TAILQ_NEXT(bp, b_vnbufs);
991 if (bp->b_lblkno >= trunclbn) {
992 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
993 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
997 bp->b_flags |= (B_INVAL | B_RELBUF);
998 bp->b_flags &= ~B_ASYNC;
1003 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
1004 (nbp->b_vp != vp) ||
1005 (nbp->b_flags & B_DELWRI) == 0)) {
1014 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
1015 nbp = TAILQ_NEXT(bp, b_vnbufs);
1016 if ((bp->b_flags & B_DELWRI) && (bp->b_lblkno < 0)) {
1017 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
1018 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
1022 if (bp->b_vp == vp) {
1023 bp->b_flags |= B_ASYNC;
1025 bp->b_flags &= ~B_ASYNC;
1027 VOP_BWRITE(bp->b_vp, bp);
1035 while (vp->v_numoutput > 0) {
1036 vp->v_flag |= VBWAIT;
1037 tsleep(&vp->v_numoutput, 0, "vbtrunc", 0);
1042 vnode_pager_setsize(vp, length);
1048 * Associate a buffer with a vnode.
1057 KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
1061 bp->b_dev = vn_todev(vp);
1063 * Insert onto list for new vnode.
1066 bp->b_xflags |= BX_VNCLEAN;
1067 bp->b_xflags &= ~BX_VNDIRTY;
1068 TAILQ_INSERT_TAIL(&vp->v_cleanblkhd, bp, b_vnbufs);
1073 * Disassociate a buffer from a vnode.
1080 struct buflists *listheadp;
1083 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1086 * Delete from old vnode list, if on one.
1090 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) {
1091 if (bp->b_xflags & BX_VNDIRTY)
1092 listheadp = &vp->v_dirtyblkhd;
1094 listheadp = &vp->v_cleanblkhd;
1095 TAILQ_REMOVE(listheadp, bp, b_vnbufs);
1096 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1098 if ((vp->v_flag & VONWORKLST) && TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
1099 vp->v_flag &= ~VONWORKLST;
1100 LIST_REMOVE(vp, v_synclist);
1103 bp->b_vp = (struct vnode *) 0;
1108 * The workitem queue.
1110 * It is useful to delay writes of file data and filesystem metadata
1111 * for tens of seconds so that quickly created and deleted files need
1112 * not waste disk bandwidth being created and removed. To realize this,
1113 * we append vnodes to a "workitem" queue. When running with a soft
1114 * updates implementation, most pending metadata dependencies should
1115 * not wait for more than a few seconds. Thus, mounted on block devices
1116 * are delayed only about a half the time that file data is delayed.
1117 * Similarly, directory updates are more critical, so are only delayed
1118 * about a third the time that file data is delayed. Thus, there are
1119 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
1120 * one each second (driven off the filesystem syncer process). The
1121 * syncer_delayno variable indicates the next queue that is to be processed.
1122 * Items that need to be processed soon are placed in this queue:
1124 * syncer_workitem_pending[syncer_delayno]
1126 * A delay of fifteen seconds is done by placing the request fifteen
1127 * entries later in the queue:
1129 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
1134 * Add an item to the syncer work queue.
1137 vn_syncer_add_to_worklist(struct vnode *vp, int delay)
1143 if (vp->v_flag & VONWORKLST) {
1144 LIST_REMOVE(vp, v_synclist);
1147 if (delay > syncer_maxdelay - 2)
1148 delay = syncer_maxdelay - 2;
1149 slot = (syncer_delayno + delay) & syncer_mask;
1151 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], vp, v_synclist);
1152 vp->v_flag |= VONWORKLST;
1156 struct thread *updatethread;
1157 static void sched_sync (void);
1158 static struct kproc_desc up_kp = {
1163 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp)
1166 * System filesystem synchronizer daemon.
1171 struct synclist *slp;
1175 struct thread *td = curthread;
1177 EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc, td,
1181 kproc_suspend_loop();
1183 starttime = time_second;
1186 * Push files whose dirty time has expired. Be careful
1187 * of interrupt race on slp queue.
1190 slp = &syncer_workitem_pending[syncer_delayno];
1191 syncer_delayno += 1;
1192 if (syncer_delayno == syncer_maxdelay)
1196 while ((vp = LIST_FIRST(slp)) != NULL) {
1197 if (VOP_ISLOCKED(vp, NULL) == 0) {
1198 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1199 (void) VOP_FSYNC(vp, MNT_LAZY, td);
1200 VOP_UNLOCK(vp, 0, td);
1203 if (LIST_FIRST(slp) == vp) {
1205 * Note: v_tag VT_VFS vps can remain on the
1206 * worklist too with no dirty blocks, but
1207 * since sync_fsync() moves it to a different
1210 if (TAILQ_EMPTY(&vp->v_dirtyblkhd) &&
1211 !vn_isdisk(vp, NULL))
1212 panic("sched_sync: fsync failed vp %p tag %d", vp, vp->v_tag);
1214 * Put us back on the worklist. The worklist
1215 * routine will remove us from our current
1216 * position and then add us back in at a later
1219 vn_syncer_add_to_worklist(vp, syncdelay);
1225 * Do soft update processing.
1228 (*bioops.io_sync)(NULL);
1231 * The variable rushjob allows the kernel to speed up the
1232 * processing of the filesystem syncer process. A rushjob
1233 * value of N tells the filesystem syncer to process the next
1234 * N seconds worth of work on its queue ASAP. Currently rushjob
1235 * is used by the soft update code to speed up the filesystem
1236 * syncer process when the incore state is getting so far
1237 * ahead of the disk that the kernel memory pool is being
1238 * threatened with exhaustion.
1245 * If it has taken us less than a second to process the
1246 * current work, then wait. Otherwise start right over
1247 * again. We can still lose time if any single round
1248 * takes more than two seconds, but it does not really
1249 * matter as we are just trying to generally pace the
1250 * filesystem activity.
1252 if (time_second == starttime)
1253 tsleep(&lbolt, 0, "syncer", 0);
1258 * Request the syncer daemon to speed up its work.
1259 * We never push it to speed up more than half of its
1260 * normal turn time, otherwise it could take over the cpu.
1262 * YYY wchan field protected by the BGL.
1268 if (updatethread->td_wchan == &lbolt) { /* YYY */
1269 unsleep(updatethread);
1270 lwkt_schedule(updatethread);
1273 if (rushjob < syncdelay / 2) {
1275 stat_rush_requests += 1;
1282 * Associate a p-buffer with a vnode.
1284 * Also sets B_PAGING flag to indicate that vnode is not fully associated
1285 * with the buffer. i.e. the bp has not been linked into the vnode or
1294 KASSERT(bp->b_vp == NULL, ("pbgetvp: not free"));
1297 bp->b_flags |= B_PAGING;
1298 bp->b_dev = vn_todev(vp);
1302 * Disassociate a p-buffer from a vnode.
1309 KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL"));
1312 if (TAILQ_NEXT(bp, b_vnbufs) != NULL) {
1314 "relpbuf(): b_vp was probably reassignbuf()d %p %x",
1319 bp->b_vp = (struct vnode *) 0;
1320 bp->b_flags &= ~B_PAGING;
1324 pbreassignbuf(bp, newvp)
1326 struct vnode *newvp;
1328 if ((bp->b_flags & B_PAGING) == 0) {
1330 "pbreassignbuf() on non phys bp %p",
1338 * Reassign a buffer from one vnode to another.
1339 * Used to assign file specific control information
1340 * (indirect blocks) to the vnode to which they belong.
1343 reassignbuf(bp, newvp)
1345 struct vnode *newvp;
1347 struct buflists *listheadp;
1351 if (newvp == NULL) {
1352 printf("reassignbuf: NULL");
1358 * B_PAGING flagged buffers cannot be reassigned because their vp
1359 * is not fully linked in.
1361 if (bp->b_flags & B_PAGING)
1362 panic("cannot reassign paging buffer");
1366 * Delete from old vnode list, if on one.
1368 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) {
1369 if (bp->b_xflags & BX_VNDIRTY)
1370 listheadp = &bp->b_vp->v_dirtyblkhd;
1372 listheadp = &bp->b_vp->v_cleanblkhd;
1373 TAILQ_REMOVE(listheadp, bp, b_vnbufs);
1374 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1375 if (bp->b_vp != newvp) {
1377 bp->b_vp = NULL; /* for clarification */
1381 * If dirty, put on list of dirty buffers; otherwise insert onto list
1384 if (bp->b_flags & B_DELWRI) {
1387 listheadp = &newvp->v_dirtyblkhd;
1388 if ((newvp->v_flag & VONWORKLST) == 0) {
1389 switch (newvp->v_type) {
1395 if (newvp->v_specmountpoint != NULL) {
1403 vn_syncer_add_to_worklist(newvp, delay);
1405 bp->b_xflags |= BX_VNDIRTY;
1406 tbp = TAILQ_FIRST(listheadp);
1408 bp->b_lblkno == 0 ||
1409 (bp->b_lblkno > 0 && tbp->b_lblkno < 0) ||
1410 (bp->b_lblkno > 0 && bp->b_lblkno < tbp->b_lblkno)) {
1411 TAILQ_INSERT_HEAD(listheadp, bp, b_vnbufs);
1412 ++reassignbufsortgood;
1413 } else if (bp->b_lblkno < 0) {
1414 TAILQ_INSERT_TAIL(listheadp, bp, b_vnbufs);
1415 ++reassignbufsortgood;
1416 } else if (reassignbufmethod == 1) {
1418 * New sorting algorithm, only handle sequential case,
1419 * otherwise append to end (but before metadata)
1421 if ((tbp = gbincore(newvp, bp->b_lblkno - 1)) != NULL &&
1422 (tbp->b_xflags & BX_VNDIRTY)) {
1424 * Found the best place to insert the buffer
1426 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1427 ++reassignbufsortgood;
1430 * Missed, append to end, but before meta-data.
1431 * We know that the head buffer in the list is
1432 * not meta-data due to prior conditionals.
1434 * Indirect effects: NFS second stage write
1435 * tends to wind up here, giving maximum
1436 * distance between the unstable write and the
1439 tbp = TAILQ_LAST(listheadp, buflists);
1440 while (tbp && tbp->b_lblkno < 0)
1441 tbp = TAILQ_PREV(tbp, buflists, b_vnbufs);
1442 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1443 ++reassignbufsortbad;
1447 * Old sorting algorithm, scan queue and insert
1450 while ((ttbp = TAILQ_NEXT(tbp, b_vnbufs)) &&
1451 (ttbp->b_lblkno < bp->b_lblkno)) {
1455 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1458 bp->b_xflags |= BX_VNCLEAN;
1459 TAILQ_INSERT_TAIL(&newvp->v_cleanblkhd, bp, b_vnbufs);
1460 if ((newvp->v_flag & VONWORKLST) &&
1461 TAILQ_EMPTY(&newvp->v_dirtyblkhd)) {
1462 newvp->v_flag &= ~VONWORKLST;
1463 LIST_REMOVE(newvp, v_synclist);
1466 if (bp->b_vp != newvp) {
1474 * Create a vnode for a block device.
1475 * Used for mounting the root file system.
1490 error = getnewvnode(VT_NON, (struct mount *)0, spec_vnodeop_p, &nvp);
1503 * Add a vnode to the alias list hung off the dev_t.
1505 * The reason for this gunk is that multiple vnodes can reference
1506 * the same physical device, so checking vp->v_usecount to see
1507 * how many users there are is inadequate; the v_usecount for
1508 * the vnodes need to be accumulated. vcount() does that.
1511 addaliasu(struct vnode *nvp, udev_t nvp_rdev)
1515 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1516 panic("addaliasu on non-special vnode");
1517 dev = udev2dev(nvp_rdev, nvp->v_type == VBLK ? 1 : 0);
1526 addalias(struct vnode *nvp, dev_t dev)
1529 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1530 panic("addalias on non-special vnode");
1533 lwkt_gettoken(&spechash_token);
1534 SLIST_INSERT_HEAD(&dev->si_hlist, nvp, v_specnext);
1535 lwkt_reltoken(&spechash_token);
1539 * Grab a particular vnode from the free list, increment its
1540 * reference count and lock it. The vnode lock bit is set if the
1541 * vnode is being eliminated in vgone. The process is awakened
1542 * when the transition is completed, and an error returned to
1543 * indicate that the vnode is no longer usable (possibly having
1544 * been changed to a new file system type).
1555 * If the vnode is in the process of being cleaned out for
1556 * another use, we wait for the cleaning to finish and then
1557 * return failure. Cleaning is determined by checking that
1558 * the VXLOCK flag is set.
1560 if ((flags & LK_INTERLOCK) == 0) {
1561 lwkt_gettoken(&vp->v_interlock);
1563 if (vp->v_flag & VXLOCK) {
1564 if (vp->v_vxproc == curproc) {
1566 /* this can now occur in normal operation */
1567 log(LOG_INFO, "VXLOCK interlock avoided\n");
1570 vp->v_flag |= VXWANT;
1571 lwkt_reltoken(&vp->v_interlock);
1572 tsleep((caddr_t)vp, 0, "vget", 0);
1579 if (VSHOULDBUSY(vp))
1581 if (flags & LK_TYPE_MASK) {
1582 if ((error = vn_lock(vp, flags | LK_INTERLOCK, td)) != 0) {
1584 * must expand vrele here because we do not want
1585 * to call VOP_INACTIVE if the reference count
1586 * drops back to zero since it was never really
1587 * active. We must remove it from the free list
1588 * before sleeping so that multiple processes do
1589 * not try to recycle it.
1591 lwkt_gettoken(&vp->v_interlock);
1593 if (VSHOULDFREE(vp))
1597 lwkt_reltoken(&vp->v_interlock);
1601 lwkt_reltoken(&vp->v_interlock);
1606 vref(struct vnode *vp)
1608 lwkt_gettoken(&vp->v_interlock);
1610 lwkt_reltoken(&vp->v_interlock);
1614 * Vnode put/release.
1615 * If count drops to zero, call inactive routine and return to freelist.
1618 vrele(struct vnode *vp)
1620 struct thread *td = curthread; /* XXX */
1622 KASSERT(vp != NULL, ("vrele: null vp"));
1624 lwkt_gettoken(&vp->v_interlock);
1626 if (vp->v_usecount > 1) {
1629 lwkt_reltoken(&vp->v_interlock);
1634 if (vp->v_usecount == 1) {
1637 * We must call VOP_INACTIVE with the node locked.
1638 * If we are doing a vpu, the node is already locked,
1639 * but, in the case of vrele, we must explicitly lock
1640 * the vnode before calling VOP_INACTIVE
1643 if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK, td) == 0)
1644 VOP_INACTIVE(vp, td);
1645 if (VSHOULDFREE(vp))
1651 vprint("vrele: negative ref count", vp);
1652 lwkt_reltoken(&vp->v_interlock);
1654 panic("vrele: negative ref cnt");
1659 vput(struct vnode *vp)
1661 struct thread *td = curthread; /* XXX */
1663 KASSERT(vp != NULL, ("vput: null vp"));
1665 lwkt_gettoken(&vp->v_interlock);
1667 if (vp->v_usecount > 1) {
1669 VOP_UNLOCK(vp, LK_INTERLOCK, td);
1673 if (vp->v_usecount == 1) {
1676 * We must call VOP_INACTIVE with the node locked.
1677 * If we are doing a vpu, the node is already locked,
1678 * so we just need to release the vnode mutex.
1680 lwkt_reltoken(&vp->v_interlock);
1681 VOP_INACTIVE(vp, td);
1682 if (VSHOULDFREE(vp))
1688 vprint("vput: negative ref count", vp);
1690 panic("vput: negative ref cnt");
1695 * Somebody doesn't want the vnode recycled.
1705 if (VSHOULDBUSY(vp))
1711 * One less who cares about this vnode.
1720 if (vp->v_holdcnt <= 0)
1721 panic("vdrop: holdcnt");
1723 if (VSHOULDFREE(vp))
1729 * Remove any vnodes in the vnode table belonging to mount point mp.
1731 * If FORCECLOSE is not specified, there should not be any active ones,
1732 * return error if any are found (nb: this is a user error, not a
1733 * system error). If FORCECLOSE is specified, detach any active vnodes
1736 * If WRITECLOSE is set, only flush out regular file vnodes open for
1739 * SKIPSYSTEM causes any vnodes marked VSYSTEM to be skipped.
1741 * `rootrefs' specifies the base reference count for the root vnode
1742 * of this filesystem. The root vnode is considered busy if its
1743 * v_usecount exceeds this value. On a successful return, vflush()
1744 * will call vrele() on the root vnode exactly rootrefs times.
1745 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
1749 static int busyprt = 0; /* print out busy vnodes */
1750 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "");
1754 vflush(mp, rootrefs, flags)
1759 struct thread *td = curthread; /* XXX */
1760 struct vnode *vp, *nvp, *rootvp = NULL;
1762 int busy = 0, error;
1765 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
1766 ("vflush: bad args"));
1768 * Get the filesystem root vnode. We can vput() it
1769 * immediately, since with rootrefs > 0, it won't go away.
1771 if ((error = VFS_ROOT(mp, &rootvp)) != 0)
1775 lwkt_gettoken(&mntvnode_token);
1777 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp; vp = nvp) {
1779 * Make sure this vnode wasn't reclaimed in getnewvnode().
1780 * Start over if it has (it won't be on the list anymore).
1782 if (vp->v_mount != mp)
1784 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
1786 lwkt_gettoken(&vp->v_interlock);
1788 * Skip over a vnodes marked VSYSTEM.
1790 if ((flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM)) {
1791 lwkt_reltoken(&vp->v_interlock);
1795 * If WRITECLOSE is set, flush out unlinked but still open
1796 * files (even if open only for reading) and regular file
1797 * vnodes open for writing.
1799 if ((flags & WRITECLOSE) &&
1800 (vp->v_type == VNON ||
1801 (VOP_GETATTR(vp, &vattr, td) == 0 &&
1802 vattr.va_nlink > 0)) &&
1803 (vp->v_writecount == 0 || vp->v_type != VREG)) {
1804 lwkt_reltoken(&vp->v_interlock);
1809 * With v_usecount == 0, all we need to do is clear out the
1810 * vnode data structures and we are done.
1812 if (vp->v_usecount == 0) {
1813 lwkt_reltoken(&mntvnode_token);
1815 lwkt_gettoken(&mntvnode_token);
1820 * If FORCECLOSE is set, forcibly close the vnode. For block
1821 * or character devices, revert to an anonymous device. For
1822 * all other files, just kill them.
1824 if (flags & FORCECLOSE) {
1825 lwkt_reltoken(&mntvnode_token);
1826 if (vp->v_type != VBLK && vp->v_type != VCHR) {
1830 vp->v_op = spec_vnodeop_p;
1831 insmntque(vp, (struct mount *) 0);
1833 lwkt_gettoken(&mntvnode_token);
1838 vprint("vflush: busy vnode", vp);
1840 lwkt_reltoken(&vp->v_interlock);
1843 lwkt_reltoken(&mntvnode_token);
1844 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
1846 * If just the root vnode is busy, and if its refcount
1847 * is equal to `rootrefs', then go ahead and kill it.
1849 lwkt_gettoken(&rootvp->v_interlock);
1850 KASSERT(busy > 0, ("vflush: not busy"));
1851 KASSERT(rootvp->v_usecount >= rootrefs, ("vflush: rootrefs"));
1852 if (busy == 1 && rootvp->v_usecount == rootrefs) {
1856 lwkt_reltoken(&rootvp->v_interlock);
1860 for (; rootrefs > 0; rootrefs--)
1866 * We do not want to recycle the vnode too quickly.
1868 * XXX we can't move vp's around the nvnodelist without really screwing
1869 * up the efficiency of filesystem SYNC and friends. This code is
1870 * disabled until we fix the syncing code's scanning algorithm.
1873 vlruvp(struct vnode *vp)
1878 if ((mp = vp->v_mount) != NULL) {
1879 lwkt_gettoken(&mntvnode_token);
1880 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1881 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1882 lwkt_reltoken(&mntvnode_token);
1888 * Disassociate the underlying file system from a vnode.
1891 vclean(struct vnode *vp, int flags, struct thread *td)
1896 * Check to see if the vnode is in use. If so we have to reference it
1897 * before we clean it out so that its count cannot fall to zero and
1898 * generate a race against ourselves to recycle it.
1900 if ((active = vp->v_usecount))
1904 * Prevent the vnode from being recycled or brought into use while we
1907 if (vp->v_flag & VXLOCK)
1908 panic("vclean: deadlock");
1909 vp->v_flag |= VXLOCK;
1910 vp->v_vxproc = curproc;
1912 * Even if the count is zero, the VOP_INACTIVE routine may still
1913 * have the object locked while it cleans it out. The VOP_LOCK
1914 * ensures that the VOP_INACTIVE routine is done with its work.
1915 * For active vnodes, it ensures that no other activity can
1916 * occur while the underlying object is being cleaned out.
1918 VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK, td);
1921 * Clean out any buffers associated with the vnode.
1923 vinvalbuf(vp, V_SAVE, td, 0, 0);
1925 VOP_DESTROYVOBJECT(vp);
1928 * If purging an active vnode, it must be closed and
1929 * deactivated before being reclaimed. Note that the
1930 * VOP_INACTIVE will unlock the vnode.
1933 if (flags & DOCLOSE)
1934 VOP_CLOSE(vp, FNONBLOCK, td);
1935 VOP_INACTIVE(vp, td);
1938 * Any other processes trying to obtain this lock must first
1939 * wait for VXLOCK to clear, then call the new lock operation.
1941 VOP_UNLOCK(vp, 0, td);
1944 * Reclaim the vnode.
1946 if (VOP_RECLAIM(vp, td))
1947 panic("vclean: cannot reclaim");
1951 * Inline copy of vrele() since VOP_INACTIVE
1952 * has already been called.
1954 lwkt_gettoken(&vp->v_interlock);
1955 if (--vp->v_usecount <= 0) {
1957 if (vp->v_usecount < 0 || vp->v_writecount != 0) {
1958 vprint("vclean: bad ref count", vp);
1959 panic("vclean: ref cnt");
1964 lwkt_reltoken(&vp->v_interlock);
1968 vp->v_vnlock = NULL;
1970 if (VSHOULDFREE(vp))
1974 * Done with purge, notify sleepers of the grim news.
1976 vp->v_op = dead_vnodeop_p;
1979 vp->v_flag &= ~VXLOCK;
1980 vp->v_vxproc = NULL;
1981 if (vp->v_flag & VXWANT) {
1982 vp->v_flag &= ~VXWANT;
1983 wakeup((caddr_t) vp);
1988 * Eliminate all activity associated with the requested vnode
1989 * and with all vnodes aliased to the requested vnode.
1993 struct vop_revoke_args /* {
1998 struct vnode *vp, *vq;
2001 KASSERT((ap->a_flags & REVOKEALL) != 0, ("vop_revoke"));
2005 * If a vgone (or vclean) is already in progress,
2006 * wait until it is done and return.
2008 if (vp->v_flag & VXLOCK) {
2009 vp->v_flag |= VXWANT;
2010 lwkt_reltoken(&vp->v_interlock);
2011 tsleep((caddr_t)vp, 0, "vop_revokeall", 0);
2016 lwkt_gettoken(&spechash_token);
2017 vq = SLIST_FIRST(&dev->si_hlist);
2018 lwkt_reltoken(&spechash_token);
2027 * Recycle an unused vnode to the front of the free list.
2028 * Release the passed interlock if the vnode will be recycled.
2031 vrecycle(struct vnode *vp, struct lwkt_token *inter_lkp, struct thread *td)
2033 lwkt_gettoken(&vp->v_interlock);
2034 if (vp->v_usecount == 0) {
2036 lwkt_reltoken(inter_lkp);
2041 lwkt_reltoken(&vp->v_interlock);
2046 * Eliminate all activity associated with a vnode
2047 * in preparation for reuse.
2050 vgone(struct vnode *vp)
2052 struct thread *td = curthread; /* XXX */
2054 lwkt_gettoken(&vp->v_interlock);
2059 * vgone, with the vp interlock held.
2062 vgonel(struct vnode *vp, struct thread *td)
2067 * If a vgone (or vclean) is already in progress,
2068 * wait until it is done and return.
2070 if (vp->v_flag & VXLOCK) {
2071 vp->v_flag |= VXWANT;
2072 lwkt_reltoken(&vp->v_interlock);
2073 tsleep((caddr_t)vp, 0, "vgone", 0);
2078 * Clean out the filesystem specific data.
2080 vclean(vp, DOCLOSE, td);
2081 lwkt_gettoken(&vp->v_interlock);
2084 * Delete from old mount point vnode list, if on one.
2086 if (vp->v_mount != NULL)
2087 insmntque(vp, (struct mount *)0);
2089 * If special device, remove it from special device alias list
2092 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
2093 lwkt_gettoken(&spechash_token);
2094 SLIST_REMOVE(&vp->v_hashchain, vp, vnode, v_specnext);
2095 freedev(vp->v_rdev);
2096 lwkt_reltoken(&spechash_token);
2101 * If it is on the freelist and not already at the head,
2102 * move it to the head of the list. The test of the
2103 * VDOOMED flag and the reference count of zero is because
2104 * it will be removed from the free list by getnewvnode,
2105 * but will not have its reference count incremented until
2106 * after calling vgone. If the reference count were
2107 * incremented first, vgone would (incorrectly) try to
2108 * close the previous instance of the underlying object.
2110 if (vp->v_usecount == 0 && !(vp->v_flag & VDOOMED)) {
2112 lwkt_gettoken(&vnode_free_list_token);
2113 if (vp->v_flag & VFREE)
2114 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
2117 vp->v_flag |= VFREE;
2118 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2119 lwkt_reltoken(&vnode_free_list_token);
2124 lwkt_reltoken(&vp->v_interlock);
2128 * Lookup a vnode by device number.
2131 vfinddev(dev, type, vpp)
2138 lwkt_gettoken(&spechash_token);
2139 SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) {
2140 if (type == vp->v_type) {
2142 lwkt_reltoken(&spechash_token);
2146 lwkt_reltoken(&spechash_token);
2151 * Calculate the total number of references to a special device.
2161 lwkt_gettoken(&spechash_token);
2162 SLIST_FOREACH(vq, &vp->v_hashchain, v_specnext)
2163 count += vq->v_usecount;
2164 lwkt_reltoken(&spechash_token);
2169 * Same as above, but using the dev_t as argument
2178 vp = SLIST_FIRST(&dev->si_hlist);
2185 * Print out a description of a vnode.
2187 static char *typename[] =
2188 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
2198 printf("%s: %p: ", label, (void *)vp);
2200 printf("%p: ", (void *)vp);
2201 printf("type %s, usecount %d, writecount %d, refcount %d,",
2202 typename[vp->v_type], vp->v_usecount, vp->v_writecount,
2205 if (vp->v_flag & VROOT)
2206 strcat(buf, "|VROOT");
2207 if (vp->v_flag & VTEXT)
2208 strcat(buf, "|VTEXT");
2209 if (vp->v_flag & VSYSTEM)
2210 strcat(buf, "|VSYSTEM");
2211 if (vp->v_flag & VXLOCK)
2212 strcat(buf, "|VXLOCK");
2213 if (vp->v_flag & VXWANT)
2214 strcat(buf, "|VXWANT");
2215 if (vp->v_flag & VBWAIT)
2216 strcat(buf, "|VBWAIT");
2217 if (vp->v_flag & VDOOMED)
2218 strcat(buf, "|VDOOMED");
2219 if (vp->v_flag & VFREE)
2220 strcat(buf, "|VFREE");
2221 if (vp->v_flag & VOBJBUF)
2222 strcat(buf, "|VOBJBUF");
2224 printf(" flags (%s)", &buf[1]);
2225 if (vp->v_data == NULL) {
2234 #include <ddb/ddb.h>
2236 * List all of the locked vnodes in the system.
2237 * Called when debugging the kernel.
2239 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
2241 struct thread *td = curthread; /* XXX */
2242 struct mount *mp, *nmp;
2245 printf("Locked vnodes\n");
2246 lwkt_gettoken(&mountlist_token);
2247 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2248 if (vfs_busy(mp, LK_NOWAIT, &mountlist_token, td)) {
2249 nmp = TAILQ_NEXT(mp, mnt_list);
2252 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
2253 if (VOP_ISLOCKED(vp, NULL))
2254 vprint((char *)0, vp);
2256 lwkt_gettoken(&mountlist_token);
2257 nmp = TAILQ_NEXT(mp, mnt_list);
2260 lwkt_reltoken(&mountlist_token);
2265 * Top level filesystem related information gathering.
2267 static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS);
2270 vfs_sysctl(SYSCTL_HANDLER_ARGS)
2272 int *name = (int *)arg1 - 1; /* XXX */
2273 u_int namelen = arg2 + 1; /* XXX */
2274 struct vfsconf *vfsp;
2276 #if 1 || defined(COMPAT_PRELITE2)
2277 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
2279 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
2283 /* all sysctl names at this level are at least name and field */
2285 return (ENOTDIR); /* overloaded */
2286 if (name[0] != VFS_GENERIC) {
2287 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2288 if (vfsp->vfc_typenum == name[0])
2291 return (EOPNOTSUPP);
2292 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
2293 oldp, oldlenp, newp, newlen, p));
2297 case VFS_MAXTYPENUM:
2300 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
2303 return (ENOTDIR); /* overloaded */
2304 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2305 if (vfsp->vfc_typenum == name[2])
2308 return (EOPNOTSUPP);
2309 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
2311 return (EOPNOTSUPP);
2314 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
2315 "Generic filesystem");
2317 #if 1 || defined(COMPAT_PRELITE2)
2320 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
2323 struct vfsconf *vfsp;
2324 struct ovfsconf ovfs;
2326 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
2327 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
2328 strcpy(ovfs.vfc_name, vfsp->vfc_name);
2329 ovfs.vfc_index = vfsp->vfc_typenum;
2330 ovfs.vfc_refcount = vfsp->vfc_refcount;
2331 ovfs.vfc_flags = vfsp->vfc_flags;
2332 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
2339 #endif /* 1 || COMPAT_PRELITE2 */
2342 #define KINFO_VNODESLOP 10
2344 * Dump vnode list (via sysctl).
2345 * Copyout address of vnode followed by vnode.
2349 sysctl_vnode(SYSCTL_HANDLER_ARGS)
2351 struct proc *p = curproc; /* XXX */
2352 struct mount *mp, *nmp;
2353 struct vnode *nvp, *vp;
2356 #define VPTRSZ sizeof (struct vnode *)
2357 #define VNODESZ sizeof (struct vnode)
2360 if (!req->oldptr) /* Make an estimate */
2361 return (SYSCTL_OUT(req, 0,
2362 (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ)));
2364 lwkt_gettoken(&mountlist_token);
2365 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2366 if (vfs_busy(mp, LK_NOWAIT, &mountlist_token, p)) {
2367 nmp = TAILQ_NEXT(mp, mnt_list);
2371 lwkt_gettoken(&mntvnode_token);
2372 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
2376 * Check that the vp is still associated with
2377 * this filesystem. RACE: could have been
2378 * recycled onto the same filesystem.
2380 if (vp->v_mount != mp) {
2381 lwkt_reltoken(&mntvnode_token);
2384 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
2385 lwkt_reltoken(&mntvnode_token);
2386 if ((error = SYSCTL_OUT(req, &vp, VPTRSZ)) ||
2387 (error = SYSCTL_OUT(req, vp, VNODESZ)))
2389 lwkt_gettoken(&mntvnode_token);
2391 lwkt_reltoken(&mntvnode_token);
2392 lwkt_gettoken(&mountlist_token);
2393 nmp = TAILQ_NEXT(mp, mnt_list);
2396 lwkt_reltoken(&mountlist_token);
2404 * Exporting the vnode list on large systems causes them to crash.
2405 * Exporting the vnode list on medium systems causes sysctl to coredump.
2408 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD,
2409 0, 0, sysctl_vnode, "S,vnode", "");
2413 * Check to see if a filesystem is mounted on a block device.
2420 if (vp->v_specmountpoint != NULL)
2426 * Unmount all filesystems. The list is traversed in reverse order
2427 * of mounting to avoid dependencies.
2433 struct thread *td = curthread;
2436 if (td->td_proc == NULL)
2437 td = initproc->p_thread; /* XXX XXX use proc0 instead? */
2440 * Since this only runs when rebooting, it is not interlocked.
2442 while(!TAILQ_EMPTY(&mountlist)) {
2443 mp = TAILQ_LAST(&mountlist, mntlist);
2444 error = dounmount(mp, MNT_FORCE, td);
2446 TAILQ_REMOVE(&mountlist, mp, mnt_list);
2447 printf("unmount of %s failed (",
2448 mp->mnt_stat.f_mntonname);
2452 printf("%d)\n", error);
2454 /* The unmount has removed mp from the mountlist */
2460 * Build hash lists of net addresses and hang them off the mount point.
2461 * Called by ufs_mount() to set up the lists of export addresses.
2464 vfs_hang_addrlist(mp, nep, argp)
2466 struct netexport *nep;
2467 struct export_args *argp;
2470 struct radix_node_head *rnh;
2472 struct radix_node *rn;
2473 struct sockaddr *saddr, *smask = 0;
2477 if (argp->ex_addrlen == 0) {
2478 if (mp->mnt_flag & MNT_DEFEXPORTED)
2480 np = &nep->ne_defexported;
2481 np->netc_exflags = argp->ex_flags;
2482 np->netc_anon = argp->ex_anon;
2483 np->netc_anon.cr_ref = 1;
2484 mp->mnt_flag |= MNT_DEFEXPORTED;
2488 if (argp->ex_addrlen < 0 || argp->ex_addrlen > MLEN)
2490 if (argp->ex_masklen < 0 || argp->ex_masklen > MLEN)
2493 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
2494 np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK);
2495 bzero((caddr_t) np, i);
2496 saddr = (struct sockaddr *) (np + 1);
2497 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
2499 if (saddr->sa_len > argp->ex_addrlen)
2500 saddr->sa_len = argp->ex_addrlen;
2501 if (argp->ex_masklen) {
2502 smask = (struct sockaddr *) ((caddr_t) saddr + argp->ex_addrlen);
2503 error = copyin(argp->ex_mask, (caddr_t) smask, argp->ex_masklen);
2506 if (smask->sa_len > argp->ex_masklen)
2507 smask->sa_len = argp->ex_masklen;
2509 i = saddr->sa_family;
2510 if ((rnh = nep->ne_rtable[i]) == 0) {
2512 * Seems silly to initialize every AF when most are not used,
2513 * do so on demand here
2515 for (dom = domains; dom; dom = dom->dom_next)
2516 if (dom->dom_family == i && dom->dom_rtattach) {
2517 dom->dom_rtattach((void **) &nep->ne_rtable[i],
2521 if ((rnh = nep->ne_rtable[i]) == 0) {
2526 rn = (*rnh->rnh_addaddr) ((caddr_t) saddr, (caddr_t) smask, rnh,
2528 if (rn == 0 || np != (struct netcred *) rn) { /* already exists */
2532 np->netc_exflags = argp->ex_flags;
2533 np->netc_anon = argp->ex_anon;
2534 np->netc_anon.cr_ref = 1;
2537 free(np, M_NETADDR);
2543 vfs_free_netcred(rn, w)
2544 struct radix_node *rn;
2547 struct radix_node_head *rnh = (struct radix_node_head *) w;
2549 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
2550 free((caddr_t) rn, M_NETADDR);
2555 * Free the net address hash lists that are hanging off the mount points.
2558 vfs_free_addrlist(nep)
2559 struct netexport *nep;
2562 struct radix_node_head *rnh;
2564 for (i = 0; i <= AF_MAX; i++)
2565 if ((rnh = nep->ne_rtable[i])) {
2566 (*rnh->rnh_walktree) (rnh, vfs_free_netcred,
2568 free((caddr_t) rnh, M_RTABLE);
2569 nep->ne_rtable[i] = 0;
2574 vfs_export(mp, nep, argp)
2576 struct netexport *nep;
2577 struct export_args *argp;
2581 if (argp->ex_flags & MNT_DELEXPORT) {
2582 if (mp->mnt_flag & MNT_EXPUBLIC) {
2583 vfs_setpublicfs(NULL, NULL, NULL);
2584 mp->mnt_flag &= ~MNT_EXPUBLIC;
2586 vfs_free_addrlist(nep);
2587 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
2589 if (argp->ex_flags & MNT_EXPORTED) {
2590 if (argp->ex_flags & MNT_EXPUBLIC) {
2591 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
2593 mp->mnt_flag |= MNT_EXPUBLIC;
2595 if ((error = vfs_hang_addrlist(mp, nep, argp)))
2597 mp->mnt_flag |= MNT_EXPORTED;
2604 * Set the publicly exported filesystem (WebNFS). Currently, only
2605 * one public filesystem is possible in the spec (RFC 2054 and 2055)
2608 vfs_setpublicfs(mp, nep, argp)
2610 struct netexport *nep;
2611 struct export_args *argp;
2618 * mp == NULL -> invalidate the current info, the FS is
2619 * no longer exported. May be called from either vfs_export
2620 * or unmount, so check if it hasn't already been done.
2623 if (nfs_pub.np_valid) {
2624 nfs_pub.np_valid = 0;
2625 if (nfs_pub.np_index != NULL) {
2626 FREE(nfs_pub.np_index, M_TEMP);
2627 nfs_pub.np_index = NULL;
2634 * Only one allowed at a time.
2636 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
2640 * Get real filehandle for root of exported FS.
2642 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
2643 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
2645 if ((error = VFS_ROOT(mp, &rvp)))
2648 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
2654 * If an indexfile was specified, pull it in.
2656 if (argp->ex_indexfile != NULL) {
2657 MALLOC(nfs_pub.np_index, char *, MAXNAMLEN + 1, M_TEMP,
2659 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
2660 MAXNAMLEN, (size_t *)0);
2663 * Check for illegal filenames.
2665 for (cp = nfs_pub.np_index; *cp; cp++) {
2673 FREE(nfs_pub.np_index, M_TEMP);
2678 nfs_pub.np_mount = mp;
2679 nfs_pub.np_valid = 1;
2684 vfs_export_lookup(mp, nep, nam)
2686 struct netexport *nep;
2687 struct sockaddr *nam;
2690 struct radix_node_head *rnh;
2691 struct sockaddr *saddr;
2694 if (mp->mnt_flag & MNT_EXPORTED) {
2696 * Lookup in the export list first.
2700 rnh = nep->ne_rtable[saddr->sa_family];
2702 np = (struct netcred *)
2703 (*rnh->rnh_matchaddr)((caddr_t)saddr,
2705 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
2710 * If no address match, use the default if it exists.
2712 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
2713 np = &nep->ne_defexported;
2719 * perform msync on all vnodes under a mount point
2720 * the mount point must be locked.
2723 vfs_msync(struct mount *mp, int flags)
2725 struct thread *td = curthread; /* XXX */
2726 struct vnode *vp, *nvp;
2727 struct vm_object *obj;
2731 lwkt_gettoken(&mntvnode_token);
2733 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nvp) {
2734 if (vp->v_mount != mp) {
2739 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
2741 if (vp->v_flag & VXLOCK) /* XXX: what if MNT_WAIT? */
2745 * There could be hundreds of thousands of vnodes, we cannot
2746 * afford to do anything heavy-weight until we have a fairly
2747 * good indication that there is something to do.
2749 if ((vp->v_flag & VOBJDIRTY) &&
2750 (flags == MNT_WAIT || VOP_ISLOCKED(vp, NULL) == 0)) {
2751 lwkt_reltoken(&mntvnode_token);
2753 LK_EXCLUSIVE | LK_RETRY | LK_NOOBJ, td)) {
2754 if (VOP_GETVOBJECT(vp, &obj) == 0) {
2755 vm_object_page_clean(obj, 0, 0, flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
2759 lwkt_gettoken(&mntvnode_token);
2760 if (TAILQ_NEXT(vp, v_nmntvnodes) != nvp) {
2767 lwkt_reltoken(&mntvnode_token);
2771 * Create the VM object needed for VMIO and mmap support. This
2772 * is done for all VREG files in the system. Some filesystems might
2773 * afford the additional metadata buffering capability of the
2774 * VMIO code by making the device node be VMIO mode also.
2776 * vp must be locked when vfs_object_create is called.
2779 vfs_object_create(struct vnode *vp, struct thread *td)
2781 return (VOP_CREATEVOBJECT(vp, td));
2791 lwkt_gettoken(&vnode_free_list_token);
2792 KASSERT((vp->v_flag & VFREE) == 0, ("vnode already free"));
2793 if (vp->v_flag & VAGE) {
2794 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2796 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
2799 lwkt_reltoken(&vnode_free_list_token);
2800 vp->v_flag &= ~VAGE;
2801 vp->v_flag |= VFREE;
2812 lwkt_gettoken(&vnode_free_list_token);
2813 KASSERT((vp->v_flag & VFREE) != 0, ("vnode not free"));
2814 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
2816 lwkt_reltoken(&vnode_free_list_token);
2817 vp->v_flag &= ~(VFREE|VAGE);
2822 * Record a process's interest in events which might happen to
2823 * a vnode. Because poll uses the historic select-style interface
2824 * internally, this routine serves as both the ``check for any
2825 * pending events'' and the ``record my interest in future events''
2826 * functions. (These are done together, while the lock is held,
2827 * to avoid race conditions.)
2830 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
2832 lwkt_gettoken(&vp->v_pollinfo.vpi_token);
2833 if (vp->v_pollinfo.vpi_revents & events) {
2835 * This leaves events we are not interested
2836 * in available for the other process which
2837 * which presumably had requested them
2838 * (otherwise they would never have been
2841 events &= vp->v_pollinfo.vpi_revents;
2842 vp->v_pollinfo.vpi_revents &= ~events;
2844 lwkt_reltoken(&vp->v_pollinfo.vpi_token);
2847 vp->v_pollinfo.vpi_events |= events;
2848 selrecord(td, &vp->v_pollinfo.vpi_selinfo);
2849 lwkt_reltoken(&vp->v_pollinfo.vpi_token);
2854 * Note the occurrence of an event. If the VN_POLLEVENT macro is used,
2855 * it is possible for us to miss an event due to race conditions, but
2856 * that condition is expected to be rare, so for the moment it is the
2857 * preferred interface.
2860 vn_pollevent(vp, events)
2864 lwkt_gettoken(&vp->v_pollinfo.vpi_token);
2865 if (vp->v_pollinfo.vpi_events & events) {
2867 * We clear vpi_events so that we don't
2868 * call selwakeup() twice if two events are
2869 * posted before the polling process(es) is
2870 * awakened. This also ensures that we take at
2871 * most one selwakeup() if the polling process
2872 * is no longer interested. However, it does
2873 * mean that only one event can be noticed at
2874 * a time. (Perhaps we should only clear those
2875 * event bits which we note?) XXX
2877 vp->v_pollinfo.vpi_events = 0; /* &= ~events ??? */
2878 vp->v_pollinfo.vpi_revents |= events;
2879 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2881 lwkt_reltoken(&vp->v_pollinfo.vpi_token);
2885 * Wake up anyone polling on vp because it is being revoked.
2886 * This depends on dead_poll() returning POLLHUP for correct
2893 lwkt_gettoken(&vp->v_pollinfo.vpi_token);
2894 if (vp->v_pollinfo.vpi_events) {
2895 vp->v_pollinfo.vpi_events = 0;
2896 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2898 lwkt_reltoken(&vp->v_pollinfo.vpi_token);
2904 * Routine to create and manage a filesystem syncer vnode.
2906 #define sync_close ((int (*) (struct vop_close_args *))nullop)
2907 static int sync_fsync (struct vop_fsync_args *);
2908 static int sync_inactive (struct vop_inactive_args *);
2909 static int sync_reclaim (struct vop_reclaim_args *);
2910 #define sync_lock ((int (*) (struct vop_lock_args *))vop_nolock)
2911 #define sync_unlock ((int (*) (struct vop_unlock_args *))vop_nounlock)
2912 static int sync_print (struct vop_print_args *);
2913 #define sync_islocked ((int(*) (struct vop_islocked_args *))vop_noislocked)
2915 static vop_t **sync_vnodeop_p;
2916 static struct vnodeopv_entry_desc sync_vnodeop_entries[] = {
2917 { &vop_default_desc, (vop_t *) vop_eopnotsupp },
2918 { &vop_close_desc, (vop_t *) sync_close }, /* close */
2919 { &vop_fsync_desc, (vop_t *) sync_fsync }, /* fsync */
2920 { &vop_inactive_desc, (vop_t *) sync_inactive }, /* inactive */
2921 { &vop_reclaim_desc, (vop_t *) sync_reclaim }, /* reclaim */
2922 { &vop_lock_desc, (vop_t *) sync_lock }, /* lock */
2923 { &vop_unlock_desc, (vop_t *) sync_unlock }, /* unlock */
2924 { &vop_print_desc, (vop_t *) sync_print }, /* print */
2925 { &vop_islocked_desc, (vop_t *) sync_islocked }, /* islocked */
2928 static struct vnodeopv_desc sync_vnodeop_opv_desc =
2929 { &sync_vnodeop_p, sync_vnodeop_entries };
2931 VNODEOP_SET(sync_vnodeop_opv_desc);
2934 * Create a new filesystem syncer vnode for the specified mount point.
2937 vfs_allocate_syncvnode(mp)
2941 static long start, incr, next;
2944 /* Allocate a new vnode */
2945 if ((error = getnewvnode(VT_VFS, mp, sync_vnodeop_p, &vp)) != 0) {
2946 mp->mnt_syncer = NULL;
2951 * Place the vnode onto the syncer worklist. We attempt to
2952 * scatter them about on the list so that they will go off
2953 * at evenly distributed times even if all the filesystems
2954 * are mounted at once.
2957 if (next == 0 || next > syncer_maxdelay) {
2961 start = syncer_maxdelay / 2;
2962 incr = syncer_maxdelay;
2966 vn_syncer_add_to_worklist(vp, syncdelay > 0 ? next % syncdelay : 0);
2967 mp->mnt_syncer = vp;
2972 * Do a lazy sync of the filesystem.
2976 struct vop_fsync_args /* {
2978 struct ucred *a_cred;
2980 struct thread *a_td;
2983 struct vnode *syncvp = ap->a_vp;
2984 struct mount *mp = syncvp->v_mount;
2985 struct thread *td = ap->a_td;
2989 * We only need to do something if this is a lazy evaluation.
2991 if (ap->a_waitfor != MNT_LAZY)
2995 * Move ourselves to the back of the sync list.
2997 vn_syncer_add_to_worklist(syncvp, syncdelay);
3000 * Walk the list of vnodes pushing all that are dirty and
3001 * not already on the sync list.
3003 lwkt_gettoken(&mountlist_token);
3004 if (vfs_busy(mp, LK_EXCLUSIVE | LK_NOWAIT, &mountlist_token, td) != 0) {
3005 lwkt_reltoken(&mountlist_token);
3008 asyncflag = mp->mnt_flag & MNT_ASYNC;
3009 mp->mnt_flag &= ~MNT_ASYNC;
3010 vfs_msync(mp, MNT_NOWAIT);
3011 VFS_SYNC(mp, MNT_LAZY, td);
3013 mp->mnt_flag |= MNT_ASYNC;
3019 * The syncer vnode is no referenced.
3023 struct vop_inactive_args /* {
3034 * The syncer vnode is no longer needed and is being decommissioned.
3036 * Modifications to the worklist must be protected at splbio().
3040 struct vop_reclaim_args /* {
3044 struct vnode *vp = ap->a_vp;
3048 vp->v_mount->mnt_syncer = NULL;
3049 if (vp->v_flag & VONWORKLST) {
3050 LIST_REMOVE(vp, v_synclist);
3051 vp->v_flag &= ~VONWORKLST;
3059 * Print out a syncer vnode.
3063 struct vop_print_args /* {
3067 struct vnode *vp = ap->a_vp;
3069 printf("syncer vnode");
3070 if (vp->v_vnlock != NULL)
3071 lockmgr_printinfo(vp->v_vnlock);
3077 * extract the dev_t from a VBLK or VCHR
3083 if (vp->v_type != VBLK && vp->v_type != VCHR)
3085 return (vp->v_rdev);
3089 * Check if vnode represents a disk device
3096 if (vp->v_type != VBLK && vp->v_type != VCHR) {
3101 if (vp->v_rdev == NULL) {
3106 if (!dev_dport(vp->v_rdev)) {
3111 if (!(dev_dflags(vp->v_rdev) & D_DISK)) {
3123 struct nameidata *ndp;
3126 if (!(flags & NDF_NO_FREE_PNBUF) &&
3127 (ndp->ni_cnd.cn_flags & HASBUF)) {
3128 zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
3129 ndp->ni_cnd.cn_flags &= ~HASBUF;
3131 if (!(flags & NDF_NO_DVP_UNLOCK) &&
3132 (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
3133 ndp->ni_dvp != ndp->ni_vp)
3134 VOP_UNLOCK(ndp->ni_dvp, 0, ndp->ni_cnd.cn_td);
3135 if (!(flags & NDF_NO_DVP_RELE) &&
3136 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
3140 if (!(flags & NDF_NO_VP_UNLOCK) &&
3141 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
3142 VOP_UNLOCK(ndp->ni_vp, 0, ndp->ni_cnd.cn_td);
3143 if (!(flags & NDF_NO_VP_RELE) &&
3148 if (!(flags & NDF_NO_STARTDIR_RELE) &&
3149 (ndp->ni_cnd.cn_flags & SAVESTART)) {
3150 vrele(ndp->ni_startdir);
3151 ndp->ni_startdir = NULL;