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.10 2003/07/06 21:23:51 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 lwkt_token mountlist_token;
129 struct lwkt_token mntvnode_token;
130 int nfs_mount_type = -1;
131 static struct lwkt_token mntid_token;
132 static struct lwkt_token vnode_free_list_token;
133 static struct lwkt_token spechash_token;
134 struct nfs_public nfs_pub; /* publicly exported FS */
135 static vm_zone_t vnode_zone;
138 * The workitem queue.
140 #define SYNCER_MAXDELAY 32
141 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
142 time_t syncdelay = 30; /* max time to delay syncing data */
143 time_t filedelay = 30; /* time to delay syncing files */
144 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0, "");
145 time_t dirdelay = 29; /* time to delay syncing directories */
146 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0, "");
147 time_t metadelay = 28; /* time to delay syncing metadata */
148 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0, "");
149 static int rushjob; /* number of slots to run ASAP */
150 static int stat_rush_requests; /* number of times I/O speeded up */
151 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0, "");
153 static int syncer_delayno = 0;
154 static long syncer_mask;
155 LIST_HEAD(synclist, vnode);
156 static struct synclist *syncer_workitem_pending;
159 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
160 &desiredvnodes, 0, "Maximum number of vnodes");
161 static int minvnodes;
162 SYSCTL_INT(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
163 &minvnodes, 0, "Minimum number of vnodes");
164 static int vnlru_nowhere = 0;
165 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW, &vnlru_nowhere, 0,
166 "Number of times the vnlru process ran without success");
168 static void vfs_free_addrlist __P((struct netexport *nep));
169 static int vfs_free_netcred __P((struct radix_node *rn, void *w));
170 static int vfs_hang_addrlist __P((struct mount *mp, struct netexport *nep,
171 struct export_args *argp));
174 * Initialize the vnode management data structures.
180 desiredvnodes = maxproc + vmstats.v_page_count / 4;
181 minvnodes = desiredvnodes / 4;
182 lwkt_inittoken(&mntvnode_token);
183 lwkt_inittoken(&mntid_token);
184 lwkt_inittoken(&spechash_token);
185 TAILQ_INIT(&vnode_free_list);
186 lwkt_inittoken(&vnode_free_list_token);
187 vnode_zone = zinit("VNODE", sizeof (struct vnode), 0, 0, 5);
189 * Initialize the filesystem syncer.
191 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE,
193 syncer_maxdelay = syncer_mask + 1;
197 * Mark a mount point as busy. Used to synchronize access and to delay
198 * unmounting. Interlock is not released on failure.
201 vfs_busy(struct mount *mp, int flags, struct lwkt_token *interlkp,
206 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
207 if (flags & LK_NOWAIT)
209 mp->mnt_kern_flag |= MNTK_MWAIT;
211 lwkt_reltoken(interlkp);
214 * Since all busy locks are shared except the exclusive
215 * lock granted when unmounting, the only place that a
216 * wakeup needs to be done is at the release of the
217 * exclusive lock at the end of dounmount.
219 tsleep((caddr_t)mp, PVFS, "vfs_busy", 0);
221 lwkt_gettoken(interlkp);
225 lkflags = LK_SHARED | LK_NOPAUSE;
227 lkflags |= LK_INTERLOCK;
228 if (lockmgr(&mp->mnt_lock, lkflags, interlkp, td))
229 panic("vfs_busy: unexpected lock failure");
234 * Free a busy filesystem.
237 vfs_unbusy(struct mount *mp, struct thread *td)
239 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td);
243 * Lookup a filesystem type, and if found allocate and initialize
244 * a mount structure for it.
246 * Devname is usually updated by mount(8) after booting.
249 vfs_rootmountalloc(char *fstypename, char *devname, struct mount **mpp)
251 struct thread *td = curthread; /* XXX */
252 struct vfsconf *vfsp;
255 if (fstypename == NULL)
257 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
258 if (!strcmp(vfsp->vfc_name, fstypename))
262 mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
263 bzero((char *)mp, (u_long)sizeof(struct mount));
264 lockinit(&mp->mnt_lock, PVFS, "vfslock", VLKTIMEOUT, LK_NOPAUSE);
265 (void)vfs_busy(mp, LK_NOWAIT, 0, td);
266 TAILQ_INIT(&mp->mnt_nvnodelist);
267 TAILQ_INIT(&mp->mnt_reservedvnlist);
268 mp->mnt_nvnodelistsize = 0;
270 mp->mnt_op = vfsp->vfc_vfsops;
271 mp->mnt_flag = MNT_RDONLY;
272 mp->mnt_vnodecovered = NULLVP;
273 vfsp->vfc_refcount++;
274 mp->mnt_iosize_max = DFLTPHYS;
275 mp->mnt_stat.f_type = vfsp->vfc_typenum;
276 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
277 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
278 mp->mnt_stat.f_mntonname[0] = '/';
279 mp->mnt_stat.f_mntonname[1] = 0;
280 (void) copystr(devname, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 0);
286 * Find an appropriate filesystem to use for the root. If a filesystem
287 * has not been preselected, walk through the list of known filesystems
288 * trying those that have mountroot routines, and try them until one
289 * works or we have tried them all.
291 #ifdef notdef /* XXX JH */
293 lite2_vfs_mountroot()
295 struct vfsconf *vfsp;
296 extern int (*lite2_mountroot) __P((void));
299 if (lite2_mountroot != NULL)
300 return ((*lite2_mountroot)());
301 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
302 if (vfsp->vfc_mountroot == NULL)
304 if ((error = (*vfsp->vfc_mountroot)()) == 0)
306 printf("%s_mountroot failed: %d\n", vfsp->vfc_name, error);
313 * Lookup a mount point by filesystem identifier.
319 register struct mount *mp;
321 lwkt_gettoken(&mountlist_token);
322 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
323 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
324 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
325 lwkt_reltoken(&mountlist_token);
329 lwkt_reltoken(&mountlist_token);
330 return ((struct mount *) 0);
334 * Get a new unique fsid. Try to make its val[0] unique, since this value
335 * will be used to create fake device numbers for stat(). Also try (but
336 * not so hard) make its val[0] unique mod 2^16, since some emulators only
337 * support 16-bit device numbers. We end up with unique val[0]'s for the
338 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
340 * Keep in mind that several mounts may be running in parallel. Starting
341 * the search one past where the previous search terminated is both a
342 * micro-optimization and a defense against returning the same fsid to
349 static u_int16_t mntid_base;
353 lwkt_gettoken(&mntid_token);
354 mtype = mp->mnt_vfc->vfc_typenum;
355 tfsid.val[1] = mtype;
356 mtype = (mtype & 0xFF) << 24;
358 tfsid.val[0] = makeudev(255,
359 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
361 if (vfs_getvfs(&tfsid) == NULL)
364 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
365 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
366 lwkt_reltoken(&mntid_token);
370 * Knob to control the precision of file timestamps:
372 * 0 = seconds only; nanoseconds zeroed.
373 * 1 = seconds and nanoseconds, accurate within 1/HZ.
374 * 2 = seconds and nanoseconds, truncated to microseconds.
375 * >=3 = seconds and nanoseconds, maximum precision.
377 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
379 static int timestamp_precision = TSP_SEC;
380 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
381 ×tamp_precision, 0, "");
384 * Get a current timestamp.
388 struct timespec *tsp;
392 switch (timestamp_precision) {
394 tsp->tv_sec = time_second;
402 TIMEVAL_TO_TIMESPEC(&tv, tsp);
412 * Set vnode attributes to VNOVAL
416 register struct vattr *vap;
420 vap->va_size = VNOVAL;
421 vap->va_bytes = VNOVAL;
422 vap->va_mode = VNOVAL;
423 vap->va_nlink = VNOVAL;
424 vap->va_uid = VNOVAL;
425 vap->va_gid = VNOVAL;
426 vap->va_fsid = VNOVAL;
427 vap->va_fileid = VNOVAL;
428 vap->va_blocksize = VNOVAL;
429 vap->va_rdev = VNOVAL;
430 vap->va_atime.tv_sec = VNOVAL;
431 vap->va_atime.tv_nsec = VNOVAL;
432 vap->va_mtime.tv_sec = VNOVAL;
433 vap->va_mtime.tv_nsec = VNOVAL;
434 vap->va_ctime.tv_sec = VNOVAL;
435 vap->va_ctime.tv_nsec = VNOVAL;
436 vap->va_flags = VNOVAL;
437 vap->va_gen = VNOVAL;
442 * This routine is called when we have too many vnodes. It attempts
443 * to free <count> vnodes and will potentially free vnodes that still
444 * have VM backing store (VM backing store is typically the cause
445 * of a vnode blowout so we want to do this). Therefore, this operation
446 * is not considered cheap.
448 * A number of conditions may prevent a vnode from being reclaimed.
449 * the buffer cache may have references on the vnode, a directory
450 * vnode may still have references due to the namei cache representing
451 * underlying files, or the vnode may be in active use. It is not
452 * desireable to reuse such vnodes. These conditions may cause the
453 * number of vnodes to reach some minimum value regardless of what
454 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
457 vlrureclaim(struct mount *mp)
467 * Calculate the trigger point, don't allow user
468 * screwups to blow us up. This prevents us from
469 * recycling vnodes with lots of resident pages. We
470 * aren't trying to free memory, we are trying to
473 usevnodes = desiredvnodes;
476 trigger = vmstats.v_page_count * 2 / usevnodes;
479 gen = lwkt_gettoken(&mntvnode_token);
480 count = mp->mnt_nvnodelistsize / 10 + 1;
481 while (count && (vp = TAILQ_FIRST(&mp->mnt_nvnodelist)) != NULL) {
482 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
483 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
485 if (vp->v_type != VNON &&
486 vp->v_type != VBAD &&
487 VMIGHTFREE(vp) && /* critical path opt */
488 (vp->v_object == NULL || vp->v_object->resident_page_count < trigger)
490 lwkt_gettoken(&vp->v_interlock);
491 if (lwkt_gentoken(&mntvnode_token, &gen) == 0) {
492 if (VMIGHTFREE(vp)) {
493 vgonel(vp, curthread);
496 lwkt_reltoken(&vp->v_interlock);
499 lwkt_reltoken(&vp->v_interlock);
504 lwkt_reltoken(&mntvnode_token);
509 * Attempt to recycle vnodes in a context that is always safe to block.
510 * Calling vlrurecycle() from the bowels of file system code has some
511 * interesting deadlock problems.
513 static struct thread *vnlruthread;
514 static int vnlruproc_sig;
519 struct mount *mp, *nmp;
522 struct thread *td = curthread;
524 EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc, td,
529 kproc_suspend_loop();
530 if (numvnodes - freevnodes <= desiredvnodes * 9 / 10) {
532 wakeup(&vnlruproc_sig);
533 tsleep(td, PVFS, "vlruwt", hz);
537 lwkt_gettoken(&mountlist_token);
538 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
539 if (vfs_busy(mp, LK_NOWAIT, &mountlist_token, td)) {
540 nmp = TAILQ_NEXT(mp, mnt_list);
543 done += vlrureclaim(mp);
544 lwkt_gettoken(&mountlist_token);
545 nmp = TAILQ_NEXT(mp, mnt_list);
548 lwkt_reltoken(&mountlist_token);
551 tsleep(td, PPAUSE, "vlrup", hz * 3);
557 static struct kproc_desc vnlru_kp = {
562 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &vnlru_kp)
565 * Routines having to do with the management of the vnode table.
567 extern vop_t **dead_vnodeop_p;
570 * Return the next vnode from the free list.
573 getnewvnode(tag, mp, vops, vpp)
582 struct thread *td = curthread; /* XXX */
583 struct vnode *vp = NULL;
589 * Try to reuse vnodes if we hit the max. This situation only
590 * occurs in certain large-memory (2G+) situations. We cannot
591 * attempt to directly reclaim vnodes due to nasty recursion
594 while (numvnodes - freevnodes > desiredvnodes) {
595 if (vnlruproc_sig == 0) {
596 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
599 tsleep(&vnlruproc_sig, PVFS, "vlruwk", hz);
604 * Attempt to reuse a vnode already on the free list, allocating
605 * a new vnode if we can't find one or if we have not reached a
606 * good minimum for good LRU performance.
608 gen = lwkt_gettoken(&vnode_free_list_token);
609 if (freevnodes >= wantfreevnodes && numvnodes >= minvnodes) {
612 for (count = 0; count < freevnodes; count++) {
613 vp = TAILQ_FIRST(&vnode_free_list);
614 if (vp == NULL || vp->v_usecount)
615 panic("getnewvnode: free vnode isn't");
618 * Get the vnode's interlock, then re-obtain
619 * vnode_free_list_token in case we lost it. If we
620 * did lose it while getting the vnode interlock,
621 * even if we got it back again, then retry.
623 vgen = lwkt_gettoken(&vp->v_interlock);
624 if (lwkt_gentoken(&vnode_free_list_token, &gen) != 0) {
626 lwkt_reltoken(&vp->v_interlock);
632 * Whew! We have both tokens. Since we didn't lose
633 * the free list VFREE had better still be set. But
634 * we aren't out of the woods yet. We have to get
635 * the object (may block). If the vnode is not
636 * suitable then move it to the end of the list
637 * if we can. If we can't move it to the end of the
640 if ((VOP_GETVOBJECT(vp, &object) == 0 &&
641 (object->resident_page_count || object->ref_count))
643 if (lwkt_gentoken(&vp->v_interlock, &vgen) == 0 &&
644 lwkt_gentoken(&vnode_free_list_token, &gen) == 0
646 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
647 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
651 lwkt_reltoken(&vp->v_interlock);
657 * Still not out of the woods. VOBJECT might have
658 * blocked, if we did not retain our tokens we have
661 if (lwkt_gentoken(&vp->v_interlock, &vgen) != 0 ||
662 lwkt_gentoken(&vnode_free_list_token, &gen) != 0) {
667 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
668 KKASSERT(vp->v_flag & VFREE);
670 if (LIST_FIRST(&vp->v_cache_src)) {
672 * note: nameileafonly sysctl is temporary,
673 * for debugging only, and will eventually be
676 if (nameileafonly > 0) {
678 * Do not reuse namei-cached directory
679 * vnodes that have cached
682 if (cache_leaf_test(vp) < 0) {
683 lwkt_reltoken(&vp->v_interlock);
684 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
688 } else if (nameileafonly < 0 ||
689 vmiodirenable == 0) {
691 * Do not reuse namei-cached directory
692 * vnodes if nameileafonly is -1 or
693 * if VMIO backing for directories is
694 * turned off (otherwise we reuse them
697 lwkt_reltoken(&vp->v_interlock);
698 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
708 vp->v_flag |= VDOOMED;
709 vp->v_flag &= ~VFREE;
711 lwkt_reltoken(&vnode_free_list_token);
712 cache_purge(vp); /* YYY may block */
714 if (vp->v_type != VBAD) {
717 lwkt_reltoken(&vp->v_interlock);
725 panic("cleaned vnode isn't");
728 panic("Clean vnode has pending I/O's");
738 vp->v_writecount = 0; /* XXX */
740 lwkt_reltoken(&vnode_free_list_token);
741 vp = (struct vnode *) zalloc(vnode_zone);
742 bzero((char *) vp, sizeof *vp);
743 lwkt_inittoken(&vp->v_interlock);
746 LIST_INIT(&vp->v_cache_src);
747 TAILQ_INIT(&vp->v_cache_dst);
751 TAILQ_INIT(&vp->v_cleanblkhd);
752 TAILQ_INIT(&vp->v_dirtyblkhd);
762 vfs_object_create(vp, td);
767 * Move a vnode from one mount queue to another.
771 register struct vnode *vp;
772 register struct mount *mp;
775 lwkt_gettoken(&mntvnode_token);
777 * Delete from old mount point vnode list, if on one.
779 if (vp->v_mount != NULL) {
780 KASSERT(vp->v_mount->mnt_nvnodelistsize > 0,
781 ("bad mount point vnode list size"));
782 TAILQ_REMOVE(&vp->v_mount->mnt_nvnodelist, vp, v_nmntvnodes);
783 vp->v_mount->mnt_nvnodelistsize--;
786 * Insert into list of vnodes for the new mount point, if available.
788 if ((vp->v_mount = mp) == NULL) {
789 lwkt_reltoken(&mntvnode_token);
792 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
793 mp->mnt_nvnodelistsize++;
794 lwkt_reltoken(&mntvnode_token);
798 * Update outstanding I/O count and do wakeup if requested.
802 register struct buf *bp;
804 register struct vnode *vp;
806 bp->b_flags &= ~B_WRITEINPROG;
807 if ((vp = bp->b_vp)) {
809 if (vp->v_numoutput < 0)
810 panic("vwakeup: neg numoutput");
811 if ((vp->v_numoutput == 0) && (vp->v_flag & VBWAIT)) {
812 vp->v_flag &= ~VBWAIT;
813 wakeup((caddr_t) &vp->v_numoutput);
819 * Flush out and invalidate all buffers associated with a vnode.
820 * Called with the underlying object locked.
823 vinvalbuf(struct vnode *vp, int flags, struct thread *td,
824 int slpflag, int slptimeo)
826 register struct buf *bp;
827 struct buf *nbp, *blist;
831 if (flags & V_SAVE) {
833 while (vp->v_numoutput) {
834 vp->v_flag |= VBWAIT;
835 error = tsleep((caddr_t)&vp->v_numoutput,
836 slpflag | (PRIBIO + 1), "vinvlbuf", slptimeo);
842 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
844 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) != 0)
847 if (vp->v_numoutput > 0 ||
848 !TAILQ_EMPTY(&vp->v_dirtyblkhd))
849 panic("vinvalbuf: dirty bufs");
855 blist = TAILQ_FIRST(&vp->v_cleanblkhd);
857 blist = TAILQ_FIRST(&vp->v_dirtyblkhd);
861 for (bp = blist; bp; bp = nbp) {
862 nbp = TAILQ_NEXT(bp, b_vnbufs);
863 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
864 error = BUF_TIMELOCK(bp,
865 LK_EXCLUSIVE | LK_SLEEPFAIL,
866 "vinvalbuf", slpflag, slptimeo);
873 * XXX Since there are no node locks for NFS, I
874 * believe there is a slight chance that a delayed
875 * write will occur while sleeping just above, so
876 * check for it. Note that vfs_bio_awrite expects
877 * buffers to reside on a queue, while VOP_BWRITE and
880 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
883 if (bp->b_vp == vp) {
884 if (bp->b_flags & B_CLUSTEROK) {
889 bp->b_flags |= B_ASYNC;
890 VOP_BWRITE(bp->b_vp, bp);
894 (void) VOP_BWRITE(bp->b_vp, bp);
899 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
900 bp->b_flags &= ~B_ASYNC;
906 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
907 * have write I/O in-progress but if there is a VM object then the
908 * VM object can also have read-I/O in-progress.
911 while (vp->v_numoutput > 0) {
912 vp->v_flag |= VBWAIT;
913 tsleep(&vp->v_numoutput, PVM, "vnvlbv", 0);
915 if (VOP_GETVOBJECT(vp, &object) == 0) {
916 while (object->paging_in_progress)
917 vm_object_pip_sleep(object, "vnvlbx");
919 } while (vp->v_numoutput > 0);
924 * Destroy the copy in the VM cache, too.
926 lwkt_gettoken(&vp->v_interlock);
927 if (VOP_GETVOBJECT(vp, &object) == 0) {
928 vm_object_page_remove(object, 0, 0,
929 (flags & V_SAVE) ? TRUE : FALSE);
931 lwkt_reltoken(&vp->v_interlock);
933 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd) || !TAILQ_EMPTY(&vp->v_cleanblkhd))
934 panic("vinvalbuf: flush failed");
939 * Truncate a file's buffer and pages to a specified length. This
940 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
944 vtruncbuf(struct vnode *vp, struct thread *td, off_t length, int blksize)
952 * Round up to the *next* lbn.
954 trunclbn = (length + blksize - 1) / blksize;
961 for (bp = TAILQ_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
962 nbp = TAILQ_NEXT(bp, b_vnbufs);
963 if (bp->b_lblkno >= trunclbn) {
964 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
965 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
969 bp->b_flags |= (B_INVAL | B_RELBUF);
970 bp->b_flags &= ~B_ASYNC;
975 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
977 (nbp->b_flags & B_DELWRI))) {
983 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
984 nbp = TAILQ_NEXT(bp, b_vnbufs);
985 if (bp->b_lblkno >= trunclbn) {
986 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
987 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
991 bp->b_flags |= (B_INVAL | B_RELBUF);
992 bp->b_flags &= ~B_ASYNC;
997 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
999 (nbp->b_flags & B_DELWRI) == 0)) {
1008 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
1009 nbp = TAILQ_NEXT(bp, b_vnbufs);
1010 if ((bp->b_flags & B_DELWRI) && (bp->b_lblkno < 0)) {
1011 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
1012 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
1016 if (bp->b_vp == vp) {
1017 bp->b_flags |= B_ASYNC;
1019 bp->b_flags &= ~B_ASYNC;
1021 VOP_BWRITE(bp->b_vp, bp);
1029 while (vp->v_numoutput > 0) {
1030 vp->v_flag |= VBWAIT;
1031 tsleep(&vp->v_numoutput, PVM, "vbtrunc", 0);
1036 vnode_pager_setsize(vp, length);
1042 * Associate a buffer with a vnode.
1046 register struct vnode *vp;
1047 register struct buf *bp;
1051 KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
1055 bp->b_dev = vn_todev(vp);
1057 * Insert onto list for new vnode.
1060 bp->b_xflags |= BX_VNCLEAN;
1061 bp->b_xflags &= ~BX_VNDIRTY;
1062 TAILQ_INSERT_TAIL(&vp->v_cleanblkhd, bp, b_vnbufs);
1067 * Disassociate a buffer from a vnode.
1071 register struct buf *bp;
1074 struct buflists *listheadp;
1077 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1080 * Delete from old vnode list, if on one.
1084 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) {
1085 if (bp->b_xflags & BX_VNDIRTY)
1086 listheadp = &vp->v_dirtyblkhd;
1088 listheadp = &vp->v_cleanblkhd;
1089 TAILQ_REMOVE(listheadp, bp, b_vnbufs);
1090 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1092 if ((vp->v_flag & VONWORKLST) && TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
1093 vp->v_flag &= ~VONWORKLST;
1094 LIST_REMOVE(vp, v_synclist);
1097 bp->b_vp = (struct vnode *) 0;
1102 * The workitem queue.
1104 * It is useful to delay writes of file data and filesystem metadata
1105 * for tens of seconds so that quickly created and deleted files need
1106 * not waste disk bandwidth being created and removed. To realize this,
1107 * we append vnodes to a "workitem" queue. When running with a soft
1108 * updates implementation, most pending metadata dependencies should
1109 * not wait for more than a few seconds. Thus, mounted on block devices
1110 * are delayed only about a half the time that file data is delayed.
1111 * Similarly, directory updates are more critical, so are only delayed
1112 * about a third the time that file data is delayed. Thus, there are
1113 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
1114 * one each second (driven off the filesystem syncer process). The
1115 * syncer_delayno variable indicates the next queue that is to be processed.
1116 * Items that need to be processed soon are placed in this queue:
1118 * syncer_workitem_pending[syncer_delayno]
1120 * A delay of fifteen seconds is done by placing the request fifteen
1121 * entries later in the queue:
1123 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
1128 * Add an item to the syncer work queue.
1131 vn_syncer_add_to_worklist(struct vnode *vp, int delay)
1137 if (vp->v_flag & VONWORKLST) {
1138 LIST_REMOVE(vp, v_synclist);
1141 if (delay > syncer_maxdelay - 2)
1142 delay = syncer_maxdelay - 2;
1143 slot = (syncer_delayno + delay) & syncer_mask;
1145 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], vp, v_synclist);
1146 vp->v_flag |= VONWORKLST;
1150 struct thread *updatethread;
1151 static void sched_sync __P((void));
1152 static struct kproc_desc up_kp = {
1157 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp)
1160 * System filesystem synchronizer daemon.
1165 struct synclist *slp;
1169 struct thread *td = curthread;
1171 EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc, td,
1175 kproc_suspend_loop();
1177 starttime = time_second;
1180 * Push files whose dirty time has expired. Be careful
1181 * of interrupt race on slp queue.
1184 slp = &syncer_workitem_pending[syncer_delayno];
1185 syncer_delayno += 1;
1186 if (syncer_delayno == syncer_maxdelay)
1190 while ((vp = LIST_FIRST(slp)) != NULL) {
1191 if (VOP_ISLOCKED(vp, NULL) == 0) {
1192 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1193 (void) VOP_FSYNC(vp, MNT_LAZY, td);
1194 VOP_UNLOCK(vp, 0, td);
1197 if (LIST_FIRST(slp) == vp) {
1199 * Note: v_tag VT_VFS vps can remain on the
1200 * worklist too with no dirty blocks, but
1201 * since sync_fsync() moves it to a different
1204 if (TAILQ_EMPTY(&vp->v_dirtyblkhd) &&
1205 !vn_isdisk(vp, NULL))
1206 panic("sched_sync: fsync failed vp %p tag %d", vp, vp->v_tag);
1208 * Put us back on the worklist. The worklist
1209 * routine will remove us from our current
1210 * position and then add us back in at a later
1213 vn_syncer_add_to_worklist(vp, syncdelay);
1219 * Do soft update processing.
1222 (*bioops.io_sync)(NULL);
1225 * The variable rushjob allows the kernel to speed up the
1226 * processing of the filesystem syncer process. A rushjob
1227 * value of N tells the filesystem syncer to process the next
1228 * N seconds worth of work on its queue ASAP. Currently rushjob
1229 * is used by the soft update code to speed up the filesystem
1230 * syncer process when the incore state is getting so far
1231 * ahead of the disk that the kernel memory pool is being
1232 * threatened with exhaustion.
1239 * If it has taken us less than a second to process the
1240 * current work, then wait. Otherwise start right over
1241 * again. We can still lose time if any single round
1242 * takes more than two seconds, but it does not really
1243 * matter as we are just trying to generally pace the
1244 * filesystem activity.
1246 if (time_second == starttime)
1247 tsleep(&lbolt, PPAUSE, "syncer", 0);
1252 * Request the syncer daemon to speed up its work.
1253 * We never push it to speed up more than half of its
1254 * normal turn time, otherwise it could take over the cpu.
1262 if (updatethread->td_proc->p_wchan == &lbolt) /* YYY */
1263 setrunnable(updatethread->td_proc);
1265 if (rushjob < syncdelay / 2) {
1267 stat_rush_requests += 1;
1274 * Associate a p-buffer with a vnode.
1276 * Also sets B_PAGING flag to indicate that vnode is not fully associated
1277 * with the buffer. i.e. the bp has not been linked into the vnode or
1282 register struct vnode *vp;
1283 register struct buf *bp;
1286 KASSERT(bp->b_vp == NULL, ("pbgetvp: not free"));
1289 bp->b_flags |= B_PAGING;
1290 bp->b_dev = vn_todev(vp);
1294 * Disassociate a p-buffer from a vnode.
1298 register struct buf *bp;
1301 KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL"));
1304 if (TAILQ_NEXT(bp, b_vnbufs) != NULL) {
1306 "relpbuf(): b_vp was probably reassignbuf()d %p %x",
1311 bp->b_vp = (struct vnode *) 0;
1312 bp->b_flags &= ~B_PAGING;
1316 pbreassignbuf(bp, newvp)
1318 struct vnode *newvp;
1320 if ((bp->b_flags & B_PAGING) == 0) {
1322 "pbreassignbuf() on non phys bp %p",
1330 * Reassign a buffer from one vnode to another.
1331 * Used to assign file specific control information
1332 * (indirect blocks) to the vnode to which they belong.
1335 reassignbuf(bp, newvp)
1336 register struct buf *bp;
1337 register struct vnode *newvp;
1339 struct buflists *listheadp;
1343 if (newvp == NULL) {
1344 printf("reassignbuf: NULL");
1350 * B_PAGING flagged buffers cannot be reassigned because their vp
1351 * is not fully linked in.
1353 if (bp->b_flags & B_PAGING)
1354 panic("cannot reassign paging buffer");
1358 * Delete from old vnode list, if on one.
1360 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) {
1361 if (bp->b_xflags & BX_VNDIRTY)
1362 listheadp = &bp->b_vp->v_dirtyblkhd;
1364 listheadp = &bp->b_vp->v_cleanblkhd;
1365 TAILQ_REMOVE(listheadp, bp, b_vnbufs);
1366 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1367 if (bp->b_vp != newvp) {
1369 bp->b_vp = NULL; /* for clarification */
1373 * If dirty, put on list of dirty buffers; otherwise insert onto list
1376 if (bp->b_flags & B_DELWRI) {
1379 listheadp = &newvp->v_dirtyblkhd;
1380 if ((newvp->v_flag & VONWORKLST) == 0) {
1381 switch (newvp->v_type) {
1387 if (newvp->v_specmountpoint != NULL) {
1395 vn_syncer_add_to_worklist(newvp, delay);
1397 bp->b_xflags |= BX_VNDIRTY;
1398 tbp = TAILQ_FIRST(listheadp);
1400 bp->b_lblkno == 0 ||
1401 (bp->b_lblkno > 0 && tbp->b_lblkno < 0) ||
1402 (bp->b_lblkno > 0 && bp->b_lblkno < tbp->b_lblkno)) {
1403 TAILQ_INSERT_HEAD(listheadp, bp, b_vnbufs);
1404 ++reassignbufsortgood;
1405 } else if (bp->b_lblkno < 0) {
1406 TAILQ_INSERT_TAIL(listheadp, bp, b_vnbufs);
1407 ++reassignbufsortgood;
1408 } else if (reassignbufmethod == 1) {
1410 * New sorting algorithm, only handle sequential case,
1411 * otherwise append to end (but before metadata)
1413 if ((tbp = gbincore(newvp, bp->b_lblkno - 1)) != NULL &&
1414 (tbp->b_xflags & BX_VNDIRTY)) {
1416 * Found the best place to insert the buffer
1418 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1419 ++reassignbufsortgood;
1422 * Missed, append to end, but before meta-data.
1423 * We know that the head buffer in the list is
1424 * not meta-data due to prior conditionals.
1426 * Indirect effects: NFS second stage write
1427 * tends to wind up here, giving maximum
1428 * distance between the unstable write and the
1431 tbp = TAILQ_LAST(listheadp, buflists);
1432 while (tbp && tbp->b_lblkno < 0)
1433 tbp = TAILQ_PREV(tbp, buflists, b_vnbufs);
1434 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1435 ++reassignbufsortbad;
1439 * Old sorting algorithm, scan queue and insert
1442 while ((ttbp = TAILQ_NEXT(tbp, b_vnbufs)) &&
1443 (ttbp->b_lblkno < bp->b_lblkno)) {
1447 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1450 bp->b_xflags |= BX_VNCLEAN;
1451 TAILQ_INSERT_TAIL(&newvp->v_cleanblkhd, bp, b_vnbufs);
1452 if ((newvp->v_flag & VONWORKLST) &&
1453 TAILQ_EMPTY(&newvp->v_dirtyblkhd)) {
1454 newvp->v_flag &= ~VONWORKLST;
1455 LIST_REMOVE(newvp, v_synclist);
1458 if (bp->b_vp != newvp) {
1466 * Create a vnode for a block device.
1467 * Used for mounting the root file system.
1474 register struct vnode *vp;
1482 error = getnewvnode(VT_NON, (struct mount *)0, spec_vnodeop_p, &nvp);
1495 * Add vnode to the alias list hung off the dev_t.
1497 * The reason for this gunk is that multiple vnodes can reference
1498 * the same physical device, so checking vp->v_usecount to see
1499 * how many users there are is inadequate; the v_usecount for
1500 * the vnodes need to be accumulated. vcount() does that.
1503 addaliasu(nvp, nvp_rdev)
1508 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1509 panic("addaliasu on non-special vnode");
1510 addalias(nvp, udev2dev(nvp_rdev, nvp->v_type == VBLK ? 1 : 0));
1519 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1520 panic("addalias on non-special vnode");
1523 lwkt_gettoken(&spechash_token);
1524 SLIST_INSERT_HEAD(&dev->si_hlist, nvp, v_specnext);
1525 lwkt_reltoken(&spechash_token);
1529 * Grab a particular vnode from the free list, increment its
1530 * reference count and lock it. The vnode lock bit is set if the
1531 * vnode is being eliminated in vgone. The process is awakened
1532 * when the transition is completed, and an error returned to
1533 * indicate that the vnode is no longer usable (possibly having
1534 * been changed to a new file system type).
1545 * If the vnode is in the process of being cleaned out for
1546 * another use, we wait for the cleaning to finish and then
1547 * return failure. Cleaning is determined by checking that
1548 * the VXLOCK flag is set.
1550 if ((flags & LK_INTERLOCK) == 0) {
1551 lwkt_gettoken(&vp->v_interlock);
1553 if (vp->v_flag & VXLOCK) {
1554 if (vp->v_vxproc == curproc) {
1556 /* this can now occur in normal operation */
1557 log(LOG_INFO, "VXLOCK interlock avoided\n");
1560 vp->v_flag |= VXWANT;
1561 lwkt_reltoken(&vp->v_interlock);
1562 tsleep((caddr_t)vp, PINOD, "vget", 0);
1569 if (VSHOULDBUSY(vp))
1571 if (flags & LK_TYPE_MASK) {
1572 if ((error = vn_lock(vp, flags | LK_INTERLOCK, td)) != 0) {
1574 * must expand vrele here because we do not want
1575 * to call VOP_INACTIVE if the reference count
1576 * drops back to zero since it was never really
1577 * active. We must remove it from the free list
1578 * before sleeping so that multiple processes do
1579 * not try to recycle it.
1581 lwkt_gettoken(&vp->v_interlock);
1583 if (VSHOULDFREE(vp))
1587 lwkt_reltoken(&vp->v_interlock);
1591 lwkt_reltoken(&vp->v_interlock);
1596 vref(struct vnode *vp)
1598 lwkt_gettoken(&vp->v_interlock);
1600 lwkt_reltoken(&vp->v_interlock);
1604 * Vnode put/release.
1605 * If count drops to zero, call inactive routine and return to freelist.
1608 vrele(struct vnode *vp)
1610 struct thread *td = curthread; /* XXX */
1612 KASSERT(vp != NULL, ("vrele: null vp"));
1614 lwkt_gettoken(&vp->v_interlock);
1616 if (vp->v_usecount > 1) {
1619 lwkt_reltoken(&vp->v_interlock);
1624 if (vp->v_usecount == 1) {
1627 * We must call VOP_INACTIVE with the node locked.
1628 * If we are doing a vpu, the node is already locked,
1629 * but, in the case of vrele, we must explicitly lock
1630 * the vnode before calling VOP_INACTIVE
1633 if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK, td) == 0)
1634 VOP_INACTIVE(vp, td);
1635 if (VSHOULDFREE(vp))
1641 vprint("vrele: negative ref count", vp);
1642 lwkt_reltoken(&vp->v_interlock);
1644 panic("vrele: negative ref cnt");
1649 vput(struct vnode *vp)
1651 struct thread *td = curthread; /* XXX */
1653 KASSERT(vp != NULL, ("vput: null vp"));
1655 lwkt_gettoken(&vp->v_interlock);
1657 if (vp->v_usecount > 1) {
1659 VOP_UNLOCK(vp, LK_INTERLOCK, td);
1663 if (vp->v_usecount == 1) {
1666 * We must call VOP_INACTIVE with the node locked.
1667 * If we are doing a vpu, the node is already locked,
1668 * so we just need to release the vnode mutex.
1670 lwkt_reltoken(&vp->v_interlock);
1671 VOP_INACTIVE(vp, td);
1672 if (VSHOULDFREE(vp))
1678 vprint("vput: negative ref count", vp);
1680 panic("vput: negative ref cnt");
1685 * Somebody doesn't want the vnode recycled.
1689 register struct vnode *vp;
1695 if (VSHOULDBUSY(vp))
1701 * One less who cares about this vnode.
1705 register struct vnode *vp;
1710 if (vp->v_holdcnt <= 0)
1711 panic("vdrop: holdcnt");
1713 if (VSHOULDFREE(vp))
1719 * Remove any vnodes in the vnode table belonging to mount point mp.
1721 * If FORCECLOSE is not specified, there should not be any active ones,
1722 * return error if any are found (nb: this is a user error, not a
1723 * system error). If FORCECLOSE is specified, detach any active vnodes
1726 * If WRITECLOSE is set, only flush out regular file vnodes open for
1729 * SKIPSYSTEM causes any vnodes marked VSYSTEM to be skipped.
1731 * `rootrefs' specifies the base reference count for the root vnode
1732 * of this filesystem. The root vnode is considered busy if its
1733 * v_usecount exceeds this value. On a successful return, vflush()
1734 * will call vrele() on the root vnode exactly rootrefs times.
1735 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
1739 static int busyprt = 0; /* print out busy vnodes */
1740 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "");
1744 vflush(mp, rootrefs, flags)
1749 struct thread *td = curthread; /* XXX */
1750 struct vnode *vp, *nvp, *rootvp = NULL;
1752 int busy = 0, error;
1755 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
1756 ("vflush: bad args"));
1758 * Get the filesystem root vnode. We can vput() it
1759 * immediately, since with rootrefs > 0, it won't go away.
1761 if ((error = VFS_ROOT(mp, &rootvp)) != 0)
1765 lwkt_gettoken(&mntvnode_token);
1767 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp; vp = nvp) {
1769 * Make sure this vnode wasn't reclaimed in getnewvnode().
1770 * Start over if it has (it won't be on the list anymore).
1772 if (vp->v_mount != mp)
1774 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
1776 lwkt_gettoken(&vp->v_interlock);
1778 * Skip over a vnodes marked VSYSTEM.
1780 if ((flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM)) {
1781 lwkt_reltoken(&vp->v_interlock);
1785 * If WRITECLOSE is set, flush out unlinked but still open
1786 * files (even if open only for reading) and regular file
1787 * vnodes open for writing.
1789 if ((flags & WRITECLOSE) &&
1790 (vp->v_type == VNON ||
1791 (VOP_GETATTR(vp, &vattr, td) == 0 &&
1792 vattr.va_nlink > 0)) &&
1793 (vp->v_writecount == 0 || vp->v_type != VREG)) {
1794 lwkt_reltoken(&vp->v_interlock);
1799 * With v_usecount == 0, all we need to do is clear out the
1800 * vnode data structures and we are done.
1802 if (vp->v_usecount == 0) {
1803 lwkt_reltoken(&mntvnode_token);
1805 lwkt_gettoken(&mntvnode_token);
1810 * If FORCECLOSE is set, forcibly close the vnode. For block
1811 * or character devices, revert to an anonymous device. For
1812 * all other files, just kill them.
1814 if (flags & FORCECLOSE) {
1815 lwkt_reltoken(&mntvnode_token);
1816 if (vp->v_type != VBLK && vp->v_type != VCHR) {
1820 vp->v_op = spec_vnodeop_p;
1821 insmntque(vp, (struct mount *) 0);
1823 lwkt_gettoken(&mntvnode_token);
1828 vprint("vflush: busy vnode", vp);
1830 lwkt_reltoken(&vp->v_interlock);
1833 lwkt_reltoken(&mntvnode_token);
1834 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
1836 * If just the root vnode is busy, and if its refcount
1837 * is equal to `rootrefs', then go ahead and kill it.
1839 lwkt_gettoken(&rootvp->v_interlock);
1840 KASSERT(busy > 0, ("vflush: not busy"));
1841 KASSERT(rootvp->v_usecount >= rootrefs, ("vflush: rootrefs"));
1842 if (busy == 1 && rootvp->v_usecount == rootrefs) {
1846 lwkt_reltoken(&rootvp->v_interlock);
1850 for (; rootrefs > 0; rootrefs--)
1856 * We do not want to recycle the vnode too quickly.
1858 * XXX we can't move vp's around the nvnodelist without really screwing
1859 * up the efficiency of filesystem SYNC and friends. This code is
1860 * disabled until we fix the syncing code's scanning algorithm.
1863 vlruvp(struct vnode *vp)
1868 if ((mp = vp->v_mount) != NULL) {
1869 lwkt_gettoken(&mntvnode_token);
1870 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1871 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1872 lwkt_reltoken(&mntvnode_token);
1878 * Disassociate the underlying file system from a vnode.
1881 vclean(struct vnode *vp, int flags, struct thread *td)
1886 * Check to see if the vnode is in use. If so we have to reference it
1887 * before we clean it out so that its count cannot fall to zero and
1888 * generate a race against ourselves to recycle it.
1890 if ((active = vp->v_usecount))
1894 * Prevent the vnode from being recycled or brought into use while we
1897 if (vp->v_flag & VXLOCK)
1898 panic("vclean: deadlock");
1899 vp->v_flag |= VXLOCK;
1900 vp->v_vxproc = curproc;
1902 * Even if the count is zero, the VOP_INACTIVE routine may still
1903 * have the object locked while it cleans it out. The VOP_LOCK
1904 * ensures that the VOP_INACTIVE routine is done with its work.
1905 * For active vnodes, it ensures that no other activity can
1906 * occur while the underlying object is being cleaned out.
1908 VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK, td);
1911 * Clean out any buffers associated with the vnode.
1913 vinvalbuf(vp, V_SAVE, td, 0, 0);
1915 VOP_DESTROYVOBJECT(vp);
1918 * If purging an active vnode, it must be closed and
1919 * deactivated before being reclaimed. Note that the
1920 * VOP_INACTIVE will unlock the vnode.
1923 if (flags & DOCLOSE)
1924 VOP_CLOSE(vp, FNONBLOCK, td);
1925 VOP_INACTIVE(vp, td);
1928 * Any other processes trying to obtain this lock must first
1929 * wait for VXLOCK to clear, then call the new lock operation.
1931 VOP_UNLOCK(vp, 0, td);
1934 * Reclaim the vnode.
1936 if (VOP_RECLAIM(vp, td))
1937 panic("vclean: cannot reclaim");
1941 * Inline copy of vrele() since VOP_INACTIVE
1942 * has already been called.
1944 lwkt_gettoken(&vp->v_interlock);
1945 if (--vp->v_usecount <= 0) {
1947 if (vp->v_usecount < 0 || vp->v_writecount != 0) {
1948 vprint("vclean: bad ref count", vp);
1949 panic("vclean: ref cnt");
1954 lwkt_reltoken(&vp->v_interlock);
1958 vp->v_vnlock = NULL;
1960 if (VSHOULDFREE(vp))
1964 * Done with purge, notify sleepers of the grim news.
1966 vp->v_op = dead_vnodeop_p;
1969 vp->v_flag &= ~VXLOCK;
1970 vp->v_vxproc = NULL;
1971 if (vp->v_flag & VXWANT) {
1972 vp->v_flag &= ~VXWANT;
1973 wakeup((caddr_t) vp);
1978 * Eliminate all activity associated with the requested vnode
1979 * and with all vnodes aliased to the requested vnode.
1983 struct vop_revoke_args /* {
1988 struct vnode *vp, *vq;
1991 KASSERT((ap->a_flags & REVOKEALL) != 0, ("vop_revoke"));
1995 * If a vgone (or vclean) is already in progress,
1996 * wait until it is done and return.
1998 if (vp->v_flag & VXLOCK) {
1999 vp->v_flag |= VXWANT;
2000 lwkt_reltoken(&vp->v_interlock);
2001 tsleep((caddr_t)vp, PINOD, "vop_revokeall", 0);
2006 lwkt_gettoken(&spechash_token);
2007 vq = SLIST_FIRST(&dev->si_hlist);
2008 lwkt_reltoken(&spechash_token);
2017 * Recycle an unused vnode to the front of the free list.
2018 * Release the passed interlock if the vnode will be recycled.
2021 vrecycle(struct vnode *vp, struct lwkt_token *inter_lkp, struct thread *td)
2023 lwkt_gettoken(&vp->v_interlock);
2024 if (vp->v_usecount == 0) {
2026 lwkt_reltoken(inter_lkp);
2031 lwkt_reltoken(&vp->v_interlock);
2036 * Eliminate all activity associated with a vnode
2037 * in preparation for reuse.
2040 vgone(struct vnode *vp)
2042 struct thread *td = curthread; /* XXX */
2044 lwkt_gettoken(&vp->v_interlock);
2049 * vgone, with the vp interlock held.
2052 vgonel(struct vnode *vp, struct thread *td)
2057 * If a vgone (or vclean) is already in progress,
2058 * wait until it is done and return.
2060 if (vp->v_flag & VXLOCK) {
2061 vp->v_flag |= VXWANT;
2062 lwkt_reltoken(&vp->v_interlock);
2063 tsleep((caddr_t)vp, PINOD, "vgone", 0);
2068 * Clean out the filesystem specific data.
2070 vclean(vp, DOCLOSE, td);
2071 lwkt_gettoken(&vp->v_interlock);
2074 * Delete from old mount point vnode list, if on one.
2076 if (vp->v_mount != NULL)
2077 insmntque(vp, (struct mount *)0);
2079 * If special device, remove it from special device alias list
2082 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
2083 lwkt_gettoken(&spechash_token);
2084 SLIST_REMOVE(&vp->v_hashchain, vp, vnode, v_specnext);
2085 freedev(vp->v_rdev);
2086 lwkt_reltoken(&spechash_token);
2091 * If it is on the freelist and not already at the head,
2092 * move it to the head of the list. The test of the
2093 * VDOOMED flag and the reference count of zero is because
2094 * it will be removed from the free list by getnewvnode,
2095 * but will not have its reference count incremented until
2096 * after calling vgone. If the reference count were
2097 * incremented first, vgone would (incorrectly) try to
2098 * close the previous instance of the underlying object.
2100 if (vp->v_usecount == 0 && !(vp->v_flag & VDOOMED)) {
2102 lwkt_gettoken(&vnode_free_list_token);
2103 if (vp->v_flag & VFREE)
2104 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
2107 vp->v_flag |= VFREE;
2108 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2109 lwkt_reltoken(&vnode_free_list_token);
2114 lwkt_reltoken(&vp->v_interlock);
2118 * Lookup a vnode by device number.
2121 vfinddev(dev, type, vpp)
2128 lwkt_gettoken(&spechash_token);
2129 SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) {
2130 if (type == vp->v_type) {
2132 lwkt_reltoken(&spechash_token);
2136 lwkt_reltoken(&spechash_token);
2141 * Calculate the total number of references to a special device.
2151 lwkt_gettoken(&spechash_token);
2152 SLIST_FOREACH(vq, &vp->v_hashchain, v_specnext)
2153 count += vq->v_usecount;
2154 lwkt_reltoken(&spechash_token);
2159 * Same as above, but using the dev_t as argument
2168 vp = SLIST_FIRST(&dev->si_hlist);
2175 * Print out a description of a vnode.
2177 static char *typename[] =
2178 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
2188 printf("%s: %p: ", label, (void *)vp);
2190 printf("%p: ", (void *)vp);
2191 printf("type %s, usecount %d, writecount %d, refcount %d,",
2192 typename[vp->v_type], vp->v_usecount, vp->v_writecount,
2195 if (vp->v_flag & VROOT)
2196 strcat(buf, "|VROOT");
2197 if (vp->v_flag & VTEXT)
2198 strcat(buf, "|VTEXT");
2199 if (vp->v_flag & VSYSTEM)
2200 strcat(buf, "|VSYSTEM");
2201 if (vp->v_flag & VXLOCK)
2202 strcat(buf, "|VXLOCK");
2203 if (vp->v_flag & VXWANT)
2204 strcat(buf, "|VXWANT");
2205 if (vp->v_flag & VBWAIT)
2206 strcat(buf, "|VBWAIT");
2207 if (vp->v_flag & VDOOMED)
2208 strcat(buf, "|VDOOMED");
2209 if (vp->v_flag & VFREE)
2210 strcat(buf, "|VFREE");
2211 if (vp->v_flag & VOBJBUF)
2212 strcat(buf, "|VOBJBUF");
2214 printf(" flags (%s)", &buf[1]);
2215 if (vp->v_data == NULL) {
2224 #include <ddb/ddb.h>
2226 * List all of the locked vnodes in the system.
2227 * Called when debugging the kernel.
2229 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
2231 struct thread *td = curthread; /* XXX */
2232 struct mount *mp, *nmp;
2235 printf("Locked vnodes\n");
2236 lwkt_gettoken(&mountlist_token);
2237 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2238 if (vfs_busy(mp, LK_NOWAIT, &mountlist_token, td)) {
2239 nmp = TAILQ_NEXT(mp, mnt_list);
2242 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
2243 if (VOP_ISLOCKED(vp, NULL))
2244 vprint((char *)0, vp);
2246 lwkt_gettoken(&mountlist_token);
2247 nmp = TAILQ_NEXT(mp, mnt_list);
2250 lwkt_reltoken(&mountlist_token);
2255 * Top level filesystem related information gathering.
2257 static int sysctl_ovfs_conf __P((SYSCTL_HANDLER_ARGS));
2260 vfs_sysctl(SYSCTL_HANDLER_ARGS)
2262 int *name = (int *)arg1 - 1; /* XXX */
2263 u_int namelen = arg2 + 1; /* XXX */
2264 struct vfsconf *vfsp;
2266 #if 1 || defined(COMPAT_PRELITE2)
2267 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
2269 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
2273 /* all sysctl names at this level are at least name and field */
2275 return (ENOTDIR); /* overloaded */
2276 if (name[0] != VFS_GENERIC) {
2277 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2278 if (vfsp->vfc_typenum == name[0])
2281 return (EOPNOTSUPP);
2282 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
2283 oldp, oldlenp, newp, newlen, p));
2287 case VFS_MAXTYPENUM:
2290 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
2293 return (ENOTDIR); /* overloaded */
2294 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2295 if (vfsp->vfc_typenum == name[2])
2298 return (EOPNOTSUPP);
2299 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
2301 return (EOPNOTSUPP);
2304 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
2305 "Generic filesystem");
2307 #if 1 || defined(COMPAT_PRELITE2)
2310 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
2313 struct vfsconf *vfsp;
2314 struct ovfsconf ovfs;
2316 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
2317 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
2318 strcpy(ovfs.vfc_name, vfsp->vfc_name);
2319 ovfs.vfc_index = vfsp->vfc_typenum;
2320 ovfs.vfc_refcount = vfsp->vfc_refcount;
2321 ovfs.vfc_flags = vfsp->vfc_flags;
2322 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
2329 #endif /* 1 || COMPAT_PRELITE2 */
2332 #define KINFO_VNODESLOP 10
2334 * Dump vnode list (via sysctl).
2335 * Copyout address of vnode followed by vnode.
2339 sysctl_vnode(SYSCTL_HANDLER_ARGS)
2341 struct proc *p = curproc; /* XXX */
2342 struct mount *mp, *nmp;
2343 struct vnode *nvp, *vp;
2346 #define VPTRSZ sizeof (struct vnode *)
2347 #define VNODESZ sizeof (struct vnode)
2350 if (!req->oldptr) /* Make an estimate */
2351 return (SYSCTL_OUT(req, 0,
2352 (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ)));
2354 lwkt_gettoken(&mountlist_token);
2355 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2356 if (vfs_busy(mp, LK_NOWAIT, &mountlist_token, p)) {
2357 nmp = TAILQ_NEXT(mp, mnt_list);
2361 lwkt_gettoken(&mntvnode_token);
2362 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
2366 * Check that the vp is still associated with
2367 * this filesystem. RACE: could have been
2368 * recycled onto the same filesystem.
2370 if (vp->v_mount != mp) {
2371 lwkt_reltoken(&mntvnode_token);
2374 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
2375 lwkt_reltoken(&mntvnode_token);
2376 if ((error = SYSCTL_OUT(req, &vp, VPTRSZ)) ||
2377 (error = SYSCTL_OUT(req, vp, VNODESZ)))
2379 lwkt_gettoken(&mntvnode_token);
2381 lwkt_reltoken(&mntvnode_token);
2382 lwkt_gettoken(&mountlist_token);
2383 nmp = TAILQ_NEXT(mp, mnt_list);
2386 lwkt_reltoken(&mountlist_token);
2394 * Exporting the vnode list on large systems causes them to crash.
2395 * Exporting the vnode list on medium systems causes sysctl to coredump.
2398 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD,
2399 0, 0, sysctl_vnode, "S,vnode", "");
2403 * Check to see if a filesystem is mounted on a block device.
2410 if (vp->v_specmountpoint != NULL)
2416 * Unmount all filesystems. The list is traversed in reverse order
2417 * of mounting to avoid dependencies.
2423 struct thread *td = curthread;
2426 if (td->td_proc == NULL)
2427 td = initproc->p_thread; /* XXX XXX use proc0 instead? */
2430 * Since this only runs when rebooting, it is not interlocked.
2432 while(!TAILQ_EMPTY(&mountlist)) {
2433 mp = TAILQ_LAST(&mountlist, mntlist);
2434 error = dounmount(mp, MNT_FORCE, td);
2436 TAILQ_REMOVE(&mountlist, mp, mnt_list);
2437 printf("unmount of %s failed (",
2438 mp->mnt_stat.f_mntonname);
2442 printf("%d)\n", error);
2444 /* The unmount has removed mp from the mountlist */
2450 * Build hash lists of net addresses and hang them off the mount point.
2451 * Called by ufs_mount() to set up the lists of export addresses.
2454 vfs_hang_addrlist(mp, nep, argp)
2456 struct netexport *nep;
2457 struct export_args *argp;
2459 register struct netcred *np;
2460 register struct radix_node_head *rnh;
2462 struct radix_node *rn;
2463 struct sockaddr *saddr, *smask = 0;
2467 if (argp->ex_addrlen == 0) {
2468 if (mp->mnt_flag & MNT_DEFEXPORTED)
2470 np = &nep->ne_defexported;
2471 np->netc_exflags = argp->ex_flags;
2472 np->netc_anon = argp->ex_anon;
2473 np->netc_anon.cr_ref = 1;
2474 mp->mnt_flag |= MNT_DEFEXPORTED;
2478 if (argp->ex_addrlen > MLEN)
2481 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
2482 np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK);
2483 bzero((caddr_t) np, i);
2484 saddr = (struct sockaddr *) (np + 1);
2485 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
2487 if (saddr->sa_len > argp->ex_addrlen)
2488 saddr->sa_len = argp->ex_addrlen;
2489 if (argp->ex_masklen) {
2490 smask = (struct sockaddr *) ((caddr_t) saddr + argp->ex_addrlen);
2491 error = copyin(argp->ex_mask, (caddr_t) smask, argp->ex_masklen);
2494 if (smask->sa_len > argp->ex_masklen)
2495 smask->sa_len = argp->ex_masklen;
2497 i = saddr->sa_family;
2498 if ((rnh = nep->ne_rtable[i]) == 0) {
2500 * Seems silly to initialize every AF when most are not used,
2501 * do so on demand here
2503 for (dom = domains; dom; dom = dom->dom_next)
2504 if (dom->dom_family == i && dom->dom_rtattach) {
2505 dom->dom_rtattach((void **) &nep->ne_rtable[i],
2509 if ((rnh = nep->ne_rtable[i]) == 0) {
2514 rn = (*rnh->rnh_addaddr) ((caddr_t) saddr, (caddr_t) smask, rnh,
2516 if (rn == 0 || np != (struct netcred *) rn) { /* already exists */
2520 np->netc_exflags = argp->ex_flags;
2521 np->netc_anon = argp->ex_anon;
2522 np->netc_anon.cr_ref = 1;
2525 free(np, M_NETADDR);
2531 vfs_free_netcred(rn, w)
2532 struct radix_node *rn;
2535 register struct radix_node_head *rnh = (struct radix_node_head *) w;
2537 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
2538 free((caddr_t) rn, M_NETADDR);
2543 * Free the net address hash lists that are hanging off the mount points.
2546 vfs_free_addrlist(nep)
2547 struct netexport *nep;
2550 register struct radix_node_head *rnh;
2552 for (i = 0; i <= AF_MAX; i++)
2553 if ((rnh = nep->ne_rtable[i])) {
2554 (*rnh->rnh_walktree) (rnh, vfs_free_netcred,
2556 free((caddr_t) rnh, M_RTABLE);
2557 nep->ne_rtable[i] = 0;
2562 vfs_export(mp, nep, argp)
2564 struct netexport *nep;
2565 struct export_args *argp;
2569 if (argp->ex_flags & MNT_DELEXPORT) {
2570 if (mp->mnt_flag & MNT_EXPUBLIC) {
2571 vfs_setpublicfs(NULL, NULL, NULL);
2572 mp->mnt_flag &= ~MNT_EXPUBLIC;
2574 vfs_free_addrlist(nep);
2575 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
2577 if (argp->ex_flags & MNT_EXPORTED) {
2578 if (argp->ex_flags & MNT_EXPUBLIC) {
2579 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
2581 mp->mnt_flag |= MNT_EXPUBLIC;
2583 if ((error = vfs_hang_addrlist(mp, nep, argp)))
2585 mp->mnt_flag |= MNT_EXPORTED;
2592 * Set the publicly exported filesystem (WebNFS). Currently, only
2593 * one public filesystem is possible in the spec (RFC 2054 and 2055)
2596 vfs_setpublicfs(mp, nep, argp)
2598 struct netexport *nep;
2599 struct export_args *argp;
2606 * mp == NULL -> invalidate the current info, the FS is
2607 * no longer exported. May be called from either vfs_export
2608 * or unmount, so check if it hasn't already been done.
2611 if (nfs_pub.np_valid) {
2612 nfs_pub.np_valid = 0;
2613 if (nfs_pub.np_index != NULL) {
2614 FREE(nfs_pub.np_index, M_TEMP);
2615 nfs_pub.np_index = NULL;
2622 * Only one allowed at a time.
2624 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
2628 * Get real filehandle for root of exported FS.
2630 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
2631 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
2633 if ((error = VFS_ROOT(mp, &rvp)))
2636 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
2642 * If an indexfile was specified, pull it in.
2644 if (argp->ex_indexfile != NULL) {
2645 MALLOC(nfs_pub.np_index, char *, MAXNAMLEN + 1, M_TEMP,
2647 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
2648 MAXNAMLEN, (size_t *)0);
2651 * Check for illegal filenames.
2653 for (cp = nfs_pub.np_index; *cp; cp++) {
2661 FREE(nfs_pub.np_index, M_TEMP);
2666 nfs_pub.np_mount = mp;
2667 nfs_pub.np_valid = 1;
2672 vfs_export_lookup(mp, nep, nam)
2673 register struct mount *mp;
2674 struct netexport *nep;
2675 struct sockaddr *nam;
2677 register struct netcred *np;
2678 register struct radix_node_head *rnh;
2679 struct sockaddr *saddr;
2682 if (mp->mnt_flag & MNT_EXPORTED) {
2684 * Lookup in the export list first.
2688 rnh = nep->ne_rtable[saddr->sa_family];
2690 np = (struct netcred *)
2691 (*rnh->rnh_matchaddr)((caddr_t)saddr,
2693 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
2698 * If no address match, use the default if it exists.
2700 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
2701 np = &nep->ne_defexported;
2707 * perform msync on all vnodes under a mount point
2708 * the mount point must be locked.
2711 vfs_msync(struct mount *mp, int flags)
2713 struct thread *td = curthread; /* XXX */
2714 struct vnode *vp, *nvp;
2715 struct vm_object *obj;
2719 lwkt_gettoken(&mntvnode_token);
2721 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nvp) {
2722 if (vp->v_mount != mp) {
2727 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
2729 if (vp->v_flag & VXLOCK) /* XXX: what if MNT_WAIT? */
2733 * There could be hundreds of thousands of vnodes, we cannot
2734 * afford to do anything heavy-weight until we have a fairly
2735 * good indication that there is something to do.
2737 if ((vp->v_flag & VOBJDIRTY) &&
2738 (flags == MNT_WAIT || VOP_ISLOCKED(vp, NULL) == 0)) {
2739 lwkt_reltoken(&mntvnode_token);
2741 LK_EXCLUSIVE | LK_RETRY | LK_NOOBJ, td)) {
2742 if (VOP_GETVOBJECT(vp, &obj) == 0) {
2743 vm_object_page_clean(obj, 0, 0, flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
2747 lwkt_gettoken(&mntvnode_token);
2748 if (TAILQ_NEXT(vp, v_nmntvnodes) != nvp) {
2755 lwkt_reltoken(&mntvnode_token);
2759 * Create the VM object needed for VMIO and mmap support. This
2760 * is done for all VREG files in the system. Some filesystems might
2761 * afford the additional metadata buffering capability of the
2762 * VMIO code by making the device node be VMIO mode also.
2764 * vp must be locked when vfs_object_create is called.
2767 vfs_object_create(struct vnode *vp, struct thread *td)
2769 return (VOP_CREATEVOBJECT(vp, td));
2779 lwkt_gettoken(&vnode_free_list_token);
2780 KASSERT((vp->v_flag & VFREE) == 0, ("vnode already free"));
2781 if (vp->v_flag & VAGE) {
2782 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2784 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
2787 lwkt_reltoken(&vnode_free_list_token);
2788 vp->v_flag &= ~VAGE;
2789 vp->v_flag |= VFREE;
2800 lwkt_gettoken(&vnode_free_list_token);
2801 KASSERT((vp->v_flag & VFREE) != 0, ("vnode not free"));
2802 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
2804 lwkt_reltoken(&vnode_free_list_token);
2805 vp->v_flag &= ~(VFREE|VAGE);
2810 * Record a process's interest in events which might happen to
2811 * a vnode. Because poll uses the historic select-style interface
2812 * internally, this routine serves as both the ``check for any
2813 * pending events'' and the ``record my interest in future events''
2814 * functions. (These are done together, while the lock is held,
2815 * to avoid race conditions.)
2818 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
2820 lwkt_gettoken(&vp->v_pollinfo.vpi_token);
2821 if (vp->v_pollinfo.vpi_revents & events) {
2823 * This leaves events we are not interested
2824 * in available for the other process which
2825 * which presumably had requested them
2826 * (otherwise they would never have been
2829 events &= vp->v_pollinfo.vpi_revents;
2830 vp->v_pollinfo.vpi_revents &= ~events;
2832 lwkt_reltoken(&vp->v_pollinfo.vpi_token);
2835 vp->v_pollinfo.vpi_events |= events;
2836 selrecord(td, &vp->v_pollinfo.vpi_selinfo);
2837 lwkt_reltoken(&vp->v_pollinfo.vpi_token);
2842 * Note the occurrence of an event. If the VN_POLLEVENT macro is used,
2843 * it is possible for us to miss an event due to race conditions, but
2844 * that condition is expected to be rare, so for the moment it is the
2845 * preferred interface.
2848 vn_pollevent(vp, events)
2852 lwkt_gettoken(&vp->v_pollinfo.vpi_token);
2853 if (vp->v_pollinfo.vpi_events & events) {
2855 * We clear vpi_events so that we don't
2856 * call selwakeup() twice if two events are
2857 * posted before the polling process(es) is
2858 * awakened. This also ensures that we take at
2859 * most one selwakeup() if the polling process
2860 * is no longer interested. However, it does
2861 * mean that only one event can be noticed at
2862 * a time. (Perhaps we should only clear those
2863 * event bits which we note?) XXX
2865 vp->v_pollinfo.vpi_events = 0; /* &= ~events ??? */
2866 vp->v_pollinfo.vpi_revents |= events;
2867 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2869 lwkt_reltoken(&vp->v_pollinfo.vpi_token);
2873 * Wake up anyone polling on vp because it is being revoked.
2874 * This depends on dead_poll() returning POLLHUP for correct
2881 lwkt_gettoken(&vp->v_pollinfo.vpi_token);
2882 if (vp->v_pollinfo.vpi_events) {
2883 vp->v_pollinfo.vpi_events = 0;
2884 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2886 lwkt_reltoken(&vp->v_pollinfo.vpi_token);
2892 * Routine to create and manage a filesystem syncer vnode.
2894 #define sync_close ((int (*) __P((struct vop_close_args *)))nullop)
2895 static int sync_fsync __P((struct vop_fsync_args *));
2896 static int sync_inactive __P((struct vop_inactive_args *));
2897 static int sync_reclaim __P((struct vop_reclaim_args *));
2898 #define sync_lock ((int (*) __P((struct vop_lock_args *)))vop_nolock)
2899 #define sync_unlock ((int (*) __P((struct vop_unlock_args *)))vop_nounlock)
2900 static int sync_print __P((struct vop_print_args *));
2901 #define sync_islocked ((int(*) __P((struct vop_islocked_args *)))vop_noislocked)
2903 static vop_t **sync_vnodeop_p;
2904 static struct vnodeopv_entry_desc sync_vnodeop_entries[] = {
2905 { &vop_default_desc, (vop_t *) vop_eopnotsupp },
2906 { &vop_close_desc, (vop_t *) sync_close }, /* close */
2907 { &vop_fsync_desc, (vop_t *) sync_fsync }, /* fsync */
2908 { &vop_inactive_desc, (vop_t *) sync_inactive }, /* inactive */
2909 { &vop_reclaim_desc, (vop_t *) sync_reclaim }, /* reclaim */
2910 { &vop_lock_desc, (vop_t *) sync_lock }, /* lock */
2911 { &vop_unlock_desc, (vop_t *) sync_unlock }, /* unlock */
2912 { &vop_print_desc, (vop_t *) sync_print }, /* print */
2913 { &vop_islocked_desc, (vop_t *) sync_islocked }, /* islocked */
2916 static struct vnodeopv_desc sync_vnodeop_opv_desc =
2917 { &sync_vnodeop_p, sync_vnodeop_entries };
2919 VNODEOP_SET(sync_vnodeop_opv_desc);
2922 * Create a new filesystem syncer vnode for the specified mount point.
2925 vfs_allocate_syncvnode(mp)
2929 static long start, incr, next;
2932 /* Allocate a new vnode */
2933 if ((error = getnewvnode(VT_VFS, mp, sync_vnodeop_p, &vp)) != 0) {
2934 mp->mnt_syncer = NULL;
2939 * Place the vnode onto the syncer worklist. We attempt to
2940 * scatter them about on the list so that they will go off
2941 * at evenly distributed times even if all the filesystems
2942 * are mounted at once.
2945 if (next == 0 || next > syncer_maxdelay) {
2949 start = syncer_maxdelay / 2;
2950 incr = syncer_maxdelay;
2954 vn_syncer_add_to_worklist(vp, syncdelay > 0 ? next % syncdelay : 0);
2955 mp->mnt_syncer = vp;
2960 * Do a lazy sync of the filesystem.
2964 struct vop_fsync_args /* {
2966 struct ucred *a_cred;
2968 struct thread *a_td;
2971 struct vnode *syncvp = ap->a_vp;
2972 struct mount *mp = syncvp->v_mount;
2973 struct thread *td = ap->a_td;
2977 * We only need to do something if this is a lazy evaluation.
2979 if (ap->a_waitfor != MNT_LAZY)
2983 * Move ourselves to the back of the sync list.
2985 vn_syncer_add_to_worklist(syncvp, syncdelay);
2988 * Walk the list of vnodes pushing all that are dirty and
2989 * not already on the sync list.
2991 lwkt_gettoken(&mountlist_token);
2992 if (vfs_busy(mp, LK_EXCLUSIVE | LK_NOWAIT, &mountlist_token, td) != 0) {
2993 lwkt_reltoken(&mountlist_token);
2996 asyncflag = mp->mnt_flag & MNT_ASYNC;
2997 mp->mnt_flag &= ~MNT_ASYNC;
2998 vfs_msync(mp, MNT_NOWAIT);
2999 VFS_SYNC(mp, MNT_LAZY, td);
3001 mp->mnt_flag |= MNT_ASYNC;
3007 * The syncer vnode is no referenced.
3011 struct vop_inactive_args /* {
3022 * The syncer vnode is no longer needed and is being decommissioned.
3024 * Modifications to the worklist must be protected at splbio().
3028 struct vop_reclaim_args /* {
3032 struct vnode *vp = ap->a_vp;
3036 vp->v_mount->mnt_syncer = NULL;
3037 if (vp->v_flag & VONWORKLST) {
3038 LIST_REMOVE(vp, v_synclist);
3039 vp->v_flag &= ~VONWORKLST;
3047 * Print out a syncer vnode.
3051 struct vop_print_args /* {
3055 struct vnode *vp = ap->a_vp;
3057 printf("syncer vnode");
3058 if (vp->v_vnlock != NULL)
3059 lockmgr_printinfo(vp->v_vnlock);
3065 * extract the dev_t from a VBLK or VCHR
3071 if (vp->v_type != VBLK && vp->v_type != VCHR)
3073 return (vp->v_rdev);
3077 * Check if vnode represents a disk device
3084 if (vp->v_type != VBLK && vp->v_type != VCHR) {
3089 if (vp->v_rdev == NULL) {
3094 if (!devsw(vp->v_rdev)) {
3099 if (!(devsw(vp->v_rdev)->d_flags & D_DISK)) {
3111 struct nameidata *ndp;
3114 if (!(flags & NDF_NO_FREE_PNBUF) &&
3115 (ndp->ni_cnd.cn_flags & HASBUF)) {
3116 zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
3117 ndp->ni_cnd.cn_flags &= ~HASBUF;
3119 if (!(flags & NDF_NO_DVP_UNLOCK) &&
3120 (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
3121 ndp->ni_dvp != ndp->ni_vp)
3122 VOP_UNLOCK(ndp->ni_dvp, 0, ndp->ni_cnd.cn_td);
3123 if (!(flags & NDF_NO_DVP_RELE) &&
3124 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
3128 if (!(flags & NDF_NO_VP_UNLOCK) &&
3129 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
3130 VOP_UNLOCK(ndp->ni_vp, 0, ndp->ni_cnd.cn_td);
3131 if (!(flags & NDF_NO_VP_RELE) &&
3136 if (!(flags & NDF_NO_STARTDIR_RELE) &&
3137 (ndp->ni_cnd.cn_flags & SAVESTART)) {
3138 vrele(ndp->ni_startdir);
3139 ndp->ni_startdir = NULL;