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.38 2004/08/28 19:02:05 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, lwkt_tokref_t vlock,
91 int flags, struct thread *td);
93 static unsigned long numvnodes;
94 SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, "");
96 enum vtype iftovt_tab[16] = {
97 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
98 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
100 int vttoif_tab[9] = {
101 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
102 S_IFSOCK, S_IFIFO, S_IFMT,
105 static TAILQ_HEAD(freelst, vnode) vnode_free_list; /* vnode free list */
107 static u_long wantfreevnodes = 25;
108 SYSCTL_INT(_debug, OID_AUTO, wantfreevnodes, CTLFLAG_RW,
109 &wantfreevnodes, 0, "");
110 static u_long freevnodes = 0;
111 SYSCTL_INT(_debug, OID_AUTO, freevnodes, CTLFLAG_RD,
114 static int reassignbufcalls;
115 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW,
116 &reassignbufcalls, 0, "");
117 static int reassignbufloops;
118 SYSCTL_INT(_vfs, OID_AUTO, reassignbufloops, CTLFLAG_RW,
119 &reassignbufloops, 0, "");
120 static int reassignbufsortgood;
121 SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortgood, CTLFLAG_RW,
122 &reassignbufsortgood, 0, "");
123 static int reassignbufsortbad;
124 SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortbad, CTLFLAG_RW,
125 &reassignbufsortbad, 0, "");
126 static int reassignbufmethod = 1;
127 SYSCTL_INT(_vfs, OID_AUTO, reassignbufmethod, CTLFLAG_RW,
128 &reassignbufmethod, 0, "");
130 #ifdef ENABLE_VFS_IOOPT
132 SYSCTL_INT(_vfs, OID_AUTO, ioopt, CTLFLAG_RW, &vfs_ioopt, 0, "");
135 struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist); /* mounted fs */
136 struct lwkt_token mountlist_token;
137 struct lwkt_token mntvnode_token;
138 int nfs_mount_type = -1;
139 static struct lwkt_token mntid_token;
140 static struct lwkt_token vnode_free_list_token;
141 static struct lwkt_token spechash_token;
142 struct nfs_public nfs_pub; /* publicly exported FS */
143 static vm_zone_t vnode_zone;
146 * The workitem queue.
148 #define SYNCER_MAXDELAY 32
149 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
150 time_t syncdelay = 30; /* max time to delay syncing data */
151 SYSCTL_INT(_kern, OID_AUTO, syncdelay, CTLFLAG_RW,
152 &syncdelay, 0, "VFS data synchronization delay");
153 time_t filedelay = 30; /* time to delay syncing files */
154 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW,
155 &filedelay, 0, "File synchronization delay");
156 time_t dirdelay = 29; /* time to delay syncing directories */
157 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW,
158 &dirdelay, 0, "Directory synchronization delay");
159 time_t metadelay = 28; /* time to delay syncing metadata */
160 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW,
161 &metadelay, 0, "VFS metadata synchronization delay");
162 static int rushjob; /* number of slots to run ASAP */
163 static int stat_rush_requests; /* number of times I/O speeded up */
164 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW,
165 &stat_rush_requests, 0, "");
167 static int syncer_delayno = 0;
168 static long syncer_mask;
169 LIST_HEAD(synclist, vnode);
170 static struct synclist *syncer_workitem_pending;
173 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
174 &desiredvnodes, 0, "Maximum number of vnodes");
175 static int minvnodes;
176 SYSCTL_INT(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
177 &minvnodes, 0, "Minimum number of vnodes");
178 static int vnlru_nowhere = 0;
179 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW,
181 "Number of times the vnlru process ran without success");
183 static void vfs_free_addrlist (struct netexport *nep);
184 static int vfs_free_netcred (struct radix_node *rn, void *w);
185 static int vfs_hang_addrlist (struct mount *mp, struct netexport *nep,
186 struct export_args *argp);
188 #define VSHOULDFREE(vp) \
189 (!((vp)->v_flag & (VFREE|VDOOMED)) && \
190 !(vp)->v_holdcnt && !(vp)->v_usecount && \
191 (!(vp)->v_object || \
192 !((vp)->v_object->ref_count || (vp)->v_object->resident_page_count)))
194 #define VMIGHTFREE(vp) \
195 (((vp)->v_flag & (VFREE|VDOOMED|VXLOCK)) == 0 && \
196 cache_leaf_test(vp) == 0 && (vp)->v_usecount == 0)
198 #define VSHOULDBUSY(vp) \
199 (((vp)->v_flag & VFREE) && \
200 ((vp)->v_holdcnt || (vp)->v_usecount))
202 static void vbusy(struct vnode *vp);
203 static void vfree(struct vnode *vp);
204 static void vmaybefree(struct vnode *vp);
206 extern int dev_ref_debug;
207 extern struct vnodeopv_entry_desc spec_vnodeop_entries[];
210 * NOTE: the vnode interlock must be held on call.
213 vmaybefree(struct vnode *vp)
220 * Initialize the vnode management data structures.
226 * Desired vnodes is a result of the physical page count
227 * and the size of kernel's heap. It scales in proportion
228 * to the amount of available physical memory. This can
229 * cause trouble on 64-bit and large memory platforms.
231 /* desiredvnodes = maxproc + vmstats.v_page_count / 4; */
233 min(maxproc + vmstats.v_page_count /4,
234 2 * (VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) /
235 (5 * (sizeof(struct vm_object) + sizeof(struct vnode))));
237 minvnodes = desiredvnodes / 4;
238 lwkt_token_init(&mountlist_token);
239 lwkt_token_init(&mntvnode_token);
240 lwkt_token_init(&mntid_token);
241 lwkt_token_init(&spechash_token);
242 TAILQ_INIT(&vnode_free_list);
243 lwkt_token_init(&vnode_free_list_token);
244 vnode_zone = zinit("VNODE", sizeof (struct vnode), 0, 0, 5);
246 * Initialize the filesystem syncer.
248 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE,
250 syncer_maxdelay = syncer_mask + 1;
254 * Mark a mount point as busy. Used to synchronize access and to delay
255 * unmounting. Interlock is not released on failure.
258 vfs_busy(struct mount *mp, int flags,
259 lwkt_tokref_t interlkp, struct thread *td)
263 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
264 if (flags & LK_NOWAIT)
266 mp->mnt_kern_flag |= MNTK_MWAIT;
268 * Since all busy locks are shared except the exclusive
269 * lock granted when unmounting, the only place that a
270 * wakeup needs to be done is at the release of the
271 * exclusive lock at the end of dounmount.
273 * note: interlkp is a serializer and thus can be safely
274 * held through any sleep
276 tsleep((caddr_t)mp, 0, "vfs_busy", 0);
279 lkflags = LK_SHARED | LK_NOPAUSE;
281 lkflags |= LK_INTERLOCK;
282 if (lockmgr(&mp->mnt_lock, lkflags, interlkp, td))
283 panic("vfs_busy: unexpected lock failure");
288 * Free a busy filesystem.
291 vfs_unbusy(struct mount *mp, struct thread *td)
293 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td);
297 * Lookup a filesystem type, and if found allocate and initialize
298 * a mount structure for it.
300 * Devname is usually updated by mount(8) after booting.
303 vfs_rootmountalloc(char *fstypename, char *devname, struct mount **mpp)
305 struct thread *td = curthread; /* XXX */
306 struct vfsconf *vfsp;
309 if (fstypename == NULL)
311 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
312 if (!strcmp(vfsp->vfc_name, fstypename))
317 mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
318 bzero((char *)mp, (u_long)sizeof(struct mount));
319 lockinit(&mp->mnt_lock, 0, "vfslock", VLKTIMEOUT, LK_NOPAUSE);
320 vfs_busy(mp, LK_NOWAIT, NULL, td);
321 TAILQ_INIT(&mp->mnt_nvnodelist);
322 TAILQ_INIT(&mp->mnt_reservedvnlist);
323 mp->mnt_nvnodelistsize = 0;
325 mp->mnt_op = vfsp->vfc_vfsops;
326 mp->mnt_flag = MNT_RDONLY;
327 mp->mnt_vnodecovered = NULLVP;
328 vfsp->vfc_refcount++;
329 mp->mnt_iosize_max = DFLTPHYS;
330 mp->mnt_stat.f_type = vfsp->vfc_typenum;
331 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
332 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
333 mp->mnt_stat.f_mntonname[0] = '/';
334 mp->mnt_stat.f_mntonname[1] = 0;
335 (void) copystr(devname, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 0);
341 * Lookup a mount point by filesystem identifier.
344 vfs_getvfs(fsid_t *fsid)
349 lwkt_gettoken(&ilock, &mountlist_token);
350 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
351 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
352 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
356 lwkt_reltoken(&ilock);
361 * Get a new unique fsid. Try to make its val[0] unique, since this value
362 * will be used to create fake device numbers for stat(). Also try (but
363 * not so hard) make its val[0] unique mod 2^16, since some emulators only
364 * support 16-bit device numbers. We end up with unique val[0]'s for the
365 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
367 * Keep in mind that several mounts may be running in parallel. Starting
368 * the search one past where the previous search terminated is both a
369 * micro-optimization and a defense against returning the same fsid to
373 vfs_getnewfsid(struct mount *mp)
375 static u_int16_t mntid_base;
380 lwkt_gettoken(&ilock, &mntid_token);
381 mtype = mp->mnt_vfc->vfc_typenum;
382 tfsid.val[1] = mtype;
383 mtype = (mtype & 0xFF) << 24;
385 tfsid.val[0] = makeudev(255,
386 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
388 if (vfs_getvfs(&tfsid) == NULL)
391 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
392 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
393 lwkt_reltoken(&ilock);
397 * Knob to control the precision of file timestamps:
399 * 0 = seconds only; nanoseconds zeroed.
400 * 1 = seconds and nanoseconds, accurate within 1/HZ.
401 * 2 = seconds and nanoseconds, truncated to microseconds.
402 * >=3 = seconds and nanoseconds, maximum precision.
404 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
406 static int timestamp_precision = TSP_SEC;
407 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
408 ×tamp_precision, 0, "");
411 * Get a current timestamp.
414 vfs_timestamp(struct timespec *tsp)
418 switch (timestamp_precision) {
420 tsp->tv_sec = time_second;
428 TIMEVAL_TO_TIMESPEC(&tv, tsp);
438 * Set vnode attributes to VNOVAL
441 vattr_null(struct vattr *vap)
444 vap->va_size = VNOVAL;
445 vap->va_bytes = VNOVAL;
446 vap->va_mode = VNOVAL;
447 vap->va_nlink = VNOVAL;
448 vap->va_uid = VNOVAL;
449 vap->va_gid = VNOVAL;
450 vap->va_fsid = VNOVAL;
451 vap->va_fileid = VNOVAL;
452 vap->va_blocksize = VNOVAL;
453 vap->va_rdev = VNOVAL;
454 vap->va_atime.tv_sec = VNOVAL;
455 vap->va_atime.tv_nsec = VNOVAL;
456 vap->va_mtime.tv_sec = VNOVAL;
457 vap->va_mtime.tv_nsec = VNOVAL;
458 vap->va_ctime.tv_sec = VNOVAL;
459 vap->va_ctime.tv_nsec = VNOVAL;
460 vap->va_flags = VNOVAL;
461 vap->va_gen = VNOVAL;
466 * This routine is called when we have too many vnodes. It attempts
467 * to free <count> vnodes and will potentially free vnodes that still
468 * have VM backing store (VM backing store is typically the cause
469 * of a vnode blowout so we want to do this). Therefore, this operation
470 * is not considered cheap.
472 * A number of conditions may prevent a vnode from being reclaimed.
473 * the buffer cache may have references on the vnode, a directory
474 * vnode may still have references due to the namei cache representing
475 * underlying files, or the vnode may be in active use. It is not
476 * desireable to reuse such vnodes. These conditions may cause the
477 * number of vnodes to reach some minimum value regardless of what
478 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
481 vlrureclaim(struct mount *mp)
492 * Calculate the trigger point, don't allow user
493 * screwups to blow us up. This prevents us from
494 * recycling vnodes with lots of resident pages. We
495 * aren't trying to free memory, we are trying to
498 usevnodes = desiredvnodes;
501 trigger = vmstats.v_page_count * 2 / usevnodes;
504 lwkt_gettoken(&ilock, &mntvnode_token);
505 count = mp->mnt_nvnodelistsize / 10 + 1;
506 while (count && (vp = TAILQ_FIRST(&mp->mnt_nvnodelist)) != NULL) {
510 * The VP will stick around while we hold mntvnode_token,
511 * at least until we block, so we can safely do an initial
512 * check. But we have to check again after obtaining
513 * the vnode interlock. vp->v_interlock points to stable
514 * storage so it's ok if the vp gets ripped out from
515 * under us while we are blocked.
517 if (vp->v_type == VNON ||
518 vp->v_type == VBAD ||
519 !VMIGHTFREE(vp) || /* critical path opt */
521 vp->v_object->resident_page_count >= trigger)
523 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
524 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist,vp, v_nmntvnodes);
530 * Get the interlock, delay moving the node to the tail so
531 * we don't race against new additions to the mountlist.
533 lwkt_gettoken(&vlock, vp->v_interlock);
534 if (TAILQ_FIRST(&mp->mnt_nvnodelist) != vp) {
535 lwkt_reltoken(&vlock);
538 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
539 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist,vp, v_nmntvnodes);
544 if (vp->v_type == VNON ||
545 vp->v_type == VBAD ||
546 !VMIGHTFREE(vp) || /* critical path opt */
548 vp->v_object->resident_page_count >= trigger)
550 lwkt_reltoken(&vlock);
554 vgonel(vp, &vlock, curthread);
558 lwkt_reltoken(&ilock);
563 * Attempt to recycle vnodes in a context that is always safe to block.
564 * Calling vlrurecycle() from the bowels of file system code has some
565 * interesting deadlock problems.
567 static struct thread *vnlruthread;
568 static int vnlruproc_sig;
573 struct mount *mp, *nmp;
577 struct thread *td = curthread;
579 EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc, td,
584 kproc_suspend_loop();
585 if (numvnodes - freevnodes <= desiredvnodes * 9 / 10) {
587 wakeup(&vnlruproc_sig);
588 tsleep(td, 0, "vlruwt", hz);
592 lwkt_gettoken(&ilock, &mountlist_token);
593 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
594 if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
595 nmp = TAILQ_NEXT(mp, mnt_list);
598 done += vlrureclaim(mp);
599 lwkt_gettokref(&ilock);
600 nmp = TAILQ_NEXT(mp, mnt_list);
603 lwkt_reltoken(&ilock);
606 tsleep(td, 0, "vlrup", hz * 3);
612 static struct kproc_desc vnlru_kp = {
617 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &vnlru_kp)
620 * Routines having to do with the management of the vnode table.
624 * Return the next vnode from the free list.
627 getnewvnode(enum vtagtype tag, struct mount *mp, struct vop_ops *ops,
628 struct vnode **vpp, int lktimeout, int lkflags)
631 struct thread *td = curthread; /* XXX */
632 struct vnode *vp = NULL;
638 s = splbio(); /* YYY remove me */
641 * Try to reuse vnodes if we hit the max. This situation only
642 * occurs in certain large-memory (2G+) situations. We cannot
643 * attempt to directly reclaim vnodes due to nasty recursion
646 while (numvnodes - freevnodes > desiredvnodes) {
647 if (vnlruproc_sig == 0) {
648 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
651 tsleep(&vnlruproc_sig, 0, "vlruwk", hz);
656 * Attempt to reuse a vnode already on the free list, allocating
657 * a new vnode if we can't find one or if we have not reached a
658 * good minimum for good LRU performance.
660 lwkt_gettoken(&ilock, &vnode_free_list_token);
661 if (freevnodes >= wantfreevnodes && numvnodes >= minvnodes) {
664 for (count = 0; count < freevnodes; count++) {
668 * Pull the next vnode off the free list and do some
669 * sanity checks. Note that regardless of how we
670 * block, if freevnodes is non-zero there had better
671 * be something on the list.
673 vp = TAILQ_FIRST(&vnode_free_list);
675 panic("getnewvnode: free vnode isn't");
678 * Move the vnode to the end of the list so other
679 * processes do not double-block trying to recycle
680 * the same vnode (as an optimization), then get
683 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
684 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
687 * Skip vnodes that are in the process of being
688 * held or referenced. Since the act of adding or
689 * removing a vnode on the freelist requires a token
690 * and may block, the ref count may be adjusted
691 * prior to its addition or removal.
693 if (VSHOULDBUSY(vp)) {
700 * Obtain the vnode interlock and check that the
701 * vnode is still on the free list.
703 * This normally devolves into a degenerate case so
704 * it is optimal. Loop up if it isn't. Note that
705 * the vnode could be in the middle of being moved
706 * off the free list (the VSHOULDBUSY() check) and
707 * must be skipped if so.
709 lwkt_gettoken(&vlock, vp->v_interlock);
710 TAILQ_FOREACH_REVERSE(xvp, &vnode_free_list,
711 freelst, v_freelist) {
715 if (vp != xvp || VSHOULDBUSY(vp)) {
721 * We now safely own the vnode. If the vnode has
722 * an object do not recycle it if its VM object
723 * has resident pages or references.
725 if ((VOP_GETVOBJECT(vp, &object) == 0 &&
726 (object->resident_page_count || object->ref_count))
728 lwkt_reltoken(&vlock);
734 * We can almost reuse this vnode. But we don't want
735 * to recycle it if the vnode has children in the
736 * namecache because that breaks the namecache's
737 * path element chain. (YYY use nc_refs for the
740 KKASSERT(vp->v_flag & VFREE);
741 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
743 if (TAILQ_FIRST(&vp->v_namecache) == NULL ||
744 cache_leaf_test(vp) >= 0) {
745 /* ok, we can reuse this vnode */
748 lwkt_reltoken(&vlock);
749 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
755 * If vp is non-NULL we hold it's interlock.
758 vp->v_flag |= VDOOMED;
759 vp->v_flag &= ~VFREE;
761 lwkt_reltoken(&ilock);
762 cache_purge(vp); /* YYY may block */
764 if (vp->v_type != VBAD) {
765 vgonel(vp, &vlock, td);
767 lwkt_reltoken(&vlock);
775 panic("cleaned vnode isn't");
778 panic("Clean vnode has pending I/O's");
788 vp->v_writecount = 0; /* XXX */
789 lockreinit(&vp->v_lock, 0, "vnode", lktimeout, lkflags);
792 * A brand-new vnode (we could use malloc() here I think) XXX
794 lwkt_reltoken(&ilock);
795 vp = zalloc(vnode_zone);
796 bzero(vp, sizeof(*vp));
797 vp->v_interlock = lwkt_token_pool_get(vp);
798 lwkt_token_init(&vp->v_pollinfo.vpi_token);
799 lockinit(&vp->v_lock, 0, "vnode", lktimeout, lkflags);
801 TAILQ_INIT(&vp->v_namecache);
805 TAILQ_INIT(&vp->v_cleanblkhd);
806 TAILQ_INIT(&vp->v_dirtyblkhd);
816 * Placing the vnode on the mount point's queue makes it visible.
817 * We had better already have a ref on it.
821 vfs_object_create(vp, td);
826 * Move a vnode from one mount queue to another.
829 insmntque(struct vnode *vp, struct mount *mp)
833 lwkt_gettoken(&ilock, &mntvnode_token);
835 * Delete from old mount point vnode list, if on one.
837 if (vp->v_mount != NULL) {
838 KASSERT(vp->v_mount->mnt_nvnodelistsize > 0,
839 ("bad mount point vnode list size"));
840 TAILQ_REMOVE(&vp->v_mount->mnt_nvnodelist, vp, v_nmntvnodes);
841 vp->v_mount->mnt_nvnodelistsize--;
844 * Insert into list of vnodes for the new mount point, if available.
846 if ((vp->v_mount = mp) == NULL) {
847 lwkt_reltoken(&ilock);
850 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
851 mp->mnt_nvnodelistsize++;
852 lwkt_reltoken(&ilock);
856 * Update outstanding I/O count and do wakeup if requested.
859 vwakeup(struct buf *bp)
863 bp->b_flags &= ~B_WRITEINPROG;
864 if ((vp = bp->b_vp)) {
866 if (vp->v_numoutput < 0)
867 panic("vwakeup: neg numoutput");
868 if ((vp->v_numoutput == 0) && (vp->v_flag & VBWAIT)) {
869 vp->v_flag &= ~VBWAIT;
870 wakeup((caddr_t) &vp->v_numoutput);
876 * Flush out and invalidate all buffers associated with a vnode.
877 * Called with the underlying object locked.
880 vinvalbuf(struct vnode *vp, int flags, struct thread *td,
881 int slpflag, int slptimeo)
884 struct buf *nbp, *blist;
889 if (flags & V_SAVE) {
891 while (vp->v_numoutput) {
892 vp->v_flag |= VBWAIT;
893 error = tsleep((caddr_t)&vp->v_numoutput,
894 slpflag, "vinvlbuf", slptimeo);
900 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
902 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) != 0)
905 if (vp->v_numoutput > 0 ||
906 !TAILQ_EMPTY(&vp->v_dirtyblkhd))
907 panic("vinvalbuf: dirty bufs");
913 blist = TAILQ_FIRST(&vp->v_cleanblkhd);
915 blist = TAILQ_FIRST(&vp->v_dirtyblkhd);
919 for (bp = blist; bp; bp = nbp) {
920 nbp = TAILQ_NEXT(bp, b_vnbufs);
921 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
922 error = BUF_TIMELOCK(bp,
923 LK_EXCLUSIVE | LK_SLEEPFAIL,
924 "vinvalbuf", slpflag, slptimeo);
931 * XXX Since there are no node locks for NFS, I
932 * believe there is a slight chance that a delayed
933 * write will occur while sleeping just above, so
934 * check for it. Note that vfs_bio_awrite expects
935 * buffers to reside on a queue, while VOP_BWRITE and
938 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
941 if (bp->b_vp == vp) {
942 if (bp->b_flags & B_CLUSTEROK) {
947 bp->b_flags |= B_ASYNC;
948 VOP_BWRITE(bp->b_vp, bp);
952 (void) VOP_BWRITE(bp->b_vp, bp);
957 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
958 bp->b_flags &= ~B_ASYNC;
964 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
965 * have write I/O in-progress but if there is a VM object then the
966 * VM object can also have read-I/O in-progress.
969 while (vp->v_numoutput > 0) {
970 vp->v_flag |= VBWAIT;
971 tsleep(&vp->v_numoutput, 0, "vnvlbv", 0);
973 if (VOP_GETVOBJECT(vp, &object) == 0) {
974 while (object->paging_in_progress)
975 vm_object_pip_sleep(object, "vnvlbx");
977 } while (vp->v_numoutput > 0);
982 * Destroy the copy in the VM cache, too.
984 lwkt_gettoken(&vlock, vp->v_interlock);
985 if (VOP_GETVOBJECT(vp, &object) == 0) {
986 vm_object_page_remove(object, 0, 0,
987 (flags & V_SAVE) ? TRUE : FALSE);
989 lwkt_reltoken(&vlock);
991 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd) || !TAILQ_EMPTY(&vp->v_cleanblkhd))
992 panic("vinvalbuf: flush failed");
997 * Truncate a file's buffer and pages to a specified length. This
998 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
1002 vtruncbuf(struct vnode *vp, struct thread *td, off_t length, int blksize)
1010 * Round up to the *next* lbn.
1012 trunclbn = (length + blksize - 1) / blksize;
1019 for (bp = TAILQ_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
1020 nbp = TAILQ_NEXT(bp, b_vnbufs);
1021 if (bp->b_lblkno >= trunclbn) {
1022 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
1023 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
1027 bp->b_flags |= (B_INVAL | B_RELBUF);
1028 bp->b_flags &= ~B_ASYNC;
1033 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
1034 (nbp->b_vp != vp) ||
1035 (nbp->b_flags & B_DELWRI))) {
1041 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
1042 nbp = TAILQ_NEXT(bp, b_vnbufs);
1043 if (bp->b_lblkno >= trunclbn) {
1044 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
1045 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
1049 bp->b_flags |= (B_INVAL | B_RELBUF);
1050 bp->b_flags &= ~B_ASYNC;
1055 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
1056 (nbp->b_vp != vp) ||
1057 (nbp->b_flags & B_DELWRI) == 0)) {
1066 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
1067 nbp = TAILQ_NEXT(bp, b_vnbufs);
1068 if ((bp->b_flags & B_DELWRI) && (bp->b_lblkno < 0)) {
1069 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
1070 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
1074 if (bp->b_vp == vp) {
1075 bp->b_flags |= B_ASYNC;
1077 bp->b_flags &= ~B_ASYNC;
1079 VOP_BWRITE(bp->b_vp, bp);
1087 while (vp->v_numoutput > 0) {
1088 vp->v_flag |= VBWAIT;
1089 tsleep(&vp->v_numoutput, 0, "vbtrunc", 0);
1094 vnode_pager_setsize(vp, length);
1100 * Associate a buffer with a vnode.
1103 bgetvp(struct vnode *vp, struct buf *bp)
1107 KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
1111 bp->b_dev = vn_todev(vp);
1113 * Insert onto list for new vnode.
1116 bp->b_xflags |= BX_VNCLEAN;
1117 bp->b_xflags &= ~BX_VNDIRTY;
1118 TAILQ_INSERT_TAIL(&vp->v_cleanblkhd, bp, b_vnbufs);
1123 * Disassociate a buffer from a vnode.
1126 brelvp(struct buf *bp)
1129 struct buflists *listheadp;
1132 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1135 * Delete from old vnode list, if on one.
1139 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) {
1140 if (bp->b_xflags & BX_VNDIRTY)
1141 listheadp = &vp->v_dirtyblkhd;
1143 listheadp = &vp->v_cleanblkhd;
1144 TAILQ_REMOVE(listheadp, bp, b_vnbufs);
1145 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1147 if ((vp->v_flag & VONWORKLST) && TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
1148 vp->v_flag &= ~VONWORKLST;
1149 LIST_REMOVE(vp, v_synclist);
1152 bp->b_vp = (struct vnode *) 0;
1157 * The workitem queue.
1159 * It is useful to delay writes of file data and filesystem metadata
1160 * for tens of seconds so that quickly created and deleted files need
1161 * not waste disk bandwidth being created and removed. To realize this,
1162 * we append vnodes to a "workitem" queue. When running with a soft
1163 * updates implementation, most pending metadata dependencies should
1164 * not wait for more than a few seconds. Thus, mounted on block devices
1165 * are delayed only about a half the time that file data is delayed.
1166 * Similarly, directory updates are more critical, so are only delayed
1167 * about a third the time that file data is delayed. Thus, there are
1168 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
1169 * one each second (driven off the filesystem syncer process). The
1170 * syncer_delayno variable indicates the next queue that is to be processed.
1171 * Items that need to be processed soon are placed in this queue:
1173 * syncer_workitem_pending[syncer_delayno]
1175 * A delay of fifteen seconds is done by placing the request fifteen
1176 * entries later in the queue:
1178 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
1183 * Add an item to the syncer work queue.
1186 vn_syncer_add_to_worklist(struct vnode *vp, int delay)
1192 if (vp->v_flag & VONWORKLST) {
1193 LIST_REMOVE(vp, v_synclist);
1196 if (delay > syncer_maxdelay - 2)
1197 delay = syncer_maxdelay - 2;
1198 slot = (syncer_delayno + delay) & syncer_mask;
1200 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], vp, v_synclist);
1201 vp->v_flag |= VONWORKLST;
1205 struct thread *updatethread;
1206 static void sched_sync (void);
1207 static struct kproc_desc up_kp = {
1212 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp)
1215 * System filesystem synchronizer daemon.
1220 struct synclist *slp;
1224 struct thread *td = curthread;
1226 EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc, td,
1230 kproc_suspend_loop();
1232 starttime = time_second;
1235 * Push files whose dirty time has expired. Be careful
1236 * of interrupt race on slp queue.
1239 slp = &syncer_workitem_pending[syncer_delayno];
1240 syncer_delayno += 1;
1241 if (syncer_delayno == syncer_maxdelay)
1245 while ((vp = LIST_FIRST(slp)) != NULL) {
1246 if (VOP_ISLOCKED(vp, NULL) == 0) {
1247 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1248 (void) VOP_FSYNC(vp, MNT_LAZY, td);
1249 VOP_UNLOCK(vp, NULL, 0, td);
1252 if (LIST_FIRST(slp) == vp) {
1254 * Note: v_tag VT_VFS vps can remain on the
1255 * worklist too with no dirty blocks, but
1256 * since sync_fsync() moves it to a different
1259 if (TAILQ_EMPTY(&vp->v_dirtyblkhd) &&
1260 !vn_isdisk(vp, NULL))
1261 panic("sched_sync: fsync failed vp %p tag %d", vp, vp->v_tag);
1263 * Put us back on the worklist. The worklist
1264 * routine will remove us from our current
1265 * position and then add us back in at a later
1268 vn_syncer_add_to_worklist(vp, syncdelay);
1274 * Do soft update processing.
1277 (*bioops.io_sync)(NULL);
1280 * The variable rushjob allows the kernel to speed up the
1281 * processing of the filesystem syncer process. A rushjob
1282 * value of N tells the filesystem syncer to process the next
1283 * N seconds worth of work on its queue ASAP. Currently rushjob
1284 * is used by the soft update code to speed up the filesystem
1285 * syncer process when the incore state is getting so far
1286 * ahead of the disk that the kernel memory pool is being
1287 * threatened with exhaustion.
1294 * If it has taken us less than a second to process the
1295 * current work, then wait. Otherwise start right over
1296 * again. We can still lose time if any single round
1297 * takes more than two seconds, but it does not really
1298 * matter as we are just trying to generally pace the
1299 * filesystem activity.
1301 if (time_second == starttime)
1302 tsleep(&lbolt, 0, "syncer", 0);
1307 * Request the syncer daemon to speed up its work.
1308 * We never push it to speed up more than half of its
1309 * normal turn time, otherwise it could take over the cpu.
1311 * YYY wchan field protected by the BGL.
1314 speedup_syncer(void)
1317 if (updatethread->td_wchan == &lbolt) { /* YYY */
1318 unsleep(updatethread);
1319 lwkt_schedule(updatethread);
1322 if (rushjob < syncdelay / 2) {
1324 stat_rush_requests += 1;
1331 * Associate a p-buffer with a vnode.
1333 * Also sets B_PAGING flag to indicate that vnode is not fully associated
1334 * with the buffer. i.e. the bp has not been linked into the vnode or
1338 pbgetvp(struct vnode *vp, struct buf *bp)
1340 KASSERT(bp->b_vp == NULL, ("pbgetvp: not free"));
1343 bp->b_flags |= B_PAGING;
1344 bp->b_dev = vn_todev(vp);
1348 * Disassociate a p-buffer from a vnode.
1351 pbrelvp(struct buf *bp)
1353 KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL"));
1356 if (TAILQ_NEXT(bp, b_vnbufs) != NULL) {
1358 "relpbuf(): b_vp was probably reassignbuf()d %p %x",
1363 bp->b_vp = (struct vnode *) 0;
1364 bp->b_flags &= ~B_PAGING;
1368 pbreassignbuf(struct buf *bp, struct vnode *newvp)
1370 if ((bp->b_flags & B_PAGING) == 0) {
1372 "pbreassignbuf() on non phys bp %p",
1380 * Reassign a buffer from one vnode to another.
1381 * Used to assign file specific control information
1382 * (indirect blocks) to the vnode to which they belong.
1385 reassignbuf(struct buf *bp, struct vnode *newvp)
1387 struct buflists *listheadp;
1391 if (newvp == NULL) {
1392 printf("reassignbuf: NULL");
1398 * B_PAGING flagged buffers cannot be reassigned because their vp
1399 * is not fully linked in.
1401 if (bp->b_flags & B_PAGING)
1402 panic("cannot reassign paging buffer");
1406 * Delete from old vnode list, if on one.
1408 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) {
1409 if (bp->b_xflags & BX_VNDIRTY)
1410 listheadp = &bp->b_vp->v_dirtyblkhd;
1412 listheadp = &bp->b_vp->v_cleanblkhd;
1413 TAILQ_REMOVE(listheadp, bp, b_vnbufs);
1414 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1415 if (bp->b_vp != newvp) {
1417 bp->b_vp = NULL; /* for clarification */
1421 * If dirty, put on list of dirty buffers; otherwise insert onto list
1424 if (bp->b_flags & B_DELWRI) {
1427 listheadp = &newvp->v_dirtyblkhd;
1428 if ((newvp->v_flag & VONWORKLST) == 0) {
1429 switch (newvp->v_type) {
1435 if (newvp->v_rdev &&
1436 newvp->v_rdev->si_mountpoint != NULL) {
1444 vn_syncer_add_to_worklist(newvp, delay);
1446 bp->b_xflags |= BX_VNDIRTY;
1447 tbp = TAILQ_FIRST(listheadp);
1449 bp->b_lblkno == 0 ||
1450 (bp->b_lblkno > 0 && tbp->b_lblkno < 0) ||
1451 (bp->b_lblkno > 0 && bp->b_lblkno < tbp->b_lblkno)) {
1452 TAILQ_INSERT_HEAD(listheadp, bp, b_vnbufs);
1453 ++reassignbufsortgood;
1454 } else if (bp->b_lblkno < 0) {
1455 TAILQ_INSERT_TAIL(listheadp, bp, b_vnbufs);
1456 ++reassignbufsortgood;
1457 } else if (reassignbufmethod == 1) {
1459 * New sorting algorithm, only handle sequential case,
1460 * otherwise append to end (but before metadata)
1462 if ((tbp = gbincore(newvp, bp->b_lblkno - 1)) != NULL &&
1463 (tbp->b_xflags & BX_VNDIRTY)) {
1465 * Found the best place to insert the buffer
1467 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1468 ++reassignbufsortgood;
1471 * Missed, append to end, but before meta-data.
1472 * We know that the head buffer in the list is
1473 * not meta-data due to prior conditionals.
1475 * Indirect effects: NFS second stage write
1476 * tends to wind up here, giving maximum
1477 * distance between the unstable write and the
1480 tbp = TAILQ_LAST(listheadp, buflists);
1481 while (tbp && tbp->b_lblkno < 0)
1482 tbp = TAILQ_PREV(tbp, buflists, b_vnbufs);
1483 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1484 ++reassignbufsortbad;
1488 * Old sorting algorithm, scan queue and insert
1491 while ((ttbp = TAILQ_NEXT(tbp, b_vnbufs)) &&
1492 (ttbp->b_lblkno < bp->b_lblkno)) {
1496 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1499 bp->b_xflags |= BX_VNCLEAN;
1500 TAILQ_INSERT_TAIL(&newvp->v_cleanblkhd, bp, b_vnbufs);
1501 if ((newvp->v_flag & VONWORKLST) &&
1502 TAILQ_EMPTY(&newvp->v_dirtyblkhd)) {
1503 newvp->v_flag &= ~VONWORKLST;
1504 LIST_REMOVE(newvp, v_synclist);
1507 if (bp->b_vp != newvp) {
1515 * Create a vnode for a block device.
1516 * Used for mounting the root file system.
1519 bdevvp(dev_t dev, struct vnode **vpp)
1529 error = getnewvnode(VT_NON, NULL, spec_vnode_vops, &nvp, 0, 0);
1536 vp->v_udev = dev->si_udev;
1542 v_associate_rdev(struct vnode *vp, dev_t dev)
1546 if (dev == NULL || dev == NODEV)
1548 if (dev_is_good(dev) == 0)
1550 KKASSERT(vp->v_rdev == NULL);
1553 vp->v_rdev = reference_dev(dev);
1554 lwkt_gettoken(&ilock, &spechash_token);
1555 SLIST_INSERT_HEAD(&dev->si_hlist, vp, v_specnext);
1556 lwkt_reltoken(&ilock);
1561 v_release_rdev(struct vnode *vp)
1566 if ((dev = vp->v_rdev) != NULL) {
1567 lwkt_gettoken(&ilock, &spechash_token);
1568 SLIST_REMOVE(&dev->si_hlist, vp, vnode, v_specnext);
1569 if (dev_ref_debug && vp->v_opencount != 0) {
1570 printf("releasing rdev with non-0 "
1571 "v_opencount(%d) (revoked?)\n",
1575 vp->v_opencount = 0;
1577 lwkt_reltoken(&ilock);
1582 * Add a vnode to the alias list hung off the dev_t. We only associate
1583 * the device number with the vnode. The actual device is not associated
1584 * until the vnode is opened (usually in spec_open()), and will be
1585 * disassociated on last close.
1588 addaliasu(struct vnode *nvp, udev_t nvp_udev)
1590 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1591 panic("addaliasu on non-special vnode");
1592 nvp->v_udev = nvp_udev;
1596 * Grab a particular vnode from the free list, increment its
1597 * reference count and lock it. The vnode lock bit is set if the
1598 * vnode is being eliminated in vgone. The process is awakened
1599 * when the transition is completed, and an error returned to
1600 * indicate that the vnode is no longer usable (possibly having
1601 * been changed to a new file system type).
1603 * This code is very sensitive. We are depending on the vnode interlock
1604 * to be maintained through to the vn_lock() call, which means that we
1605 * cannot block which means that we cannot call vbusy() until after vn_lock().
1606 * If the interlock is not maintained, the VXLOCK check will not properly
1607 * interlock against a vclean()'s LK_DRAIN operation on the lock.
1610 vget(struct vnode *vp, lwkt_tokref_t vlock, int flags, thread_t td)
1616 * We need the interlock to safely modify the v_ fields. ZZZ it is
1617 * only legal to pass (1) the vnode's interlock and (2) only pass
1618 * NULL w/o LK_INTERLOCK if the vnode is *ALREADY* referenced or
1621 if ((flags & LK_INTERLOCK) == 0) {
1622 lwkt_gettoken(&vvlock, vp->v_interlock);
1627 * If the vnode is in the process of being cleaned out for
1628 * another use, we wait for the cleaning to finish and then
1629 * return failure. Cleaning is determined by checking that
1630 * the VXLOCK flag is set. It is possible for the vnode to be
1631 * self-referenced during the cleaning operation.
1633 if (vp->v_flag & VXLOCK) {
1634 if (vp->v_vxthread == curthread) {
1636 /* this can now occur in normal operation */
1637 log(LOG_INFO, "VXLOCK interlock avoided\n");
1640 vp->v_flag |= VXWANT;
1641 lwkt_reltoken(vlock);
1642 tsleep((caddr_t)vp, 0, "vget", 0);
1648 * Bump v_usecount to prevent the vnode from being recycled. The
1649 * usecount needs to be bumped before we successfully get our lock.
1652 if (flags & LK_TYPE_MASK) {
1653 if ((error = vn_lock(vp, vlock, flags | LK_INTERLOCK, td)) != 0) {
1655 * must expand vrele here because we do not want
1656 * to call VOP_INACTIVE if the reference count
1657 * drops back to zero since it was never really
1658 * active. We must remove it from the free list
1659 * before sleeping so that multiple processes do
1660 * not try to recycle it.
1662 lwkt_gettokref(vlock);
1665 lwkt_reltoken(vlock);
1669 if (VSHOULDBUSY(vp))
1670 vbusy(vp); /* interlock must be held on call */
1671 lwkt_reltoken(vlock);
1676 vref(struct vnode *vp)
1678 crit_enter(); /* YYY use crit section for moment / BGL protected */
1684 * Vnode put/release.
1685 * If count drops to zero, call inactive routine and return to freelist.
1688 vrele(struct vnode *vp)
1690 struct thread *td = curthread; /* XXX */
1693 KASSERT(vp != NULL && vp->v_usecount >= 0,
1694 ("vrele: null vp or <=0 v_usecount"));
1696 lwkt_gettoken(&vlock, vp->v_interlock);
1698 if (vp->v_usecount > 1) {
1700 lwkt_reltoken(&vlock);
1704 if (vp->v_usecount == 1) {
1707 * We must call VOP_INACTIVE with the node locked and the
1708 * usecount 0. If we are doing a vpu, the node is already
1709 * locked, but, in the case of vrele, we must explicitly lock
1710 * the vnode before calling VOP_INACTIVE.
1713 if (vn_lock(vp, NULL, LK_EXCLUSIVE, td) == 0)
1714 VOP_INACTIVE(vp, td);
1716 lwkt_reltoken(&vlock);
1719 vprint("vrele: negative ref count", vp);
1721 lwkt_reltoken(&vlock);
1722 panic("vrele: negative ref cnt");
1727 vput(struct vnode *vp)
1729 struct thread *td = curthread; /* XXX */
1732 KASSERT(vp != NULL, ("vput: null vp"));
1734 lwkt_gettoken(&vlock, vp->v_interlock);
1736 if (vp->v_usecount > 1) {
1738 VOP_UNLOCK(vp, &vlock, LK_INTERLOCK, td);
1742 if (vp->v_usecount == 1) {
1745 * We must call VOP_INACTIVE with the node locked.
1746 * If we are doing a vpu, the node is already locked,
1747 * so we just need to release the vnode mutex.
1749 VOP_INACTIVE(vp, td);
1751 lwkt_reltoken(&vlock);
1754 vprint("vput: negative ref count", vp);
1756 lwkt_reltoken(&vlock);
1757 panic("vput: negative ref cnt");
1762 * Somebody doesn't want the vnode recycled. ZZZ vnode interlock should
1763 * be held but isn't.
1766 vhold(struct vnode *vp)
1772 if (VSHOULDBUSY(vp))
1773 vbusy(vp); /* interlock must be held on call */
1778 * One less who cares about this vnode.
1781 vdrop(struct vnode *vp)
1785 lwkt_gettoken(&vlock, vp->v_interlock);
1786 if (vp->v_holdcnt <= 0)
1787 panic("vdrop: holdcnt");
1790 lwkt_reltoken(&vlock);
1796 int (*fastfunc)(struct mount *mp, struct vnode *vp, void *data),
1797 int (*slowfunc)(struct mount *mp, struct vnode *vp,
1798 lwkt_tokref_t vlock, void *data),
1808 * Scan the vnodes on the mount's vnode list. Use a placemarker
1810 pvp = zalloc(vnode_zone);
1811 pvp->v_flag |= VPLACEMARKER;
1813 lwkt_gettoken(&ilock, &mntvnode_token);
1814 TAILQ_INSERT_HEAD(&mp->mnt_nvnodelist, pvp, v_nmntvnodes);
1816 while ((vp = TAILQ_NEXT(pvp, v_nmntvnodes)) != NULL) {
1818 * Move the placemarker and skip other placemarkers we
1819 * encounter. The nothing can get in our way so the
1820 * mount point on the vp must be valid.
1822 TAILQ_REMOVE(&mp->mnt_nvnodelist, pvp, v_nmntvnodes);
1823 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, pvp, v_nmntvnodes);
1824 if (vp->v_flag & VPLACEMARKER)
1826 KKASSERT(vp->v_mount == mp);
1832 if ((r = fastfunc(mp, vp, data)) < 0)
1839 * Get the vnodes interlock and make sure it is still on the
1840 * mount list. Skip it if it has moved (we may encounter it
1841 * later). Then do the with-interlock test. The callback
1842 * is responsible for releasing the vnode interlock.
1844 * The interlock is type-stable.
1847 lwkt_gettoken(&vlock, vp->v_interlock);
1848 if (vp != TAILQ_PREV(pvp, vnodelst, v_nmntvnodes)) {
1849 printf("vmntvnodescan (debug info only): f=%p vp=%p vnode ripped out from under us\n", slowfunc, vp);
1850 lwkt_reltoken(&vlock);
1853 if ((r = slowfunc(mp, vp, &vlock, data)) != 0) {
1854 KKASSERT(lwkt_havetokref(&vlock) == 0);
1857 KKASSERT(lwkt_havetokref(&vlock) == 0);
1860 TAILQ_REMOVE(&mp->mnt_nvnodelist, pvp, v_nmntvnodes);
1861 zfree(vnode_zone, pvp);
1862 lwkt_reltoken(&ilock);
1867 * Remove any vnodes in the vnode table belonging to mount point mp.
1869 * If FORCECLOSE is not specified, there should not be any active ones,
1870 * return error if any are found (nb: this is a user error, not a
1871 * system error). If FORCECLOSE is specified, detach any active vnodes
1874 * If WRITECLOSE is set, only flush out regular file vnodes open for
1877 * SKIPSYSTEM causes any vnodes marked VSYSTEM to be skipped.
1879 * `rootrefs' specifies the base reference count for the root vnode
1880 * of this filesystem. The root vnode is considered busy if its
1881 * v_usecount exceeds this value. On a successful return, vflush()
1882 * will call vrele() on the root vnode exactly rootrefs times.
1883 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
1887 static int busyprt = 0; /* print out busy vnodes */
1888 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "");
1891 static int vflush_scan(struct mount *mp, struct vnode *vp,
1892 lwkt_tokref_t vlock, void *data);
1894 struct vflush_info {
1901 vflush(struct mount *mp, int rootrefs, int flags)
1903 struct thread *td = curthread; /* XXX */
1904 struct vnode *rootvp = NULL;
1907 struct vflush_info vflush_info;
1910 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
1911 ("vflush: bad args"));
1913 * Get the filesystem root vnode. We can vput() it
1914 * immediately, since with rootrefs > 0, it won't go away.
1916 if ((error = VFS_ROOT(mp, &rootvp)) != 0)
1921 vflush_info.busy = 0;
1922 vflush_info.flags = flags;
1923 vflush_info.td = td;
1924 vmntvnodescan(mp, NULL, vflush_scan, &vflush_info);
1926 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
1928 * If just the root vnode is busy, and if its refcount
1929 * is equal to `rootrefs', then go ahead and kill it.
1931 lwkt_gettoken(&vlock, rootvp->v_interlock);
1932 KASSERT(vflush_info.busy > 0, ("vflush: not busy"));
1933 KASSERT(rootvp->v_usecount >= rootrefs, ("vflush: rootrefs"));
1934 if (vflush_info.busy == 1 && rootvp->v_usecount == rootrefs) {
1935 vgonel(rootvp, &vlock, td);
1936 vflush_info.busy = 0;
1938 lwkt_reltoken(&vlock);
1941 if (vflush_info.busy)
1943 for (; rootrefs > 0; rootrefs--)
1949 * The scan callback is made with an interlocked vnode.
1952 vflush_scan(struct mount *mp, struct vnode *vp,
1953 lwkt_tokref_t vlock, void *data)
1955 struct vflush_info *info = data;
1959 * Skip over a vnodes marked VSYSTEM.
1961 if ((info->flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM)) {
1962 lwkt_reltoken(vlock);
1967 * If WRITECLOSE is set, flush out unlinked but still open
1968 * files (even if open only for reading) and regular file
1969 * vnodes open for writing.
1971 if ((info->flags & WRITECLOSE) &&
1972 (vp->v_type == VNON ||
1973 (VOP_GETATTR(vp, &vattr, info->td) == 0 &&
1974 vattr.va_nlink > 0)) &&
1975 (vp->v_writecount == 0 || vp->v_type != VREG)) {
1976 lwkt_reltoken(vlock);
1981 * With v_usecount == 0, all we need to do is clear out the
1982 * vnode data structures and we are done.
1984 if (vp->v_usecount == 0) {
1985 vgonel(vp, vlock, info->td);
1990 * If FORCECLOSE is set, forcibly close the vnode. For block
1991 * or character devices, revert to an anonymous device. For
1992 * all other files, just kill them.
1994 if (info->flags & FORCECLOSE) {
1995 if (vp->v_type != VBLK && vp->v_type != VCHR) {
1996 vgonel(vp, vlock, info->td);
1998 vclean(vp, vlock, 0, info->td);
1999 vp->v_ops = spec_vnode_vops;
2000 insmntque(vp, (struct mount *) 0);
2006 vprint("vflush: busy vnode", vp);
2008 lwkt_reltoken(vlock);
2014 * Disassociate the underlying file system from a vnode.
2017 vclean(struct vnode *vp, lwkt_tokref_t vlock, int flags, struct thread *td)
2022 * Check to see if the vnode is in use. If so we have to reference it
2023 * before we clean it out so that its count cannot fall to zero and
2024 * generate a race against ourselves to recycle it.
2026 if ((active = vp->v_usecount))
2030 * Prevent the vnode from being recycled or brought into use while we
2033 if (vp->v_flag & VXLOCK)
2034 panic("vclean: deadlock");
2035 vp->v_flag |= VXLOCK;
2036 vp->v_vxthread = curthread;
2039 * Even if the count is zero, the VOP_INACTIVE routine may still
2040 * have the object locked while it cleans it out. The VOP_LOCK
2041 * ensures that the VOP_INACTIVE routine is done with its work.
2042 * For active vnodes, it ensures that no other activity can
2043 * occur while the underlying object is being cleaned out.
2045 * NOTE: we continue to hold the vnode interlock through to the
2048 VOP_LOCK(vp, NULL, LK_DRAIN, td);
2051 * Clean out any buffers associated with the vnode.
2053 vinvalbuf(vp, V_SAVE, td, 0, 0);
2054 VOP_DESTROYVOBJECT(vp);
2057 * If purging an active vnode, it must be closed and
2058 * deactivated before being reclaimed. Note that the
2059 * VOP_INACTIVE will unlock the vnode.
2062 if (flags & DOCLOSE)
2063 VOP_CLOSE(vp, FNONBLOCK, td);
2064 VOP_INACTIVE(vp, td);
2067 * Any other processes trying to obtain this lock must first
2068 * wait for VXLOCK to clear, then call the new lock operation.
2070 VOP_UNLOCK(vp, NULL, 0, td);
2073 * Reclaim the vnode.
2075 if (VOP_RECLAIM(vp, td))
2076 panic("vclean: cannot reclaim");
2080 * Inline copy of vrele() since VOP_INACTIVE
2081 * has already been called.
2083 if (--vp->v_usecount <= 0) {
2085 if (vp->v_usecount < 0 || vp->v_writecount != 0) {
2086 vprint("vclean: bad ref count", vp);
2087 panic("vclean: ref cnt");
2098 * Done with purge, notify sleepers of the grim news.
2100 vp->v_ops = dead_vnode_vops;
2103 vp->v_flag &= ~VXLOCK;
2104 vp->v_vxthread = NULL;
2105 if (vp->v_flag & VXWANT) {
2106 vp->v_flag &= ~VXWANT;
2107 wakeup((caddr_t) vp);
2109 lwkt_reltoken(vlock);
2113 * Eliminate all activity associated with the requested vnode
2114 * and with all vnodes aliased to the requested vnode.
2116 * revoke { struct vnode *a_vp, int a_flags }
2119 vop_stdrevoke(struct vop_revoke_args *ap)
2121 struct vnode *vp, *vq;
2125 KASSERT((ap->a_flags & REVOKEALL) != 0, ("vop_revoke"));
2129 * If a vgone (or vclean) is already in progress,
2130 * wait until it is done and return.
2132 if (vp->v_flag & VXLOCK) {
2133 vp->v_flag |= VXWANT;
2134 /*lwkt_reltoken(vlock); ZZZ */
2135 tsleep((caddr_t)vp, 0, "vop_revokeall", 0);
2140 * If the vnode has a device association, scrap all vnodes associated
2141 * with the device. Don't let the device disappear on us while we
2142 * are scrapping the vnodes.
2144 if (vp->v_type != VCHR && vp->v_type != VBLK)
2146 if ((dev = vp->v_rdev) == NULL) {
2147 if ((dev = udev2dev(vp->v_udev, vp->v_type == VBLK)) == NODEV)
2152 lwkt_gettoken(&ilock, &spechash_token);
2153 vq = SLIST_FIRST(&dev->si_hlist);
2154 lwkt_reltoken(&ilock);
2164 * Recycle an unused vnode to the front of the free list.
2165 * Release the passed interlock if the vnode will be recycled.
2168 vrecycle(struct vnode *vp, lwkt_tokref_t inter_lkp, struct thread *td)
2172 lwkt_gettoken(&vlock, vp->v_interlock);
2173 if (vp->v_usecount == 0) {
2175 lwkt_reltoken(inter_lkp);
2176 vgonel(vp, &vlock, td);
2179 lwkt_reltoken(&vlock);
2184 * Eliminate all activity associated with a vnode
2185 * in preparation for reuse.
2188 vgone(struct vnode *vp)
2190 struct thread *td = curthread; /* XXX */
2193 lwkt_gettoken(&vlock, vp->v_interlock);
2194 vgonel(vp, &vlock, td);
2198 * vgone, with the vp interlock held.
2201 vgonel(struct vnode *vp, lwkt_tokref_t vlock, struct thread *td)
2207 * If a vgone (or vclean) is already in progress,
2208 * wait until it is done and return.
2210 if (vp->v_flag & VXLOCK) {
2211 vp->v_flag |= VXWANT;
2212 lwkt_reltoken(vlock);
2213 tsleep((caddr_t)vp, 0, "vgone", 0);
2218 * Clean out the filesystem specific data.
2220 vclean(vp, vlock, DOCLOSE, td);
2221 lwkt_gettokref(vlock);
2224 * Delete from old mount point vnode list, if on one.
2226 if (vp->v_mount != NULL)
2227 insmntque(vp, (struct mount *)0);
2230 * If special device, remove it from special device alias list
2231 * if it is on one. This should normally only occur if a vnode is
2232 * being revoked as the device should otherwise have been released
2235 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
2240 * If it is on the freelist and not already at the head,
2241 * move it to the head of the list. The test of the
2242 * VDOOMED flag and the reference count of zero is because
2243 * it will be removed from the free list by getnewvnode,
2244 * but will not have its reference count incremented until
2245 * after calling vgone. If the reference count were
2246 * incremented first, vgone would (incorrectly) try to
2247 * close the previous instance of the underlying object.
2249 if (vp->v_usecount == 0 && !(vp->v_flag & VDOOMED)) {
2251 lwkt_gettoken(&ilock, &vnode_free_list_token);
2252 if (vp->v_flag & VFREE)
2253 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
2256 vp->v_flag |= VFREE;
2257 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2258 lwkt_reltoken(&ilock);
2262 lwkt_reltoken(vlock);
2266 * Lookup a vnode by device number.
2269 vfinddev(dev_t dev, enum vtype type, struct vnode **vpp)
2274 lwkt_gettoken(&ilock, &spechash_token);
2275 SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) {
2276 if (type == vp->v_type) {
2278 lwkt_reltoken(&ilock);
2282 lwkt_reltoken(&ilock);
2287 * Calculate the total number of references to a special device. This
2288 * routine may only be called for VBLK and VCHR vnodes since v_rdev is
2289 * an overloaded field. Since udev2dev can now return NODEV, we have
2290 * to check for a NULL v_rdev.
2293 count_dev(dev_t dev)
2299 if (SLIST_FIRST(&dev->si_hlist)) {
2300 lwkt_gettoken(&ilock, &spechash_token);
2301 SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) {
2302 count += vp->v_usecount;
2304 lwkt_reltoken(&ilock);
2310 count_udev(udev_t udev)
2314 if ((dev = udev2dev(udev, 0)) == NODEV)
2316 return(count_dev(dev));
2320 vcount(struct vnode *vp)
2322 if (vp->v_rdev == NULL)
2324 return(count_dev(vp->v_rdev));
2328 * Print out a description of a vnode.
2330 static char *typename[] =
2331 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
2334 vprint(char *label, struct vnode *vp)
2339 printf("%s: %p: ", label, (void *)vp);
2341 printf("%p: ", (void *)vp);
2342 printf("type %s, usecount %d, writecount %d, refcount %d,",
2343 typename[vp->v_type], vp->v_usecount, vp->v_writecount,
2346 if (vp->v_flag & VROOT)
2347 strcat(buf, "|VROOT");
2348 if (vp->v_flag & VTEXT)
2349 strcat(buf, "|VTEXT");
2350 if (vp->v_flag & VSYSTEM)
2351 strcat(buf, "|VSYSTEM");
2352 if (vp->v_flag & VXLOCK)
2353 strcat(buf, "|VXLOCK");
2354 if (vp->v_flag & VXWANT)
2355 strcat(buf, "|VXWANT");
2356 if (vp->v_flag & VBWAIT)
2357 strcat(buf, "|VBWAIT");
2358 if (vp->v_flag & VDOOMED)
2359 strcat(buf, "|VDOOMED");
2360 if (vp->v_flag & VFREE)
2361 strcat(buf, "|VFREE");
2362 if (vp->v_flag & VOBJBUF)
2363 strcat(buf, "|VOBJBUF");
2365 printf(" flags (%s)", &buf[1]);
2366 if (vp->v_data == NULL) {
2375 #include <ddb/ddb.h>
2377 * List all of the locked vnodes in the system.
2378 * Called when debugging the kernel.
2380 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
2382 struct thread *td = curthread; /* XXX */
2384 struct mount *mp, *nmp;
2387 printf("Locked vnodes\n");
2388 lwkt_gettoken(&ilock, &mountlist_token);
2389 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2390 if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
2391 nmp = TAILQ_NEXT(mp, mnt_list);
2394 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
2395 if (VOP_ISLOCKED(vp, NULL))
2396 vprint((char *)0, vp);
2398 lwkt_gettokref(&ilock);
2399 nmp = TAILQ_NEXT(mp, mnt_list);
2402 lwkt_reltoken(&ilock);
2407 * Top level filesystem related information gathering.
2409 static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS);
2412 vfs_sysctl(SYSCTL_HANDLER_ARGS)
2414 int *name = (int *)arg1 - 1; /* XXX */
2415 u_int namelen = arg2 + 1; /* XXX */
2416 struct vfsconf *vfsp;
2418 #if 1 || defined(COMPAT_PRELITE2)
2419 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
2421 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
2425 /* all sysctl names at this level are at least name and field */
2427 return (ENOTDIR); /* overloaded */
2428 if (name[0] != VFS_GENERIC) {
2429 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2430 if (vfsp->vfc_typenum == name[0])
2433 return (EOPNOTSUPP);
2434 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
2435 oldp, oldlenp, newp, newlen, p));
2439 case VFS_MAXTYPENUM:
2442 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
2445 return (ENOTDIR); /* overloaded */
2446 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2447 if (vfsp->vfc_typenum == name[2])
2450 return (EOPNOTSUPP);
2451 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
2453 return (EOPNOTSUPP);
2456 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
2457 "Generic filesystem");
2459 #if 1 || defined(COMPAT_PRELITE2)
2462 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
2465 struct vfsconf *vfsp;
2466 struct ovfsconf ovfs;
2468 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
2469 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
2470 strcpy(ovfs.vfc_name, vfsp->vfc_name);
2471 ovfs.vfc_index = vfsp->vfc_typenum;
2472 ovfs.vfc_refcount = vfsp->vfc_refcount;
2473 ovfs.vfc_flags = vfsp->vfc_flags;
2474 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
2481 #endif /* 1 || COMPAT_PRELITE2 */
2484 #define KINFO_VNODESLOP 10
2486 * Dump vnode list (via sysctl).
2487 * Copyout address of vnode followed by vnode.
2491 sysctl_vnode(SYSCTL_HANDLER_ARGS)
2493 struct proc *p = curproc; /* XXX */
2494 struct mount *mp, *nmp;
2495 struct vnode *nvp, *vp;
2500 #define VPTRSZ sizeof (struct vnode *)
2501 #define VNODESZ sizeof (struct vnode)
2504 if (!req->oldptr) /* Make an estimate */
2505 return (SYSCTL_OUT(req, 0,
2506 (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ)));
2508 lwkt_gettoken(&ilock, &mountlist_token);
2509 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2510 if (vfs_busy(mp, LK_NOWAIT, &ilock, p)) {
2511 nmp = TAILQ_NEXT(mp, mnt_list);
2514 lwkt_gettoken(&jlock, &mntvnode_token);
2516 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
2520 * Check that the vp is still associated with
2521 * this filesystem. RACE: could have been
2522 * recycled onto the same filesystem.
2524 if (vp->v_mount != mp)
2526 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
2527 if ((error = SYSCTL_OUT(req, &vp, VPTRSZ)) ||
2528 (error = SYSCTL_OUT(req, vp, VNODESZ))) {
2529 lwkt_reltoken(&jlock);
2533 lwkt_reltoken(&jlock);
2534 lwkt_gettokref(&ilock);
2535 nmp = TAILQ_NEXT(mp, mnt_list); /* ZZZ */
2538 lwkt_reltoken(&ilock);
2546 * Exporting the vnode list on large systems causes them to crash.
2547 * Exporting the vnode list on medium systems causes sysctl to coredump.
2550 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD,
2551 0, 0, sysctl_vnode, "S,vnode", "");
2555 * Check to see if a filesystem is mounted on a block device.
2558 vfs_mountedon(struct vnode *vp)
2562 if ((dev = vp->v_rdev) == NULL)
2563 dev = udev2dev(vp->v_udev, (vp->v_type == VBLK));
2564 if (dev != NODEV && dev->si_mountpoint)
2570 * Unmount all filesystems. The list is traversed in reverse order
2571 * of mounting to avoid dependencies.
2574 vfs_unmountall(void)
2577 struct thread *td = curthread;
2580 if (td->td_proc == NULL)
2581 td = initproc->p_thread; /* XXX XXX use proc0 instead? */
2584 * Since this only runs when rebooting, it is not interlocked.
2586 while(!TAILQ_EMPTY(&mountlist)) {
2587 mp = TAILQ_LAST(&mountlist, mntlist);
2588 error = dounmount(mp, MNT_FORCE, td);
2590 TAILQ_REMOVE(&mountlist, mp, mnt_list);
2591 printf("unmount of %s failed (",
2592 mp->mnt_stat.f_mntonname);
2596 printf("%d)\n", error);
2598 /* The unmount has removed mp from the mountlist */
2604 * Build hash lists of net addresses and hang them off the mount point.
2605 * Called by ufs_mount() to set up the lists of export addresses.
2608 vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
2609 struct export_args *argp)
2612 struct radix_node_head *rnh;
2614 struct radix_node *rn;
2615 struct sockaddr *saddr, *smask = 0;
2619 if (argp->ex_addrlen == 0) {
2620 if (mp->mnt_flag & MNT_DEFEXPORTED)
2622 np = &nep->ne_defexported;
2623 np->netc_exflags = argp->ex_flags;
2624 np->netc_anon = argp->ex_anon;
2625 np->netc_anon.cr_ref = 1;
2626 mp->mnt_flag |= MNT_DEFEXPORTED;
2630 if (argp->ex_addrlen < 0 || argp->ex_addrlen > MLEN)
2632 if (argp->ex_masklen < 0 || argp->ex_masklen > MLEN)
2635 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
2636 np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK);
2637 bzero((caddr_t) np, i);
2638 saddr = (struct sockaddr *) (np + 1);
2639 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
2641 if (saddr->sa_len > argp->ex_addrlen)
2642 saddr->sa_len = argp->ex_addrlen;
2643 if (argp->ex_masklen) {
2644 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
2645 error = copyin(argp->ex_mask, (caddr_t)smask, argp->ex_masklen);
2648 if (smask->sa_len > argp->ex_masklen)
2649 smask->sa_len = argp->ex_masklen;
2651 i = saddr->sa_family;
2652 if ((rnh = nep->ne_rtable[i]) == 0) {
2654 * Seems silly to initialize every AF when most are not used,
2655 * do so on demand here
2657 for (dom = domains; dom; dom = dom->dom_next)
2658 if (dom->dom_family == i && dom->dom_rtattach) {
2659 dom->dom_rtattach((void **) &nep->ne_rtable[i],
2663 if ((rnh = nep->ne_rtable[i]) == 0) {
2668 rn = (*rnh->rnh_addaddr) ((caddr_t) saddr, (caddr_t) smask, rnh,
2670 if (rn == 0 || np != (struct netcred *) rn) { /* already exists */
2674 np->netc_exflags = argp->ex_flags;
2675 np->netc_anon = argp->ex_anon;
2676 np->netc_anon.cr_ref = 1;
2679 free(np, M_NETADDR);
2685 vfs_free_netcred(struct radix_node *rn, void *w)
2687 struct radix_node_head *rnh = (struct radix_node_head *) w;
2689 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
2690 free((caddr_t) rn, M_NETADDR);
2695 * Free the net address hash lists that are hanging off the mount points.
2698 vfs_free_addrlist(struct netexport *nep)
2701 struct radix_node_head *rnh;
2703 for (i = 0; i <= AF_MAX; i++)
2704 if ((rnh = nep->ne_rtable[i])) {
2705 (*rnh->rnh_walktree) (rnh, vfs_free_netcred,
2707 free((caddr_t) rnh, M_RTABLE);
2708 nep->ne_rtable[i] = 0;
2713 vfs_export(struct mount *mp, struct netexport *nep, struct export_args *argp)
2717 if (argp->ex_flags & MNT_DELEXPORT) {
2718 if (mp->mnt_flag & MNT_EXPUBLIC) {
2719 vfs_setpublicfs(NULL, NULL, NULL);
2720 mp->mnt_flag &= ~MNT_EXPUBLIC;
2722 vfs_free_addrlist(nep);
2723 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
2725 if (argp->ex_flags & MNT_EXPORTED) {
2726 if (argp->ex_flags & MNT_EXPUBLIC) {
2727 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
2729 mp->mnt_flag |= MNT_EXPUBLIC;
2731 if ((error = vfs_hang_addrlist(mp, nep, argp)))
2733 mp->mnt_flag |= MNT_EXPORTED;
2740 * Set the publicly exported filesystem (WebNFS). Currently, only
2741 * one public filesystem is possible in the spec (RFC 2054 and 2055)
2744 vfs_setpublicfs(struct mount *mp, struct netexport *nep,
2745 struct export_args *argp)
2752 * mp == NULL -> invalidate the current info, the FS is
2753 * no longer exported. May be called from either vfs_export
2754 * or unmount, so check if it hasn't already been done.
2757 if (nfs_pub.np_valid) {
2758 nfs_pub.np_valid = 0;
2759 if (nfs_pub.np_index != NULL) {
2760 FREE(nfs_pub.np_index, M_TEMP);
2761 nfs_pub.np_index = NULL;
2768 * Only one allowed at a time.
2770 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
2774 * Get real filehandle for root of exported FS.
2776 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
2777 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
2779 if ((error = VFS_ROOT(mp, &rvp)))
2782 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
2788 * If an indexfile was specified, pull it in.
2790 if (argp->ex_indexfile != NULL) {
2791 MALLOC(nfs_pub.np_index, char *, MAXNAMLEN + 1, M_TEMP,
2793 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
2794 MAXNAMLEN, (size_t *)0);
2797 * Check for illegal filenames.
2799 for (cp = nfs_pub.np_index; *cp; cp++) {
2807 FREE(nfs_pub.np_index, M_TEMP);
2812 nfs_pub.np_mount = mp;
2813 nfs_pub.np_valid = 1;
2818 vfs_export_lookup(struct mount *mp, struct netexport *nep,
2819 struct sockaddr *nam)
2822 struct radix_node_head *rnh;
2823 struct sockaddr *saddr;
2826 if (mp->mnt_flag & MNT_EXPORTED) {
2828 * Lookup in the export list first.
2832 rnh = nep->ne_rtable[saddr->sa_family];
2834 np = (struct netcred *)
2835 (*rnh->rnh_matchaddr)((caddr_t)saddr,
2837 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
2842 * If no address match, use the default if it exists.
2844 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
2845 np = &nep->ne_defexported;
2851 * perform msync on all vnodes under a mount point. The mount point must
2852 * be locked. This code is also responsible for lazy-freeing unreferenced
2853 * vnodes whos VM objects no longer contain pages.
2855 * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state.
2857 static int vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data);
2858 static int vfs_msync_scan2(struct mount *mp, struct vnode *vp,
2859 lwkt_tokref_t vlock, void *data);
2862 vfs_msync(struct mount *mp, int flags)
2864 vmntvnodescan(mp, vfs_msync_scan1, vfs_msync_scan2, (void *)flags);
2868 * scan1 is a fast pre-check. There could be hundreds of thousands of
2869 * vnodes, we cannot afford to do anything heavy weight until we have a
2870 * fairly good indication that there is work to do.
2874 vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data)
2876 int flags = (int)data;
2878 if ((vp->v_flag & VXLOCK) == 0) {
2879 if (VSHOULDFREE(vp))
2881 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
2882 (vp->v_flag & VOBJDIRTY) &&
2883 (flags == MNT_WAIT || VOP_ISLOCKED(vp, NULL) == 0)) {
2892 vfs_msync_scan2(struct mount *mp, struct vnode *vp,
2893 lwkt_tokref_t vlock, void *data)
2897 int flags = (int)data;
2899 if (vp->v_flag & VXLOCK)
2902 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
2903 (vp->v_flag & VOBJDIRTY) &&
2904 (flags == MNT_WAIT || VOP_ISLOCKED(vp, NULL) == 0)) {
2905 error = vget(vp, vlock, LK_EXCLUSIVE | LK_RETRY | LK_NOOBJ | LK_INTERLOCK, curthread);
2907 if (VOP_GETVOBJECT(vp, &obj) == 0) {
2908 vm_object_page_clean(obj, 0, 0,
2909 flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
2916 lwkt_reltoken(vlock);
2921 * Create the VM object needed for VMIO and mmap support. This
2922 * is done for all VREG files in the system. Some filesystems might
2923 * afford the additional metadata buffering capability of the
2924 * VMIO code by making the device node be VMIO mode also.
2926 * vp must be locked when vfs_object_create is called.
2929 vfs_object_create(struct vnode *vp, struct thread *td)
2931 return (VOP_CREATEVOBJECT(vp, td));
2935 * NOTE: the vnode interlock must be held during the call. We have to recheck
2936 * the VFREE flag since the vnode may have been removed from the free list
2937 * while we were blocked on vnode_free_list_token. The use or hold count
2938 * must have already been bumped by the caller.
2941 vbusy(struct vnode *vp)
2945 lwkt_gettoken(&ilock, &vnode_free_list_token);
2946 if ((vp->v_flag & VFREE) != 0) {
2947 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
2949 vp->v_flag &= ~(VFREE|VAGE);
2951 lwkt_reltoken(&ilock);
2955 * NOTE: the vnode interlock must be held during the call. The use or hold
2956 * count must have already been bumped by the caller. We use a VINFREE to
2957 * interlock against other calls to vfree() which might occur while we
2958 * are blocked. The vnode cannot be reused until it has actually been
2959 * placed on the free list, so there are no other races even though the
2960 * use and hold counts are 0.
2963 vfree(struct vnode *vp)
2967 if ((vp->v_flag & VINFREE) == 0) {
2968 vp->v_flag |= VINFREE;
2969 lwkt_gettoken(&ilock, &vnode_free_list_token); /* can block */
2970 KASSERT((vp->v_flag & VFREE) == 0, ("vnode already free"));
2971 if (vp->v_flag & VAGE) {
2972 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2974 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
2977 vp->v_flag &= ~(VAGE|VINFREE);
2978 vp->v_flag |= VFREE;
2979 lwkt_reltoken(&ilock); /* can block */
2985 * Record a process's interest in events which might happen to
2986 * a vnode. Because poll uses the historic select-style interface
2987 * internally, this routine serves as both the ``check for any
2988 * pending events'' and the ``record my interest in future events''
2989 * functions. (These are done together, while the lock is held,
2990 * to avoid race conditions.)
2993 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
2997 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
2998 if (vp->v_pollinfo.vpi_revents & events) {
3000 * This leaves events we are not interested
3001 * in available for the other process which
3002 * which presumably had requested them
3003 * (otherwise they would never have been
3006 events &= vp->v_pollinfo.vpi_revents;
3007 vp->v_pollinfo.vpi_revents &= ~events;
3009 lwkt_reltoken(&ilock);
3012 vp->v_pollinfo.vpi_events |= events;
3013 selrecord(td, &vp->v_pollinfo.vpi_selinfo);
3014 lwkt_reltoken(&ilock);
3019 * Note the occurrence of an event. If the VN_POLLEVENT macro is used,
3020 * it is possible for us to miss an event due to race conditions, but
3021 * that condition is expected to be rare, so for the moment it is the
3022 * preferred interface.
3025 vn_pollevent(struct vnode *vp, int events)
3029 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
3030 if (vp->v_pollinfo.vpi_events & events) {
3032 * We clear vpi_events so that we don't
3033 * call selwakeup() twice if two events are
3034 * posted before the polling process(es) is
3035 * awakened. This also ensures that we take at
3036 * most one selwakeup() if the polling process
3037 * is no longer interested. However, it does
3038 * mean that only one event can be noticed at
3039 * a time. (Perhaps we should only clear those
3040 * event bits which we note?) XXX
3042 vp->v_pollinfo.vpi_events = 0; /* &= ~events ??? */
3043 vp->v_pollinfo.vpi_revents |= events;
3044 selwakeup(&vp->v_pollinfo.vpi_selinfo);
3046 lwkt_reltoken(&ilock);
3050 * Wake up anyone polling on vp because it is being revoked.
3051 * This depends on dead_poll() returning POLLHUP for correct
3055 vn_pollgone(struct vnode *vp)
3059 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
3060 if (vp->v_pollinfo.vpi_events) {
3061 vp->v_pollinfo.vpi_events = 0;
3062 selwakeup(&vp->v_pollinfo.vpi_selinfo);
3064 lwkt_reltoken(&ilock);
3070 * Routine to create and manage a filesystem syncer vnode.
3072 #define sync_close ((int (*) (struct vop_close_args *))nullop)
3073 static int sync_fsync (struct vop_fsync_args *);
3074 static int sync_inactive (struct vop_inactive_args *);
3075 static int sync_reclaim (struct vop_reclaim_args *);
3076 #define sync_lock ((int (*) (struct vop_lock_args *))vop_stdlock)
3077 #define sync_unlock ((int (*) (struct vop_unlock_args *))vop_stdunlock)
3078 static int sync_print (struct vop_print_args *);
3079 #define sync_islocked ((int(*) (struct vop_islocked_args *))vop_stdislocked)
3081 static struct vop_ops *sync_vnode_vops;
3082 static struct vnodeopv_entry_desc sync_vnodeop_entries[] = {
3083 { &vop_default_desc, vop_eopnotsupp },
3084 { &vop_close_desc, (void *) sync_close }, /* close */
3085 { &vop_fsync_desc, (void *) sync_fsync }, /* fsync */
3086 { &vop_inactive_desc, (void *) sync_inactive }, /* inactive */
3087 { &vop_reclaim_desc, (void *) sync_reclaim }, /* reclaim */
3088 { &vop_lock_desc, (void *) sync_lock }, /* lock */
3089 { &vop_unlock_desc, (void *) sync_unlock }, /* unlock */
3090 { &vop_print_desc, (void *) sync_print }, /* print */
3091 { &vop_islocked_desc, (void *) sync_islocked }, /* islocked */
3095 static struct vnodeopv_desc sync_vnodeop_opv_desc =
3096 { &sync_vnode_vops, sync_vnodeop_entries };
3098 VNODEOP_SET(sync_vnodeop_opv_desc);
3101 * Create a new filesystem syncer vnode for the specified mount point.
3102 * This vnode is placed on the worklist and is responsible for sync'ing
3105 * NOTE: read-only mounts are also placed on the worklist. The filesystem
3106 * sync code is also responsible for cleaning up vnodes.
3109 vfs_allocate_syncvnode(struct mount *mp)
3112 static long start, incr, next;
3115 /* Allocate a new vnode */
3116 error = getnewvnode(VT_VFS, mp, sync_vnode_vops, &vp, 0, 0);
3118 mp->mnt_syncer = NULL;
3123 * Place the vnode onto the syncer worklist. We attempt to
3124 * scatter them about on the list so that they will go off
3125 * at evenly distributed times even if all the filesystems
3126 * are mounted at once.
3129 if (next == 0 || next > syncer_maxdelay) {
3133 start = syncer_maxdelay / 2;
3134 incr = syncer_maxdelay;
3138 vn_syncer_add_to_worklist(vp, syncdelay > 0 ? next % syncdelay : 0);
3139 mp->mnt_syncer = vp;
3144 * Do a lazy sync of the filesystem.
3146 * sync_fsync { struct vnode *a_vp, struct ucred *a_cred, int a_waitfor,
3147 * struct thread *a_td }
3150 sync_fsync(struct vop_fsync_args *ap)
3152 struct vnode *syncvp = ap->a_vp;
3153 struct mount *mp = syncvp->v_mount;
3154 struct thread *td = ap->a_td;
3159 * We only need to do something if this is a lazy evaluation.
3161 if (ap->a_waitfor != MNT_LAZY)
3165 * Move ourselves to the back of the sync list.
3167 vn_syncer_add_to_worklist(syncvp, syncdelay);
3170 * Walk the list of vnodes pushing all that are dirty and
3171 * not already on the sync list, and freeing vnodes which have
3172 * no refs and whos VM objects are empty. vfs_msync() handles
3173 * the VM issues and must be called whether the mount is readonly
3176 lwkt_gettoken(&ilock, &mountlist_token);
3177 if (vfs_busy(mp, LK_EXCLUSIVE | LK_NOWAIT, &ilock, td) != 0) {
3178 lwkt_reltoken(&ilock);
3181 if (mp->mnt_flag & MNT_RDONLY) {
3182 vfs_msync(mp, MNT_NOWAIT);
3184 asyncflag = mp->mnt_flag & MNT_ASYNC;
3185 mp->mnt_flag &= ~MNT_ASYNC; /* ZZZ hack */
3186 vfs_msync(mp, MNT_NOWAIT);
3187 VFS_SYNC(mp, MNT_LAZY, td);
3189 mp->mnt_flag |= MNT_ASYNC;
3196 * The syncer vnode is no referenced.
3198 * sync_inactive { struct vnode *a_vp, struct proc *a_p }
3201 sync_inactive(struct vop_inactive_args *ap)
3203 VOP_UNLOCK(ap->a_vp, NULL, 0, ap->a_td);
3209 * The syncer vnode is no longer needed and is being decommissioned.
3211 * Modifications to the worklist must be protected at splbio().
3213 * sync_reclaim { struct vnode *a_vp }
3216 sync_reclaim(struct vop_reclaim_args *ap)
3218 struct vnode *vp = ap->a_vp;
3222 vp->v_mount->mnt_syncer = NULL;
3223 if (vp->v_flag & VONWORKLST) {
3224 LIST_REMOVE(vp, v_synclist);
3225 vp->v_flag &= ~VONWORKLST;
3233 * Print out a syncer vnode.
3235 * sync_print { struct vnode *a_vp }
3238 sync_print(struct vop_print_args *ap)
3240 struct vnode *vp = ap->a_vp;
3242 printf("syncer vnode");
3243 lockmgr_printinfo(&vp->v_lock);
3249 * extract the dev_t from a VBLK or VCHR. The vnode must have been opened
3250 * (or v_rdev might be NULL).
3253 vn_todev(struct vnode *vp)
3255 if (vp->v_type != VBLK && vp->v_type != VCHR)
3257 KKASSERT(vp->v_rdev != NULL);
3258 return (vp->v_rdev);
3262 * Check if vnode represents a disk device. The vnode does not need to be
3266 vn_isdisk(struct vnode *vp, int *errp)
3270 if (vp->v_type != VBLK && vp->v_type != VCHR) {
3276 if ((dev = vp->v_rdev) == NULL)
3277 dev = udev2dev(vp->v_udev, (vp->v_type == VBLK));
3278 if (dev == NULL || dev == NODEV) {
3283 if (dev_is_good(dev) == 0) {
3288 if ((dev_dflags(dev) & D_DISK) == 0) {
3299 NDFREE(struct nameidata *ndp, const uint flags)
3301 if (!(flags & NDF_NO_FREE_PNBUF) &&
3302 (ndp->ni_cnd.cn_flags & CNP_HASBUF)) {
3303 zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
3304 ndp->ni_cnd.cn_flags &= ~CNP_HASBUF;
3306 if (!(flags & NDF_NO_DNCP_RELE) &&
3307 (ndp->ni_cnd.cn_flags & CNP_WANTDNCP) &&
3309 cache_drop(ndp->ni_dncp);
3310 ndp->ni_dncp = NULL;
3312 if (!(flags & NDF_NO_NCP_RELE) &&
3313 (ndp->ni_cnd.cn_flags & CNP_WANTNCP) &&
3315 cache_drop(ndp->ni_ncp);
3318 if (!(flags & NDF_NO_DVP_UNLOCK) &&
3319 (ndp->ni_cnd.cn_flags & CNP_LOCKPARENT) &&
3320 ndp->ni_dvp != ndp->ni_vp) {
3321 VOP_UNLOCK(ndp->ni_dvp, NULL, 0, ndp->ni_cnd.cn_td);
3323 if (!(flags & NDF_NO_DVP_RELE) &&
3324 (ndp->ni_cnd.cn_flags & (CNP_LOCKPARENT|CNP_WANTPARENT))) {
3328 if (!(flags & NDF_NO_VP_UNLOCK) &&
3329 (ndp->ni_cnd.cn_flags & CNP_LOCKLEAF) && ndp->ni_vp) {
3330 VOP_UNLOCK(ndp->ni_vp, NULL, 0, ndp->ni_cnd.cn_td);
3332 if (!(flags & NDF_NO_VP_RELE) &&
3337 if (!(flags & NDF_NO_STARTDIR_RELE) &&
3338 (ndp->ni_cnd.cn_flags & CNP_SAVESTART)) {
3339 vrele(ndp->ni_startdir);
3340 ndp->ni_startdir = NULL;
3344 #ifdef DEBUG_VFS_LOCKS
3347 assert_vop_locked(struct vnode *vp, const char *str)
3349 if (vp && IS_LOCKING_VFS(vp) && !VOP_ISLOCKED(vp, NULL)) {
3350 panic("%s: %p is not locked shared but should be", str, vp);
3355 assert_vop_unlocked(struct vnode *vp, const char *str)
3357 if (vp && IS_LOCKING_VFS(vp)) {
3358 if (VOP_ISLOCKED(vp, curthread) == LK_EXCLUSIVE) {
3359 panic("%s: %p is locked but should not be", str, vp);