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.25 2004/02/10 07:34:42 dillon Exp $
44 * External virtual filesystem routines
48 #include <sys/param.h>
49 #include <sys/systm.h>
52 #include <sys/dirent.h>
53 #include <sys/domain.h>
54 #include <sys/eventhandler.h>
55 #include <sys/fcntl.h>
56 #include <sys/kernel.h>
57 #include <sys/kthread.h>
58 #include <sys/malloc.h>
60 #include <sys/mount.h>
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>
76 #include <vm/vm_kern.h>
78 #include <vm/vm_map.h>
79 #include <vm/vm_page.h>
80 #include <vm/vm_pager.h>
81 #include <vm/vnode_pager.h>
82 #include <vm/vm_zone.h>
85 #include <sys/thread2.h>
87 static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
89 static void insmntque (struct vnode *vp, struct mount *mp);
90 static void vclean (struct vnode *vp, int flags, struct thread *td);
91 static unsigned long numvnodes;
92 static void vlruvp(struct vnode *vp);
93 SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, "");
95 enum vtype iftovt_tab[16] = {
96 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
97 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
100 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
101 S_IFSOCK, S_IFIFO, S_IFMT,
104 static TAILQ_HEAD(freelst, vnode) vnode_free_list; /* vnode free list */
106 static u_long wantfreevnodes = 25;
107 SYSCTL_INT(_debug, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, "");
108 static u_long freevnodes = 0;
109 SYSCTL_INT(_debug, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0, "");
111 static int reassignbufcalls;
112 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0, "");
113 static int reassignbufloops;
114 SYSCTL_INT(_vfs, OID_AUTO, reassignbufloops, CTLFLAG_RW, &reassignbufloops, 0, "");
115 static int reassignbufsortgood;
116 SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortgood, CTLFLAG_RW, &reassignbufsortgood, 0, "");
117 static int reassignbufsortbad;
118 SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortbad, CTLFLAG_RW, &reassignbufsortbad, 0, "");
119 static int reassignbufmethod = 1;
120 SYSCTL_INT(_vfs, OID_AUTO, reassignbufmethod, CTLFLAG_RW, &reassignbufmethod, 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 SYSCTL_INT(_kern, OID_AUTO, syncdelay, CTLFLAG_RW, &syncdelay, 0,
144 "VFS data synchronization delay");
145 time_t filedelay = 30; /* time to delay syncing files */
146 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0,
147 "File synchronization delay");
148 time_t dirdelay = 29; /* time to delay syncing directories */
149 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0,
150 "Directory synchronization delay");
151 time_t metadelay = 28; /* time to delay syncing metadata */
152 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0,
153 "VFS metadata synchronization delay");
154 static int rushjob; /* number of slots to run ASAP */
155 static int stat_rush_requests; /* number of times I/O speeded up */
156 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0, "");
158 static int syncer_delayno = 0;
159 static long syncer_mask;
160 LIST_HEAD(synclist, vnode);
161 static struct synclist *syncer_workitem_pending;
164 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
165 &desiredvnodes, 0, "Maximum number of vnodes");
166 static int minvnodes;
167 SYSCTL_INT(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
168 &minvnodes, 0, "Minimum number of vnodes");
169 static int vnlru_nowhere = 0;
170 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW, &vnlru_nowhere, 0,
171 "Number of times the vnlru process ran without success");
173 static void vfs_free_addrlist (struct netexport *nep);
174 static int vfs_free_netcred (struct radix_node *rn, void *w);
175 static int vfs_hang_addrlist (struct mount *mp, struct netexport *nep,
176 struct export_args *argp);
179 * Initialize the vnode management data structures.
186 * Desired vnodes is a result of the physical page count
187 * and the size of kernel's heap. It scales in proportion
188 * to the amount of available physical memory. This can
189 * cause trouble on 64-bit and large memory platforms.
191 /* desiredvnodes = maxproc + vmstats.v_page_count / 4; */
193 min(maxproc + vmstats.v_page_count /4,
194 2 * (VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) /
195 (5 * (sizeof(struct vm_object) + sizeof(struct vnode))));
197 minvnodes = desiredvnodes / 4;
198 lwkt_inittoken(&mountlist_token);
199 lwkt_inittoken(&mntvnode_token);
200 lwkt_inittoken(&mntid_token);
201 lwkt_inittoken(&spechash_token);
202 TAILQ_INIT(&vnode_free_list);
203 lwkt_inittoken(&vnode_free_list_token);
204 vnode_zone = zinit("VNODE", sizeof (struct vnode), 0, 0, 5);
206 * Initialize the filesystem syncer.
208 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE,
210 syncer_maxdelay = syncer_mask + 1;
214 * Mark a mount point as busy. Used to synchronize access and to delay
215 * unmounting. Interlock is not released on failure.
218 vfs_busy(struct mount *mp, int flags, struct lwkt_token *interlkp,
223 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
224 if (flags & LK_NOWAIT)
226 mp->mnt_kern_flag |= MNTK_MWAIT;
228 lwkt_reltoken(interlkp);
231 * Since all busy locks are shared except the exclusive
232 * lock granted when unmounting, the only place that a
233 * wakeup needs to be done is at the release of the
234 * exclusive lock at the end of dounmount.
236 tsleep((caddr_t)mp, 0, "vfs_busy", 0);
238 lwkt_gettoken(interlkp);
242 lkflags = LK_SHARED | LK_NOPAUSE;
244 lkflags |= LK_INTERLOCK;
245 if (lockmgr(&mp->mnt_lock, lkflags, interlkp, td))
246 panic("vfs_busy: unexpected lock failure");
251 * Free a busy filesystem.
254 vfs_unbusy(struct mount *mp, struct thread *td)
256 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td);
260 * Lookup a filesystem type, and if found allocate and initialize
261 * a mount structure for it.
263 * Devname is usually updated by mount(8) after booting.
266 vfs_rootmountalloc(char *fstypename, char *devname, struct mount **mpp)
268 struct thread *td = curthread; /* XXX */
269 struct vfsconf *vfsp;
272 if (fstypename == NULL)
274 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
275 if (!strcmp(vfsp->vfc_name, fstypename))
279 mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
280 bzero((char *)mp, (u_long)sizeof(struct mount));
281 lockinit(&mp->mnt_lock, 0, "vfslock", VLKTIMEOUT, LK_NOPAUSE);
282 (void)vfs_busy(mp, LK_NOWAIT, 0, td);
283 TAILQ_INIT(&mp->mnt_nvnodelist);
284 TAILQ_INIT(&mp->mnt_reservedvnlist);
285 mp->mnt_nvnodelistsize = 0;
287 mp->mnt_op = vfsp->vfc_vfsops;
288 mp->mnt_flag = MNT_RDONLY;
289 mp->mnt_vnodecovered = NULLVP;
290 vfsp->vfc_refcount++;
291 mp->mnt_iosize_max = DFLTPHYS;
292 mp->mnt_stat.f_type = vfsp->vfc_typenum;
293 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
294 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
295 mp->mnt_stat.f_mntonname[0] = '/';
296 mp->mnt_stat.f_mntonname[1] = 0;
297 (void) copystr(devname, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 0);
303 * Find an appropriate filesystem to use for the root. If a filesystem
304 * has not been preselected, walk through the list of known filesystems
305 * trying those that have mountroot routines, and try them until one
306 * works or we have tried them all.
308 #ifdef notdef /* XXX JH */
310 lite2_vfs_mountroot()
312 struct vfsconf *vfsp;
313 extern int (*lite2_mountroot) (void);
316 if (lite2_mountroot != NULL)
317 return ((*lite2_mountroot)());
318 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
319 if (vfsp->vfc_mountroot == NULL)
321 if ((error = (*vfsp->vfc_mountroot)()) == 0)
323 printf("%s_mountroot failed: %d\n", vfsp->vfc_name, error);
330 * Lookup a mount point by filesystem identifier.
338 lwkt_gettoken(&mountlist_token);
339 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
340 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
341 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
342 lwkt_reltoken(&mountlist_token);
346 lwkt_reltoken(&mountlist_token);
347 return ((struct mount *) 0);
351 * Get a new unique fsid. Try to make its val[0] unique, since this value
352 * will be used to create fake device numbers for stat(). Also try (but
353 * not so hard) make its val[0] unique mod 2^16, since some emulators only
354 * support 16-bit device numbers. We end up with unique val[0]'s for the
355 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
357 * Keep in mind that several mounts may be running in parallel. Starting
358 * the search one past where the previous search terminated is both a
359 * micro-optimization and a defense against returning the same fsid to
366 static u_int16_t mntid_base;
370 lwkt_gettoken(&mntid_token);
371 mtype = mp->mnt_vfc->vfc_typenum;
372 tfsid.val[1] = mtype;
373 mtype = (mtype & 0xFF) << 24;
375 tfsid.val[0] = makeudev(255,
376 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
378 if (vfs_getvfs(&tfsid) == NULL)
381 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
382 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
383 lwkt_reltoken(&mntid_token);
387 * Knob to control the precision of file timestamps:
389 * 0 = seconds only; nanoseconds zeroed.
390 * 1 = seconds and nanoseconds, accurate within 1/HZ.
391 * 2 = seconds and nanoseconds, truncated to microseconds.
392 * >=3 = seconds and nanoseconds, maximum precision.
394 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
396 static int timestamp_precision = TSP_SEC;
397 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
398 ×tamp_precision, 0, "");
401 * Get a current timestamp.
405 struct timespec *tsp;
409 switch (timestamp_precision) {
411 tsp->tv_sec = time_second;
419 TIMEVAL_TO_TIMESPEC(&tv, tsp);
429 * Set vnode attributes to VNOVAL
437 vap->va_size = VNOVAL;
438 vap->va_bytes = VNOVAL;
439 vap->va_mode = VNOVAL;
440 vap->va_nlink = VNOVAL;
441 vap->va_uid = VNOVAL;
442 vap->va_gid = VNOVAL;
443 vap->va_fsid = VNOVAL;
444 vap->va_fileid = VNOVAL;
445 vap->va_blocksize = VNOVAL;
446 vap->va_rdev = VNOVAL;
447 vap->va_atime.tv_sec = VNOVAL;
448 vap->va_atime.tv_nsec = VNOVAL;
449 vap->va_mtime.tv_sec = VNOVAL;
450 vap->va_mtime.tv_nsec = VNOVAL;
451 vap->va_ctime.tv_sec = VNOVAL;
452 vap->va_ctime.tv_nsec = VNOVAL;
453 vap->va_flags = VNOVAL;
454 vap->va_gen = VNOVAL;
459 * This routine is called when we have too many vnodes. It attempts
460 * to free <count> vnodes and will potentially free vnodes that still
461 * have VM backing store (VM backing store is typically the cause
462 * of a vnode blowout so we want to do this). Therefore, this operation
463 * is not considered cheap.
465 * A number of conditions may prevent a vnode from being reclaimed.
466 * the buffer cache may have references on the vnode, a directory
467 * vnode may still have references due to the namei cache representing
468 * underlying files, or the vnode may be in active use. It is not
469 * desireable to reuse such vnodes. These conditions may cause the
470 * number of vnodes to reach some minimum value regardless of what
471 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
474 vlrureclaim(struct mount *mp)
484 * Calculate the trigger point, don't allow user
485 * screwups to blow us up. This prevents us from
486 * recycling vnodes with lots of resident pages. We
487 * aren't trying to free memory, we are trying to
490 usevnodes = desiredvnodes;
493 trigger = vmstats.v_page_count * 2 / usevnodes;
496 gen = lwkt_gettoken(&mntvnode_token);
497 count = mp->mnt_nvnodelistsize / 10 + 1;
498 while (count && (vp = TAILQ_FIRST(&mp->mnt_nvnodelist)) != NULL) {
499 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
500 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
502 if (vp->v_type != VNON &&
503 vp->v_type != VBAD &&
504 VMIGHTFREE(vp) && /* critical path opt */
505 (vp->v_object == NULL || vp->v_object->resident_page_count < trigger)
507 lwkt_gettoken(&vp->v_interlock);
508 if (lwkt_gentoken(&mntvnode_token, &gen) == 0) {
509 if (VMIGHTFREE(vp)) {
510 vgonel(vp, curthread);
513 lwkt_reltoken(&vp->v_interlock);
516 lwkt_reltoken(&vp->v_interlock);
521 lwkt_reltoken(&mntvnode_token);
526 * Attempt to recycle vnodes in a context that is always safe to block.
527 * Calling vlrurecycle() from the bowels of file system code has some
528 * interesting deadlock problems.
530 static struct thread *vnlruthread;
531 static int vnlruproc_sig;
536 struct mount *mp, *nmp;
539 struct thread *td = curthread;
541 EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc, td,
546 kproc_suspend_loop();
547 if (numvnodes - freevnodes <= desiredvnodes * 9 / 10) {
549 wakeup(&vnlruproc_sig);
550 tsleep(td, 0, "vlruwt", hz);
554 lwkt_gettoken(&mountlist_token);
555 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
556 if (vfs_busy(mp, LK_NOWAIT, &mountlist_token, td)) {
557 nmp = TAILQ_NEXT(mp, mnt_list);
560 done += vlrureclaim(mp);
561 lwkt_gettoken(&mountlist_token);
562 nmp = TAILQ_NEXT(mp, mnt_list);
565 lwkt_reltoken(&mountlist_token);
568 tsleep(td, 0, "vlrup", hz * 3);
574 static struct kproc_desc vnlru_kp = {
579 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &vnlru_kp)
582 * Routines having to do with the management of the vnode table.
584 extern vop_t **dead_vnodeop_p;
587 * Return the next vnode from the free list.
590 getnewvnode(tag, mp, vops, vpp)
599 struct thread *td = curthread; /* XXX */
600 struct vnode *vp = NULL;
606 * Try to reuse vnodes if we hit the max. This situation only
607 * occurs in certain large-memory (2G+) situations. We cannot
608 * attempt to directly reclaim vnodes due to nasty recursion
611 while (numvnodes - freevnodes > desiredvnodes) {
612 if (vnlruproc_sig == 0) {
613 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
616 tsleep(&vnlruproc_sig, 0, "vlruwk", hz);
621 * Attempt to reuse a vnode already on the free list, allocating
622 * a new vnode if we can't find one or if we have not reached a
623 * good minimum for good LRU performance.
625 gen = lwkt_gettoken(&vnode_free_list_token);
626 if (freevnodes >= wantfreevnodes && numvnodes >= minvnodes) {
629 for (count = 0; count < freevnodes; count++) {
630 vp = TAILQ_FIRST(&vnode_free_list);
631 if (vp == NULL || vp->v_usecount)
632 panic("getnewvnode: free vnode isn't");
635 * Get the vnode's interlock, then re-obtain
636 * vnode_free_list_token in case we lost it. If we
637 * did lose it while getting the vnode interlock,
638 * even if we got it back again, then retry.
640 vgen = lwkt_gettoken(&vp->v_interlock);
641 if (lwkt_gentoken(&vnode_free_list_token, &gen) != 0) {
643 lwkt_reltoken(&vp->v_interlock);
649 * Whew! We have both tokens. Since we didn't lose
650 * the free list VFREE had better still be set. But
651 * we aren't out of the woods yet. We have to get
652 * the object (may block). If the vnode is not
653 * suitable then move it to the end of the list
654 * if we can. If we can't move it to the end of the
657 if ((VOP_GETVOBJECT(vp, &object) == 0 &&
658 (object->resident_page_count || object->ref_count))
660 if (lwkt_gentoken(&vp->v_interlock, &vgen) == 0 &&
661 lwkt_gentoken(&vnode_free_list_token, &gen) == 0
663 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
664 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
668 lwkt_reltoken(&vp->v_interlock);
674 * Still not out of the woods. VOBJECT might have
675 * blocked, if we did not retain our tokens we have
678 if (lwkt_gentoken(&vp->v_interlock, &vgen) != 0 ||
679 lwkt_gentoken(&vnode_free_list_token, &gen) != 0) {
684 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
685 KKASSERT(vp->v_flag & VFREE);
688 * If we have children in the namecache we cannot
689 * reuse the vnode yet because it will break the
690 * namecache chain (YYY use nc_refs for the check?)
692 if (TAILQ_FIRST(&vp->v_namecache)) {
693 if (cache_leaf_test(vp) < 0) {
694 lwkt_reltoken(&vp->v_interlock);
695 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
705 vp->v_flag |= VDOOMED;
706 vp->v_flag &= ~VFREE;
708 lwkt_reltoken(&vnode_free_list_token);
709 cache_purge(vp); /* YYY may block */
711 if (vp->v_type != VBAD) {
714 lwkt_reltoken(&vp->v_interlock);
722 panic("cleaned vnode isn't");
725 panic("Clean vnode has pending I/O's");
735 vp->v_writecount = 0; /* XXX */
737 lwkt_reltoken(&vnode_free_list_token);
738 vp = (struct vnode *) zalloc(vnode_zone);
739 bzero((char *) vp, sizeof *vp);
740 lwkt_inittoken(&vp->v_interlock);
741 lwkt_inittoken(&vp->v_pollinfo.vpi_token);
744 TAILQ_INIT(&vp->v_namecache);
748 TAILQ_INIT(&vp->v_cleanblkhd);
749 TAILQ_INIT(&vp->v_dirtyblkhd);
759 vfs_object_create(vp, td);
764 * Move a vnode from one mount queue to another.
772 lwkt_gettoken(&mntvnode_token);
774 * Delete from old mount point vnode list, if on one.
776 if (vp->v_mount != NULL) {
777 KASSERT(vp->v_mount->mnt_nvnodelistsize > 0,
778 ("bad mount point vnode list size"));
779 TAILQ_REMOVE(&vp->v_mount->mnt_nvnodelist, vp, v_nmntvnodes);
780 vp->v_mount->mnt_nvnodelistsize--;
783 * Insert into list of vnodes for the new mount point, if available.
785 if ((vp->v_mount = mp) == NULL) {
786 lwkt_reltoken(&mntvnode_token);
789 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
790 mp->mnt_nvnodelistsize++;
791 lwkt_reltoken(&mntvnode_token);
795 * Update outstanding I/O count and do wakeup if requested.
803 bp->b_flags &= ~B_WRITEINPROG;
804 if ((vp = bp->b_vp)) {
806 if (vp->v_numoutput < 0)
807 panic("vwakeup: neg numoutput");
808 if ((vp->v_numoutput == 0) && (vp->v_flag & VBWAIT)) {
809 vp->v_flag &= ~VBWAIT;
810 wakeup((caddr_t) &vp->v_numoutput);
816 * Flush out and invalidate all buffers associated with a vnode.
817 * Called with the underlying object locked.
820 vinvalbuf(struct vnode *vp, int flags, struct thread *td,
821 int slpflag, int slptimeo)
824 struct buf *nbp, *blist;
828 if (flags & V_SAVE) {
830 while (vp->v_numoutput) {
831 vp->v_flag |= VBWAIT;
832 error = tsleep((caddr_t)&vp->v_numoutput,
833 slpflag, "vinvlbuf", slptimeo);
839 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
841 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) != 0)
844 if (vp->v_numoutput > 0 ||
845 !TAILQ_EMPTY(&vp->v_dirtyblkhd))
846 panic("vinvalbuf: dirty bufs");
852 blist = TAILQ_FIRST(&vp->v_cleanblkhd);
854 blist = TAILQ_FIRST(&vp->v_dirtyblkhd);
858 for (bp = blist; bp; bp = nbp) {
859 nbp = TAILQ_NEXT(bp, b_vnbufs);
860 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
861 error = BUF_TIMELOCK(bp,
862 LK_EXCLUSIVE | LK_SLEEPFAIL,
863 "vinvalbuf", slpflag, slptimeo);
870 * XXX Since there are no node locks for NFS, I
871 * believe there is a slight chance that a delayed
872 * write will occur while sleeping just above, so
873 * check for it. Note that vfs_bio_awrite expects
874 * buffers to reside on a queue, while VOP_BWRITE and
877 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
880 if (bp->b_vp == vp) {
881 if (bp->b_flags & B_CLUSTEROK) {
886 bp->b_flags |= B_ASYNC;
887 VOP_BWRITE(bp->b_vp, bp);
891 (void) VOP_BWRITE(bp->b_vp, bp);
896 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
897 bp->b_flags &= ~B_ASYNC;
903 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
904 * have write I/O in-progress but if there is a VM object then the
905 * VM object can also have read-I/O in-progress.
908 while (vp->v_numoutput > 0) {
909 vp->v_flag |= VBWAIT;
910 tsleep(&vp->v_numoutput, 0, "vnvlbv", 0);
912 if (VOP_GETVOBJECT(vp, &object) == 0) {
913 while (object->paging_in_progress)
914 vm_object_pip_sleep(object, "vnvlbx");
916 } while (vp->v_numoutput > 0);
921 * Destroy the copy in the VM cache, too.
923 lwkt_gettoken(&vp->v_interlock);
924 if (VOP_GETVOBJECT(vp, &object) == 0) {
925 vm_object_page_remove(object, 0, 0,
926 (flags & V_SAVE) ? TRUE : FALSE);
928 lwkt_reltoken(&vp->v_interlock);
930 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd) || !TAILQ_EMPTY(&vp->v_cleanblkhd))
931 panic("vinvalbuf: flush failed");
936 * Truncate a file's buffer and pages to a specified length. This
937 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
941 vtruncbuf(struct vnode *vp, struct thread *td, off_t length, int blksize)
949 * Round up to the *next* lbn.
951 trunclbn = (length + blksize - 1) / blksize;
958 for (bp = TAILQ_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
959 nbp = TAILQ_NEXT(bp, b_vnbufs);
960 if (bp->b_lblkno >= trunclbn) {
961 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
962 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
966 bp->b_flags |= (B_INVAL | B_RELBUF);
967 bp->b_flags &= ~B_ASYNC;
972 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
974 (nbp->b_flags & B_DELWRI))) {
980 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
981 nbp = TAILQ_NEXT(bp, b_vnbufs);
982 if (bp->b_lblkno >= trunclbn) {
983 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
984 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
988 bp->b_flags |= (B_INVAL | B_RELBUF);
989 bp->b_flags &= ~B_ASYNC;
994 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
996 (nbp->b_flags & B_DELWRI) == 0)) {
1005 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
1006 nbp = TAILQ_NEXT(bp, b_vnbufs);
1007 if ((bp->b_flags & B_DELWRI) && (bp->b_lblkno < 0)) {
1008 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
1009 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
1013 if (bp->b_vp == vp) {
1014 bp->b_flags |= B_ASYNC;
1016 bp->b_flags &= ~B_ASYNC;
1018 VOP_BWRITE(bp->b_vp, bp);
1026 while (vp->v_numoutput > 0) {
1027 vp->v_flag |= VBWAIT;
1028 tsleep(&vp->v_numoutput, 0, "vbtrunc", 0);
1033 vnode_pager_setsize(vp, length);
1039 * Associate a buffer with a vnode.
1048 KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
1052 bp->b_dev = vn_todev(vp);
1054 * Insert onto list for new vnode.
1057 bp->b_xflags |= BX_VNCLEAN;
1058 bp->b_xflags &= ~BX_VNDIRTY;
1059 TAILQ_INSERT_TAIL(&vp->v_cleanblkhd, bp, b_vnbufs);
1064 * Disassociate a buffer from a vnode.
1071 struct buflists *listheadp;
1074 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1077 * Delete from old vnode list, if on one.
1081 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) {
1082 if (bp->b_xflags & BX_VNDIRTY)
1083 listheadp = &vp->v_dirtyblkhd;
1085 listheadp = &vp->v_cleanblkhd;
1086 TAILQ_REMOVE(listheadp, bp, b_vnbufs);
1087 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1089 if ((vp->v_flag & VONWORKLST) && TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
1090 vp->v_flag &= ~VONWORKLST;
1091 LIST_REMOVE(vp, v_synclist);
1094 bp->b_vp = (struct vnode *) 0;
1099 * The workitem queue.
1101 * It is useful to delay writes of file data and filesystem metadata
1102 * for tens of seconds so that quickly created and deleted files need
1103 * not waste disk bandwidth being created and removed. To realize this,
1104 * we append vnodes to a "workitem" queue. When running with a soft
1105 * updates implementation, most pending metadata dependencies should
1106 * not wait for more than a few seconds. Thus, mounted on block devices
1107 * are delayed only about a half the time that file data is delayed.
1108 * Similarly, directory updates are more critical, so are only delayed
1109 * about a third the time that file data is delayed. Thus, there are
1110 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
1111 * one each second (driven off the filesystem syncer process). The
1112 * syncer_delayno variable indicates the next queue that is to be processed.
1113 * Items that need to be processed soon are placed in this queue:
1115 * syncer_workitem_pending[syncer_delayno]
1117 * A delay of fifteen seconds is done by placing the request fifteen
1118 * entries later in the queue:
1120 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
1125 * Add an item to the syncer work queue.
1128 vn_syncer_add_to_worklist(struct vnode *vp, int delay)
1134 if (vp->v_flag & VONWORKLST) {
1135 LIST_REMOVE(vp, v_synclist);
1138 if (delay > syncer_maxdelay - 2)
1139 delay = syncer_maxdelay - 2;
1140 slot = (syncer_delayno + delay) & syncer_mask;
1142 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], vp, v_synclist);
1143 vp->v_flag |= VONWORKLST;
1147 struct thread *updatethread;
1148 static void sched_sync (void);
1149 static struct kproc_desc up_kp = {
1154 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp)
1157 * System filesystem synchronizer daemon.
1162 struct synclist *slp;
1166 struct thread *td = curthread;
1168 EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc, td,
1172 kproc_suspend_loop();
1174 starttime = time_second;
1177 * Push files whose dirty time has expired. Be careful
1178 * of interrupt race on slp queue.
1181 slp = &syncer_workitem_pending[syncer_delayno];
1182 syncer_delayno += 1;
1183 if (syncer_delayno == syncer_maxdelay)
1187 while ((vp = LIST_FIRST(slp)) != NULL) {
1188 if (VOP_ISLOCKED(vp, NULL) == 0) {
1189 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1190 (void) VOP_FSYNC(vp, MNT_LAZY, td);
1191 VOP_UNLOCK(vp, 0, td);
1194 if (LIST_FIRST(slp) == vp) {
1196 * Note: v_tag VT_VFS vps can remain on the
1197 * worklist too with no dirty blocks, but
1198 * since sync_fsync() moves it to a different
1201 if (TAILQ_EMPTY(&vp->v_dirtyblkhd) &&
1202 !vn_isdisk(vp, NULL))
1203 panic("sched_sync: fsync failed vp %p tag %d", vp, vp->v_tag);
1205 * Put us back on the worklist. The worklist
1206 * routine will remove us from our current
1207 * position and then add us back in at a later
1210 vn_syncer_add_to_worklist(vp, syncdelay);
1216 * Do soft update processing.
1219 (*bioops.io_sync)(NULL);
1222 * The variable rushjob allows the kernel to speed up the
1223 * processing of the filesystem syncer process. A rushjob
1224 * value of N tells the filesystem syncer to process the next
1225 * N seconds worth of work on its queue ASAP. Currently rushjob
1226 * is used by the soft update code to speed up the filesystem
1227 * syncer process when the incore state is getting so far
1228 * ahead of the disk that the kernel memory pool is being
1229 * threatened with exhaustion.
1236 * If it has taken us less than a second to process the
1237 * current work, then wait. Otherwise start right over
1238 * again. We can still lose time if any single round
1239 * takes more than two seconds, but it does not really
1240 * matter as we are just trying to generally pace the
1241 * filesystem activity.
1243 if (time_second == starttime)
1244 tsleep(&lbolt, 0, "syncer", 0);
1249 * Request the syncer daemon to speed up its work.
1250 * We never push it to speed up more than half of its
1251 * normal turn time, otherwise it could take over the cpu.
1253 * YYY wchan field protected by the BGL.
1259 if (updatethread->td_wchan == &lbolt) { /* YYY */
1260 unsleep(updatethread);
1261 lwkt_schedule(updatethread);
1264 if (rushjob < syncdelay / 2) {
1266 stat_rush_requests += 1;
1273 * Associate a p-buffer with a vnode.
1275 * Also sets B_PAGING flag to indicate that vnode is not fully associated
1276 * with the buffer. i.e. the bp has not been linked into the vnode or
1285 KASSERT(bp->b_vp == NULL, ("pbgetvp: not free"));
1288 bp->b_flags |= B_PAGING;
1289 bp->b_dev = vn_todev(vp);
1293 * Disassociate a p-buffer from a vnode.
1300 KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL"));
1303 if (TAILQ_NEXT(bp, b_vnbufs) != NULL) {
1305 "relpbuf(): b_vp was probably reassignbuf()d %p %x",
1310 bp->b_vp = (struct vnode *) 0;
1311 bp->b_flags &= ~B_PAGING;
1315 pbreassignbuf(bp, newvp)
1317 struct vnode *newvp;
1319 if ((bp->b_flags & B_PAGING) == 0) {
1321 "pbreassignbuf() on non phys bp %p",
1329 * Reassign a buffer from one vnode to another.
1330 * Used to assign file specific control information
1331 * (indirect blocks) to the vnode to which they belong.
1334 reassignbuf(bp, newvp)
1336 struct vnode *newvp;
1338 struct buflists *listheadp;
1342 if (newvp == NULL) {
1343 printf("reassignbuf: NULL");
1349 * B_PAGING flagged buffers cannot be reassigned because their vp
1350 * is not fully linked in.
1352 if (bp->b_flags & B_PAGING)
1353 panic("cannot reassign paging buffer");
1357 * Delete from old vnode list, if on one.
1359 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) {
1360 if (bp->b_xflags & BX_VNDIRTY)
1361 listheadp = &bp->b_vp->v_dirtyblkhd;
1363 listheadp = &bp->b_vp->v_cleanblkhd;
1364 TAILQ_REMOVE(listheadp, bp, b_vnbufs);
1365 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1366 if (bp->b_vp != newvp) {
1368 bp->b_vp = NULL; /* for clarification */
1372 * If dirty, put on list of dirty buffers; otherwise insert onto list
1375 if (bp->b_flags & B_DELWRI) {
1378 listheadp = &newvp->v_dirtyblkhd;
1379 if ((newvp->v_flag & VONWORKLST) == 0) {
1380 switch (newvp->v_type) {
1386 if (newvp->v_specmountpoint != NULL) {
1394 vn_syncer_add_to_worklist(newvp, delay);
1396 bp->b_xflags |= BX_VNDIRTY;
1397 tbp = TAILQ_FIRST(listheadp);
1399 bp->b_lblkno == 0 ||
1400 (bp->b_lblkno > 0 && tbp->b_lblkno < 0) ||
1401 (bp->b_lblkno > 0 && bp->b_lblkno < tbp->b_lblkno)) {
1402 TAILQ_INSERT_HEAD(listheadp, bp, b_vnbufs);
1403 ++reassignbufsortgood;
1404 } else if (bp->b_lblkno < 0) {
1405 TAILQ_INSERT_TAIL(listheadp, bp, b_vnbufs);
1406 ++reassignbufsortgood;
1407 } else if (reassignbufmethod == 1) {
1409 * New sorting algorithm, only handle sequential case,
1410 * otherwise append to end (but before metadata)
1412 if ((tbp = gbincore(newvp, bp->b_lblkno - 1)) != NULL &&
1413 (tbp->b_xflags & BX_VNDIRTY)) {
1415 * Found the best place to insert the buffer
1417 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1418 ++reassignbufsortgood;
1421 * Missed, append to end, but before meta-data.
1422 * We know that the head buffer in the list is
1423 * not meta-data due to prior conditionals.
1425 * Indirect effects: NFS second stage write
1426 * tends to wind up here, giving maximum
1427 * distance between the unstable write and the
1430 tbp = TAILQ_LAST(listheadp, buflists);
1431 while (tbp && tbp->b_lblkno < 0)
1432 tbp = TAILQ_PREV(tbp, buflists, b_vnbufs);
1433 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1434 ++reassignbufsortbad;
1438 * Old sorting algorithm, scan queue and insert
1441 while ((ttbp = TAILQ_NEXT(tbp, b_vnbufs)) &&
1442 (ttbp->b_lblkno < bp->b_lblkno)) {
1446 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1449 bp->b_xflags |= BX_VNCLEAN;
1450 TAILQ_INSERT_TAIL(&newvp->v_cleanblkhd, bp, b_vnbufs);
1451 if ((newvp->v_flag & VONWORKLST) &&
1452 TAILQ_EMPTY(&newvp->v_dirtyblkhd)) {
1453 newvp->v_flag &= ~VONWORKLST;
1454 LIST_REMOVE(newvp, v_synclist);
1457 if (bp->b_vp != newvp) {
1465 * Create a vnode for a block device.
1466 * Used for mounting the root file system.
1481 error = getnewvnode(VT_NON, (struct mount *)0, spec_vnodeop_p, &nvp);
1494 * Add a vnode to the alias list hung off the dev_t.
1496 * The reason for this gunk is that multiple vnodes can reference
1497 * the same physical device, so checking vp->v_usecount to see
1498 * how many users there are is inadequate; the v_usecount for
1499 * the vnodes need to be accumulated. vcount() does that.
1502 addaliasu(struct vnode *nvp, udev_t nvp_rdev)
1506 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1507 panic("addaliasu on non-special vnode");
1508 dev = udev2dev(nvp_rdev, nvp->v_type == VBLK ? 1 : 0);
1517 addalias(struct vnode *nvp, dev_t dev)
1520 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1521 panic("addalias on non-special vnode");
1524 lwkt_gettoken(&spechash_token);
1525 SLIST_INSERT_HEAD(&dev->si_hlist, nvp, v_specnext);
1526 lwkt_reltoken(&spechash_token);
1530 * Grab a particular vnode from the free list, increment its
1531 * reference count and lock it. The vnode lock bit is set if the
1532 * vnode is being eliminated in vgone. The process is awakened
1533 * when the transition is completed, and an error returned to
1534 * indicate that the vnode is no longer usable (possibly having
1535 * been changed to a new file system type).
1546 * If the vnode is in the process of being cleaned out for
1547 * another use, we wait for the cleaning to finish and then
1548 * return failure. Cleaning is determined by checking that
1549 * the VXLOCK flag is set.
1551 if (vp->v_flag & VXLOCK) {
1552 if (vp->v_vxproc == curproc) {
1554 /* this can now occur in normal operation */
1555 log(LOG_INFO, "VXLOCK interlock avoided\n");
1558 vp->v_flag |= VXWANT;
1559 tsleep((caddr_t)vp, 0, "vget", 0);
1565 * Bump v_usecount to prevent the vnode from being cleaned. If the
1566 * vnode gets cleaned unexpectedly we could wind up calling lockmgr
1567 * on a lock embedded in an inode which is then ripped out from
1570 vp->v_usecount++; /* XXX MP */
1572 if ((flags & LK_INTERLOCK) == 0) {
1573 lwkt_gettoken(&vp->v_interlock);
1576 if (VSHOULDBUSY(vp))
1578 if (flags & LK_TYPE_MASK) {
1579 if ((error = vn_lock(vp, flags | LK_INTERLOCK, td)) != 0) {
1581 * must expand vrele here because we do not want
1582 * to call VOP_INACTIVE if the reference count
1583 * drops back to zero since it was never really
1584 * active. We must remove it from the free list
1585 * before sleeping so that multiple processes do
1586 * not try to recycle it.
1588 lwkt_gettoken(&vp->v_interlock);
1590 if (VSHOULDFREE(vp))
1594 lwkt_reltoken(&vp->v_interlock);
1598 lwkt_reltoken(&vp->v_interlock);
1603 vref(struct vnode *vp)
1605 vp->v_usecount++; /* XXX MP */
1609 * Vnode put/release.
1610 * If count drops to zero, call inactive routine and return to freelist.
1613 vrele(struct vnode *vp)
1615 struct thread *td = curthread; /* XXX */
1617 KASSERT(vp != NULL, ("vrele: null vp"));
1619 lwkt_gettoken(&vp->v_interlock);
1621 if (vp->v_usecount > 1) {
1624 lwkt_reltoken(&vp->v_interlock);
1629 if (vp->v_usecount == 1) {
1632 * We must call VOP_INACTIVE with the node locked.
1633 * If we are doing a vpu, the node is already locked,
1634 * but, in the case of vrele, we must explicitly lock
1635 * the vnode before calling VOP_INACTIVE
1638 if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK, td) == 0)
1639 VOP_INACTIVE(vp, td);
1640 if (VSHOULDFREE(vp))
1646 vprint("vrele: negative ref count", vp);
1647 lwkt_reltoken(&vp->v_interlock);
1649 panic("vrele: negative ref cnt");
1654 vput(struct vnode *vp)
1656 struct thread *td = curthread; /* XXX */
1658 KASSERT(vp != NULL, ("vput: null vp"));
1660 lwkt_gettoken(&vp->v_interlock);
1662 if (vp->v_usecount > 1) {
1664 VOP_UNLOCK(vp, LK_INTERLOCK, td);
1668 if (vp->v_usecount == 1) {
1671 * We must call VOP_INACTIVE with the node locked.
1672 * If we are doing a vpu, the node is already locked,
1673 * so we just need to release the vnode mutex.
1675 lwkt_reltoken(&vp->v_interlock);
1676 VOP_INACTIVE(vp, td);
1677 if (VSHOULDFREE(vp))
1683 vprint("vput: negative ref count", vp);
1685 panic("vput: negative ref cnt");
1690 * Somebody doesn't want the vnode recycled.
1700 if (VSHOULDBUSY(vp))
1706 * One less who cares about this vnode.
1715 if (vp->v_holdcnt <= 0)
1716 panic("vdrop: holdcnt");
1718 if (VSHOULDFREE(vp))
1724 * Remove any vnodes in the vnode table belonging to mount point mp.
1726 * If FORCECLOSE is not specified, there should not be any active ones,
1727 * return error if any are found (nb: this is a user error, not a
1728 * system error). If FORCECLOSE is specified, detach any active vnodes
1731 * If WRITECLOSE is set, only flush out regular file vnodes open for
1734 * SKIPSYSTEM causes any vnodes marked VSYSTEM to be skipped.
1736 * `rootrefs' specifies the base reference count for the root vnode
1737 * of this filesystem. The root vnode is considered busy if its
1738 * v_usecount exceeds this value. On a successful return, vflush()
1739 * will call vrele() on the root vnode exactly rootrefs times.
1740 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
1744 static int busyprt = 0; /* print out busy vnodes */
1745 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "");
1749 vflush(mp, rootrefs, flags)
1754 struct thread *td = curthread; /* XXX */
1755 struct vnode *vp, *nvp, *rootvp = NULL;
1757 int busy = 0, error;
1760 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
1761 ("vflush: bad args"));
1763 * Get the filesystem root vnode. We can vput() it
1764 * immediately, since with rootrefs > 0, it won't go away.
1766 if ((error = VFS_ROOT(mp, &rootvp)) != 0)
1770 lwkt_gettoken(&mntvnode_token);
1772 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp; vp = nvp) {
1774 * Make sure this vnode wasn't reclaimed in getnewvnode().
1775 * Start over if it has (it won't be on the list anymore).
1777 if (vp->v_mount != mp)
1779 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
1781 lwkt_gettoken(&vp->v_interlock);
1783 * Skip over a vnodes marked VSYSTEM.
1785 if ((flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM)) {
1786 lwkt_reltoken(&vp->v_interlock);
1790 * If WRITECLOSE is set, flush out unlinked but still open
1791 * files (even if open only for reading) and regular file
1792 * vnodes open for writing.
1794 if ((flags & WRITECLOSE) &&
1795 (vp->v_type == VNON ||
1796 (VOP_GETATTR(vp, &vattr, td) == 0 &&
1797 vattr.va_nlink > 0)) &&
1798 (vp->v_writecount == 0 || vp->v_type != VREG)) {
1799 lwkt_reltoken(&vp->v_interlock);
1804 * With v_usecount == 0, all we need to do is clear out the
1805 * vnode data structures and we are done.
1807 if (vp->v_usecount == 0) {
1808 lwkt_reltoken(&mntvnode_token);
1810 lwkt_gettoken(&mntvnode_token);
1815 * If FORCECLOSE is set, forcibly close the vnode. For block
1816 * or character devices, revert to an anonymous device. For
1817 * all other files, just kill them.
1819 if (flags & FORCECLOSE) {
1820 lwkt_reltoken(&mntvnode_token);
1821 if (vp->v_type != VBLK && vp->v_type != VCHR) {
1825 vp->v_op = spec_vnodeop_p;
1826 insmntque(vp, (struct mount *) 0);
1828 lwkt_gettoken(&mntvnode_token);
1833 vprint("vflush: busy vnode", vp);
1835 lwkt_reltoken(&vp->v_interlock);
1838 lwkt_reltoken(&mntvnode_token);
1839 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
1841 * If just the root vnode is busy, and if its refcount
1842 * is equal to `rootrefs', then go ahead and kill it.
1844 lwkt_gettoken(&rootvp->v_interlock);
1845 KASSERT(busy > 0, ("vflush: not busy"));
1846 KASSERT(rootvp->v_usecount >= rootrefs, ("vflush: rootrefs"));
1847 if (busy == 1 && rootvp->v_usecount == rootrefs) {
1851 lwkt_reltoken(&rootvp->v_interlock);
1855 for (; rootrefs > 0; rootrefs--)
1861 * We do not want to recycle the vnode too quickly.
1863 * XXX we can't move vp's around the nvnodelist without really screwing
1864 * up the efficiency of filesystem SYNC and friends. This code is
1865 * disabled until we fix the syncing code's scanning algorithm.
1868 vlruvp(struct vnode *vp)
1873 if ((mp = vp->v_mount) != NULL) {
1874 lwkt_gettoken(&mntvnode_token);
1875 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1876 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1877 lwkt_reltoken(&mntvnode_token);
1883 * Disassociate the underlying file system from a vnode.
1886 vclean(struct vnode *vp, int flags, struct thread *td)
1891 * Check to see if the vnode is in use. If so we have to reference it
1892 * before we clean it out so that its count cannot fall to zero and
1893 * generate a race against ourselves to recycle it.
1895 if ((active = vp->v_usecount))
1899 * Prevent the vnode from being recycled or brought into use while we
1902 if (vp->v_flag & VXLOCK)
1903 panic("vclean: deadlock");
1904 vp->v_flag |= VXLOCK;
1905 vp->v_vxproc = curproc;
1907 * Even if the count is zero, the VOP_INACTIVE routine may still
1908 * have the object locked while it cleans it out. The VOP_LOCK
1909 * ensures that the VOP_INACTIVE routine is done with its work.
1910 * For active vnodes, it ensures that no other activity can
1911 * occur while the underlying object is being cleaned out.
1913 VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK, td);
1916 * Clean out any buffers associated with the vnode.
1918 vinvalbuf(vp, V_SAVE, td, 0, 0);
1920 VOP_DESTROYVOBJECT(vp);
1923 * If purging an active vnode, it must be closed and
1924 * deactivated before being reclaimed. Note that the
1925 * VOP_INACTIVE will unlock the vnode.
1928 if (flags & DOCLOSE)
1929 VOP_CLOSE(vp, FNONBLOCK, td);
1930 VOP_INACTIVE(vp, td);
1933 * Any other processes trying to obtain this lock must first
1934 * wait for VXLOCK to clear, then call the new lock operation.
1936 VOP_UNLOCK(vp, 0, td);
1939 * Reclaim the vnode.
1941 if (VOP_RECLAIM(vp, td))
1942 panic("vclean: cannot reclaim");
1946 * Inline copy of vrele() since VOP_INACTIVE
1947 * has already been called.
1949 lwkt_gettoken(&vp->v_interlock);
1950 if (--vp->v_usecount <= 0) {
1952 if (vp->v_usecount < 0 || vp->v_writecount != 0) {
1953 vprint("vclean: bad ref count", vp);
1954 panic("vclean: ref cnt");
1959 lwkt_reltoken(&vp->v_interlock);
1963 vp->v_vnlock = NULL;
1965 if (VSHOULDFREE(vp))
1969 * Done with purge, notify sleepers of the grim news.
1971 vp->v_op = dead_vnodeop_p;
1974 vp->v_flag &= ~VXLOCK;
1975 vp->v_vxproc = NULL;
1976 if (vp->v_flag & VXWANT) {
1977 vp->v_flag &= ~VXWANT;
1978 wakeup((caddr_t) vp);
1983 * Eliminate all activity associated with the requested vnode
1984 * and with all vnodes aliased to the requested vnode.
1988 struct vop_revoke_args /* {
1993 struct vnode *vp, *vq;
1996 KASSERT((ap->a_flags & REVOKEALL) != 0, ("vop_revoke"));
2000 * If a vgone (or vclean) is already in progress,
2001 * wait until it is done and return.
2003 if (vp->v_flag & VXLOCK) {
2004 vp->v_flag |= VXWANT;
2005 lwkt_reltoken(&vp->v_interlock);
2006 tsleep((caddr_t)vp, 0, "vop_revokeall", 0);
2011 lwkt_gettoken(&spechash_token);
2012 vq = SLIST_FIRST(&dev->si_hlist);
2013 lwkt_reltoken(&spechash_token);
2022 * Recycle an unused vnode to the front of the free list.
2023 * Release the passed interlock if the vnode will be recycled.
2026 vrecycle(struct vnode *vp, struct lwkt_token *inter_lkp, struct thread *td)
2028 lwkt_gettoken(&vp->v_interlock);
2029 if (vp->v_usecount == 0) {
2031 lwkt_reltoken(inter_lkp);
2036 lwkt_reltoken(&vp->v_interlock);
2041 * Eliminate all activity associated with a vnode
2042 * in preparation for reuse.
2045 vgone(struct vnode *vp)
2047 struct thread *td = curthread; /* XXX */
2049 lwkt_gettoken(&vp->v_interlock);
2054 * vgone, with the vp interlock held.
2057 vgonel(struct vnode *vp, struct thread *td)
2062 * If a vgone (or vclean) is already in progress,
2063 * wait until it is done and return.
2065 if (vp->v_flag & VXLOCK) {
2066 vp->v_flag |= VXWANT;
2067 lwkt_reltoken(&vp->v_interlock);
2068 tsleep((caddr_t)vp, 0, "vgone", 0);
2073 * Clean out the filesystem specific data.
2075 vclean(vp, DOCLOSE, td);
2076 lwkt_gettoken(&vp->v_interlock);
2079 * Delete from old mount point vnode list, if on one.
2081 if (vp->v_mount != NULL)
2082 insmntque(vp, (struct mount *)0);
2084 * If special device, remove it from special device alias list
2087 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
2088 lwkt_gettoken(&spechash_token);
2089 SLIST_REMOVE(&vp->v_hashchain, vp, vnode, v_specnext);
2090 freedev(vp->v_rdev);
2091 lwkt_reltoken(&spechash_token);
2096 * If it is on the freelist and not already at the head,
2097 * move it to the head of the list. The test of the
2098 * VDOOMED flag and the reference count of zero is because
2099 * it will be removed from the free list by getnewvnode,
2100 * but will not have its reference count incremented until
2101 * after calling vgone. If the reference count were
2102 * incremented first, vgone would (incorrectly) try to
2103 * close the previous instance of the underlying object.
2105 if (vp->v_usecount == 0 && !(vp->v_flag & VDOOMED)) {
2107 lwkt_gettoken(&vnode_free_list_token);
2108 if (vp->v_flag & VFREE)
2109 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
2112 vp->v_flag |= VFREE;
2113 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2114 lwkt_reltoken(&vnode_free_list_token);
2119 lwkt_reltoken(&vp->v_interlock);
2123 * Lookup a vnode by device number.
2126 vfinddev(dev, type, vpp)
2133 lwkt_gettoken(&spechash_token);
2134 SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) {
2135 if (type == vp->v_type) {
2137 lwkt_reltoken(&spechash_token);
2141 lwkt_reltoken(&spechash_token);
2146 * Calculate the total number of references to a special device.
2156 lwkt_gettoken(&spechash_token);
2157 SLIST_FOREACH(vq, &vp->v_hashchain, v_specnext)
2158 count += vq->v_usecount;
2159 lwkt_reltoken(&spechash_token);
2164 * Same as above, but using the dev_t as argument
2173 vp = SLIST_FIRST(&dev->si_hlist);
2180 * Print out a description of a vnode.
2182 static char *typename[] =
2183 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
2193 printf("%s: %p: ", label, (void *)vp);
2195 printf("%p: ", (void *)vp);
2196 printf("type %s, usecount %d, writecount %d, refcount %d,",
2197 typename[vp->v_type], vp->v_usecount, vp->v_writecount,
2200 if (vp->v_flag & VROOT)
2201 strcat(buf, "|VROOT");
2202 if (vp->v_flag & VTEXT)
2203 strcat(buf, "|VTEXT");
2204 if (vp->v_flag & VSYSTEM)
2205 strcat(buf, "|VSYSTEM");
2206 if (vp->v_flag & VXLOCK)
2207 strcat(buf, "|VXLOCK");
2208 if (vp->v_flag & VXWANT)
2209 strcat(buf, "|VXWANT");
2210 if (vp->v_flag & VBWAIT)
2211 strcat(buf, "|VBWAIT");
2212 if (vp->v_flag & VDOOMED)
2213 strcat(buf, "|VDOOMED");
2214 if (vp->v_flag & VFREE)
2215 strcat(buf, "|VFREE");
2216 if (vp->v_flag & VOBJBUF)
2217 strcat(buf, "|VOBJBUF");
2219 printf(" flags (%s)", &buf[1]);
2220 if (vp->v_data == NULL) {
2229 #include <ddb/ddb.h>
2231 * List all of the locked vnodes in the system.
2232 * Called when debugging the kernel.
2234 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
2236 struct thread *td = curthread; /* XXX */
2237 struct mount *mp, *nmp;
2240 printf("Locked vnodes\n");
2241 lwkt_gettoken(&mountlist_token);
2242 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2243 if (vfs_busy(mp, LK_NOWAIT, &mountlist_token, td)) {
2244 nmp = TAILQ_NEXT(mp, mnt_list);
2247 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
2248 if (VOP_ISLOCKED(vp, NULL))
2249 vprint((char *)0, vp);
2251 lwkt_gettoken(&mountlist_token);
2252 nmp = TAILQ_NEXT(mp, mnt_list);
2255 lwkt_reltoken(&mountlist_token);
2260 * Top level filesystem related information gathering.
2262 static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS);
2265 vfs_sysctl(SYSCTL_HANDLER_ARGS)
2267 int *name = (int *)arg1 - 1; /* XXX */
2268 u_int namelen = arg2 + 1; /* XXX */
2269 struct vfsconf *vfsp;
2271 #if 1 || defined(COMPAT_PRELITE2)
2272 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
2274 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
2278 /* all sysctl names at this level are at least name and field */
2280 return (ENOTDIR); /* overloaded */
2281 if (name[0] != VFS_GENERIC) {
2282 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2283 if (vfsp->vfc_typenum == name[0])
2286 return (EOPNOTSUPP);
2287 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
2288 oldp, oldlenp, newp, newlen, p));
2292 case VFS_MAXTYPENUM:
2295 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
2298 return (ENOTDIR); /* overloaded */
2299 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2300 if (vfsp->vfc_typenum == name[2])
2303 return (EOPNOTSUPP);
2304 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
2306 return (EOPNOTSUPP);
2309 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
2310 "Generic filesystem");
2312 #if 1 || defined(COMPAT_PRELITE2)
2315 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
2318 struct vfsconf *vfsp;
2319 struct ovfsconf ovfs;
2321 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
2322 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
2323 strcpy(ovfs.vfc_name, vfsp->vfc_name);
2324 ovfs.vfc_index = vfsp->vfc_typenum;
2325 ovfs.vfc_refcount = vfsp->vfc_refcount;
2326 ovfs.vfc_flags = vfsp->vfc_flags;
2327 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
2334 #endif /* 1 || COMPAT_PRELITE2 */
2337 #define KINFO_VNODESLOP 10
2339 * Dump vnode list (via sysctl).
2340 * Copyout address of vnode followed by vnode.
2344 sysctl_vnode(SYSCTL_HANDLER_ARGS)
2346 struct proc *p = curproc; /* XXX */
2347 struct mount *mp, *nmp;
2348 struct vnode *nvp, *vp;
2351 #define VPTRSZ sizeof (struct vnode *)
2352 #define VNODESZ sizeof (struct vnode)
2355 if (!req->oldptr) /* Make an estimate */
2356 return (SYSCTL_OUT(req, 0,
2357 (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ)));
2359 lwkt_gettoken(&mountlist_token);
2360 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2361 if (vfs_busy(mp, LK_NOWAIT, &mountlist_token, p)) {
2362 nmp = TAILQ_NEXT(mp, mnt_list);
2366 lwkt_gettoken(&mntvnode_token);
2367 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
2371 * Check that the vp is still associated with
2372 * this filesystem. RACE: could have been
2373 * recycled onto the same filesystem.
2375 if (vp->v_mount != mp) {
2376 lwkt_reltoken(&mntvnode_token);
2379 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
2380 lwkt_reltoken(&mntvnode_token);
2381 if ((error = SYSCTL_OUT(req, &vp, VPTRSZ)) ||
2382 (error = SYSCTL_OUT(req, vp, VNODESZ)))
2384 lwkt_gettoken(&mntvnode_token);
2386 lwkt_reltoken(&mntvnode_token);
2387 lwkt_gettoken(&mountlist_token);
2388 nmp = TAILQ_NEXT(mp, mnt_list);
2391 lwkt_reltoken(&mountlist_token);
2399 * Exporting the vnode list on large systems causes them to crash.
2400 * Exporting the vnode list on medium systems causes sysctl to coredump.
2403 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD,
2404 0, 0, sysctl_vnode, "S,vnode", "");
2408 * Check to see if a filesystem is mounted on a block device.
2415 if (vp->v_specmountpoint != NULL)
2421 * Unmount all filesystems. The list is traversed in reverse order
2422 * of mounting to avoid dependencies.
2428 struct thread *td = curthread;
2431 if (td->td_proc == NULL)
2432 td = initproc->p_thread; /* XXX XXX use proc0 instead? */
2435 * Since this only runs when rebooting, it is not interlocked.
2437 while(!TAILQ_EMPTY(&mountlist)) {
2438 mp = TAILQ_LAST(&mountlist, mntlist);
2439 error = dounmount(mp, MNT_FORCE, td);
2441 TAILQ_REMOVE(&mountlist, mp, mnt_list);
2442 printf("unmount of %s failed (",
2443 mp->mnt_stat.f_mntonname);
2447 printf("%d)\n", error);
2449 /* The unmount has removed mp from the mountlist */
2455 * Build hash lists of net addresses and hang them off the mount point.
2456 * Called by ufs_mount() to set up the lists of export addresses.
2459 vfs_hang_addrlist(mp, nep, argp)
2461 struct netexport *nep;
2462 struct export_args *argp;
2465 struct radix_node_head *rnh;
2467 struct radix_node *rn;
2468 struct sockaddr *saddr, *smask = 0;
2472 if (argp->ex_addrlen == 0) {
2473 if (mp->mnt_flag & MNT_DEFEXPORTED)
2475 np = &nep->ne_defexported;
2476 np->netc_exflags = argp->ex_flags;
2477 np->netc_anon = argp->ex_anon;
2478 np->netc_anon.cr_ref = 1;
2479 mp->mnt_flag |= MNT_DEFEXPORTED;
2483 if (argp->ex_addrlen < 0 || argp->ex_addrlen > MLEN)
2485 if (argp->ex_masklen < 0 || argp->ex_masklen > MLEN)
2488 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
2489 np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK);
2490 bzero((caddr_t) np, i);
2491 saddr = (struct sockaddr *) (np + 1);
2492 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
2494 if (saddr->sa_len > argp->ex_addrlen)
2495 saddr->sa_len = argp->ex_addrlen;
2496 if (argp->ex_masklen) {
2497 smask = (struct sockaddr *) ((caddr_t) saddr + argp->ex_addrlen);
2498 error = copyin(argp->ex_mask, (caddr_t) smask, argp->ex_masklen);
2501 if (smask->sa_len > argp->ex_masklen)
2502 smask->sa_len = argp->ex_masklen;
2504 i = saddr->sa_family;
2505 if ((rnh = nep->ne_rtable[i]) == 0) {
2507 * Seems silly to initialize every AF when most are not used,
2508 * do so on demand here
2510 for (dom = domains; dom; dom = dom->dom_next)
2511 if (dom->dom_family == i && dom->dom_rtattach) {
2512 dom->dom_rtattach((void **) &nep->ne_rtable[i],
2516 if ((rnh = nep->ne_rtable[i]) == 0) {
2521 rn = (*rnh->rnh_addaddr) ((caddr_t) saddr, (caddr_t) smask, rnh,
2523 if (rn == 0 || np != (struct netcred *) rn) { /* already exists */
2527 np->netc_exflags = argp->ex_flags;
2528 np->netc_anon = argp->ex_anon;
2529 np->netc_anon.cr_ref = 1;
2532 free(np, M_NETADDR);
2538 vfs_free_netcred(rn, w)
2539 struct radix_node *rn;
2542 struct radix_node_head *rnh = (struct radix_node_head *) w;
2544 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
2545 free((caddr_t) rn, M_NETADDR);
2550 * Free the net address hash lists that are hanging off the mount points.
2553 vfs_free_addrlist(nep)
2554 struct netexport *nep;
2557 struct radix_node_head *rnh;
2559 for (i = 0; i <= AF_MAX; i++)
2560 if ((rnh = nep->ne_rtable[i])) {
2561 (*rnh->rnh_walktree) (rnh, vfs_free_netcred,
2563 free((caddr_t) rnh, M_RTABLE);
2564 nep->ne_rtable[i] = 0;
2569 vfs_export(mp, nep, argp)
2571 struct netexport *nep;
2572 struct export_args *argp;
2576 if (argp->ex_flags & MNT_DELEXPORT) {
2577 if (mp->mnt_flag & MNT_EXPUBLIC) {
2578 vfs_setpublicfs(NULL, NULL, NULL);
2579 mp->mnt_flag &= ~MNT_EXPUBLIC;
2581 vfs_free_addrlist(nep);
2582 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
2584 if (argp->ex_flags & MNT_EXPORTED) {
2585 if (argp->ex_flags & MNT_EXPUBLIC) {
2586 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
2588 mp->mnt_flag |= MNT_EXPUBLIC;
2590 if ((error = vfs_hang_addrlist(mp, nep, argp)))
2592 mp->mnt_flag |= MNT_EXPORTED;
2599 * Set the publicly exported filesystem (WebNFS). Currently, only
2600 * one public filesystem is possible in the spec (RFC 2054 and 2055)
2603 vfs_setpublicfs(mp, nep, argp)
2605 struct netexport *nep;
2606 struct export_args *argp;
2613 * mp == NULL -> invalidate the current info, the FS is
2614 * no longer exported. May be called from either vfs_export
2615 * or unmount, so check if it hasn't already been done.
2618 if (nfs_pub.np_valid) {
2619 nfs_pub.np_valid = 0;
2620 if (nfs_pub.np_index != NULL) {
2621 FREE(nfs_pub.np_index, M_TEMP);
2622 nfs_pub.np_index = NULL;
2629 * Only one allowed at a time.
2631 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
2635 * Get real filehandle for root of exported FS.
2637 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
2638 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
2640 if ((error = VFS_ROOT(mp, &rvp)))
2643 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
2649 * If an indexfile was specified, pull it in.
2651 if (argp->ex_indexfile != NULL) {
2652 MALLOC(nfs_pub.np_index, char *, MAXNAMLEN + 1, M_TEMP,
2654 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
2655 MAXNAMLEN, (size_t *)0);
2658 * Check for illegal filenames.
2660 for (cp = nfs_pub.np_index; *cp; cp++) {
2668 FREE(nfs_pub.np_index, M_TEMP);
2673 nfs_pub.np_mount = mp;
2674 nfs_pub.np_valid = 1;
2679 vfs_export_lookup(mp, nep, nam)
2681 struct netexport *nep;
2682 struct sockaddr *nam;
2685 struct radix_node_head *rnh;
2686 struct sockaddr *saddr;
2689 if (mp->mnt_flag & MNT_EXPORTED) {
2691 * Lookup in the export list first.
2695 rnh = nep->ne_rtable[saddr->sa_family];
2697 np = (struct netcred *)
2698 (*rnh->rnh_matchaddr)((caddr_t)saddr,
2700 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
2705 * If no address match, use the default if it exists.
2707 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
2708 np = &nep->ne_defexported;
2714 * perform msync on all vnodes under a mount point
2715 * the mount point must be locked.
2718 vfs_msync(struct mount *mp, int flags)
2720 struct thread *td = curthread; /* XXX */
2721 struct vnode *vp, *nvp;
2722 struct vm_object *obj;
2726 lwkt_gettoken(&mntvnode_token);
2728 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nvp) {
2729 if (vp->v_mount != mp) {
2734 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
2736 if (vp->v_flag & VXLOCK) /* XXX: what if MNT_WAIT? */
2740 * There could be hundreds of thousands of vnodes, we cannot
2741 * afford to do anything heavy-weight until we have a fairly
2742 * good indication that there is something to do.
2744 if ((vp->v_flag & VOBJDIRTY) &&
2745 (flags == MNT_WAIT || VOP_ISLOCKED(vp, NULL) == 0)) {
2746 lwkt_reltoken(&mntvnode_token);
2748 LK_EXCLUSIVE | LK_RETRY | LK_NOOBJ, td)) {
2749 if (VOP_GETVOBJECT(vp, &obj) == 0) {
2750 vm_object_page_clean(obj, 0, 0, flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
2754 lwkt_gettoken(&mntvnode_token);
2755 if (TAILQ_NEXT(vp, v_nmntvnodes) != nvp) {
2762 lwkt_reltoken(&mntvnode_token);
2766 * Create the VM object needed for VMIO and mmap support. This
2767 * is done for all VREG files in the system. Some filesystems might
2768 * afford the additional metadata buffering capability of the
2769 * VMIO code by making the device node be VMIO mode also.
2771 * vp must be locked when vfs_object_create is called.
2774 vfs_object_create(struct vnode *vp, struct thread *td)
2776 return (VOP_CREATEVOBJECT(vp, td));
2786 lwkt_gettoken(&vnode_free_list_token);
2787 KASSERT((vp->v_flag & VFREE) == 0, ("vnode already free"));
2788 if (vp->v_flag & VAGE) {
2789 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2791 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
2794 lwkt_reltoken(&vnode_free_list_token);
2795 vp->v_flag &= ~VAGE;
2796 vp->v_flag |= VFREE;
2807 lwkt_gettoken(&vnode_free_list_token);
2808 KASSERT((vp->v_flag & VFREE) != 0, ("vnode not free"));
2809 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
2811 lwkt_reltoken(&vnode_free_list_token);
2812 vp->v_flag &= ~(VFREE|VAGE);
2817 * Record a process's interest in events which might happen to
2818 * a vnode. Because poll uses the historic select-style interface
2819 * internally, this routine serves as both the ``check for any
2820 * pending events'' and the ``record my interest in future events''
2821 * functions. (These are done together, while the lock is held,
2822 * to avoid race conditions.)
2825 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
2827 lwkt_gettoken(&vp->v_pollinfo.vpi_token);
2828 if (vp->v_pollinfo.vpi_revents & events) {
2830 * This leaves events we are not interested
2831 * in available for the other process which
2832 * which presumably had requested them
2833 * (otherwise they would never have been
2836 events &= vp->v_pollinfo.vpi_revents;
2837 vp->v_pollinfo.vpi_revents &= ~events;
2839 lwkt_reltoken(&vp->v_pollinfo.vpi_token);
2842 vp->v_pollinfo.vpi_events |= events;
2843 selrecord(td, &vp->v_pollinfo.vpi_selinfo);
2844 lwkt_reltoken(&vp->v_pollinfo.vpi_token);
2849 * Note the occurrence of an event. If the VN_POLLEVENT macro is used,
2850 * it is possible for us to miss an event due to race conditions, but
2851 * that condition is expected to be rare, so for the moment it is the
2852 * preferred interface.
2855 vn_pollevent(vp, events)
2859 lwkt_gettoken(&vp->v_pollinfo.vpi_token);
2860 if (vp->v_pollinfo.vpi_events & events) {
2862 * We clear vpi_events so that we don't
2863 * call selwakeup() twice if two events are
2864 * posted before the polling process(es) is
2865 * awakened. This also ensures that we take at
2866 * most one selwakeup() if the polling process
2867 * is no longer interested. However, it does
2868 * mean that only one event can be noticed at
2869 * a time. (Perhaps we should only clear those
2870 * event bits which we note?) XXX
2872 vp->v_pollinfo.vpi_events = 0; /* &= ~events ??? */
2873 vp->v_pollinfo.vpi_revents |= events;
2874 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2876 lwkt_reltoken(&vp->v_pollinfo.vpi_token);
2880 * Wake up anyone polling on vp because it is being revoked.
2881 * This depends on dead_poll() returning POLLHUP for correct
2888 lwkt_gettoken(&vp->v_pollinfo.vpi_token);
2889 if (vp->v_pollinfo.vpi_events) {
2890 vp->v_pollinfo.vpi_events = 0;
2891 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2893 lwkt_reltoken(&vp->v_pollinfo.vpi_token);
2899 * Routine to create and manage a filesystem syncer vnode.
2901 #define sync_close ((int (*) (struct vop_close_args *))nullop)
2902 static int sync_fsync (struct vop_fsync_args *);
2903 static int sync_inactive (struct vop_inactive_args *);
2904 static int sync_reclaim (struct vop_reclaim_args *);
2905 #define sync_lock ((int (*) (struct vop_lock_args *))vop_nolock)
2906 #define sync_unlock ((int (*) (struct vop_unlock_args *))vop_nounlock)
2907 static int sync_print (struct vop_print_args *);
2908 #define sync_islocked ((int(*) (struct vop_islocked_args *))vop_noislocked)
2910 static vop_t **sync_vnodeop_p;
2911 static struct vnodeopv_entry_desc sync_vnodeop_entries[] = {
2912 { &vop_default_desc, (vop_t *) vop_eopnotsupp },
2913 { &vop_close_desc, (vop_t *) sync_close }, /* close */
2914 { &vop_fsync_desc, (vop_t *) sync_fsync }, /* fsync */
2915 { &vop_inactive_desc, (vop_t *) sync_inactive }, /* inactive */
2916 { &vop_reclaim_desc, (vop_t *) sync_reclaim }, /* reclaim */
2917 { &vop_lock_desc, (vop_t *) sync_lock }, /* lock */
2918 { &vop_unlock_desc, (vop_t *) sync_unlock }, /* unlock */
2919 { &vop_print_desc, (vop_t *) sync_print }, /* print */
2920 { &vop_islocked_desc, (vop_t *) sync_islocked }, /* islocked */
2923 static struct vnodeopv_desc sync_vnodeop_opv_desc =
2924 { &sync_vnodeop_p, sync_vnodeop_entries };
2926 VNODEOP_SET(sync_vnodeop_opv_desc);
2929 * Create a new filesystem syncer vnode for the specified mount point.
2932 vfs_allocate_syncvnode(mp)
2936 static long start, incr, next;
2939 /* Allocate a new vnode */
2940 if ((error = getnewvnode(VT_VFS, mp, sync_vnodeop_p, &vp)) != 0) {
2941 mp->mnt_syncer = NULL;
2946 * Place the vnode onto the syncer worklist. We attempt to
2947 * scatter them about on the list so that they will go off
2948 * at evenly distributed times even if all the filesystems
2949 * are mounted at once.
2952 if (next == 0 || next > syncer_maxdelay) {
2956 start = syncer_maxdelay / 2;
2957 incr = syncer_maxdelay;
2961 vn_syncer_add_to_worklist(vp, syncdelay > 0 ? next % syncdelay : 0);
2962 mp->mnt_syncer = vp;
2967 * Do a lazy sync of the filesystem.
2971 struct vop_fsync_args /* {
2973 struct ucred *a_cred;
2975 struct thread *a_td;
2978 struct vnode *syncvp = ap->a_vp;
2979 struct mount *mp = syncvp->v_mount;
2980 struct thread *td = ap->a_td;
2984 * We only need to do something if this is a lazy evaluation.
2986 if (ap->a_waitfor != MNT_LAZY)
2990 * Move ourselves to the back of the sync list.
2992 vn_syncer_add_to_worklist(syncvp, syncdelay);
2995 * Walk the list of vnodes pushing all that are dirty and
2996 * not already on the sync list.
2998 lwkt_gettoken(&mountlist_token);
2999 if (vfs_busy(mp, LK_EXCLUSIVE | LK_NOWAIT, &mountlist_token, td) != 0) {
3000 lwkt_reltoken(&mountlist_token);
3003 asyncflag = mp->mnt_flag & MNT_ASYNC;
3004 mp->mnt_flag &= ~MNT_ASYNC;
3005 vfs_msync(mp, MNT_NOWAIT);
3006 VFS_SYNC(mp, MNT_LAZY, td);
3008 mp->mnt_flag |= MNT_ASYNC;
3014 * The syncer vnode is no referenced.
3018 struct vop_inactive_args /* {
3029 * The syncer vnode is no longer needed and is being decommissioned.
3031 * Modifications to the worklist must be protected at splbio().
3035 struct vop_reclaim_args /* {
3039 struct vnode *vp = ap->a_vp;
3043 vp->v_mount->mnt_syncer = NULL;
3044 if (vp->v_flag & VONWORKLST) {
3045 LIST_REMOVE(vp, v_synclist);
3046 vp->v_flag &= ~VONWORKLST;
3054 * Print out a syncer vnode.
3058 struct vop_print_args /* {
3062 struct vnode *vp = ap->a_vp;
3064 printf("syncer vnode");
3065 if (vp->v_vnlock != NULL)
3066 lockmgr_printinfo(vp->v_vnlock);
3072 * extract the dev_t from a VBLK or VCHR
3078 if (vp->v_type != VBLK && vp->v_type != VCHR)
3080 return (vp->v_rdev);
3084 * Check if vnode represents a disk device
3091 if (vp->v_type != VBLK && vp->v_type != VCHR) {
3096 if (vp->v_rdev == NULL) {
3101 if (!dev_dport(vp->v_rdev)) {
3106 if (!(dev_dflags(vp->v_rdev) & D_DISK)) {
3118 struct nameidata *ndp;
3121 if (!(flags & NDF_NO_FREE_PNBUF) &&
3122 (ndp->ni_cnd.cn_flags & CNP_HASBUF)) {
3123 zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
3124 ndp->ni_cnd.cn_flags &= ~CNP_HASBUF;
3126 if (!(flags & NDF_NO_DNCP_RELE) &&
3127 (ndp->ni_cnd.cn_flags & CNP_WANTDNCP) &&
3129 cache_drop(ndp->ni_dncp);
3130 ndp->ni_dncp = NULL;
3132 if (!(flags & NDF_NO_NCP_RELE) &&
3133 (ndp->ni_cnd.cn_flags & CNP_WANTNCP) &&
3135 cache_drop(ndp->ni_ncp);
3138 if (!(flags & NDF_NO_DVP_UNLOCK) &&
3139 (ndp->ni_cnd.cn_flags & CNP_LOCKPARENT) &&
3140 ndp->ni_dvp != ndp->ni_vp) {
3141 VOP_UNLOCK(ndp->ni_dvp, 0, ndp->ni_cnd.cn_td);
3143 if (!(flags & NDF_NO_DVP_RELE) &&
3144 (ndp->ni_cnd.cn_flags & (CNP_LOCKPARENT|CNP_WANTPARENT))) {
3148 if (!(flags & NDF_NO_VP_UNLOCK) &&
3149 (ndp->ni_cnd.cn_flags & CNP_LOCKLEAF) && ndp->ni_vp) {
3150 VOP_UNLOCK(ndp->ni_vp, 0, ndp->ni_cnd.cn_td);
3152 if (!(flags & NDF_NO_VP_RELE) &&
3157 if (!(flags & NDF_NO_STARTDIR_RELE) &&
3158 (ndp->ni_cnd.cn_flags & CNP_SAVESTART)) {
3159 vrele(ndp->ni_startdir);
3160 ndp->ni_startdir = NULL;