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 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95
38 * External virtual filesystem routines
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
48 #include <sys/systm.h>
52 #include <sys/dirent.h>
53 #include <sys/event.h>
54 #include <sys/eventhandler.h>
55 #include <sys/extattr.h>
57 #include <sys/fcntl.h>
59 #include <sys/kernel.h>
60 #include <sys/kthread.h>
61 #include <sys/malloc.h>
62 #include <sys/mount.h>
63 #include <sys/namei.h>
65 #include <sys/reboot.h>
66 #include <sys/sleepqueue.h>
68 #include <sys/sysctl.h>
69 #include <sys/syslog.h>
70 #include <sys/vmmeter.h>
71 #include <sys/vnode.h>
73 #include <machine/stdarg.h>
75 #include <security/mac/mac_framework.h>
78 #include <vm/vm_object.h>
79 #include <vm/vm_extern.h>
81 #include <vm/vm_map.h>
82 #include <vm/vm_page.h>
83 #include <vm/vm_kern.h>
90 static MALLOC_DEFINE(M_NETADDR, "subr_export_host", "Export host address structure");
92 static void delmntque(struct vnode *vp);
93 static void insmntque(struct vnode *vp, struct mount *mp);
94 static int flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo,
95 int slpflag, int slptimeo);
96 static void syncer_shutdown(void *arg, int howto);
97 static int vtryrecycle(struct vnode *vp);
98 static void vbusy(struct vnode *vp);
99 static void vdropl(struct vnode *vp);
100 static void vinactive(struct vnode *, struct thread *);
101 static void v_incr_usecount(struct vnode *);
102 static void v_decr_usecount(struct vnode *);
103 static void v_decr_useonly(struct vnode *);
104 static void v_upgrade_usecount(struct vnode *);
105 static void vfree(struct vnode *);
106 static void vnlru_free(int);
107 static void vdestroy(struct vnode *);
108 static void vgonel(struct vnode *);
109 static void vfs_knllock(void *arg);
110 static void vfs_knlunlock(void *arg);
111 static int vfs_knllocked(void *arg);
115 * Enable Giant pushdown based on whether or not the vm is mpsafe in this
116 * build. Without mpsafevm the buffer cache can not run Giant free.
118 #if !defined(__powerpc__)
123 TUNABLE_INT("debug.mpsafevfs", &mpsafe_vfs);
124 SYSCTL_INT(_debug, OID_AUTO, mpsafevfs, CTLFLAG_RD, &mpsafe_vfs, 0,
128 * Number of vnodes in existence. Increased whenever getnewvnode()
129 * allocates a new vnode, decreased on vdestroy() called on VI_DOOMed
132 static unsigned long numvnodes;
134 SYSCTL_LONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, "");
137 * Conversion tables for conversion from vnode types to inode formats
140 enum vtype iftovt_tab[16] = {
141 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
142 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
144 int vttoif_tab[10] = {
145 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
146 S_IFSOCK, S_IFIFO, S_IFMT, S_IFMT
150 * List of vnodes that are ready for recycling.
152 static TAILQ_HEAD(freelst, vnode) vnode_free_list;
155 * Free vnode target. Free vnodes may simply be files which have been stat'd
156 * but not read. This is somewhat common, and a small cache of such files
157 * should be kept to avoid recreation costs.
159 static u_long wantfreevnodes;
160 SYSCTL_LONG(_vfs, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, "");
161 /* Number of vnodes in the free list. */
162 static u_long freevnodes;
163 SYSCTL_LONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0, "");
166 * Various variables used for debugging the new implementation of
168 * XXX these are probably of (very) limited utility now.
170 static int reassignbufcalls;
171 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0, "");
174 * Cache for the mount type id assigned to NFS. This is used for
175 * special checks in nfs/nfs_nqlease.c and vm/vnode_pager.c.
177 int nfs_mount_type = -1;
179 /* To keep more than one thread at a time from running vfs_getnewfsid */
180 static struct mtx mntid_mtx;
183 * Lock for any access to the following:
188 static struct mtx vnode_free_list_mtx;
190 /* Publicly exported FS */
191 struct nfs_public nfs_pub;
193 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */
194 static uma_zone_t vnode_zone;
195 static uma_zone_t vnodepoll_zone;
197 /* Set to 1 to print out reclaim of active vnodes */
201 * The workitem queue.
203 * It is useful to delay writes of file data and filesystem metadata
204 * for tens of seconds so that quickly created and deleted files need
205 * not waste disk bandwidth being created and removed. To realize this,
206 * we append vnodes to a "workitem" queue. When running with a soft
207 * updates implementation, most pending metadata dependencies should
208 * not wait for more than a few seconds. Thus, mounted on block devices
209 * are delayed only about a half the time that file data is delayed.
210 * Similarly, directory updates are more critical, so are only delayed
211 * about a third the time that file data is delayed. Thus, there are
212 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
213 * one each second (driven off the filesystem syncer process). The
214 * syncer_delayno variable indicates the next queue that is to be processed.
215 * Items that need to be processed soon are placed in this queue:
217 * syncer_workitem_pending[syncer_delayno]
219 * A delay of fifteen seconds is done by placing the request fifteen
220 * entries later in the queue:
222 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
225 static int syncer_delayno;
226 static long syncer_mask;
227 LIST_HEAD(synclist, bufobj);
228 static struct synclist *syncer_workitem_pending;
230 * The sync_mtx protects:
235 * syncer_workitem_pending
236 * syncer_worklist_len
239 static struct mtx sync_mtx;
241 #define SYNCER_MAXDELAY 32
242 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
243 static int syncdelay = 30; /* max time to delay syncing data */
244 static int filedelay = 30; /* time to delay syncing files */
245 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0, "");
246 static int dirdelay = 29; /* time to delay syncing directories */
247 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0, "");
248 static int metadelay = 28; /* time to delay syncing metadata */
249 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0, "");
250 static int rushjob; /* number of slots to run ASAP */
251 static int stat_rush_requests; /* number of times I/O speeded up */
252 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0, "");
255 * When shutting down the syncer, run it at four times normal speed.
257 #define SYNCER_SHUTDOWN_SPEEDUP 4
258 static int sync_vnode_count;
259 static int syncer_worklist_len;
260 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY }
264 * Number of vnodes we want to exist at any one time. This is mostly used
265 * to size hash tables in vnode-related code. It is normally not used in
266 * getnewvnode(), as wantfreevnodes is normally nonzero.)
268 * XXX desiredvnodes is historical cruft and should not exist.
271 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
272 &desiredvnodes, 0, "Maximum number of vnodes");
273 SYSCTL_INT(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
274 &wantfreevnodes, 0, "Minimum number of vnodes (legacy)");
275 static int vnlru_nowhere;
276 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW,
277 &vnlru_nowhere, 0, "Number of times the vnlru process ran without success");
280 * Macros to control when a vnode is freed and recycled. All require
281 * the vnode interlock.
283 #define VCANRECYCLE(vp) (((vp)->v_iflag & VI_FREE) && !(vp)->v_holdcnt)
284 #define VSHOULDFREE(vp) (!((vp)->v_iflag & VI_FREE) && !(vp)->v_holdcnt)
285 #define VSHOULDBUSY(vp) (((vp)->v_iflag & VI_FREE) && (vp)->v_holdcnt)
289 * Initialize the vnode management data structures.
291 #ifndef MAXVNODES_MAX
292 #define MAXVNODES_MAX 100000
295 vntblinit(void *dummy __unused)
299 * Desiredvnodes is a function of the physical memory size and
300 * the kernel's heap size. Specifically, desiredvnodes scales
301 * in proportion to the physical memory size until two fifths
302 * of the kernel's heap size is consumed by vnodes and vm
305 desiredvnodes = min(maxproc + cnt.v_page_count / 4, 2 * vm_kmem_size /
306 (5 * (sizeof(struct vm_object) + sizeof(struct vnode))));
307 if (desiredvnodes > MAXVNODES_MAX) {
309 printf("Reducing kern.maxvnodes %d -> %d\n",
310 desiredvnodes, MAXVNODES_MAX);
311 desiredvnodes = MAXVNODES_MAX;
313 wantfreevnodes = desiredvnodes / 4;
314 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF);
315 TAILQ_INIT(&vnode_free_list);
316 mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF);
317 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL,
318 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
319 vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo),
320 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
322 * Initialize the filesystem syncer.
324 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE,
326 syncer_maxdelay = syncer_mask + 1;
327 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF);
329 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL)
333 * Mark a mount point as busy. Used to synchronize access and to delay
334 * unmounting. Interlock is not released on failure.
337 vfs_busy(struct mount *mp, int flags, struct mtx *interlkp,
344 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
345 if (flags & LK_NOWAIT) {
351 mtx_unlock(interlkp);
352 mp->mnt_kern_flag |= MNTK_MWAIT;
354 * Since all busy locks are shared except the exclusive
355 * lock granted when unmounting, the only place that a
356 * wakeup needs to be done is at the release of the
357 * exclusive lock at the end of dounmount.
359 msleep(mp, MNT_MTX(mp), PVFS, "vfs_busy", 0);
367 mtx_unlock(interlkp);
368 lkflags = LK_SHARED | LK_INTERLOCK;
369 if (lockmgr(&mp->mnt_lock, lkflags, MNT_MTX(mp), td))
370 panic("vfs_busy: unexpected lock failure");
375 * Free a busy filesystem.
378 vfs_unbusy(struct mount *mp, struct thread *td)
381 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td);
386 * Lookup a mount point by filesystem identifier.
389 vfs_getvfs(fsid_t *fsid)
393 mtx_lock(&mountlist_mtx);
394 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
395 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
396 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
398 mtx_unlock(&mountlist_mtx);
402 mtx_unlock(&mountlist_mtx);
403 return ((struct mount *) 0);
407 * Check if a user can access priveledged mount options.
410 vfs_suser(struct mount *mp, struct thread *td)
414 if ((mp->mnt_flag & MNT_USER) == 0 ||
415 mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
416 if ((error = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 0)
423 * Get a new unique fsid. Try to make its val[0] unique, since this value
424 * will be used to create fake device numbers for stat(). Also try (but
425 * not so hard) make its val[0] unique mod 2^16, since some emulators only
426 * support 16-bit device numbers. We end up with unique val[0]'s for the
427 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
429 * Keep in mind that several mounts may be running in parallel. Starting
430 * the search one past where the previous search terminated is both a
431 * micro-optimization and a defense against returning the same fsid to
435 vfs_getnewfsid(struct mount *mp)
437 static u_int16_t mntid_base;
442 mtx_lock(&mntid_mtx);
443 mtype = mp->mnt_vfc->vfc_typenum;
444 tfsid.val[1] = mtype;
445 mtype = (mtype & 0xFF) << 24;
447 tfsid.val[0] = makedev(255,
448 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
450 if ((nmp = vfs_getvfs(&tfsid)) == NULL)
454 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
455 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
456 mtx_unlock(&mntid_mtx);
460 * Knob to control the precision of file timestamps:
462 * 0 = seconds only; nanoseconds zeroed.
463 * 1 = seconds and nanoseconds, accurate within 1/HZ.
464 * 2 = seconds and nanoseconds, truncated to microseconds.
465 * >=3 = seconds and nanoseconds, maximum precision.
467 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
469 static int timestamp_precision = TSP_SEC;
470 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
471 ×tamp_precision, 0, "");
474 * Get a current timestamp.
477 vfs_timestamp(struct timespec *tsp)
481 switch (timestamp_precision) {
483 tsp->tv_sec = time_second;
491 TIMEVAL_TO_TIMESPEC(&tv, tsp);
501 * Set vnode attributes to VNOVAL
504 vattr_null(struct vattr *vap)
508 vap->va_size = VNOVAL;
509 vap->va_bytes = VNOVAL;
510 vap->va_mode = VNOVAL;
511 vap->va_nlink = VNOVAL;
512 vap->va_uid = VNOVAL;
513 vap->va_gid = VNOVAL;
514 vap->va_fsid = VNOVAL;
515 vap->va_fileid = VNOVAL;
516 vap->va_blocksize = VNOVAL;
517 vap->va_rdev = VNOVAL;
518 vap->va_atime.tv_sec = VNOVAL;
519 vap->va_atime.tv_nsec = VNOVAL;
520 vap->va_mtime.tv_sec = VNOVAL;
521 vap->va_mtime.tv_nsec = VNOVAL;
522 vap->va_ctime.tv_sec = VNOVAL;
523 vap->va_ctime.tv_nsec = VNOVAL;
524 vap->va_birthtime.tv_sec = VNOVAL;
525 vap->va_birthtime.tv_nsec = VNOVAL;
526 vap->va_flags = VNOVAL;
527 vap->va_gen = VNOVAL;
532 * This routine is called when we have too many vnodes. It attempts
533 * to free <count> vnodes and will potentially free vnodes that still
534 * have VM backing store (VM backing store is typically the cause
535 * of a vnode blowout so we want to do this). Therefore, this operation
536 * is not considered cheap.
538 * A number of conditions may prevent a vnode from being reclaimed.
539 * the buffer cache may have references on the vnode, a directory
540 * vnode may still have references due to the namei cache representing
541 * underlying files, or the vnode may be in active use. It is not
542 * desireable to reuse such vnodes. These conditions may cause the
543 * number of vnodes to reach some minimum value regardless of what
544 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
547 vlrureclaim(struct mount *mp)
557 * Calculate the trigger point, don't allow user
558 * screwups to blow us up. This prevents us from
559 * recycling vnodes with lots of resident pages. We
560 * aren't trying to free memory, we are trying to
563 usevnodes = desiredvnodes;
566 trigger = cnt.v_page_count * 2 / usevnodes;
569 vn_start_write(NULL, &mp, V_WAIT);
571 count = mp->mnt_nvnodelistsize / 10 + 1;
573 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
574 while (vp != NULL && vp->v_type == VMARKER)
575 vp = TAILQ_NEXT(vp, v_nmntvnodes);
578 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
579 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
584 * If it's been deconstructed already, it's still
585 * referenced, or it exceeds the trigger, skip it.
587 if (vp->v_usecount || !LIST_EMPTY(&(vp)->v_cache_src) ||
588 (vp->v_iflag & VI_DOOMED) != 0 || (vp->v_object != NULL &&
589 vp->v_object->resident_page_count > trigger)) {
595 if (VOP_LOCK(vp, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT, td)) {
597 goto next_iter_mntunlocked;
601 * v_usecount may have been bumped after VOP_LOCK() dropped
602 * the vnode interlock and before it was locked again.
604 * It is not necessary to recheck VI_DOOMED because it can
605 * only be set by another thread that holds both the vnode
606 * lock and vnode interlock. If another thread has the
607 * vnode lock before we get to VOP_LOCK() and obtains the
608 * vnode interlock after VOP_LOCK() drops the vnode
609 * interlock, the other thread will be unable to drop the
610 * vnode lock before our VOP_LOCK() call fails.
612 if (vp->v_usecount || !LIST_EMPTY(&(vp)->v_cache_src) ||
613 (vp->v_object != NULL &&
614 vp->v_object->resident_page_count > trigger)) {
615 VOP_UNLOCK(vp, LK_INTERLOCK, td);
616 goto next_iter_mntunlocked;
618 KASSERT((vp->v_iflag & VI_DOOMED) == 0,
619 ("VI_DOOMED unexpectedly detected in vlrureclaim()"));
621 VOP_UNLOCK(vp, 0, td);
624 next_iter_mntunlocked:
625 if ((count % 256) != 0)
629 if ((count % 256) != 0)
638 vn_finished_write(mp);
643 * Attempt to keep the free list at wantfreevnodes length.
646 vnlru_free(int count)
651 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
652 for (; count > 0; count--) {
653 vp = TAILQ_FIRST(&vnode_free_list);
655 * The list can be modified while the free_list_mtx
656 * has been dropped and vp could be NULL here.
660 VNASSERT(vp->v_op != NULL, vp,
661 ("vnlru_free: vnode already reclaimed."));
662 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
664 * Don't recycle if we can't get the interlock.
666 if (!VI_TRYLOCK(vp)) {
667 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
670 VNASSERT(VCANRECYCLE(vp), vp,
671 ("vp inconsistent on freelist"));
673 vp->v_iflag &= ~VI_FREE;
675 mtx_unlock(&vnode_free_list_mtx);
677 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
679 VFS_UNLOCK_GIANT(vfslocked);
681 * If the recycled succeeded this vdrop will actually free
682 * the vnode. If not it will simply place it back on
686 mtx_lock(&vnode_free_list_mtx);
690 * Attempt to recycle vnodes in a context that is always safe to block.
691 * Calling vlrurecycle() from the bowels of filesystem code has some
692 * interesting deadlock problems.
694 static struct proc *vnlruproc;
695 static int vnlruproc_sig;
700 struct mount *mp, *nmp;
702 struct proc *p = vnlruproc;
703 struct thread *td = FIRST_THREAD_IN_PROC(p);
707 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, p,
711 kthread_suspend_check(p);
712 mtx_lock(&vnode_free_list_mtx);
713 if (freevnodes > wantfreevnodes)
714 vnlru_free(freevnodes - wantfreevnodes);
715 if (numvnodes <= desiredvnodes * 9 / 10) {
717 wakeup(&vnlruproc_sig);
718 msleep(vnlruproc, &vnode_free_list_mtx,
719 PVFS|PDROP, "vlruwt", hz);
722 mtx_unlock(&vnode_free_list_mtx);
724 mtx_lock(&mountlist_mtx);
725 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
727 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td)) {
728 nmp = TAILQ_NEXT(mp, mnt_list);
731 if (!VFS_NEEDSGIANT(mp)) {
736 done += vlrureclaim(mp);
739 mtx_lock(&mountlist_mtx);
740 nmp = TAILQ_NEXT(mp, mnt_list);
743 mtx_unlock(&mountlist_mtx);
746 /* These messages are temporary debugging aids */
747 if (vnlru_nowhere < 5)
748 printf("vnlru process getting nowhere..\n");
749 else if (vnlru_nowhere == 5)
750 printf("vnlru process messages stopped.\n");
753 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
759 static struct kproc_desc vnlru_kp = {
764 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &vnlru_kp)
767 * Routines having to do with the management of the vnode table.
771 vdestroy(struct vnode *vp)
775 CTR1(KTR_VFS, "vdestroy vp %p", vp);
776 mtx_lock(&vnode_free_list_mtx);
778 mtx_unlock(&vnode_free_list_mtx);
780 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
781 ("cleaned vnode still on the free list."));
782 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't"));
783 VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count"));
784 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count"));
785 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count"));
786 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's"));
787 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0"));
788 VNASSERT(bo->bo_clean.bv_root == NULL, vp, ("cleanblkroot not NULL"));
789 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0"));
790 VNASSERT(bo->bo_dirty.bv_root == NULL, vp, ("dirtyblkroot not NULL"));
791 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst"));
792 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src"));
795 mac_destroy_vnode(vp);
797 if (vp->v_pollinfo != NULL) {
798 knlist_destroy(&vp->v_pollinfo->vpi_selinfo.si_note);
799 mtx_destroy(&vp->v_pollinfo->vpi_lock);
800 uma_zfree(vnodepoll_zone, vp->v_pollinfo);
803 /* XXX Elsewhere we can detect an already freed vnode via NULL v_op. */
806 lockdestroy(vp->v_vnlock);
807 mtx_destroy(&vp->v_interlock);
808 uma_zfree(vnode_zone, vp);
812 * Try to recycle a freed vnode. We abort if anyone picks up a reference
813 * before we actually vgone(). This function must be called with the vnode
814 * held to prevent the vnode from being returned to the free list midway
818 vtryrecycle(struct vnode *vp)
820 struct thread *td = curthread;
823 CTR1(KTR_VFS, "vtryrecycle: trying vp %p", vp);
824 VNASSERT(vp->v_holdcnt, vp,
825 ("vtryrecycle: Recycling vp %p without a reference.", vp));
827 * This vnode may found and locked via some other list, if so we
828 * can't recycle it yet.
830 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT, td) != 0)
831 return (EWOULDBLOCK);
833 * Don't recycle if its filesystem is being suspended.
835 if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) {
836 VOP_UNLOCK(vp, 0, td);
840 * If we got this far, we need to acquire the interlock and see if
841 * anyone picked up this vnode from another list. If not, we will
842 * mark it with DOOMED via vgonel() so that anyone who does find it
846 if (vp->v_usecount) {
847 VOP_UNLOCK(vp, LK_INTERLOCK, td);
848 vn_finished_write(vnmp);
851 if ((vp->v_iflag & VI_DOOMED) == 0)
853 VOP_UNLOCK(vp, LK_INTERLOCK, td);
854 vn_finished_write(vnmp);
855 CTR1(KTR_VFS, "vtryrecycle: recycled vp %p", vp);
860 * Return the next vnode from the free list.
863 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops,
866 struct vnode *vp = NULL;
869 mtx_lock(&vnode_free_list_mtx);
871 * Lend our context to reclaim vnodes if they've exceeded the max.
873 if (freevnodes > wantfreevnodes)
876 * Wait for available vnodes.
878 if (numvnodes > desiredvnodes) {
879 if (mp != NULL && (mp->mnt_kern_flag & MNTK_SUSPEND)) {
881 * File system is beeing suspended, we cannot risk a
882 * deadlock here, so allocate new vnode anyway.
884 if (freevnodes > wantfreevnodes)
885 vnlru_free(freevnodes - wantfreevnodes);
888 if (vnlruproc_sig == 0) {
889 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
892 msleep(&vnlruproc_sig, &vnode_free_list_mtx, PVFS,
894 #if 0 /* XXX Not all VFS_VGET/ffs_vget callers check returns. */
895 if (numvnodes > desiredvnodes) {
896 mtx_unlock(&vnode_free_list_mtx);
903 mtx_unlock(&vnode_free_list_mtx);
904 vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK|M_ZERO);
908 vp->v_vnlock = &vp->v_lock;
909 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF);
911 * By default, don't allow shared locks unless filesystems
914 lockinit(vp->v_vnlock, PVFS, tag, VLKTIMEOUT, LK_NOSHARE);
920 bo->bo_mtx = &vp->v_interlock;
921 bo->bo_ops = &buf_ops_bio;
923 TAILQ_INIT(&bo->bo_clean.bv_hd);
924 TAILQ_INIT(&bo->bo_dirty.bv_hd);
926 * Initialize namecache.
928 LIST_INIT(&vp->v_cache_src);
929 TAILQ_INIT(&vp->v_cache_dst);
931 * Finalize various vnode identity bits.
940 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0)
941 mac_associate_vnode_singlelabel(mp, vp);
943 printf("NULL mp in getnewvnode()\n");
947 bo->bo_bsize = mp->mnt_stat.f_iosize;
948 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0)
949 vp->v_vflag |= VV_NOKNOTE;
952 CTR2(KTR_VFS, "getnewvnode: mp %p vp %p", mp, vp);
958 * Delete from old mount point vnode list, if on one.
961 delmntque(struct vnode *vp)
970 VNASSERT(mp->mnt_nvnodelistsize > 0, vp,
971 ("bad mount point vnode list size"));
972 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
973 mp->mnt_nvnodelistsize--;
979 * Insert into list of vnodes for the new mount point, if available.
982 insmntque(struct vnode *vp, struct mount *mp)
986 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)"));
989 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
990 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp,
991 ("neg mount point vnode list size"));
992 mp->mnt_nvnodelistsize++;
997 * Flush out and invalidate all buffers associated with a bufobj
998 * Called with the underlying object locked.
1001 bufobj_invalbuf(struct bufobj *bo, int flags, struct thread *td, int slpflag,
1007 if (flags & V_SAVE) {
1008 error = bufobj_wwait(bo, slpflag, slptimeo);
1013 if (bo->bo_dirty.bv_cnt > 0) {
1015 if ((error = BO_SYNC(bo, MNT_WAIT, td)) != 0)
1018 * XXX We could save a lock/unlock if this was only
1019 * enabled under INVARIANTS
1022 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)
1023 panic("vinvalbuf: dirty bufs");
1027 * If you alter this loop please notice that interlock is dropped and
1028 * reacquired in flushbuflist. Special care is needed to ensure that
1029 * no race conditions occur from this.
1032 error = flushbuflist(&bo->bo_clean,
1033 flags, bo, slpflag, slptimeo);
1035 error = flushbuflist(&bo->bo_dirty,
1036 flags, bo, slpflag, slptimeo);
1037 if (error != 0 && error != EAGAIN) {
1041 } while (error != 0);
1044 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
1045 * have write I/O in-progress but if there is a VM object then the
1046 * VM object can also have read-I/O in-progress.
1049 bufobj_wwait(bo, 0, 0);
1051 if (bo->bo_object != NULL) {
1052 VM_OBJECT_LOCK(bo->bo_object);
1053 vm_object_pip_wait(bo->bo_object, "bovlbx");
1054 VM_OBJECT_UNLOCK(bo->bo_object);
1057 } while (bo->bo_numoutput > 0);
1061 * Destroy the copy in the VM cache, too.
1063 if (bo->bo_object != NULL) {
1064 VM_OBJECT_LOCK(bo->bo_object);
1065 vm_object_page_remove(bo->bo_object, 0, 0,
1066 (flags & V_SAVE) ? TRUE : FALSE);
1067 VM_OBJECT_UNLOCK(bo->bo_object);
1072 if ((flags & (V_ALT | V_NORMAL)) == 0 &&
1073 (bo->bo_dirty.bv_cnt > 0 || bo->bo_clean.bv_cnt > 0))
1074 panic("vinvalbuf: flush failed");
1081 * Flush out and invalidate all buffers associated with a vnode.
1082 * Called with the underlying object locked.
1085 vinvalbuf(struct vnode *vp, int flags, struct thread *td, int slpflag,
1089 CTR2(KTR_VFS, "vinvalbuf vp %p flags %d", vp, flags);
1090 ASSERT_VOP_LOCKED(vp, "vinvalbuf");
1091 return (bufobj_invalbuf(&vp->v_bufobj, flags, td, slpflag, slptimeo));
1095 * Flush out buffers on the specified list.
1099 flushbuflist( struct bufv *bufv, int flags, struct bufobj *bo, int slpflag,
1102 struct buf *bp, *nbp;
1107 ASSERT_BO_LOCKED(bo);
1110 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) {
1111 if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) ||
1112 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) {
1118 lblkno = nbp->b_lblkno;
1119 xflags = nbp->b_xflags &
1120 (BX_BKGRDMARKER | BX_VNDIRTY | BX_VNCLEAN);
1123 error = BUF_TIMELOCK(bp,
1124 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_MTX(bo),
1125 "flushbuf", slpflag, slptimeo);
1128 return (error != ENOLCK ? error : EAGAIN);
1130 KASSERT(bp->b_bufobj == bo,
1131 ("bp %p wrong b_bufobj %p should be %p",
1132 bp, bp->b_bufobj, bo));
1133 if (bp->b_bufobj != bo) { /* XXX: necessary ? */
1139 * XXX Since there are no node locks for NFS, I
1140 * believe there is a slight chance that a delayed
1141 * write will occur while sleeping just above, so
1144 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
1147 bp->b_flags |= B_ASYNC;
1150 return (EAGAIN); /* XXX: why not loop ? */
1153 bp->b_flags |= (B_INVAL | B_RELBUF);
1154 bp->b_flags &= ~B_ASYNC;
1158 (nbp->b_bufobj != bo ||
1159 nbp->b_lblkno != lblkno ||
1161 (BX_BKGRDMARKER | BX_VNDIRTY | BX_VNCLEAN)) != xflags))
1162 break; /* nbp invalid */
1168 * Truncate a file's buffer and pages to a specified length. This
1169 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
1173 vtruncbuf(struct vnode *vp, struct ucred *cred, struct thread *td,
1174 off_t length, int blksize)
1176 struct buf *bp, *nbp;
1181 CTR2(KTR_VFS, "vtruncbuf vp %p length %jd", vp, length);
1183 * Round up to the *next* lbn.
1185 trunclbn = (length + blksize - 1) / blksize;
1187 ASSERT_VOP_LOCKED(vp, "vtruncbuf");
1194 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) {
1195 if (bp->b_lblkno < trunclbn)
1198 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1199 VI_MTX(vp)) == ENOLCK)
1203 bp->b_flags |= (B_INVAL | B_RELBUF);
1204 bp->b_flags &= ~B_ASYNC;
1209 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
1210 (nbp->b_vp != vp) ||
1211 (nbp->b_flags & B_DELWRI))) {
1217 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1218 if (bp->b_lblkno < trunclbn)
1221 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1222 VI_MTX(vp)) == ENOLCK)
1225 bp->b_flags |= (B_INVAL | B_RELBUF);
1226 bp->b_flags &= ~B_ASYNC;
1230 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
1231 (nbp->b_vp != vp) ||
1232 (nbp->b_flags & B_DELWRI) == 0)) {
1241 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1242 if (bp->b_lblkno > 0)
1245 * Since we hold the vnode lock this should only
1246 * fail if we're racing with the buf daemon.
1249 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1250 VI_MTX(vp)) == ENOLCK) {
1253 VNASSERT((bp->b_flags & B_DELWRI), vp,
1254 ("buf(%p) on dirty queue without DELWRI", bp));
1263 bufobj_wwait(bo, 0, 0);
1265 vnode_pager_setsize(vp, length);
1271 * buf_splay() - splay tree core for the clean/dirty list of buffers in
1274 * NOTE: We have to deal with the special case of a background bitmap
1275 * buffer, a situation where two buffers will have the same logical
1276 * block offset. We want (1) only the foreground buffer to be accessed
1277 * in a lookup and (2) must differentiate between the foreground and
1278 * background buffer in the splay tree algorithm because the splay
1279 * tree cannot normally handle multiple entities with the same 'index'.
1280 * We accomplish this by adding differentiating flags to the splay tree's
1285 buf_splay(daddr_t lblkno, b_xflags_t xflags, struct buf *root)
1288 struct buf *lefttreemax, *righttreemin, *y;
1292 lefttreemax = righttreemin = &dummy;
1294 if (lblkno < root->b_lblkno ||
1295 (lblkno == root->b_lblkno &&
1296 (xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
1297 if ((y = root->b_left) == NULL)
1299 if (lblkno < y->b_lblkno) {
1301 root->b_left = y->b_right;
1304 if ((y = root->b_left) == NULL)
1307 /* Link into the new root's right tree. */
1308 righttreemin->b_left = root;
1309 righttreemin = root;
1310 } else if (lblkno > root->b_lblkno ||
1311 (lblkno == root->b_lblkno &&
1312 (xflags & BX_BKGRDMARKER) > (root->b_xflags & BX_BKGRDMARKER))) {
1313 if ((y = root->b_right) == NULL)
1315 if (lblkno > y->b_lblkno) {
1317 root->b_right = y->b_left;
1320 if ((y = root->b_right) == NULL)
1323 /* Link into the new root's left tree. */
1324 lefttreemax->b_right = root;
1331 /* Assemble the new root. */
1332 lefttreemax->b_right = root->b_left;
1333 righttreemin->b_left = root->b_right;
1334 root->b_left = dummy.b_right;
1335 root->b_right = dummy.b_left;
1340 buf_vlist_remove(struct buf *bp)
1345 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1346 ASSERT_BO_LOCKED(bp->b_bufobj);
1347 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) !=
1348 (BX_VNDIRTY|BX_VNCLEAN),
1349 ("buf_vlist_remove: Buf %p is on two lists", bp));
1350 if (bp->b_xflags & BX_VNDIRTY)
1351 bv = &bp->b_bufobj->bo_dirty;
1353 bv = &bp->b_bufobj->bo_clean;
1354 if (bp != bv->bv_root) {
1355 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root);
1356 KASSERT(root == bp, ("splay lookup failed in remove"));
1358 if (bp->b_left == NULL) {
1361 root = buf_splay(bp->b_lblkno, bp->b_xflags, bp->b_left);
1362 root->b_right = bp->b_right;
1365 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs);
1367 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1371 * Add the buffer to the sorted clean or dirty block list using a
1372 * splay tree algorithm.
1374 * NOTE: xflags is passed as a constant, optimizing this inline function!
1377 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags)
1382 ASSERT_BO_LOCKED(bo);
1383 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0,
1384 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags));
1385 bp->b_xflags |= xflags;
1386 if (xflags & BX_VNDIRTY)
1391 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root);
1395 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs);
1396 } else if (bp->b_lblkno < root->b_lblkno ||
1397 (bp->b_lblkno == root->b_lblkno &&
1398 (bp->b_xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
1399 bp->b_left = root->b_left;
1401 root->b_left = NULL;
1402 TAILQ_INSERT_BEFORE(root, bp, b_bobufs);
1404 bp->b_right = root->b_right;
1406 root->b_right = NULL;
1407 TAILQ_INSERT_AFTER(&bv->bv_hd, root, bp, b_bobufs);
1414 * Lookup a buffer using the splay tree. Note that we specifically avoid
1415 * shadow buffers used in background bitmap writes.
1417 * This code isn't quite efficient as it could be because we are maintaining
1418 * two sorted lists and do not know which list the block resides in.
1420 * During a "make buildworld" the desired buffer is found at one of
1421 * the roots more than 60% of the time. Thus, checking both roots
1422 * before performing either splay eliminates unnecessary splays on the
1423 * first tree splayed.
1426 gbincore(struct bufobj *bo, daddr_t lblkno)
1430 ASSERT_BO_LOCKED(bo);
1431 if ((bp = bo->bo_clean.bv_root) != NULL &&
1432 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1434 if ((bp = bo->bo_dirty.bv_root) != NULL &&
1435 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1437 if ((bp = bo->bo_clean.bv_root) != NULL) {
1438 bo->bo_clean.bv_root = bp = buf_splay(lblkno, 0, bp);
1439 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1442 if ((bp = bo->bo_dirty.bv_root) != NULL) {
1443 bo->bo_dirty.bv_root = bp = buf_splay(lblkno, 0, bp);
1444 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1451 * Associate a buffer with a vnode.
1454 bgetvp(struct vnode *vp, struct buf *bp)
1457 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free"));
1459 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags);
1460 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp,
1461 ("bgetvp: bp already attached! %p", bp));
1463 ASSERT_VI_LOCKED(vp, "bgetvp");
1465 if (VFS_NEEDSGIANT(vp->v_mount) ||
1466 vp->v_bufobj.bo_flag & BO_NEEDSGIANT)
1467 bp->b_flags |= B_NEEDSGIANT;
1469 bp->b_bufobj = &vp->v_bufobj;
1471 * Insert onto list for new vnode.
1473 buf_vlist_add(bp, &vp->v_bufobj, BX_VNCLEAN);
1477 * Disassociate a buffer from a vnode.
1480 brelvp(struct buf *bp)
1485 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1486 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1489 * Delete from old vnode list, if on one.
1491 vp = bp->b_vp; /* XXX */
1494 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1495 buf_vlist_remove(bp);
1497 panic("brelvp: Buffer %p not on queue.", bp);
1498 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
1499 bo->bo_flag &= ~BO_ONWORKLST;
1500 mtx_lock(&sync_mtx);
1501 LIST_REMOVE(bo, bo_synclist);
1502 syncer_worklist_len--;
1503 mtx_unlock(&sync_mtx);
1505 bp->b_flags &= ~B_NEEDSGIANT;
1507 bp->b_bufobj = NULL;
1512 * Add an item to the syncer work queue.
1515 vn_syncer_add_to_worklist(struct bufobj *bo, int delay)
1519 ASSERT_BO_LOCKED(bo);
1521 mtx_lock(&sync_mtx);
1522 if (bo->bo_flag & BO_ONWORKLST)
1523 LIST_REMOVE(bo, bo_synclist);
1525 bo->bo_flag |= BO_ONWORKLST;
1526 syncer_worklist_len++;
1529 if (delay > syncer_maxdelay - 2)
1530 delay = syncer_maxdelay - 2;
1531 slot = (syncer_delayno + delay) & syncer_mask;
1533 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], bo, bo_synclist);
1534 mtx_unlock(&sync_mtx);
1538 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS)
1542 mtx_lock(&sync_mtx);
1543 len = syncer_worklist_len - sync_vnode_count;
1544 mtx_unlock(&sync_mtx);
1545 error = SYSCTL_OUT(req, &len, sizeof(len));
1549 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, CTLTYPE_INT | CTLFLAG_RD, NULL, 0,
1550 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length");
1552 static struct proc *updateproc;
1553 static void sched_sync(void);
1554 static struct kproc_desc up_kp = {
1559 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp)
1562 sync_vnode(struct bufobj *bo, struct thread *td)
1567 vp = bo->__bo_vnode; /* XXX */
1568 if (VOP_ISLOCKED(vp, NULL) != 0)
1570 if (VI_TRYLOCK(vp) == 0)
1573 * We use vhold in case the vnode does not
1574 * successfully sync. vhold prevents the vnode from
1575 * going away when we unlock the sync_mtx so that
1576 * we can acquire the vnode interlock.
1579 mtx_unlock(&sync_mtx);
1581 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1583 mtx_lock(&sync_mtx);
1586 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1587 (void) VOP_FSYNC(vp, MNT_LAZY, td);
1588 VOP_UNLOCK(vp, 0, td);
1589 vn_finished_write(mp);
1591 if ((bo->bo_flag & BO_ONWORKLST) != 0) {
1593 * Put us back on the worklist. The worklist
1594 * routine will remove us from our current
1595 * position and then add us back in at a later
1598 vn_syncer_add_to_worklist(bo, syncdelay);
1601 mtx_lock(&sync_mtx);
1606 * System filesystem synchronizer daemon.
1611 struct synclist *next;
1612 struct synclist *slp;
1615 struct thread *td = FIRST_THREAD_IN_PROC(updateproc);
1616 static int dummychan;
1618 int net_worklist_len;
1619 int syncer_final_iter;
1625 syncer_final_iter = 0;
1627 syncer_state = SYNCER_RUNNING;
1628 starttime = time_uptime;
1629 td->td_pflags |= TDP_NORUNNINGBUF;
1631 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc,
1634 mtx_lock(&sync_mtx);
1636 if (syncer_state == SYNCER_FINAL_DELAY &&
1637 syncer_final_iter == 0) {
1638 mtx_unlock(&sync_mtx);
1639 kthread_suspend_check(td->td_proc);
1640 mtx_lock(&sync_mtx);
1642 net_worklist_len = syncer_worklist_len - sync_vnode_count;
1643 if (syncer_state != SYNCER_RUNNING &&
1644 starttime != time_uptime) {
1646 printf("\nSyncing disks, vnodes remaining...");
1649 printf("%d ", net_worklist_len);
1651 starttime = time_uptime;
1654 * Push files whose dirty time has expired. Be careful
1655 * of interrupt race on slp queue.
1657 * Skip over empty worklist slots when shutting down.
1660 slp = &syncer_workitem_pending[syncer_delayno];
1661 syncer_delayno += 1;
1662 if (syncer_delayno == syncer_maxdelay)
1664 next = &syncer_workitem_pending[syncer_delayno];
1666 * If the worklist has wrapped since the
1667 * it was emptied of all but syncer vnodes,
1668 * switch to the FINAL_DELAY state and run
1669 * for one more second.
1671 if (syncer_state == SYNCER_SHUTTING_DOWN &&
1672 net_worklist_len == 0 &&
1673 last_work_seen == syncer_delayno) {
1674 syncer_state = SYNCER_FINAL_DELAY;
1675 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP;
1677 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) &&
1678 syncer_worklist_len > 0);
1681 * Keep track of the last time there was anything
1682 * on the worklist other than syncer vnodes.
1683 * Return to the SHUTTING_DOWN state if any
1686 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING)
1687 last_work_seen = syncer_delayno;
1688 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY)
1689 syncer_state = SYNCER_SHUTTING_DOWN;
1690 while ((bo = LIST_FIRST(slp)) != NULL) {
1691 error = sync_vnode(bo, td);
1693 LIST_REMOVE(bo, bo_synclist);
1694 LIST_INSERT_HEAD(next, bo, bo_synclist);
1698 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0)
1699 syncer_final_iter--;
1701 * The variable rushjob allows the kernel to speed up the
1702 * processing of the filesystem syncer process. A rushjob
1703 * value of N tells the filesystem syncer to process the next
1704 * N seconds worth of work on its queue ASAP. Currently rushjob
1705 * is used by the soft update code to speed up the filesystem
1706 * syncer process when the incore state is getting so far
1707 * ahead of the disk that the kernel memory pool is being
1708 * threatened with exhaustion.
1715 * Just sleep for a short period of time between
1716 * iterations when shutting down to allow some I/O
1719 * If it has taken us less than a second to process the
1720 * current work, then wait. Otherwise start right over
1721 * again. We can still lose time if any single round
1722 * takes more than two seconds, but it does not really
1723 * matter as we are just trying to generally pace the
1724 * filesystem activity.
1726 if (syncer_state != SYNCER_RUNNING)
1727 msleep(&dummychan, &sync_mtx, PPAUSE, "syncfnl",
1728 hz / SYNCER_SHUTDOWN_SPEEDUP);
1729 else if (time_uptime == starttime)
1730 msleep(&lbolt, &sync_mtx, PPAUSE, "syncer", 0);
1735 * Request the syncer daemon to speed up its work.
1736 * We never push it to speed up more than half of its
1737 * normal turn time, otherwise it could take over the cpu.
1740 speedup_syncer(void)
1745 td = FIRST_THREAD_IN_PROC(updateproc);
1746 sleepq_remove(td, &lbolt);
1747 mtx_lock(&sync_mtx);
1748 if (rushjob < syncdelay / 2) {
1750 stat_rush_requests += 1;
1753 mtx_unlock(&sync_mtx);
1758 * Tell the syncer to speed up its work and run though its work
1759 * list several times, then tell it to shut down.
1762 syncer_shutdown(void *arg, int howto)
1766 if (howto & RB_NOSYNC)
1768 td = FIRST_THREAD_IN_PROC(updateproc);
1769 sleepq_remove(td, &lbolt);
1770 mtx_lock(&sync_mtx);
1771 syncer_state = SYNCER_SHUTTING_DOWN;
1773 mtx_unlock(&sync_mtx);
1774 kproc_shutdown(arg, howto);
1778 * Reassign a buffer from one vnode to another.
1779 * Used to assign file specific control information
1780 * (indirect blocks) to the vnode to which they belong.
1783 reassignbuf(struct buf *bp)
1796 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X",
1797 bp, bp->b_vp, bp->b_flags);
1799 * B_PAGING flagged buffers cannot be reassigned because their vp
1800 * is not fully linked in.
1802 if (bp->b_flags & B_PAGING)
1803 panic("cannot reassign paging buffer");
1806 * Delete from old vnode list, if on one.
1809 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1810 buf_vlist_remove(bp);
1812 panic("reassignbuf: Buffer %p not on queue.", bp);
1814 * If dirty, put on list of dirty buffers; otherwise insert onto list
1817 if (bp->b_flags & B_DELWRI) {
1818 if ((bo->bo_flag & BO_ONWORKLST) == 0) {
1819 switch (vp->v_type) {
1829 vn_syncer_add_to_worklist(bo, delay);
1831 buf_vlist_add(bp, bo, BX_VNDIRTY);
1833 buf_vlist_add(bp, bo, BX_VNCLEAN);
1835 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
1836 mtx_lock(&sync_mtx);
1837 LIST_REMOVE(bo, bo_synclist);
1838 syncer_worklist_len--;
1839 mtx_unlock(&sync_mtx);
1840 bo->bo_flag &= ~BO_ONWORKLST;
1845 bp = TAILQ_FIRST(&bv->bv_hd);
1846 KASSERT(bp == NULL || bp->b_bufobj == bo,
1847 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
1848 bp = TAILQ_LAST(&bv->bv_hd, buflists);
1849 KASSERT(bp == NULL || bp->b_bufobj == bo,
1850 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
1852 bp = TAILQ_FIRST(&bv->bv_hd);
1853 KASSERT(bp == NULL || bp->b_bufobj == bo,
1854 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
1855 bp = TAILQ_LAST(&bv->bv_hd, buflists);
1856 KASSERT(bp == NULL || bp->b_bufobj == bo,
1857 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
1863 * Increment the use and hold counts on the vnode, taking care to reference
1864 * the driver's usecount if this is a chardev. The vholdl() will remove
1865 * the vnode from the free list if it is presently free. Requires the
1866 * vnode interlock and returns with it held.
1869 v_incr_usecount(struct vnode *vp)
1872 CTR3(KTR_VFS, "v_incr_usecount: vp %p holdcnt %d usecount %d\n",
1873 vp, vp->v_holdcnt, vp->v_usecount);
1875 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
1877 vp->v_rdev->si_usecount++;
1884 * Turn a holdcnt into a use+holdcnt such that only one call to
1885 * v_decr_usecount is needed.
1888 v_upgrade_usecount(struct vnode *vp)
1891 CTR3(KTR_VFS, "v_upgrade_usecount: vp %p holdcnt %d usecount %d\n",
1892 vp, vp->v_holdcnt, vp->v_usecount);
1894 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
1896 vp->v_rdev->si_usecount++;
1902 * Decrement the vnode use and hold count along with the driver's usecount
1903 * if this is a chardev. The vdropl() below releases the vnode interlock
1904 * as it may free the vnode.
1907 v_decr_usecount(struct vnode *vp)
1910 CTR3(KTR_VFS, "v_decr_usecount: vp %p holdcnt %d usecount %d\n",
1911 vp, vp->v_holdcnt, vp->v_usecount);
1912 ASSERT_VI_LOCKED(vp, __FUNCTION__);
1913 VNASSERT(vp->v_usecount > 0, vp,
1914 ("v_decr_usecount: negative usecount"));
1916 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
1918 vp->v_rdev->si_usecount--;
1925 * Decrement only the use count and driver use count. This is intended to
1926 * be paired with a follow on vdropl() to release the remaining hold count.
1927 * In this way we may vgone() a vnode with a 0 usecount without risk of
1928 * having it end up on a free list because the hold count is kept above 0.
1931 v_decr_useonly(struct vnode *vp)
1934 CTR3(KTR_VFS, "v_decr_useonly: vp %p holdcnt %d usecount %d\n",
1935 vp, vp->v_holdcnt, vp->v_usecount);
1936 ASSERT_VI_LOCKED(vp, __FUNCTION__);
1937 VNASSERT(vp->v_usecount > 0, vp,
1938 ("v_decr_useonly: negative usecount"));
1940 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
1942 vp->v_rdev->si_usecount--;
1948 * Grab a particular vnode from the free list, increment its
1949 * reference count and lock it. The vnode lock bit is set if the
1950 * vnode is being eliminated in vgone. The process is awakened
1951 * when the transition is completed, and an error returned to
1952 * indicate that the vnode is no longer usable (possibly having
1953 * been changed to a new filesystem type).
1956 vget(struct vnode *vp, int flags, struct thread *td)
1965 VFS_ASSERT_GIANT(vp->v_mount);
1966 if ((flags & LK_INTERLOCK) == 0)
1969 * If the inactive call was deferred because vput() was called
1970 * with a shared lock, we have to do it here before another thread
1971 * gets a reference to data that should be dead.
1973 if (vp->v_iflag & VI_OWEINACT) {
1974 if (flags & LK_NOWAIT) {
1978 flags &= ~LK_TYPE_MASK;
1979 flags |= LK_EXCLUSIVE;
1983 if ((error = vn_lock(vp, flags | LK_INTERLOCK, td)) != 0) {
1988 /* Upgrade our holdcnt to a usecount. */
1989 v_upgrade_usecount(vp);
1990 if (vp->v_iflag & VI_DOOMED && (flags & LK_RETRY) == 0)
1991 panic("vget: vn_lock failed to return ENOENT\n");
1993 if (vp->v_iflag & VI_OWEINACT)
1996 if ((oldflags & LK_TYPE_MASK) == 0)
1997 VOP_UNLOCK(vp, 0, td);
2004 * Increase the reference count of a vnode.
2007 vref(struct vnode *vp)
2011 v_incr_usecount(vp);
2016 * Return reference count of a vnode.
2018 * The results of this call are only guaranteed when some mechanism other
2019 * than the VI lock is used to stop other processes from gaining references
2020 * to the vnode. This may be the case if the caller holds the only reference.
2021 * This is also useful when stale data is acceptable as race conditions may
2022 * be accounted for by some other means.
2025 vrefcnt(struct vnode *vp)
2030 usecnt = vp->v_usecount;
2038 * Vnode put/release.
2039 * If count drops to zero, call inactive routine and return to freelist.
2042 vrele(struct vnode *vp)
2044 struct thread *td = curthread; /* XXX */
2046 KASSERT(vp != NULL, ("vrele: null vp"));
2047 VFS_ASSERT_GIANT(vp->v_mount);
2051 /* Skip this v_writecount check if we're going to panic below. */
2052 VNASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, vp,
2053 ("vrele: missed vn_close"));
2055 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) &&
2056 vp->v_usecount == 1)) {
2057 v_decr_usecount(vp);
2060 if (vp->v_usecount != 1) {
2062 vprint("vrele: negative ref count", vp);
2065 panic("vrele: negative ref cnt");
2068 * We want to hold the vnode until the inactive finishes to
2069 * prevent vgone() races. We drop the use count here and the
2070 * hold count below when we're done.
2074 * We must call VOP_INACTIVE with the node locked. Mark
2075 * as VI_DOINGINACT to avoid recursion.
2077 vp->v_iflag |= VI_OWEINACT;
2078 if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK, td) == 0) {
2080 if (vp->v_usecount > 0)
2081 vp->v_iflag &= ~VI_OWEINACT;
2082 if (vp->v_iflag & VI_OWEINACT)
2084 VOP_UNLOCK(vp, 0, td);
2087 if (vp->v_usecount > 0)
2088 vp->v_iflag &= ~VI_OWEINACT;
2094 * Release an already locked vnode. This give the same effects as
2095 * unlock+vrele(), but takes less time and avoids releasing and
2096 * re-aquiring the lock (as vrele() aquires the lock internally.)
2099 vput(struct vnode *vp)
2101 struct thread *td = curthread; /* XXX */
2104 KASSERT(vp != NULL, ("vput: null vp"));
2105 ASSERT_VOP_LOCKED(vp, "vput");
2106 VFS_ASSERT_GIANT(vp->v_mount);
2108 /* Skip this v_writecount check if we're going to panic below. */
2109 VNASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, vp,
2110 ("vput: missed vn_close"));
2113 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) &&
2114 vp->v_usecount == 1)) {
2115 VOP_UNLOCK(vp, 0, td);
2116 v_decr_usecount(vp);
2120 if (vp->v_usecount != 1) {
2122 vprint("vput: negative ref count", vp);
2124 panic("vput: negative ref cnt");
2127 * We want to hold the vnode until the inactive finishes to
2128 * prevent vgone() races. We drop the use count here and the
2129 * hold count below when we're done.
2132 vp->v_iflag |= VI_OWEINACT;
2133 if (VOP_ISLOCKED(vp, NULL) != LK_EXCLUSIVE) {
2134 error = VOP_LOCK(vp, LK_EXCLUPGRADE|LK_INTERLOCK|LK_NOWAIT, td);
2137 if (vp->v_usecount > 0)
2138 vp->v_iflag &= ~VI_OWEINACT;
2142 if (vp->v_usecount > 0)
2143 vp->v_iflag &= ~VI_OWEINACT;
2144 if (vp->v_iflag & VI_OWEINACT)
2146 VOP_UNLOCK(vp, 0, td);
2152 * Somebody doesn't want the vnode recycled.
2155 vhold(struct vnode *vp)
2164 vholdl(struct vnode *vp)
2168 if (VSHOULDBUSY(vp))
2173 * Note that there is one less who cares about this vnode. vdrop() is the
2174 * opposite of vhold().
2177 vdrop(struct vnode *vp)
2185 * Drop the hold count of the vnode. If this is the last reference to
2186 * the vnode we will free it if it has been vgone'd otherwise it is
2187 * placed on the free list.
2190 vdropl(struct vnode *vp)
2193 if (vp->v_holdcnt <= 0)
2194 panic("vdrop: holdcnt %d", vp->v_holdcnt);
2196 if (vp->v_holdcnt == 0) {
2197 if (vp->v_iflag & VI_DOOMED) {
2207 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT
2208 * flags. DOINGINACT prevents us from recursing in calls to vinactive.
2209 * OWEINACT tracks whether a vnode missed a call to inactive due to a
2210 * failed lock upgrade.
2213 vinactive(struct vnode *vp, struct thread *td)
2216 ASSERT_VOP_LOCKED(vp, "vinactive");
2217 ASSERT_VI_LOCKED(vp, "vinactive");
2218 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
2219 ("vinactive: recursed on VI_DOINGINACT"));
2220 vp->v_iflag |= VI_DOINGINACT;
2221 vp->v_iflag &= ~VI_OWEINACT;
2223 VOP_INACTIVE(vp, td);
2225 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
2226 ("vinactive: lost VI_DOINGINACT"));
2227 vp->v_iflag &= ~VI_DOINGINACT;
2231 * Remove any vnodes in the vnode table belonging to mount point mp.
2233 * If FORCECLOSE is not specified, there should not be any active ones,
2234 * return error if any are found (nb: this is a user error, not a
2235 * system error). If FORCECLOSE is specified, detach any active vnodes
2238 * If WRITECLOSE is set, only flush out regular file vnodes open for
2241 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
2243 * `rootrefs' specifies the base reference count for the root vnode
2244 * of this filesystem. The root vnode is considered busy if its
2245 * v_usecount exceeds this value. On a successful return, vflush(, td)
2246 * will call vrele() on the root vnode exactly rootrefs times.
2247 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
2251 static int busyprt = 0; /* print out busy vnodes */
2252 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "");
2256 vflush( struct mount *mp, int rootrefs, int flags, struct thread *td)
2258 struct vnode *vp, *mvp, *rootvp = NULL;
2260 int busy = 0, error;
2262 CTR1(KTR_VFS, "vflush: mp %p", mp);
2264 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
2265 ("vflush: bad args"));
2267 * Get the filesystem root vnode. We can vput() it
2268 * immediately, since with rootrefs > 0, it won't go away.
2270 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp, td)) != 0)
2277 MNT_VNODE_FOREACH(vp, mp, mvp) {
2282 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE, td);
2286 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
2290 * Skip over a vnodes marked VV_SYSTEM.
2292 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
2293 VOP_UNLOCK(vp, 0, td);
2299 * If WRITECLOSE is set, flush out unlinked but still open
2300 * files (even if open only for reading) and regular file
2301 * vnodes open for writing.
2303 if (flags & WRITECLOSE) {
2304 error = VOP_GETATTR(vp, &vattr, td->td_ucred, td);
2307 if ((vp->v_type == VNON ||
2308 (error == 0 && vattr.va_nlink > 0)) &&
2309 (vp->v_writecount == 0 || vp->v_type != VREG)) {
2310 VOP_UNLOCK(vp, 0, td);
2318 * With v_usecount == 0, all we need to do is clear out the
2319 * vnode data structures and we are done.
2321 * If FORCECLOSE is set, forcibly close the vnode.
2323 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) {
2324 VNASSERT(vp->v_usecount == 0 ||
2325 (vp->v_type != VCHR && vp->v_type != VBLK), vp,
2326 ("device VNODE %p is FORCECLOSED", vp));
2332 vprint("vflush: busy vnode", vp);
2335 VOP_UNLOCK(vp, 0, td);
2340 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
2342 * If just the root vnode is busy, and if its refcount
2343 * is equal to `rootrefs', then go ahead and kill it.
2346 KASSERT(busy > 0, ("vflush: not busy"));
2347 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp,
2348 ("vflush: usecount %d < rootrefs %d",
2349 rootvp->v_usecount, rootrefs));
2350 if (busy == 1 && rootvp->v_usecount == rootrefs) {
2351 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK, td);
2353 VOP_UNLOCK(rootvp, 0, td);
2360 for (; rootrefs > 0; rootrefs--)
2366 * Recycle an unused vnode to the front of the free list.
2369 vrecycle(struct vnode *vp, struct thread *td)
2373 ASSERT_VOP_LOCKED(vp, "vrecycle");
2376 if (vp->v_usecount == 0) {
2385 * Eliminate all activity associated with a vnode
2386 * in preparation for reuse.
2389 vgone(struct vnode *vp)
2397 * vgone, with the vp interlock held.
2400 vgonel(struct vnode *vp)
2407 CTR1(KTR_VFS, "vgonel: vp %p", vp);
2408 ASSERT_VOP_LOCKED(vp, "vgonel");
2409 ASSERT_VI_LOCKED(vp, "vgonel");
2410 VNASSERT(vp->v_holdcnt, vp,
2411 ("vgonel: vp %p has no reference.", vp));
2415 * Don't vgonel if we're already doomed.
2417 if (vp->v_iflag & VI_DOOMED)
2419 vp->v_iflag |= VI_DOOMED;
2421 * Check to see if the vnode is in use. If so, we have to call
2422 * VOP_CLOSE() and VOP_INACTIVE().
2424 active = vp->v_usecount;
2425 oweinact = (vp->v_iflag & VI_OWEINACT);
2428 * Clean out any buffers associated with the vnode.
2429 * If the flush fails, just toss the buffers.
2432 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd))
2433 (void) vn_start_secondary_write(vp, &mp, V_WAIT);
2434 if (vinvalbuf(vp, V_SAVE, td, 0, 0) != 0)
2435 vinvalbuf(vp, 0, td, 0, 0);
2438 * If purging an active vnode, it must be closed and
2439 * deactivated before being reclaimed.
2442 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
2443 if (oweinact || active) {
2445 if ((vp->v_iflag & VI_DOINGINACT) == 0)
2450 * Reclaim the vnode.
2452 if (VOP_RECLAIM(vp, td))
2453 panic("vgone: cannot reclaim");
2455 vn_finished_secondary_write(mp);
2456 VNASSERT(vp->v_object == NULL, vp,
2457 ("vop_reclaim left v_object vp=%p, tag=%s", vp, vp->v_tag));
2459 * Delete from old mount point vnode list.
2464 * Done with purge, reset to the standard lock and invalidate
2468 vp->v_vnlock = &vp->v_lock;
2469 vp->v_op = &dead_vnodeops;
2475 * Calculate the total number of references to a special device.
2478 vcount(struct vnode *vp)
2483 count = vp->v_rdev->si_usecount;
2489 * Same as above, but using the struct cdev *as argument
2492 count_dev(struct cdev *dev)
2497 count = dev->si_usecount;
2503 * Print out a description of a vnode.
2505 static char *typename[] =
2506 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD",
2510 vn_printf(struct vnode *vp, const char *fmt, ...)
2518 printf("%p: ", (void *)vp);
2519 printf("tag %s, type %s\n", vp->v_tag, typename[vp->v_type]);
2520 printf(" usecount %d, writecount %d, refcount %d mountedhere %p\n",
2521 vp->v_usecount, vp->v_writecount, vp->v_holdcnt, vp->v_mountedhere);
2524 if (vp->v_vflag & VV_ROOT)
2525 strcat(buf, "|VV_ROOT");
2526 if (vp->v_vflag & VV_TEXT)
2527 strcat(buf, "|VV_TEXT");
2528 if (vp->v_vflag & VV_SYSTEM)
2529 strcat(buf, "|VV_SYSTEM");
2530 if (vp->v_vflag & VV_DELETED)
2531 strcat(buf, "|VV_DELETED");
2532 if (vp->v_iflag & VI_DOOMED)
2533 strcat(buf, "|VI_DOOMED");
2534 if (vp->v_iflag & VI_FREE)
2535 strcat(buf, "|VI_FREE");
2536 printf(" flags (%s)\n", buf + 1);
2537 if (mtx_owned(VI_MTX(vp)))
2538 printf(" VI_LOCKed");
2539 if (vp->v_object != NULL)
2540 printf(" v_object %p ref %d pages %d\n",
2541 vp->v_object, vp->v_object->ref_count,
2542 vp->v_object->resident_page_count);
2544 lockmgr_printinfo(vp->v_vnlock);
2546 if (vp->v_data != NULL)
2552 * List all of the locked vnodes in the system.
2553 * Called when debugging the kernel.
2555 DB_SHOW_COMMAND(lockedvnods, lockedvnodes)
2557 struct mount *mp, *nmp;
2561 * Note: because this is DDB, we can't obey the locking semantics
2562 * for these structures, which means we could catch an inconsistent
2563 * state and dereference a nasty pointer. Not much to be done
2566 printf("Locked vnodes\n");
2567 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2568 nmp = TAILQ_NEXT(mp, mnt_list);
2569 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
2570 if (vp->v_type != VMARKER && VOP_ISLOCKED(vp, NULL))
2573 nmp = TAILQ_NEXT(mp, mnt_list);
2578 * Show details about the given vnode.
2580 DB_SHOW_COMMAND(vnode, db_show_vnode)
2586 vp = (struct vnode *)addr;
2587 vn_printf(vp, "vnode ");
2592 * Fill in a struct xvfsconf based on a struct vfsconf.
2595 vfsconf2x(struct vfsconf *vfsp, struct xvfsconf *xvfsp)
2598 strcpy(xvfsp->vfc_name, vfsp->vfc_name);
2599 xvfsp->vfc_typenum = vfsp->vfc_typenum;
2600 xvfsp->vfc_refcount = vfsp->vfc_refcount;
2601 xvfsp->vfc_flags = vfsp->vfc_flags;
2603 * These are unused in userland, we keep them
2604 * to not break binary compatibility.
2606 xvfsp->vfc_vfsops = NULL;
2607 xvfsp->vfc_next = NULL;
2611 * Top level filesystem related information gathering.
2614 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS)
2616 struct vfsconf *vfsp;
2617 struct xvfsconf xvfsp;
2621 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
2622 bzero(&xvfsp, sizeof(xvfsp));
2623 vfsconf2x(vfsp, &xvfsp);
2624 error = SYSCTL_OUT(req, &xvfsp, sizeof xvfsp);
2631 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLFLAG_RD, NULL, 0, sysctl_vfs_conflist,
2632 "S,xvfsconf", "List of all configured filesystems");
2634 #ifndef BURN_BRIDGES
2635 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS);
2638 vfs_sysctl(SYSCTL_HANDLER_ARGS)
2640 int *name = (int *)arg1 - 1; /* XXX */
2641 u_int namelen = arg2 + 1; /* XXX */
2642 struct vfsconf *vfsp;
2643 struct xvfsconf xvfsp;
2645 printf("WARNING: userland calling deprecated sysctl, "
2646 "please rebuild world\n");
2648 #if 1 || defined(COMPAT_PRELITE2)
2649 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
2651 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
2655 case VFS_MAXTYPENUM:
2658 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
2661 return (ENOTDIR); /* overloaded */
2662 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list)
2663 if (vfsp->vfc_typenum == name[2])
2666 return (EOPNOTSUPP);
2667 bzero(&xvfsp, sizeof(xvfsp));
2668 vfsconf2x(vfsp, &xvfsp);
2669 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
2671 return (EOPNOTSUPP);
2674 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP,
2675 vfs_sysctl, "Generic filesystem");
2677 #if 1 || defined(COMPAT_PRELITE2)
2680 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
2683 struct vfsconf *vfsp;
2684 struct ovfsconf ovfs;
2686 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
2687 bzero(&ovfs, sizeof(ovfs));
2688 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
2689 strcpy(ovfs.vfc_name, vfsp->vfc_name);
2690 ovfs.vfc_index = vfsp->vfc_typenum;
2691 ovfs.vfc_refcount = vfsp->vfc_refcount;
2692 ovfs.vfc_flags = vfsp->vfc_flags;
2693 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
2700 #endif /* 1 || COMPAT_PRELITE2 */
2701 #endif /* !BURN_BRIDGES */
2703 #define KINFO_VNODESLOP 10
2706 * Dump vnode list (via sysctl).
2710 sysctl_vnode(SYSCTL_HANDLER_ARGS)
2713 struct thread *td = req->td;
2719 * Stale numvnodes access is not fatal here.
2722 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn;
2724 /* Make an estimate */
2725 return (SYSCTL_OUT(req, 0, len));
2727 error = sysctl_wire_old_buffer(req, 0);
2730 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK);
2732 mtx_lock(&mountlist_mtx);
2733 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2734 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td))
2737 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
2741 xvn[n].xv_size = sizeof *xvn;
2742 xvn[n].xv_vnode = vp;
2743 xvn[n].xv_id = 0; /* XXX compat */
2744 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field
2746 XV_COPY(writecount);
2752 xvn[n].xv_flag = vp->v_vflag;
2754 switch (vp->v_type) {
2761 if (vp->v_rdev == NULL) {
2765 xvn[n].xv_dev = dev2udev(vp->v_rdev);
2768 xvn[n].xv_socket = vp->v_socket;
2771 xvn[n].xv_fifo = vp->v_fifoinfo;
2776 /* shouldn't happen? */
2784 mtx_lock(&mountlist_mtx);
2789 mtx_unlock(&mountlist_mtx);
2791 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn);
2796 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD,
2797 0, 0, sysctl_vnode, "S,xvnode", "");
2801 * Unmount all filesystems. The list is traversed in reverse order
2802 * of mounting to avoid dependencies.
2805 vfs_unmountall(void)
2811 KASSERT(curthread != NULL, ("vfs_unmountall: NULL curthread"));
2814 * Since this only runs when rebooting, it is not interlocked.
2816 while(!TAILQ_EMPTY(&mountlist)) {
2817 mp = TAILQ_LAST(&mountlist, mntlist);
2818 error = dounmount(mp, MNT_FORCE, td);
2820 TAILQ_REMOVE(&mountlist, mp, mnt_list);
2822 * XXX: Due to the way in which we mount the root
2823 * file system off of devfs, devfs will generate a
2824 * "busy" warning when we try to unmount it before
2825 * the root. Don't print a warning as a result in
2826 * order to avoid false positive errors that may
2827 * cause needless upset.
2829 if (strcmp(mp->mnt_vfc->vfc_name, "devfs") != 0) {
2830 printf("unmount of %s failed (",
2831 mp->mnt_stat.f_mntonname);
2835 printf("%d)\n", error);
2838 /* The unmount has removed mp from the mountlist */
2844 * perform msync on all vnodes under a mount point
2845 * the mount point must be locked.
2848 vfs_msync(struct mount *mp, int flags)
2850 struct vnode *vp, *mvp;
2851 struct vm_object *obj;
2854 MNT_VNODE_FOREACH(vp, mp, mvp) {
2856 if ((vp->v_iflag & VI_OBJDIRTY) &&
2857 (flags == MNT_WAIT || VOP_ISLOCKED(vp, NULL) == 0)) {
2860 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK,
2862 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */
2870 VM_OBJECT_LOCK(obj);
2871 vm_object_page_clean(obj, 0, 0,
2873 OBJPC_SYNC : OBJPC_NOSYNC);
2874 VM_OBJECT_UNLOCK(obj);
2886 * Mark a vnode as free, putting it up for recycling.
2889 vfree(struct vnode *vp)
2892 CTR1(KTR_VFS, "vfree vp %p", vp);
2893 ASSERT_VI_LOCKED(vp, "vfree");
2894 mtx_lock(&vnode_free_list_mtx);
2895 VNASSERT(vp->v_op != NULL, vp, ("vfree: vnode already reclaimed."));
2896 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp, ("vnode already free"));
2897 VNASSERT(VSHOULDFREE(vp), vp, ("vfree: freeing when we shouldn't"));
2898 VNASSERT((vp->v_iflag & VI_DOOMED) == 0, vp,
2899 ("vfree: Freeing doomed vnode"));
2900 if (vp->v_iflag & VI_AGE) {
2901 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2903 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
2906 vp->v_iflag &= ~VI_AGE;
2907 vp->v_iflag |= VI_FREE;
2908 mtx_unlock(&vnode_free_list_mtx);
2912 * Opposite of vfree() - mark a vnode as in use.
2915 vbusy(struct vnode *vp)
2917 CTR1(KTR_VFS, "vbusy vp %p", vp);
2918 ASSERT_VI_LOCKED(vp, "vbusy");
2919 VNASSERT((vp->v_iflag & VI_FREE) != 0, vp, ("vnode not free"));
2920 VNASSERT(vp->v_op != NULL, vp, ("vbusy: vnode already reclaimed."));
2922 mtx_lock(&vnode_free_list_mtx);
2923 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
2925 vp->v_iflag &= ~(VI_FREE|VI_AGE);
2926 mtx_unlock(&vnode_free_list_mtx);
2930 * Initalize per-vnode helper structure to hold poll-related state.
2933 v_addpollinfo(struct vnode *vp)
2935 struct vpollinfo *vi;
2937 vi = uma_zalloc(vnodepoll_zone, M_WAITOK);
2938 if (vp->v_pollinfo != NULL) {
2939 uma_zfree(vnodepoll_zone, vi);
2942 vp->v_pollinfo = vi;
2943 mtx_init(&vp->v_pollinfo->vpi_lock, "vnode pollinfo", NULL, MTX_DEF);
2944 knlist_init(&vp->v_pollinfo->vpi_selinfo.si_note, vp, vfs_knllock,
2945 vfs_knlunlock, vfs_knllocked);
2949 * Record a process's interest in events which might happen to
2950 * a vnode. Because poll uses the historic select-style interface
2951 * internally, this routine serves as both the ``check for any
2952 * pending events'' and the ``record my interest in future events''
2953 * functions. (These are done together, while the lock is held,
2954 * to avoid race conditions.)
2957 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
2960 if (vp->v_pollinfo == NULL)
2962 mtx_lock(&vp->v_pollinfo->vpi_lock);
2963 if (vp->v_pollinfo->vpi_revents & events) {
2965 * This leaves events we are not interested
2966 * in available for the other process which
2967 * which presumably had requested them
2968 * (otherwise they would never have been
2971 events &= vp->v_pollinfo->vpi_revents;
2972 vp->v_pollinfo->vpi_revents &= ~events;
2974 mtx_unlock(&vp->v_pollinfo->vpi_lock);
2977 vp->v_pollinfo->vpi_events |= events;
2978 selrecord(td, &vp->v_pollinfo->vpi_selinfo);
2979 mtx_unlock(&vp->v_pollinfo->vpi_lock);
2984 * Routine to create and manage a filesystem syncer vnode.
2986 #define sync_close ((int (*)(struct vop_close_args *))nullop)
2987 static int sync_fsync(struct vop_fsync_args *);
2988 static int sync_inactive(struct vop_inactive_args *);
2989 static int sync_reclaim(struct vop_reclaim_args *);
2991 static struct vop_vector sync_vnodeops = {
2992 .vop_bypass = VOP_EOPNOTSUPP,
2993 .vop_close = sync_close, /* close */
2994 .vop_fsync = sync_fsync, /* fsync */
2995 .vop_inactive = sync_inactive, /* inactive */
2996 .vop_reclaim = sync_reclaim, /* reclaim */
2997 .vop_lock = vop_stdlock, /* lock */
2998 .vop_unlock = vop_stdunlock, /* unlock */
2999 .vop_islocked = vop_stdislocked, /* islocked */
3003 * Create a new filesystem syncer vnode for the specified mount point.
3006 vfs_allocate_syncvnode(struct mount *mp)
3009 static long start, incr, next;
3012 /* Allocate a new vnode */
3013 if ((error = getnewvnode("syncer", mp, &sync_vnodeops, &vp)) != 0) {
3014 mp->mnt_syncer = NULL;
3019 * Place the vnode onto the syncer worklist. We attempt to
3020 * scatter them about on the list so that they will go off
3021 * at evenly distributed times even if all the filesystems
3022 * are mounted at once.
3025 if (next == 0 || next > syncer_maxdelay) {
3029 start = syncer_maxdelay / 2;
3030 incr = syncer_maxdelay;
3035 vn_syncer_add_to_worklist(&vp->v_bufobj,
3036 syncdelay > 0 ? next % syncdelay : 0);
3037 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */
3038 mtx_lock(&sync_mtx);
3040 mtx_unlock(&sync_mtx);
3042 mp->mnt_syncer = vp;
3047 * Do a lazy sync of the filesystem.
3050 sync_fsync(struct vop_fsync_args *ap)
3052 struct vnode *syncvp = ap->a_vp;
3053 struct mount *mp = syncvp->v_mount;
3054 struct thread *td = ap->a_td;
3059 * We only need to do something if this is a lazy evaluation.
3061 if (ap->a_waitfor != MNT_LAZY)
3065 * Move ourselves to the back of the sync list.
3067 bo = &syncvp->v_bufobj;
3069 vn_syncer_add_to_worklist(bo, syncdelay);
3073 * Walk the list of vnodes pushing all that are dirty and
3074 * not already on the sync list.
3076 mtx_lock(&mountlist_mtx);
3077 if (vfs_busy(mp, LK_EXCLUSIVE | LK_NOWAIT, &mountlist_mtx, td) != 0) {
3078 mtx_unlock(&mountlist_mtx);
3081 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) {
3087 mp->mnt_kern_flag &= ~MNTK_ASYNC;
3089 vfs_msync(mp, MNT_NOWAIT);
3090 error = VFS_SYNC(mp, MNT_LAZY, td);
3093 if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0)
3094 mp->mnt_kern_flag |= MNTK_ASYNC;
3096 vn_finished_write(mp);
3102 * The syncer vnode is no referenced.
3105 sync_inactive(struct vop_inactive_args *ap)
3113 * The syncer vnode is no longer needed and is being decommissioned.
3115 * Modifications to the worklist must be protected by sync_mtx.
3118 sync_reclaim(struct vop_reclaim_args *ap)
3120 struct vnode *vp = ap->a_vp;
3125 vp->v_mount->mnt_syncer = NULL;
3126 if (bo->bo_flag & BO_ONWORKLST) {
3127 mtx_lock(&sync_mtx);
3128 LIST_REMOVE(bo, bo_synclist);
3129 syncer_worklist_len--;
3131 mtx_unlock(&sync_mtx);
3132 bo->bo_flag &= ~BO_ONWORKLST;
3140 * Check if vnode represents a disk device
3143 vn_isdisk(struct vnode *vp, int *errp)
3149 if (vp->v_type != VCHR)
3151 else if (vp->v_rdev == NULL)
3153 else if (vp->v_rdev->si_devsw == NULL)
3155 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK))
3160 return (error == 0);
3164 * Common filesystem object access control check routine. Accepts a
3165 * vnode's type, "mode", uid and gid, requested access mode, credentials,
3166 * and optional call-by-reference privused argument allowing vaccess()
3167 * to indicate to the caller whether privilege was used to satisfy the
3168 * request (obsoleted). Returns 0 on success, or an errno on failure.
3170 * The ifdef'd CAPABILITIES version is here for reference, but is not
3174 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid,
3175 mode_t acc_mode, struct ucred *cred, int *privused)
3178 mode_t priv_granted;
3181 * Look for a normal, non-privileged way to access the file/directory
3182 * as requested. If it exists, go with that.
3185 if (privused != NULL)
3190 /* Check the owner. */
3191 if (cred->cr_uid == file_uid) {
3192 dac_granted |= VADMIN;
3193 if (file_mode & S_IXUSR)
3194 dac_granted |= VEXEC;
3195 if (file_mode & S_IRUSR)
3196 dac_granted |= VREAD;
3197 if (file_mode & S_IWUSR)
3198 dac_granted |= (VWRITE | VAPPEND);
3200 if ((acc_mode & dac_granted) == acc_mode)
3206 /* Otherwise, check the groups (first match) */
3207 if (groupmember(file_gid, cred)) {
3208 if (file_mode & S_IXGRP)
3209 dac_granted |= VEXEC;
3210 if (file_mode & S_IRGRP)
3211 dac_granted |= VREAD;
3212 if (file_mode & S_IWGRP)
3213 dac_granted |= (VWRITE | VAPPEND);
3215 if ((acc_mode & dac_granted) == acc_mode)
3221 /* Otherwise, check everyone else. */
3222 if (file_mode & S_IXOTH)
3223 dac_granted |= VEXEC;
3224 if (file_mode & S_IROTH)
3225 dac_granted |= VREAD;
3226 if (file_mode & S_IWOTH)
3227 dac_granted |= (VWRITE | VAPPEND);
3228 if ((acc_mode & dac_granted) == acc_mode)
3233 * Build a privilege mask to determine if the set of privileges
3234 * satisfies the requirements when combined with the granted mask
3235 * from above. For each privilege, if the privilege is required,
3236 * bitwise or the request type onto the priv_granted mask.
3242 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC
3243 * requests, instead of PRIV_VFS_EXEC.
3245 if ((acc_mode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3246 !priv_check_cred(cred, PRIV_VFS_LOOKUP, SUSER_ALLOWJAIL))
3247 priv_granted |= VEXEC;
3249 if ((acc_mode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3250 !priv_check_cred(cred, PRIV_VFS_EXEC, SUSER_ALLOWJAIL))
3251 priv_granted |= VEXEC;
3254 if ((acc_mode & VREAD) && ((dac_granted & VREAD) == 0) &&
3255 !priv_check_cred(cred, PRIV_VFS_READ, SUSER_ALLOWJAIL))
3256 priv_granted |= VREAD;
3258 if ((acc_mode & VWRITE) && ((dac_granted & VWRITE) == 0) &&
3259 !priv_check_cred(cred, PRIV_VFS_WRITE, SUSER_ALLOWJAIL))
3260 priv_granted |= (VWRITE | VAPPEND);
3262 if ((acc_mode & VADMIN) && ((dac_granted & VADMIN) == 0) &&
3263 !priv_check_cred(cred, PRIV_VFS_ADMIN, SUSER_ALLOWJAIL))
3264 priv_granted |= VADMIN;
3266 if ((acc_mode & (priv_granted | dac_granted)) == acc_mode) {
3267 /* XXX audit: privilege used */
3268 if (privused != NULL)
3273 return ((acc_mode & VADMIN) ? EPERM : EACCES);
3277 * Credential check based on process requesting service, and per-attribute
3281 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred,
3282 struct thread *td, int access)
3286 * Kernel-invoked always succeeds.
3292 * Do not allow privileged processes in jail to directly manipulate
3293 * system attributes.
3295 switch (attrnamespace) {
3296 case EXTATTR_NAMESPACE_SYSTEM:
3297 /* Potentially should be: return (EPERM); */
3298 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM, 0));
3299 case EXTATTR_NAMESPACE_USER:
3300 return (VOP_ACCESS(vp, access, cred, td));
3306 #ifdef DEBUG_VFS_LOCKS
3308 * This only exists to supress warnings from unlocked specfs accesses. It is
3309 * no longer ok to have an unlocked VFS.
3311 #define IGNORE_LOCK(vp) ((vp)->v_type == VCHR || (vp)->v_type == VBAD)
3313 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */
3314 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0, "");
3316 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */
3317 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex, 0, "");
3319 int vfs_badlock_print = 1; /* Print lock violations. */
3320 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print, 0, "");
3323 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */
3324 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW, &vfs_badlock_backtrace, 0, "");
3328 vfs_badlock(const char *msg, const char *str, struct vnode *vp)
3332 if (vfs_badlock_backtrace)
3335 if (vfs_badlock_print)
3336 printf("%s: %p %s\n", str, (void *)vp, msg);
3337 if (vfs_badlock_ddb)
3338 kdb_enter("lock violation");
3342 assert_vi_locked(struct vnode *vp, const char *str)
3345 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp)))
3346 vfs_badlock("interlock is not locked but should be", str, vp);
3350 assert_vi_unlocked(struct vnode *vp, const char *str)
3353 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp)))
3354 vfs_badlock("interlock is locked but should not be", str, vp);
3358 assert_vop_locked(struct vnode *vp, const char *str)
3361 if (vp && !IGNORE_LOCK(vp) && VOP_ISLOCKED(vp, NULL) == 0)
3362 vfs_badlock("is not locked but should be", str, vp);
3366 assert_vop_unlocked(struct vnode *vp, const char *str)
3369 if (vp && !IGNORE_LOCK(vp) &&
3370 VOP_ISLOCKED(vp, curthread) == LK_EXCLUSIVE)
3371 vfs_badlock("is locked but should not be", str, vp);
3375 assert_vop_elocked(struct vnode *vp, const char *str)
3378 if (vp && !IGNORE_LOCK(vp) &&
3379 VOP_ISLOCKED(vp, curthread) != LK_EXCLUSIVE)
3380 vfs_badlock("is not exclusive locked but should be", str, vp);
3385 assert_vop_elocked_other(struct vnode *vp, const char *str)
3388 if (vp && !IGNORE_LOCK(vp) &&
3389 VOP_ISLOCKED(vp, curthread) != LK_EXCLOTHER)
3390 vfs_badlock("is not exclusive locked by another thread",
3395 assert_vop_slocked(struct vnode *vp, const char *str)
3398 if (vp && !IGNORE_LOCK(vp) &&
3399 VOP_ISLOCKED(vp, curthread) != LK_SHARED)
3400 vfs_badlock("is not locked shared but should be", str, vp);
3403 #endif /* DEBUG_VFS_LOCKS */
3406 vop_rename_pre(void *ap)
3408 struct vop_rename_args *a = ap;
3410 #ifdef DEBUG_VFS_LOCKS
3412 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME");
3413 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME");
3414 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME");
3415 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME");
3417 /* Check the source (from). */
3418 if (a->a_tdvp != a->a_fdvp && a->a_tvp != a->a_fdvp)
3419 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked");
3420 if (a->a_tvp != a->a_fvp)
3421 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked");
3423 /* Check the target. */
3425 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked");
3426 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked");
3428 if (a->a_tdvp != a->a_fdvp)
3430 if (a->a_tvp != a->a_fvp)
3438 vop_strategy_pre(void *ap)
3440 #ifdef DEBUG_VFS_LOCKS
3441 struct vop_strategy_args *a;
3448 * Cluster ops lock their component buffers but not the IO container.
3450 if ((bp->b_flags & B_CLUSTER) != 0)
3453 if (BUF_REFCNT(bp) < 1) {
3454 if (vfs_badlock_print)
3456 "VOP_STRATEGY: bp is not locked but should be\n");
3457 if (vfs_badlock_ddb)
3458 kdb_enter("lock violation");
3464 vop_lookup_pre(void *ap)
3466 #ifdef DEBUG_VFS_LOCKS
3467 struct vop_lookup_args *a;
3472 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP");
3473 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP");
3478 vop_lookup_post(void *ap, int rc)
3480 #ifdef DEBUG_VFS_LOCKS
3481 struct vop_lookup_args *a;
3489 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP");
3490 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP");
3493 ASSERT_VOP_LOCKED(vp, "VOP_LOOKUP (child)");
3498 vop_lock_pre(void *ap)
3500 #ifdef DEBUG_VFS_LOCKS
3501 struct vop_lock_args *a = ap;
3503 if ((a->a_flags & LK_INTERLOCK) == 0)
3504 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
3506 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK");
3511 vop_lock_post(void *ap, int rc)
3513 #ifdef DEBUG_VFS_LOCKS
3514 struct vop_lock_args *a = ap;
3516 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
3518 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK");
3523 vop_unlock_pre(void *ap)
3525 #ifdef DEBUG_VFS_LOCKS
3526 struct vop_unlock_args *a = ap;
3528 if (a->a_flags & LK_INTERLOCK)
3529 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK");
3530 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK");
3535 vop_unlock_post(void *ap, int rc)
3537 #ifdef DEBUG_VFS_LOCKS
3538 struct vop_unlock_args *a = ap;
3540 if (a->a_flags & LK_INTERLOCK)
3541 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK");
3546 vop_create_post(void *ap, int rc)
3548 struct vop_create_args *a = ap;
3551 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
3555 vop_link_post(void *ap, int rc)
3557 struct vop_link_args *a = ap;
3560 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK);
3561 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE);
3566 vop_mkdir_post(void *ap, int rc)
3568 struct vop_mkdir_args *a = ap;
3571 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
3575 vop_mknod_post(void *ap, int rc)
3577 struct vop_mknod_args *a = ap;
3580 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
3584 vop_remove_post(void *ap, int rc)
3586 struct vop_remove_args *a = ap;
3589 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
3590 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
3595 vop_rename_post(void *ap, int rc)
3597 struct vop_rename_args *a = ap;
3600 VFS_KNOTE_UNLOCKED(a->a_fdvp, NOTE_WRITE);
3601 VFS_KNOTE_UNLOCKED(a->a_tdvp, NOTE_WRITE);
3602 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME);
3604 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE);
3606 if (a->a_tdvp != a->a_fdvp)
3608 if (a->a_tvp != a->a_fvp)
3616 vop_rmdir_post(void *ap, int rc)
3618 struct vop_rmdir_args *a = ap;
3621 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
3622 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
3627 vop_setattr_post(void *ap, int rc)
3629 struct vop_setattr_args *a = ap;
3632 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
3636 vop_symlink_post(void *ap, int rc)
3638 struct vop_symlink_args *a = ap;
3641 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
3644 static struct knlist fs_knlist;
3647 vfs_event_init(void *arg)
3649 knlist_init(&fs_knlist, NULL, NULL, NULL, NULL);
3651 /* XXX - correct order? */
3652 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL);
3655 vfs_event_signal(fsid_t *fsid, u_int32_t event, intptr_t data __unused)
3658 KNOTE_UNLOCKED(&fs_knlist, event);
3661 static int filt_fsattach(struct knote *kn);
3662 static void filt_fsdetach(struct knote *kn);
3663 static int filt_fsevent(struct knote *kn, long hint);
3665 struct filterops fs_filtops =
3666 { 0, filt_fsattach, filt_fsdetach, filt_fsevent };
3669 filt_fsattach(struct knote *kn)
3672 kn->kn_flags |= EV_CLEAR;
3673 knlist_add(&fs_knlist, kn, 0);
3678 filt_fsdetach(struct knote *kn)
3681 knlist_remove(&fs_knlist, kn, 0);
3685 filt_fsevent(struct knote *kn, long hint)
3688 kn->kn_fflags |= hint;
3689 return (kn->kn_fflags != 0);
3693 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS)
3699 error = SYSCTL_IN(req, &vc, sizeof(vc));
3702 if (vc.vc_vers != VFS_CTL_VERS1)
3704 mp = vfs_getvfs(&vc.vc_fsid);
3707 /* ensure that a specific sysctl goes to the right filesystem. */
3708 if (strcmp(vc.vc_fstypename, "*") != 0 &&
3709 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) {
3713 VCTLTOREQ(&vc, req);
3714 error = VFS_SYSCTL(mp, vc.vc_op, req);
3719 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLFLAG_WR,
3720 NULL, 0, sysctl_vfs_ctl, "", "Sysctl by fsid");
3723 * Function to initialize a va_filerev field sensibly.
3724 * XXX: Wouldn't a random number make a lot more sense ??
3727 init_va_filerev(void)
3732 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL));
3735 static int filt_vfsread(struct knote *kn, long hint);
3736 static int filt_vfswrite(struct knote *kn, long hint);
3737 static int filt_vfsvnode(struct knote *kn, long hint);
3738 static void filt_vfsdetach(struct knote *kn);
3739 static struct filterops vfsread_filtops =
3740 { 1, NULL, filt_vfsdetach, filt_vfsread };
3741 static struct filterops vfswrite_filtops =
3742 { 1, NULL, filt_vfsdetach, filt_vfswrite };
3743 static struct filterops vfsvnode_filtops =
3744 { 1, NULL, filt_vfsdetach, filt_vfsvnode };
3747 vfs_knllock(void *arg)
3749 struct vnode *vp = arg;
3751 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
3755 vfs_knlunlock(void *arg)
3757 struct vnode *vp = arg;
3759 VOP_UNLOCK(vp, 0, curthread);
3763 vfs_knllocked(void *arg)
3765 struct vnode *vp = arg;
3767 return (VOP_ISLOCKED(vp, curthread) == LK_EXCLUSIVE);
3771 vfs_kqfilter(struct vop_kqfilter_args *ap)
3773 struct vnode *vp = ap->a_vp;
3774 struct knote *kn = ap->a_kn;
3777 switch (kn->kn_filter) {
3779 kn->kn_fop = &vfsread_filtops;
3782 kn->kn_fop = &vfswrite_filtops;
3785 kn->kn_fop = &vfsvnode_filtops;
3791 kn->kn_hook = (caddr_t)vp;
3793 if (vp->v_pollinfo == NULL)
3795 if (vp->v_pollinfo == NULL)
3797 knl = &vp->v_pollinfo->vpi_selinfo.si_note;
3798 knlist_add(knl, kn, 0);
3804 * Detach knote from vnode
3807 filt_vfsdetach(struct knote *kn)
3809 struct vnode *vp = (struct vnode *)kn->kn_hook;
3811 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo"));
3812 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
3817 filt_vfsread(struct knote *kn, long hint)
3819 struct vnode *vp = (struct vnode *)kn->kn_hook;
3823 * filesystem is gone, so set the EOF flag and schedule
3824 * the knote for deletion.
3826 if (hint == NOTE_REVOKE) {
3827 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
3831 if (VOP_GETATTR(vp, &va, curthread->td_ucred, curthread))
3834 kn->kn_data = va.va_size - kn->kn_fp->f_offset;
3835 return (kn->kn_data != 0);
3840 filt_vfswrite(struct knote *kn, long hint)
3843 * filesystem is gone, so set the EOF flag and schedule
3844 * the knote for deletion.
3846 if (hint == NOTE_REVOKE)
3847 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
3854 filt_vfsvnode(struct knote *kn, long hint)
3856 if (kn->kn_sfflags & hint)
3857 kn->kn_fflags |= hint;
3858 if (hint == NOTE_REVOKE) {
3859 kn->kn_flags |= EV_EOF;
3862 return (kn->kn_fflags != 0);
3866 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off)
3870 if (dp->d_reclen > ap->a_uio->uio_resid)
3871 return (ENAMETOOLONG);
3872 error = uiomove(dp, dp->d_reclen, ap->a_uio);
3874 if (ap->a_ncookies != NULL) {
3875 if (ap->a_cookies != NULL)
3876 free(ap->a_cookies, M_TEMP);
3877 ap->a_cookies = NULL;
3878 *ap->a_ncookies = 0;
3882 if (ap->a_ncookies == NULL)
3885 KASSERT(ap->a_cookies,
3886 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!"));
3888 *ap->a_cookies = realloc(*ap->a_cookies,
3889 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO);
3890 (*ap->a_cookies)[*ap->a_ncookies] = off;
3895 * Mark for update the access time of the file if the filesystem
3896 * supports VA_MARK_ATIME. This functionality is used by execve
3897 * and mmap, so we want to avoid the synchronous I/O implied by
3898 * directly setting va_atime for the sake of efficiency.
3901 vfs_mark_atime(struct vnode *vp, struct thread *td)
3903 struct vattr atimeattr;
3905 if ((vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0) {
3906 VATTR_NULL(&atimeattr);
3907 atimeattr.va_vaflags |= VA_MARK_ATIME;
3908 (void)VOP_SETATTR(vp, &atimeattr, td->td_ucred, td);