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$");
45 #include "opt_watchdog.h"
47 #include <sys/param.h>
48 #include <sys/systm.h>
51 #include <sys/condvar.h>
53 #include <sys/dirent.h>
54 #include <sys/event.h>
55 #include <sys/eventhandler.h>
56 #include <sys/extattr.h>
58 #include <sys/fcntl.h>
61 #include <sys/kernel.h>
62 #include <sys/kthread.h>
63 #include <sys/lockf.h>
64 #include <sys/malloc.h>
65 #include <sys/mount.h>
66 #include <sys/namei.h>
68 #include <sys/reboot.h>
69 #include <sys/sched.h>
70 #include <sys/sleepqueue.h>
72 #include <sys/sysctl.h>
73 #include <sys/syslog.h>
74 #include <sys/vmmeter.h>
75 #include <sys/vnode.h>
77 #include <sys/watchdog.h>
80 #include <machine/stdarg.h>
82 #include <security/mac/mac_framework.h>
85 #include <vm/vm_object.h>
86 #include <vm/vm_extern.h>
88 #include <vm/vm_map.h>
89 #include <vm/vm_page.h>
90 #include <vm/vm_kern.h>
100 static void delmntque(struct vnode *vp);
101 static int flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo,
102 int slpflag, int slptimeo);
103 static void syncer_shutdown(void *arg, int howto);
104 static int vtryrecycle(struct vnode *vp);
105 static void vbusy(struct vnode *vp);
106 static void vinactive(struct vnode *, struct thread *);
107 static void v_incr_usecount(struct vnode *);
108 static void v_decr_usecount(struct vnode *);
109 static void v_decr_useonly(struct vnode *);
110 static void v_upgrade_usecount(struct vnode *);
111 static void vfree(struct vnode *);
112 static void vnlru_free(int);
113 static void vgonel(struct vnode *);
114 static void vfs_knllock(void *arg);
115 static void vfs_knlunlock(void *arg);
116 static void vfs_knl_assert_locked(void *arg);
117 static void vfs_knl_assert_unlocked(void *arg);
118 static void destroy_vpollinfo(struct vpollinfo *vi);
121 * Number of vnodes in existence. Increased whenever getnewvnode()
122 * allocates a new vnode, decreased on vdestroy() called on VI_DOOMed
125 static unsigned long numvnodes;
127 SYSCTL_LONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0,
128 "Number of vnodes in existence");
131 * Conversion tables for conversion from vnode types to inode formats
134 enum vtype iftovt_tab[16] = {
135 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
136 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
138 int vttoif_tab[10] = {
139 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
140 S_IFSOCK, S_IFIFO, S_IFMT, S_IFMT
144 * List of vnodes that are ready for recycling.
146 static TAILQ_HEAD(freelst, vnode) vnode_free_list;
149 * Free vnode target. Free vnodes may simply be files which have been stat'd
150 * but not read. This is somewhat common, and a small cache of such files
151 * should be kept to avoid recreation costs.
153 static u_long wantfreevnodes;
154 SYSCTL_LONG(_vfs, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, "");
155 /* Number of vnodes in the free list. */
156 static u_long freevnodes;
157 SYSCTL_LONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0,
158 "Number of vnodes in the free list");
160 static int vlru_allow_cache_src;
161 SYSCTL_INT(_vfs, OID_AUTO, vlru_allow_cache_src, CTLFLAG_RW,
162 &vlru_allow_cache_src, 0, "Allow vlru to reclaim source vnode");
165 * Various variables used for debugging the new implementation of
167 * XXX these are probably of (very) limited utility now.
169 static int reassignbufcalls;
170 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0,
171 "Number of calls to reassignbuf");
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[2];
230 * The sync_mtx protects:
235 * syncer_workitem_pending
236 * syncer_worklist_len
239 static struct mtx sync_mtx;
240 static struct cv sync_wakeup;
242 #define SYNCER_MAXDELAY 32
243 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
244 static int syncdelay = 30; /* max time to delay syncing data */
245 static int filedelay = 30; /* time to delay syncing files */
246 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0,
247 "Time to delay syncing files (in seconds)");
248 static int dirdelay = 29; /* time to delay syncing directories */
249 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0,
250 "Time to delay syncing directories (in seconds)");
251 static int metadelay = 28; /* time to delay syncing metadata */
252 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0,
253 "Time to delay syncing metadata (in seconds)");
254 static int rushjob; /* number of slots to run ASAP */
255 static int stat_rush_requests; /* number of times I/O speeded up */
256 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0,
257 "Number of times I/O speeded up (rush requests)");
260 * When shutting down the syncer, run it at four times normal speed.
262 #define SYNCER_SHUTDOWN_SPEEDUP 4
263 static int sync_vnode_count;
264 static int syncer_worklist_len;
265 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY }
269 * Number of vnodes we want to exist at any one time. This is mostly used
270 * to size hash tables in vnode-related code. It is normally not used in
271 * getnewvnode(), as wantfreevnodes is normally nonzero.)
273 * XXX desiredvnodes is historical cruft and should not exist.
276 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
277 &desiredvnodes, 0, "Maximum number of vnodes");
278 SYSCTL_INT(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
279 &wantfreevnodes, 0, "Minimum number of vnodes (legacy)");
280 static int vnlru_nowhere;
281 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW,
282 &vnlru_nowhere, 0, "Number of times the vnlru process ran without success");
285 * Macros to control when a vnode is freed and recycled. All require
286 * the vnode interlock.
288 #define VCANRECYCLE(vp) (((vp)->v_iflag & VI_FREE) && !(vp)->v_holdcnt)
289 #define VSHOULDFREE(vp) (!((vp)->v_iflag & VI_FREE) && !(vp)->v_holdcnt)
290 #define VSHOULDBUSY(vp) (((vp)->v_iflag & VI_FREE) && (vp)->v_holdcnt)
294 * Initialize the vnode management data structures.
296 * Reevaluate the following cap on the number of vnodes after the physical
297 * memory size exceeds 512GB. In the limit, as the physical memory size
298 * grows, the ratio of physical pages to vnodes approaches sixteen to one.
300 #ifndef MAXVNODES_MAX
301 #define MAXVNODES_MAX (512 * (1024 * 1024 * 1024 / (int)PAGE_SIZE / 16))
304 vntblinit(void *dummy __unused)
306 int physvnodes, virtvnodes;
309 * Desiredvnodes is a function of the physical memory size and the
310 * kernel's heap size. Generally speaking, it scales with the
311 * physical memory size. The ratio of desiredvnodes to physical pages
312 * is one to four until desiredvnodes exceeds 98,304. Thereafter, the
313 * marginal ratio of desiredvnodes to physical pages is one to
314 * sixteen. However, desiredvnodes is limited by the kernel's heap
315 * size. The memory required by desiredvnodes vnodes and vm objects
316 * may not exceed one seventh of the kernel's heap size.
318 physvnodes = maxproc + cnt.v_page_count / 16 + 3 * min(98304 * 4,
319 cnt.v_page_count) / 16;
320 virtvnodes = vm_kmem_size / (7 * (sizeof(struct vm_object) +
321 sizeof(struct vnode)));
322 desiredvnodes = min(physvnodes, virtvnodes);
323 if (desiredvnodes > MAXVNODES_MAX) {
325 printf("Reducing kern.maxvnodes %d -> %d\n",
326 desiredvnodes, MAXVNODES_MAX);
327 desiredvnodes = MAXVNODES_MAX;
329 wantfreevnodes = desiredvnodes / 4;
330 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF);
331 TAILQ_INIT(&vnode_free_list);
332 mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF);
333 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL,
334 NULL, NULL, UMA_ALIGN_PTR, 0);
335 vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo),
336 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
338 * Initialize the filesystem syncer.
340 syncer_workitem_pending[WI_MPSAFEQ] = hashinit(syncer_maxdelay, M_VNODE,
342 syncer_workitem_pending[WI_GIANTQ] = hashinit(syncer_maxdelay, M_VNODE,
344 syncer_maxdelay = syncer_mask + 1;
345 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF);
346 cv_init(&sync_wakeup, "syncer");
348 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL);
352 * Mark a mount point as busy. Used to synchronize access and to delay
353 * unmounting. Eventually, mountlist_mtx is not released on failure.
356 vfs_busy(struct mount *mp, int flags)
359 MPASS((flags & ~MBF_MASK) == 0);
360 CTR3(KTR_VFS, "%s: mp %p with flags %d", __func__, mp, flags);
365 * If mount point is currenly being unmounted, sleep until the
366 * mount point fate is decided. If thread doing the unmounting fails,
367 * it will clear MNTK_UNMOUNT flag before waking us up, indicating
368 * that this mount point has survived the unmount attempt and vfs_busy
369 * should retry. Otherwise the unmounter thread will set MNTK_REFEXPIRE
370 * flag in addition to MNTK_UNMOUNT, indicating that mount point is
371 * about to be really destroyed. vfs_busy needs to release its
372 * reference on the mount point in this case and return with ENOENT,
373 * telling the caller that mount mount it tried to busy is no longer
376 while (mp->mnt_kern_flag & MNTK_UNMOUNT) {
377 if (flags & MBF_NOWAIT || mp->mnt_kern_flag & MNTK_REFEXPIRE) {
380 CTR1(KTR_VFS, "%s: failed busying before sleeping",
384 if (flags & MBF_MNTLSTLOCK)
385 mtx_unlock(&mountlist_mtx);
386 mp->mnt_kern_flag |= MNTK_MWAIT;
387 msleep(mp, MNT_MTX(mp), PVFS | PDROP, "vfs_busy", 0);
388 if (flags & MBF_MNTLSTLOCK)
389 mtx_lock(&mountlist_mtx);
392 if (flags & MBF_MNTLSTLOCK)
393 mtx_unlock(&mountlist_mtx);
400 * Free a busy filesystem.
403 vfs_unbusy(struct mount *mp)
406 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
409 KASSERT(mp->mnt_lockref > 0, ("negative mnt_lockref"));
411 if (mp->mnt_lockref == 0 && (mp->mnt_kern_flag & MNTK_DRAINING) != 0) {
412 MPASS(mp->mnt_kern_flag & MNTK_UNMOUNT);
413 CTR1(KTR_VFS, "%s: waking up waiters", __func__);
414 mp->mnt_kern_flag &= ~MNTK_DRAINING;
415 wakeup(&mp->mnt_lockref);
421 * Lookup a mount point by filesystem identifier.
424 vfs_getvfs(fsid_t *fsid)
428 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
429 mtx_lock(&mountlist_mtx);
430 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
431 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
432 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
434 mtx_unlock(&mountlist_mtx);
438 mtx_unlock(&mountlist_mtx);
439 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
440 return ((struct mount *) 0);
444 * Lookup a mount point by filesystem identifier, busying it before
448 vfs_busyfs(fsid_t *fsid)
453 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
454 mtx_lock(&mountlist_mtx);
455 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
456 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
457 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
458 error = vfs_busy(mp, MBF_MNTLSTLOCK);
460 mtx_unlock(&mountlist_mtx);
466 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
467 mtx_unlock(&mountlist_mtx);
468 return ((struct mount *) 0);
472 * Check if a user can access privileged mount options.
475 vfs_suser(struct mount *mp, struct thread *td)
480 * If the thread is jailed, but this is not a jail-friendly file
481 * system, deny immediately.
483 if (!(mp->mnt_vfc->vfc_flags & VFCF_JAIL) && jailed(td->td_ucred))
487 * If the file system was mounted outside the jail of the calling
488 * thread, deny immediately.
490 if (prison_check(td->td_ucred, mp->mnt_cred) != 0)
494 * If file system supports delegated administration, we don't check
495 * for the PRIV_VFS_MOUNT_OWNER privilege - it will be better verified
496 * by the file system itself.
497 * If this is not the user that did original mount, we check for
498 * the PRIV_VFS_MOUNT_OWNER privilege.
500 if (!(mp->mnt_vfc->vfc_flags & VFCF_DELEGADMIN) &&
501 mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
502 if ((error = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 0)
509 * Get a new unique fsid. Try to make its val[0] unique, since this value
510 * will be used to create fake device numbers for stat(). Also try (but
511 * not so hard) make its val[0] unique mod 2^16, since some emulators only
512 * support 16-bit device numbers. We end up with unique val[0]'s for the
513 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
515 * Keep in mind that several mounts may be running in parallel. Starting
516 * the search one past where the previous search terminated is both a
517 * micro-optimization and a defense against returning the same fsid to
521 vfs_getnewfsid(struct mount *mp)
523 static u_int16_t mntid_base;
528 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
529 mtx_lock(&mntid_mtx);
530 mtype = mp->mnt_vfc->vfc_typenum;
531 tfsid.val[1] = mtype;
532 mtype = (mtype & 0xFF) << 24;
534 tfsid.val[0] = makedev(255,
535 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
537 if ((nmp = vfs_getvfs(&tfsid)) == NULL)
541 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
542 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
543 mtx_unlock(&mntid_mtx);
547 * Knob to control the precision of file timestamps:
549 * 0 = seconds only; nanoseconds zeroed.
550 * 1 = seconds and nanoseconds, accurate within 1/HZ.
551 * 2 = seconds and nanoseconds, truncated to microseconds.
552 * >=3 = seconds and nanoseconds, maximum precision.
554 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
556 static int timestamp_precision = TSP_SEC;
557 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
558 ×tamp_precision, 0, "File timestamp precision (0: seconds, "
559 "1: sec + ns accurate to 1/HZ, 2: sec + ns truncated to ms, "
560 "3+: sec + ns (max. precision))");
563 * Get a current timestamp.
566 vfs_timestamp(struct timespec *tsp)
570 switch (timestamp_precision) {
572 tsp->tv_sec = time_second;
580 TIMEVAL_TO_TIMESPEC(&tv, tsp);
590 * Set vnode attributes to VNOVAL
593 vattr_null(struct vattr *vap)
597 vap->va_size = VNOVAL;
598 vap->va_bytes = VNOVAL;
599 vap->va_mode = VNOVAL;
600 vap->va_nlink = VNOVAL;
601 vap->va_uid = VNOVAL;
602 vap->va_gid = VNOVAL;
603 vap->va_fsid = VNOVAL;
604 vap->va_fileid = VNOVAL;
605 vap->va_blocksize = VNOVAL;
606 vap->va_rdev = VNOVAL;
607 vap->va_atime.tv_sec = VNOVAL;
608 vap->va_atime.tv_nsec = VNOVAL;
609 vap->va_mtime.tv_sec = VNOVAL;
610 vap->va_mtime.tv_nsec = VNOVAL;
611 vap->va_ctime.tv_sec = VNOVAL;
612 vap->va_ctime.tv_nsec = VNOVAL;
613 vap->va_birthtime.tv_sec = VNOVAL;
614 vap->va_birthtime.tv_nsec = VNOVAL;
615 vap->va_flags = VNOVAL;
616 vap->va_gen = VNOVAL;
621 * This routine is called when we have too many vnodes. It attempts
622 * to free <count> vnodes and will potentially free vnodes that still
623 * have VM backing store (VM backing store is typically the cause
624 * of a vnode blowout so we want to do this). Therefore, this operation
625 * is not considered cheap.
627 * A number of conditions may prevent a vnode from being reclaimed.
628 * the buffer cache may have references on the vnode, a directory
629 * vnode may still have references due to the namei cache representing
630 * underlying files, or the vnode may be in active use. It is not
631 * desireable to reuse such vnodes. These conditions may cause the
632 * number of vnodes to reach some minimum value regardless of what
633 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
636 vlrureclaim(struct mount *mp)
645 * Calculate the trigger point, don't allow user
646 * screwups to blow us up. This prevents us from
647 * recycling vnodes with lots of resident pages. We
648 * aren't trying to free memory, we are trying to
651 usevnodes = desiredvnodes;
654 trigger = cnt.v_page_count * 2 / usevnodes;
656 vn_start_write(NULL, &mp, V_WAIT);
658 count = mp->mnt_nvnodelistsize / 10 + 1;
660 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
661 while (vp != NULL && vp->v_type == VMARKER)
662 vp = TAILQ_NEXT(vp, v_nmntvnodes);
665 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
666 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
671 * If it's been deconstructed already, it's still
672 * referenced, or it exceeds the trigger, skip it.
674 if (vp->v_usecount ||
675 (!vlru_allow_cache_src &&
676 !LIST_EMPTY(&(vp)->v_cache_src)) ||
677 (vp->v_iflag & VI_DOOMED) != 0 || (vp->v_object != NULL &&
678 vp->v_object->resident_page_count > trigger)) {
684 if (VOP_LOCK(vp, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT)) {
686 goto next_iter_mntunlocked;
690 * v_usecount may have been bumped after VOP_LOCK() dropped
691 * the vnode interlock and before it was locked again.
693 * It is not necessary to recheck VI_DOOMED because it can
694 * only be set by another thread that holds both the vnode
695 * lock and vnode interlock. If another thread has the
696 * vnode lock before we get to VOP_LOCK() and obtains the
697 * vnode interlock after VOP_LOCK() drops the vnode
698 * interlock, the other thread will be unable to drop the
699 * vnode lock before our VOP_LOCK() call fails.
701 if (vp->v_usecount ||
702 (!vlru_allow_cache_src &&
703 !LIST_EMPTY(&(vp)->v_cache_src)) ||
704 (vp->v_object != NULL &&
705 vp->v_object->resident_page_count > trigger)) {
706 VOP_UNLOCK(vp, LK_INTERLOCK);
707 goto next_iter_mntunlocked;
709 KASSERT((vp->v_iflag & VI_DOOMED) == 0,
710 ("VI_DOOMED unexpectedly detected in vlrureclaim()"));
715 next_iter_mntunlocked:
716 if ((count % 256) != 0)
720 if ((count % 256) != 0)
729 vn_finished_write(mp);
734 * Attempt to keep the free list at wantfreevnodes length.
737 vnlru_free(int count)
742 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
743 for (; count > 0; count--) {
744 vp = TAILQ_FIRST(&vnode_free_list);
746 * The list can be modified while the free_list_mtx
747 * has been dropped and vp could be NULL here.
751 VNASSERT(vp->v_op != NULL, vp,
752 ("vnlru_free: vnode already reclaimed."));
753 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
755 * Don't recycle if we can't get the interlock.
757 if (!VI_TRYLOCK(vp)) {
758 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
761 VNASSERT(VCANRECYCLE(vp), vp,
762 ("vp inconsistent on freelist"));
764 vp->v_iflag &= ~VI_FREE;
766 mtx_unlock(&vnode_free_list_mtx);
768 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
770 VFS_UNLOCK_GIANT(vfslocked);
772 * If the recycled succeeded this vdrop will actually free
773 * the vnode. If not it will simply place it back on
777 mtx_lock(&vnode_free_list_mtx);
781 * Attempt to recycle vnodes in a context that is always safe to block.
782 * Calling vlrurecycle() from the bowels of filesystem code has some
783 * interesting deadlock problems.
785 static struct proc *vnlruproc;
786 static int vnlruproc_sig;
791 struct mount *mp, *nmp;
793 struct proc *p = vnlruproc;
795 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, p,
799 kproc_suspend_check(p);
800 mtx_lock(&vnode_free_list_mtx);
801 if (freevnodes > wantfreevnodes)
802 vnlru_free(freevnodes - wantfreevnodes);
803 if (numvnodes <= desiredvnodes * 9 / 10) {
805 wakeup(&vnlruproc_sig);
806 msleep(vnlruproc, &vnode_free_list_mtx,
807 PVFS|PDROP, "vlruwt", hz);
810 mtx_unlock(&vnode_free_list_mtx);
812 mtx_lock(&mountlist_mtx);
813 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
814 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
815 nmp = TAILQ_NEXT(mp, mnt_list);
818 vfslocked = VFS_LOCK_GIANT(mp);
819 done += vlrureclaim(mp);
820 VFS_UNLOCK_GIANT(vfslocked);
821 mtx_lock(&mountlist_mtx);
822 nmp = TAILQ_NEXT(mp, mnt_list);
825 mtx_unlock(&mountlist_mtx);
828 /* These messages are temporary debugging aids */
829 if (vnlru_nowhere < 5)
830 printf("vnlru process getting nowhere..\n");
831 else if (vnlru_nowhere == 5)
832 printf("vnlru process messages stopped.\n");
835 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
841 static struct kproc_desc vnlru_kp = {
846 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start,
850 * Routines having to do with the management of the vnode table.
854 vdestroy(struct vnode *vp)
858 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
859 mtx_lock(&vnode_free_list_mtx);
861 mtx_unlock(&vnode_free_list_mtx);
863 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
864 ("cleaned vnode still on the free list."));
865 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't"));
866 VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count"));
867 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count"));
868 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count"));
869 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's"));
870 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0"));
871 VNASSERT(bo->bo_clean.bv_root == NULL, vp, ("cleanblkroot not NULL"));
872 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0"));
873 VNASSERT(bo->bo_dirty.bv_root == NULL, vp, ("dirtyblkroot not NULL"));
874 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst"));
875 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src"));
876 VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for .."));
879 mac_vnode_destroy(vp);
881 if (vp->v_pollinfo != NULL)
882 destroy_vpollinfo(vp->v_pollinfo);
884 /* XXX Elsewhere we can detect an already freed vnode via NULL v_op. */
887 lockdestroy(vp->v_vnlock);
888 mtx_destroy(&vp->v_interlock);
889 mtx_destroy(BO_MTX(bo));
890 uma_zfree(vnode_zone, vp);
894 * Try to recycle a freed vnode. We abort if anyone picks up a reference
895 * before we actually vgone(). This function must be called with the vnode
896 * held to prevent the vnode from being returned to the free list midway
900 vtryrecycle(struct vnode *vp)
904 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
905 VNASSERT(vp->v_holdcnt, vp,
906 ("vtryrecycle: Recycling vp %p without a reference.", vp));
908 * This vnode may found and locked via some other list, if so we
909 * can't recycle it yet.
911 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
913 "%s: impossible to recycle, vp %p lock is already held",
915 return (EWOULDBLOCK);
918 * Don't recycle if its filesystem is being suspended.
920 if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) {
923 "%s: impossible to recycle, cannot start the write for %p",
928 * If we got this far, we need to acquire the interlock and see if
929 * anyone picked up this vnode from another list. If not, we will
930 * mark it with DOOMED via vgonel() so that anyone who does find it
934 if (vp->v_usecount) {
935 VOP_UNLOCK(vp, LK_INTERLOCK);
936 vn_finished_write(vnmp);
938 "%s: impossible to recycle, %p is already referenced",
942 if ((vp->v_iflag & VI_DOOMED) == 0)
944 VOP_UNLOCK(vp, LK_INTERLOCK);
945 vn_finished_write(vnmp);
950 * Return the next vnode from the free list.
953 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops,
956 struct vnode *vp = NULL;
959 CTR3(KTR_VFS, "%s: mp %p with tag %s", __func__, mp, tag);
960 mtx_lock(&vnode_free_list_mtx);
962 * Lend our context to reclaim vnodes if they've exceeded the max.
964 if (freevnodes > wantfreevnodes)
967 * Wait for available vnodes.
969 if (numvnodes > desiredvnodes) {
970 if (mp != NULL && (mp->mnt_kern_flag & MNTK_SUSPEND)) {
972 * File system is beeing suspended, we cannot risk a
973 * deadlock here, so allocate new vnode anyway.
975 if (freevnodes > wantfreevnodes)
976 vnlru_free(freevnodes - wantfreevnodes);
979 if (vnlruproc_sig == 0) {
980 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
983 msleep(&vnlruproc_sig, &vnode_free_list_mtx, PVFS,
985 #if 0 /* XXX Not all VFS_VGET/ffs_vget callers check returns. */
986 if (numvnodes > desiredvnodes) {
987 mtx_unlock(&vnode_free_list_mtx);
994 mtx_unlock(&vnode_free_list_mtx);
995 vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK|M_ZERO);
999 vp->v_vnlock = &vp->v_lock;
1000 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF);
1002 * By default, don't allow shared locks unless filesystems
1005 lockinit(vp->v_vnlock, PVFS, tag, VLKTIMEOUT, LK_NOSHARE);
1007 * Initialize bufobj.
1010 bo->__bo_vnode = vp;
1011 mtx_init(BO_MTX(bo), "bufobj interlock", NULL, MTX_DEF);
1012 bo->bo_ops = &buf_ops_bio;
1013 bo->bo_private = vp;
1014 TAILQ_INIT(&bo->bo_clean.bv_hd);
1015 TAILQ_INIT(&bo->bo_dirty.bv_hd);
1017 * Initialize namecache.
1019 LIST_INIT(&vp->v_cache_src);
1020 TAILQ_INIT(&vp->v_cache_dst);
1022 * Finalize various vnode identity bits.
1027 v_incr_usecount(vp);
1031 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0)
1032 mac_vnode_associate_singlelabel(mp, vp);
1033 else if (mp == NULL && vops != &dead_vnodeops)
1034 printf("NULL mp in getnewvnode()\n");
1037 bo->bo_bsize = mp->mnt_stat.f_iosize;
1038 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0)
1039 vp->v_vflag |= VV_NOKNOTE;
1047 * Delete from old mount point vnode list, if on one.
1050 delmntque(struct vnode *vp)
1059 VNASSERT(mp->mnt_nvnodelistsize > 0, vp,
1060 ("bad mount point vnode list size"));
1061 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1062 mp->mnt_nvnodelistsize--;
1068 insmntque_stddtr(struct vnode *vp, void *dtr_arg)
1072 vp->v_op = &dead_vnodeops;
1073 /* XXX non mp-safe fs may still call insmntque with vnode
1075 if (!VOP_ISLOCKED(vp))
1076 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1082 * Insert into list of vnodes for the new mount point, if available.
1085 insmntque1(struct vnode *vp, struct mount *mp,
1086 void (*dtr)(struct vnode *, void *), void *dtr_arg)
1090 KASSERT(vp->v_mount == NULL,
1091 ("insmntque: vnode already on per mount vnode list"));
1092 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)"));
1093 #ifdef DEBUG_VFS_LOCKS
1094 if (!VFS_NEEDSGIANT(mp))
1095 ASSERT_VOP_ELOCKED(vp,
1096 "insmntque: mp-safe fs and non-locked vp");
1099 if ((mp->mnt_kern_flag & MNTK_NOINSMNTQ) != 0 &&
1100 ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 ||
1101 mp->mnt_nvnodelistsize == 0)) {
1102 locked = VOP_ISLOCKED(vp);
1103 if (!locked || (locked == LK_EXCLUSIVE &&
1104 (vp->v_vflag & VV_FORCEINSMQ) == 0)) {
1113 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1114 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp,
1115 ("neg mount point vnode list size"));
1116 mp->mnt_nvnodelistsize++;
1122 insmntque(struct vnode *vp, struct mount *mp)
1125 return (insmntque1(vp, mp, insmntque_stddtr, NULL));
1129 * Flush out and invalidate all buffers associated with a bufobj
1130 * Called with the underlying object locked.
1133 bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo)
1138 if (flags & V_SAVE) {
1139 error = bufobj_wwait(bo, slpflag, slptimeo);
1144 if (bo->bo_dirty.bv_cnt > 0) {
1146 if ((error = BO_SYNC(bo, MNT_WAIT)) != 0)
1149 * XXX We could save a lock/unlock if this was only
1150 * enabled under INVARIANTS
1153 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)
1154 panic("vinvalbuf: dirty bufs");
1158 * If you alter this loop please notice that interlock is dropped and
1159 * reacquired in flushbuflist. Special care is needed to ensure that
1160 * no race conditions occur from this.
1163 error = flushbuflist(&bo->bo_clean,
1164 flags, bo, slpflag, slptimeo);
1166 error = flushbuflist(&bo->bo_dirty,
1167 flags, bo, slpflag, slptimeo);
1168 if (error != 0 && error != EAGAIN) {
1172 } while (error != 0);
1175 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
1176 * have write I/O in-progress but if there is a VM object then the
1177 * VM object can also have read-I/O in-progress.
1180 bufobj_wwait(bo, 0, 0);
1182 if (bo->bo_object != NULL) {
1183 VM_OBJECT_LOCK(bo->bo_object);
1184 vm_object_pip_wait(bo->bo_object, "bovlbx");
1185 VM_OBJECT_UNLOCK(bo->bo_object);
1188 } while (bo->bo_numoutput > 0);
1192 * Destroy the copy in the VM cache, too.
1194 if (bo->bo_object != NULL && (flags & (V_ALT | V_NORMAL)) == 0) {
1195 VM_OBJECT_LOCK(bo->bo_object);
1196 vm_object_page_remove(bo->bo_object, 0, 0,
1197 (flags & V_SAVE) ? TRUE : FALSE);
1198 VM_OBJECT_UNLOCK(bo->bo_object);
1203 if ((flags & (V_ALT | V_NORMAL)) == 0 &&
1204 (bo->bo_dirty.bv_cnt > 0 || bo->bo_clean.bv_cnt > 0))
1205 panic("vinvalbuf: flush failed");
1212 * Flush out and invalidate all buffers associated with a vnode.
1213 * Called with the underlying object locked.
1216 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
1219 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
1220 ASSERT_VOP_LOCKED(vp, "vinvalbuf");
1221 return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo));
1225 * Flush out buffers on the specified list.
1229 flushbuflist( struct bufv *bufv, int flags, struct bufobj *bo, int slpflag,
1232 struct buf *bp, *nbp;
1237 ASSERT_BO_LOCKED(bo);
1240 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) {
1241 if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) ||
1242 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) {
1248 lblkno = nbp->b_lblkno;
1249 xflags = nbp->b_xflags &
1250 (BX_BKGRDMARKER | BX_VNDIRTY | BX_VNCLEAN);
1253 error = BUF_TIMELOCK(bp,
1254 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_MTX(bo),
1255 "flushbuf", slpflag, slptimeo);
1258 return (error != ENOLCK ? error : EAGAIN);
1260 KASSERT(bp->b_bufobj == bo,
1261 ("bp %p wrong b_bufobj %p should be %p",
1262 bp, bp->b_bufobj, bo));
1263 if (bp->b_bufobj != bo) { /* XXX: necessary ? */
1269 * XXX Since there are no node locks for NFS, I
1270 * believe there is a slight chance that a delayed
1271 * write will occur while sleeping just above, so
1274 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
1279 bp->b_flags |= B_ASYNC;
1282 return (EAGAIN); /* XXX: why not loop ? */
1287 bp->b_flags |= (B_INVAL | B_RELBUF);
1288 bp->b_flags &= ~B_ASYNC;
1292 (nbp->b_bufobj != bo ||
1293 nbp->b_lblkno != lblkno ||
1295 (BX_BKGRDMARKER | BX_VNDIRTY | BX_VNCLEAN)) != xflags))
1296 break; /* nbp invalid */
1302 * Truncate a file's buffer and pages to a specified length. This
1303 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
1307 vtruncbuf(struct vnode *vp, struct ucred *cred, struct thread *td,
1308 off_t length, int blksize)
1310 struct buf *bp, *nbp;
1315 CTR5(KTR_VFS, "%s: vp %p with cred %p and block %d:%ju", __func__,
1316 vp, cred, blksize, (uintmax_t)length);
1319 * Round up to the *next* lbn.
1321 trunclbn = (length + blksize - 1) / blksize;
1323 ASSERT_VOP_LOCKED(vp, "vtruncbuf");
1330 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) {
1331 if (bp->b_lblkno < trunclbn)
1334 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1335 BO_MTX(bo)) == ENOLCK)
1341 bp->b_flags |= (B_INVAL | B_RELBUF);
1342 bp->b_flags &= ~B_ASYNC;
1348 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
1349 (nbp->b_vp != vp) ||
1350 (nbp->b_flags & B_DELWRI))) {
1356 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1357 if (bp->b_lblkno < trunclbn)
1360 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1361 BO_MTX(bo)) == ENOLCK)
1366 bp->b_flags |= (B_INVAL | B_RELBUF);
1367 bp->b_flags &= ~B_ASYNC;
1373 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
1374 (nbp->b_vp != vp) ||
1375 (nbp->b_flags & B_DELWRI) == 0)) {
1384 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1385 if (bp->b_lblkno > 0)
1388 * Since we hold the vnode lock this should only
1389 * fail if we're racing with the buf daemon.
1392 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1393 BO_MTX(bo)) == ENOLCK) {
1396 VNASSERT((bp->b_flags & B_DELWRI), vp,
1397 ("buf(%p) on dirty queue without DELWRI", bp));
1408 bufobj_wwait(bo, 0, 0);
1410 vnode_pager_setsize(vp, length);
1416 * buf_splay() - splay tree core for the clean/dirty list of buffers in
1419 * NOTE: We have to deal with the special case of a background bitmap
1420 * buffer, a situation where two buffers will have the same logical
1421 * block offset. We want (1) only the foreground buffer to be accessed
1422 * in a lookup and (2) must differentiate between the foreground and
1423 * background buffer in the splay tree algorithm because the splay
1424 * tree cannot normally handle multiple entities with the same 'index'.
1425 * We accomplish this by adding differentiating flags to the splay tree's
1430 buf_splay(daddr_t lblkno, b_xflags_t xflags, struct buf *root)
1433 struct buf *lefttreemax, *righttreemin, *y;
1437 lefttreemax = righttreemin = &dummy;
1439 if (lblkno < root->b_lblkno ||
1440 (lblkno == root->b_lblkno &&
1441 (xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
1442 if ((y = root->b_left) == NULL)
1444 if (lblkno < y->b_lblkno) {
1446 root->b_left = y->b_right;
1449 if ((y = root->b_left) == NULL)
1452 /* Link into the new root's right tree. */
1453 righttreemin->b_left = root;
1454 righttreemin = root;
1455 } else if (lblkno > root->b_lblkno ||
1456 (lblkno == root->b_lblkno &&
1457 (xflags & BX_BKGRDMARKER) > (root->b_xflags & BX_BKGRDMARKER))) {
1458 if ((y = root->b_right) == NULL)
1460 if (lblkno > y->b_lblkno) {
1462 root->b_right = y->b_left;
1465 if ((y = root->b_right) == NULL)
1468 /* Link into the new root's left tree. */
1469 lefttreemax->b_right = root;
1476 /* Assemble the new root. */
1477 lefttreemax->b_right = root->b_left;
1478 righttreemin->b_left = root->b_right;
1479 root->b_left = dummy.b_right;
1480 root->b_right = dummy.b_left;
1485 buf_vlist_remove(struct buf *bp)
1490 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1491 ASSERT_BO_LOCKED(bp->b_bufobj);
1492 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) !=
1493 (BX_VNDIRTY|BX_VNCLEAN),
1494 ("buf_vlist_remove: Buf %p is on two lists", bp));
1495 if (bp->b_xflags & BX_VNDIRTY)
1496 bv = &bp->b_bufobj->bo_dirty;
1498 bv = &bp->b_bufobj->bo_clean;
1499 if (bp != bv->bv_root) {
1500 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root);
1501 KASSERT(root == bp, ("splay lookup failed in remove"));
1503 if (bp->b_left == NULL) {
1506 root = buf_splay(bp->b_lblkno, bp->b_xflags, bp->b_left);
1507 root->b_right = bp->b_right;
1510 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs);
1512 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1516 * Add the buffer to the sorted clean or dirty block list using a
1517 * splay tree algorithm.
1519 * NOTE: xflags is passed as a constant, optimizing this inline function!
1522 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags)
1527 ASSERT_BO_LOCKED(bo);
1528 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0,
1529 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags));
1530 bp->b_xflags |= xflags;
1531 if (xflags & BX_VNDIRTY)
1536 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root);
1540 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs);
1541 } else if (bp->b_lblkno < root->b_lblkno ||
1542 (bp->b_lblkno == root->b_lblkno &&
1543 (bp->b_xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
1544 bp->b_left = root->b_left;
1546 root->b_left = NULL;
1547 TAILQ_INSERT_BEFORE(root, bp, b_bobufs);
1549 bp->b_right = root->b_right;
1551 root->b_right = NULL;
1552 TAILQ_INSERT_AFTER(&bv->bv_hd, root, bp, b_bobufs);
1559 * Lookup a buffer using the splay tree. Note that we specifically avoid
1560 * shadow buffers used in background bitmap writes.
1562 * This code isn't quite efficient as it could be because we are maintaining
1563 * two sorted lists and do not know which list the block resides in.
1565 * During a "make buildworld" the desired buffer is found at one of
1566 * the roots more than 60% of the time. Thus, checking both roots
1567 * before performing either splay eliminates unnecessary splays on the
1568 * first tree splayed.
1571 gbincore(struct bufobj *bo, daddr_t lblkno)
1575 ASSERT_BO_LOCKED(bo);
1576 if ((bp = bo->bo_clean.bv_root) != NULL &&
1577 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1579 if ((bp = bo->bo_dirty.bv_root) != NULL &&
1580 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1582 if ((bp = bo->bo_clean.bv_root) != NULL) {
1583 bo->bo_clean.bv_root = bp = buf_splay(lblkno, 0, bp);
1584 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1587 if ((bp = bo->bo_dirty.bv_root) != NULL) {
1588 bo->bo_dirty.bv_root = bp = buf_splay(lblkno, 0, bp);
1589 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1596 * Associate a buffer with a vnode.
1599 bgetvp(struct vnode *vp, struct buf *bp)
1604 ASSERT_BO_LOCKED(bo);
1605 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free"));
1607 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags);
1608 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp,
1609 ("bgetvp: bp already attached! %p", bp));
1612 if (VFS_NEEDSGIANT(vp->v_mount) || bo->bo_flag & BO_NEEDSGIANT)
1613 bp->b_flags |= B_NEEDSGIANT;
1617 * Insert onto list for new vnode.
1619 buf_vlist_add(bp, bo, BX_VNCLEAN);
1623 * Disassociate a buffer from a vnode.
1626 brelvp(struct buf *bp)
1631 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1632 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1635 * Delete from old vnode list, if on one.
1637 vp = bp->b_vp; /* XXX */
1640 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1641 buf_vlist_remove(bp);
1643 panic("brelvp: Buffer %p not on queue.", bp);
1644 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
1645 bo->bo_flag &= ~BO_ONWORKLST;
1646 mtx_lock(&sync_mtx);
1647 LIST_REMOVE(bo, bo_synclist);
1648 syncer_worklist_len--;
1649 mtx_unlock(&sync_mtx);
1651 bp->b_flags &= ~B_NEEDSGIANT;
1653 bp->b_bufobj = NULL;
1659 * Add an item to the syncer work queue.
1662 vn_syncer_add_to_worklist(struct bufobj *bo, int delay)
1666 ASSERT_BO_LOCKED(bo);
1668 mtx_lock(&sync_mtx);
1669 if (bo->bo_flag & BO_ONWORKLST)
1670 LIST_REMOVE(bo, bo_synclist);
1672 bo->bo_flag |= BO_ONWORKLST;
1673 syncer_worklist_len++;
1676 if (delay > syncer_maxdelay - 2)
1677 delay = syncer_maxdelay - 2;
1678 slot = (syncer_delayno + delay) & syncer_mask;
1680 queue = VFS_NEEDSGIANT(bo->__bo_vnode->v_mount) ? WI_GIANTQ :
1682 LIST_INSERT_HEAD(&syncer_workitem_pending[queue][slot], bo,
1684 mtx_unlock(&sync_mtx);
1688 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS)
1692 mtx_lock(&sync_mtx);
1693 len = syncer_worklist_len - sync_vnode_count;
1694 mtx_unlock(&sync_mtx);
1695 error = SYSCTL_OUT(req, &len, sizeof(len));
1699 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, CTLTYPE_INT | CTLFLAG_RD, NULL, 0,
1700 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length");
1702 static struct proc *updateproc;
1703 static void sched_sync(void);
1704 static struct kproc_desc up_kp = {
1709 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp);
1712 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td)
1717 *bo = LIST_FIRST(slp);
1720 vp = (*bo)->__bo_vnode; /* XXX */
1721 if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0)
1724 * We use vhold in case the vnode does not
1725 * successfully sync. vhold prevents the vnode from
1726 * going away when we unlock the sync_mtx so that
1727 * we can acquire the vnode interlock.
1730 mtx_unlock(&sync_mtx);
1732 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1734 mtx_lock(&sync_mtx);
1735 return (*bo == LIST_FIRST(slp));
1737 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1738 (void) VOP_FSYNC(vp, MNT_LAZY, td);
1740 vn_finished_write(mp);
1742 if (((*bo)->bo_flag & BO_ONWORKLST) != 0) {
1744 * Put us back on the worklist. The worklist
1745 * routine will remove us from our current
1746 * position and then add us back in at a later
1749 vn_syncer_add_to_worklist(*bo, syncdelay);
1753 mtx_lock(&sync_mtx);
1758 * System filesystem synchronizer daemon.
1763 struct synclist *gnext, *next;
1764 struct synclist *gslp, *slp;
1767 struct thread *td = curthread;
1769 int net_worklist_len;
1770 int syncer_final_iter;
1775 syncer_final_iter = 0;
1777 syncer_state = SYNCER_RUNNING;
1778 starttime = time_uptime;
1779 td->td_pflags |= TDP_NORUNNINGBUF;
1781 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc,
1784 mtx_lock(&sync_mtx);
1786 if (syncer_state == SYNCER_FINAL_DELAY &&
1787 syncer_final_iter == 0) {
1788 mtx_unlock(&sync_mtx);
1789 kproc_suspend_check(td->td_proc);
1790 mtx_lock(&sync_mtx);
1792 net_worklist_len = syncer_worklist_len - sync_vnode_count;
1793 if (syncer_state != SYNCER_RUNNING &&
1794 starttime != time_uptime) {
1796 printf("\nSyncing disks, vnodes remaining...");
1799 printf("%d ", net_worklist_len);
1801 starttime = time_uptime;
1804 * Push files whose dirty time has expired. Be careful
1805 * of interrupt race on slp queue.
1807 * Skip over empty worklist slots when shutting down.
1810 slp = &syncer_workitem_pending[WI_MPSAFEQ][syncer_delayno];
1811 gslp = &syncer_workitem_pending[WI_GIANTQ][syncer_delayno];
1812 syncer_delayno += 1;
1813 if (syncer_delayno == syncer_maxdelay)
1815 next = &syncer_workitem_pending[WI_MPSAFEQ][syncer_delayno];
1816 gnext = &syncer_workitem_pending[WI_GIANTQ][syncer_delayno];
1818 * If the worklist has wrapped since the
1819 * it was emptied of all but syncer vnodes,
1820 * switch to the FINAL_DELAY state and run
1821 * for one more second.
1823 if (syncer_state == SYNCER_SHUTTING_DOWN &&
1824 net_worklist_len == 0 &&
1825 last_work_seen == syncer_delayno) {
1826 syncer_state = SYNCER_FINAL_DELAY;
1827 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP;
1829 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) &&
1830 LIST_EMPTY(gslp) && syncer_worklist_len > 0);
1833 * Keep track of the last time there was anything
1834 * on the worklist other than syncer vnodes.
1835 * Return to the SHUTTING_DOWN state if any
1838 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING)
1839 last_work_seen = syncer_delayno;
1840 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY)
1841 syncer_state = SYNCER_SHUTTING_DOWN;
1842 while (!LIST_EMPTY(slp)) {
1843 error = sync_vnode(slp, &bo, td);
1845 LIST_REMOVE(bo, bo_synclist);
1846 LIST_INSERT_HEAD(next, bo, bo_synclist);
1850 if (first_printf == 0)
1851 wdog_kern_pat(WD_LASTVAL);
1854 if (!LIST_EMPTY(gslp)) {
1855 mtx_unlock(&sync_mtx);
1857 mtx_lock(&sync_mtx);
1858 while (!LIST_EMPTY(gslp)) {
1859 error = sync_vnode(gslp, &bo, td);
1861 LIST_REMOVE(bo, bo_synclist);
1862 LIST_INSERT_HEAD(gnext, bo,
1869 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0)
1870 syncer_final_iter--;
1872 * The variable rushjob allows the kernel to speed up the
1873 * processing of the filesystem syncer process. A rushjob
1874 * value of N tells the filesystem syncer to process the next
1875 * N seconds worth of work on its queue ASAP. Currently rushjob
1876 * is used by the soft update code to speed up the filesystem
1877 * syncer process when the incore state is getting so far
1878 * ahead of the disk that the kernel memory pool is being
1879 * threatened with exhaustion.
1886 * Just sleep for a short period of time between
1887 * iterations when shutting down to allow some I/O
1890 * If it has taken us less than a second to process the
1891 * current work, then wait. Otherwise start right over
1892 * again. We can still lose time if any single round
1893 * takes more than two seconds, but it does not really
1894 * matter as we are just trying to generally pace the
1895 * filesystem activity.
1897 if (syncer_state != SYNCER_RUNNING ||
1898 time_uptime == starttime) {
1900 sched_prio(td, PPAUSE);
1903 if (syncer_state != SYNCER_RUNNING)
1904 cv_timedwait(&sync_wakeup, &sync_mtx,
1905 hz / SYNCER_SHUTDOWN_SPEEDUP);
1906 else if (time_uptime == starttime)
1907 cv_timedwait(&sync_wakeup, &sync_mtx, hz);
1912 * Request the syncer daemon to speed up its work.
1913 * We never push it to speed up more than half of its
1914 * normal turn time, otherwise it could take over the cpu.
1917 speedup_syncer(void)
1921 mtx_lock(&sync_mtx);
1922 if (rushjob < syncdelay / 2) {
1924 stat_rush_requests += 1;
1927 mtx_unlock(&sync_mtx);
1928 cv_broadcast(&sync_wakeup);
1933 * Tell the syncer to speed up its work and run though its work
1934 * list several times, then tell it to shut down.
1937 syncer_shutdown(void *arg, int howto)
1940 if (howto & RB_NOSYNC)
1942 mtx_lock(&sync_mtx);
1943 syncer_state = SYNCER_SHUTTING_DOWN;
1945 mtx_unlock(&sync_mtx);
1946 cv_broadcast(&sync_wakeup);
1947 kproc_shutdown(arg, howto);
1951 * Reassign a buffer from one vnode to another.
1952 * Used to assign file specific control information
1953 * (indirect blocks) to the vnode to which they belong.
1956 reassignbuf(struct buf *bp)
1969 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X",
1970 bp, bp->b_vp, bp->b_flags);
1972 * B_PAGING flagged buffers cannot be reassigned because their vp
1973 * is not fully linked in.
1975 if (bp->b_flags & B_PAGING)
1976 panic("cannot reassign paging buffer");
1979 * Delete from old vnode list, if on one.
1982 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1983 buf_vlist_remove(bp);
1985 panic("reassignbuf: Buffer %p not on queue.", bp);
1987 * If dirty, put on list of dirty buffers; otherwise insert onto list
1990 if (bp->b_flags & B_DELWRI) {
1991 if ((bo->bo_flag & BO_ONWORKLST) == 0) {
1992 switch (vp->v_type) {
2002 vn_syncer_add_to_worklist(bo, delay);
2004 buf_vlist_add(bp, bo, BX_VNDIRTY);
2006 buf_vlist_add(bp, bo, BX_VNCLEAN);
2008 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
2009 mtx_lock(&sync_mtx);
2010 LIST_REMOVE(bo, bo_synclist);
2011 syncer_worklist_len--;
2012 mtx_unlock(&sync_mtx);
2013 bo->bo_flag &= ~BO_ONWORKLST;
2018 bp = TAILQ_FIRST(&bv->bv_hd);
2019 KASSERT(bp == NULL || bp->b_bufobj == bo,
2020 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2021 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2022 KASSERT(bp == NULL || bp->b_bufobj == bo,
2023 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2025 bp = TAILQ_FIRST(&bv->bv_hd);
2026 KASSERT(bp == NULL || bp->b_bufobj == bo,
2027 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2028 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2029 KASSERT(bp == NULL || bp->b_bufobj == bo,
2030 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2036 * Increment the use and hold counts on the vnode, taking care to reference
2037 * the driver's usecount if this is a chardev. The vholdl() will remove
2038 * the vnode from the free list if it is presently free. Requires the
2039 * vnode interlock and returns with it held.
2042 v_incr_usecount(struct vnode *vp)
2045 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2047 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2049 vp->v_rdev->si_usecount++;
2056 * Turn a holdcnt into a use+holdcnt such that only one call to
2057 * v_decr_usecount is needed.
2060 v_upgrade_usecount(struct vnode *vp)
2063 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2065 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2067 vp->v_rdev->si_usecount++;
2073 * Decrement the vnode use and hold count along with the driver's usecount
2074 * if this is a chardev. The vdropl() below releases the vnode interlock
2075 * as it may free the vnode.
2078 v_decr_usecount(struct vnode *vp)
2081 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2082 VNASSERT(vp->v_usecount > 0, vp,
2083 ("v_decr_usecount: negative usecount"));
2084 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2086 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2088 vp->v_rdev->si_usecount--;
2095 * Decrement only the use count and driver use count. This is intended to
2096 * be paired with a follow on vdropl() to release the remaining hold count.
2097 * In this way we may vgone() a vnode with a 0 usecount without risk of
2098 * having it end up on a free list because the hold count is kept above 0.
2101 v_decr_useonly(struct vnode *vp)
2104 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2105 VNASSERT(vp->v_usecount > 0, vp,
2106 ("v_decr_useonly: negative usecount"));
2107 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2109 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2111 vp->v_rdev->si_usecount--;
2117 * Grab a particular vnode from the free list, increment its
2118 * reference count and lock it. VI_DOOMED is set if the vnode
2119 * is being destroyed. Only callers who specify LK_RETRY will
2120 * see doomed vnodes. If inactive processing was delayed in
2121 * vput try to do it here.
2124 vget(struct vnode *vp, int flags, struct thread *td)
2129 VFS_ASSERT_GIANT(vp->v_mount);
2130 VNASSERT((flags & LK_TYPE_MASK) != 0, vp,
2131 ("vget: invalid lock operation"));
2132 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
2134 if ((flags & LK_INTERLOCK) == 0)
2137 if ((error = vn_lock(vp, flags | LK_INTERLOCK)) != 0) {
2139 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__,
2143 if (vp->v_iflag & VI_DOOMED && (flags & LK_RETRY) == 0)
2144 panic("vget: vn_lock failed to return ENOENT\n");
2146 /* Upgrade our holdcnt to a usecount. */
2147 v_upgrade_usecount(vp);
2149 * We don't guarantee that any particular close will
2150 * trigger inactive processing so just make a best effort
2151 * here at preventing a reference to a removed file. If
2152 * we don't succeed no harm is done.
2154 if (vp->v_iflag & VI_OWEINACT) {
2155 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE &&
2156 (flags & LK_NOWAIT) == 0)
2158 vp->v_iflag &= ~VI_OWEINACT;
2165 * Increase the reference count of a vnode.
2168 vref(struct vnode *vp)
2171 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2173 v_incr_usecount(vp);
2178 * Return reference count of a vnode.
2180 * The results of this call are only guaranteed when some mechanism other
2181 * than the VI lock is used to stop other processes from gaining references
2182 * to the vnode. This may be the case if the caller holds the only reference.
2183 * This is also useful when stale data is acceptable as race conditions may
2184 * be accounted for by some other means.
2187 vrefcnt(struct vnode *vp)
2192 usecnt = vp->v_usecount;
2198 #define VPUTX_VRELE 1
2199 #define VPUTX_VPUT 2
2200 #define VPUTX_VUNREF 3
2203 vputx(struct vnode *vp, int func)
2207 KASSERT(vp != NULL, ("vputx: null vp"));
2208 if (func == VPUTX_VUNREF)
2209 ASSERT_VOP_LOCKED(vp, "vunref");
2210 else if (func == VPUTX_VPUT)
2211 ASSERT_VOP_LOCKED(vp, "vput");
2213 KASSERT(func == VPUTX_VRELE, ("vputx: wrong func"));
2214 VFS_ASSERT_GIANT(vp->v_mount);
2215 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2218 /* Skip this v_writecount check if we're going to panic below. */
2219 VNASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, vp,
2220 ("vputx: missed vn_close"));
2223 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) &&
2224 vp->v_usecount == 1)) {
2225 if (func == VPUTX_VPUT)
2227 v_decr_usecount(vp);
2231 if (vp->v_usecount != 1) {
2232 vprint("vputx: negative ref count", vp);
2233 panic("vputx: negative ref cnt");
2235 CTR2(KTR_VFS, "%s: return vnode %p to the freelist", __func__, vp);
2237 * We want to hold the vnode until the inactive finishes to
2238 * prevent vgone() races. We drop the use count here and the
2239 * hold count below when we're done.
2243 * We must call VOP_INACTIVE with the node locked. Mark
2244 * as VI_DOINGINACT to avoid recursion.
2246 vp->v_iflag |= VI_OWEINACT;
2249 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
2253 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2254 error = VOP_LOCK(vp, LK_UPGRADE | LK_INTERLOCK |
2260 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
2264 if (vp->v_usecount > 0)
2265 vp->v_iflag &= ~VI_OWEINACT;
2267 if (vp->v_iflag & VI_OWEINACT)
2268 vinactive(vp, curthread);
2269 if (func != VPUTX_VUNREF)
2276 * Vnode put/release.
2277 * If count drops to zero, call inactive routine and return to freelist.
2280 vrele(struct vnode *vp)
2283 vputx(vp, VPUTX_VRELE);
2287 * Release an already locked vnode. This give the same effects as
2288 * unlock+vrele(), but takes less time and avoids releasing and
2289 * re-aquiring the lock (as vrele() acquires the lock internally.)
2292 vput(struct vnode *vp)
2295 vputx(vp, VPUTX_VPUT);
2299 * Release an exclusively locked vnode. Do not unlock the vnode lock.
2302 vunref(struct vnode *vp)
2305 vputx(vp, VPUTX_VUNREF);
2309 * Somebody doesn't want the vnode recycled.
2312 vhold(struct vnode *vp)
2321 vholdl(struct vnode *vp)
2324 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2326 if (VSHOULDBUSY(vp))
2331 * Note that there is one less who cares about this vnode. vdrop() is the
2332 * opposite of vhold().
2335 vdrop(struct vnode *vp)
2343 * Drop the hold count of the vnode. If this is the last reference to
2344 * the vnode we will free it if it has been vgone'd otherwise it is
2345 * placed on the free list.
2348 vdropl(struct vnode *vp)
2351 ASSERT_VI_LOCKED(vp, "vdropl");
2352 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2353 if (vp->v_holdcnt <= 0)
2354 panic("vdrop: holdcnt %d", vp->v_holdcnt);
2356 if (vp->v_holdcnt == 0) {
2357 if (vp->v_iflag & VI_DOOMED) {
2358 CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__,
2369 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT
2370 * flags. DOINGINACT prevents us from recursing in calls to vinactive.
2371 * OWEINACT tracks whether a vnode missed a call to inactive due to a
2372 * failed lock upgrade.
2375 vinactive(struct vnode *vp, struct thread *td)
2378 ASSERT_VOP_ELOCKED(vp, "vinactive");
2379 ASSERT_VI_LOCKED(vp, "vinactive");
2380 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
2381 ("vinactive: recursed on VI_DOINGINACT"));
2382 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2383 vp->v_iflag |= VI_DOINGINACT;
2384 vp->v_iflag &= ~VI_OWEINACT;
2386 VOP_INACTIVE(vp, td);
2388 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
2389 ("vinactive: lost VI_DOINGINACT"));
2390 vp->v_iflag &= ~VI_DOINGINACT;
2394 * Remove any vnodes in the vnode table belonging to mount point mp.
2396 * If FORCECLOSE is not specified, there should not be any active ones,
2397 * return error if any are found (nb: this is a user error, not a
2398 * system error). If FORCECLOSE is specified, detach any active vnodes
2401 * If WRITECLOSE is set, only flush out regular file vnodes open for
2404 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
2406 * `rootrefs' specifies the base reference count for the root vnode
2407 * of this filesystem. The root vnode is considered busy if its
2408 * v_usecount exceeds this value. On a successful return, vflush(, td)
2409 * will call vrele() on the root vnode exactly rootrefs times.
2410 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
2414 static int busyprt = 0; /* print out busy vnodes */
2415 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "Print out busy vnodes");
2419 vflush( struct mount *mp, int rootrefs, int flags, struct thread *td)
2421 struct vnode *vp, *mvp, *rootvp = NULL;
2423 int busy = 0, error;
2425 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp,
2428 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
2429 ("vflush: bad args"));
2431 * Get the filesystem root vnode. We can vput() it
2432 * immediately, since with rootrefs > 0, it won't go away.
2434 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) {
2435 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d",
2444 MNT_VNODE_FOREACH(vp, mp, mvp) {
2449 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE);
2453 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
2457 * Skip over a vnodes marked VV_SYSTEM.
2459 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
2466 * If WRITECLOSE is set, flush out unlinked but still open
2467 * files (even if open only for reading) and regular file
2468 * vnodes open for writing.
2470 if (flags & WRITECLOSE) {
2471 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
2474 if ((vp->v_type == VNON ||
2475 (error == 0 && vattr.va_nlink > 0)) &&
2476 (vp->v_writecount == 0 || vp->v_type != VREG)) {
2485 * With v_usecount == 0, all we need to do is clear out the
2486 * vnode data structures and we are done.
2488 * If FORCECLOSE is set, forcibly close the vnode.
2490 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) {
2491 VNASSERT(vp->v_usecount == 0 ||
2492 (vp->v_type != VCHR && vp->v_type != VBLK), vp,
2493 ("device VNODE %p is FORCECLOSED", vp));
2499 vprint("vflush: busy vnode", vp);
2507 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
2509 * If just the root vnode is busy, and if its refcount
2510 * is equal to `rootrefs', then go ahead and kill it.
2513 KASSERT(busy > 0, ("vflush: not busy"));
2514 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp,
2515 ("vflush: usecount %d < rootrefs %d",
2516 rootvp->v_usecount, rootrefs));
2517 if (busy == 1 && rootvp->v_usecount == rootrefs) {
2518 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK);
2520 VOP_UNLOCK(rootvp, 0);
2526 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__,
2530 for (; rootrefs > 0; rootrefs--)
2536 * Recycle an unused vnode to the front of the free list.
2539 vrecycle(struct vnode *vp, struct thread *td)
2543 ASSERT_VOP_ELOCKED(vp, "vrecycle");
2544 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2547 if (vp->v_usecount == 0) {
2556 * Eliminate all activity associated with a vnode
2557 * in preparation for reuse.
2560 vgone(struct vnode *vp)
2568 * vgone, with the vp interlock held.
2571 vgonel(struct vnode *vp)
2578 ASSERT_VOP_ELOCKED(vp, "vgonel");
2579 ASSERT_VI_LOCKED(vp, "vgonel");
2580 VNASSERT(vp->v_holdcnt, vp,
2581 ("vgonel: vp %p has no reference.", vp));
2582 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2586 * Don't vgonel if we're already doomed.
2588 if (vp->v_iflag & VI_DOOMED)
2590 vp->v_iflag |= VI_DOOMED;
2592 * Check to see if the vnode is in use. If so, we have to call
2593 * VOP_CLOSE() and VOP_INACTIVE().
2595 active = vp->v_usecount;
2596 oweinact = (vp->v_iflag & VI_OWEINACT);
2599 * Clean out any buffers associated with the vnode.
2600 * If the flush fails, just toss the buffers.
2603 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd))
2604 (void) vn_start_secondary_write(vp, &mp, V_WAIT);
2605 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0)
2606 vinvalbuf(vp, 0, 0, 0);
2609 * If purging an active vnode, it must be closed and
2610 * deactivated before being reclaimed.
2613 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
2614 if (oweinact || active) {
2616 if ((vp->v_iflag & VI_DOINGINACT) == 0)
2621 * Reclaim the vnode.
2623 if (VOP_RECLAIM(vp, td))
2624 panic("vgone: cannot reclaim");
2626 vn_finished_secondary_write(mp);
2627 VNASSERT(vp->v_object == NULL, vp,
2628 ("vop_reclaim left v_object vp=%p, tag=%s", vp, vp->v_tag));
2630 * Clear the advisory locks and wake up waiting threads.
2632 lf_purgelocks(vp, &(vp->v_lockf));
2634 * Delete from old mount point vnode list.
2639 * Done with purge, reset to the standard lock and invalidate
2643 vp->v_vnlock = &vp->v_lock;
2644 vp->v_op = &dead_vnodeops;
2650 * Calculate the total number of references to a special device.
2653 vcount(struct vnode *vp)
2658 count = vp->v_rdev->si_usecount;
2664 * Same as above, but using the struct cdev *as argument
2667 count_dev(struct cdev *dev)
2672 count = dev->si_usecount;
2678 * Print out a description of a vnode.
2680 static char *typename[] =
2681 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD",
2685 vn_printf(struct vnode *vp, const char *fmt, ...)
2688 char buf[256], buf2[16];
2694 printf("%p: ", (void *)vp);
2695 printf("tag %s, type %s\n", vp->v_tag, typename[vp->v_type]);
2696 printf(" usecount %d, writecount %d, refcount %d mountedhere %p\n",
2697 vp->v_usecount, vp->v_writecount, vp->v_holdcnt, vp->v_mountedhere);
2700 if (vp->v_vflag & VV_ROOT)
2701 strlcat(buf, "|VV_ROOT", sizeof(buf));
2702 if (vp->v_vflag & VV_ISTTY)
2703 strlcat(buf, "|VV_ISTTY", sizeof(buf));
2704 if (vp->v_vflag & VV_NOSYNC)
2705 strlcat(buf, "|VV_NOSYNC", sizeof(buf));
2706 if (vp->v_vflag & VV_CACHEDLABEL)
2707 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf));
2708 if (vp->v_vflag & VV_TEXT)
2709 strlcat(buf, "|VV_TEXT", sizeof(buf));
2710 if (vp->v_vflag & VV_COPYONWRITE)
2711 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf));
2712 if (vp->v_vflag & VV_SYSTEM)
2713 strlcat(buf, "|VV_SYSTEM", sizeof(buf));
2714 if (vp->v_vflag & VV_PROCDEP)
2715 strlcat(buf, "|VV_PROCDEP", sizeof(buf));
2716 if (vp->v_vflag & VV_NOKNOTE)
2717 strlcat(buf, "|VV_NOKNOTE", sizeof(buf));
2718 if (vp->v_vflag & VV_DELETED)
2719 strlcat(buf, "|VV_DELETED", sizeof(buf));
2720 if (vp->v_vflag & VV_MD)
2721 strlcat(buf, "|VV_MD", sizeof(buf));
2722 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC |
2723 VV_CACHEDLABEL | VV_TEXT | VV_COPYONWRITE | VV_SYSTEM | VV_PROCDEP |
2724 VV_NOKNOTE | VV_DELETED | VV_MD);
2726 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags);
2727 strlcat(buf, buf2, sizeof(buf));
2729 if (vp->v_iflag & VI_MOUNT)
2730 strlcat(buf, "|VI_MOUNT", sizeof(buf));
2731 if (vp->v_iflag & VI_AGE)
2732 strlcat(buf, "|VI_AGE", sizeof(buf));
2733 if (vp->v_iflag & VI_DOOMED)
2734 strlcat(buf, "|VI_DOOMED", sizeof(buf));
2735 if (vp->v_iflag & VI_FREE)
2736 strlcat(buf, "|VI_FREE", sizeof(buf));
2737 if (vp->v_iflag & VI_DOINGINACT)
2738 strlcat(buf, "|VI_DOINGINACT", sizeof(buf));
2739 if (vp->v_iflag & VI_OWEINACT)
2740 strlcat(buf, "|VI_OWEINACT", sizeof(buf));
2741 flags = vp->v_iflag & ~(VI_MOUNT | VI_AGE | VI_DOOMED | VI_FREE |
2742 VI_DOINGINACT | VI_OWEINACT);
2744 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags);
2745 strlcat(buf, buf2, sizeof(buf));
2747 printf(" flags (%s)\n", buf + 1);
2748 if (mtx_owned(VI_MTX(vp)))
2749 printf(" VI_LOCKed");
2750 if (vp->v_object != NULL)
2751 printf(" v_object %p ref %d pages %d\n",
2752 vp->v_object, vp->v_object->ref_count,
2753 vp->v_object->resident_page_count);
2755 lockmgr_printinfo(vp->v_vnlock);
2756 if (vp->v_data != NULL)
2762 * List all of the locked vnodes in the system.
2763 * Called when debugging the kernel.
2765 DB_SHOW_COMMAND(lockedvnods, lockedvnodes)
2767 struct mount *mp, *nmp;
2771 * Note: because this is DDB, we can't obey the locking semantics
2772 * for these structures, which means we could catch an inconsistent
2773 * state and dereference a nasty pointer. Not much to be done
2776 db_printf("Locked vnodes\n");
2777 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2778 nmp = TAILQ_NEXT(mp, mnt_list);
2779 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
2780 if (vp->v_type != VMARKER &&
2784 nmp = TAILQ_NEXT(mp, mnt_list);
2789 * Show details about the given vnode.
2791 DB_SHOW_COMMAND(vnode, db_show_vnode)
2797 vp = (struct vnode *)addr;
2798 vn_printf(vp, "vnode ");
2802 * Show details about the given mount point.
2804 DB_SHOW_COMMAND(mount, db_show_mount)
2814 /* No address given, print short info about all mount points. */
2815 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2816 db_printf("%p %s on %s (%s)\n", mp,
2817 mp->mnt_stat.f_mntfromname,
2818 mp->mnt_stat.f_mntonname,
2819 mp->mnt_stat.f_fstypename);
2823 db_printf("\nMore info: show mount <addr>\n");
2827 mp = (struct mount *)addr;
2828 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname,
2829 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename);
2832 flags = mp->mnt_flag;
2833 #define MNT_FLAG(flag) do { \
2834 if (flags & (flag)) { \
2835 if (buf[0] != '\0') \
2836 strlcat(buf, ", ", sizeof(buf)); \
2837 strlcat(buf, (#flag) + 4, sizeof(buf)); \
2841 MNT_FLAG(MNT_RDONLY);
2842 MNT_FLAG(MNT_SYNCHRONOUS);
2843 MNT_FLAG(MNT_NOEXEC);
2844 MNT_FLAG(MNT_NOSUID);
2845 MNT_FLAG(MNT_UNION);
2846 MNT_FLAG(MNT_ASYNC);
2847 MNT_FLAG(MNT_SUIDDIR);
2848 MNT_FLAG(MNT_SOFTDEP);
2849 MNT_FLAG(MNT_NOSYMFOLLOW);
2850 MNT_FLAG(MNT_GJOURNAL);
2851 MNT_FLAG(MNT_MULTILABEL);
2853 MNT_FLAG(MNT_NOATIME);
2854 MNT_FLAG(MNT_NOCLUSTERR);
2855 MNT_FLAG(MNT_NOCLUSTERW);
2856 MNT_FLAG(MNT_NFS4ACLS);
2857 MNT_FLAG(MNT_EXRDONLY);
2858 MNT_FLAG(MNT_EXPORTED);
2859 MNT_FLAG(MNT_DEFEXPORTED);
2860 MNT_FLAG(MNT_EXPORTANON);
2861 MNT_FLAG(MNT_EXKERB);
2862 MNT_FLAG(MNT_EXPUBLIC);
2863 MNT_FLAG(MNT_LOCAL);
2864 MNT_FLAG(MNT_QUOTA);
2865 MNT_FLAG(MNT_ROOTFS);
2867 MNT_FLAG(MNT_IGNORE);
2868 MNT_FLAG(MNT_UPDATE);
2869 MNT_FLAG(MNT_DELEXPORT);
2870 MNT_FLAG(MNT_RELOAD);
2871 MNT_FLAG(MNT_FORCE);
2872 MNT_FLAG(MNT_SNAPSHOT);
2873 MNT_FLAG(MNT_BYFSID);
2877 strlcat(buf, ", ", sizeof(buf));
2878 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
2881 db_printf(" mnt_flag = %s\n", buf);
2884 flags = mp->mnt_kern_flag;
2885 #define MNT_KERN_FLAG(flag) do { \
2886 if (flags & (flag)) { \
2887 if (buf[0] != '\0') \
2888 strlcat(buf, ", ", sizeof(buf)); \
2889 strlcat(buf, (#flag) + 5, sizeof(buf)); \
2893 MNT_KERN_FLAG(MNTK_UNMOUNTF);
2894 MNT_KERN_FLAG(MNTK_ASYNC);
2895 MNT_KERN_FLAG(MNTK_SOFTDEP);
2896 MNT_KERN_FLAG(MNTK_NOINSMNTQ);
2897 MNT_KERN_FLAG(MNTK_UNMOUNT);
2898 MNT_KERN_FLAG(MNTK_MWAIT);
2899 MNT_KERN_FLAG(MNTK_SUSPEND);
2900 MNT_KERN_FLAG(MNTK_SUSPEND2);
2901 MNT_KERN_FLAG(MNTK_SUSPENDED);
2902 MNT_KERN_FLAG(MNTK_MPSAFE);
2903 MNT_KERN_FLAG(MNTK_NOKNOTE);
2904 MNT_KERN_FLAG(MNTK_LOOKUP_SHARED);
2905 #undef MNT_KERN_FLAG
2908 strlcat(buf, ", ", sizeof(buf));
2909 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
2912 db_printf(" mnt_kern_flag = %s\n", buf);
2914 db_printf(" mnt_opt = ");
2915 opt = TAILQ_FIRST(mp->mnt_opt);
2917 db_printf("%s", opt->name);
2918 opt = TAILQ_NEXT(opt, link);
2919 while (opt != NULL) {
2920 db_printf(", %s", opt->name);
2921 opt = TAILQ_NEXT(opt, link);
2927 db_printf(" mnt_stat = { version=%u type=%u flags=0x%016jx "
2928 "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju "
2929 "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju "
2930 "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n",
2931 (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags,
2932 (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize,
2933 (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree,
2934 (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files,
2935 (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites,
2936 (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads,
2937 (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax,
2938 (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]);
2940 db_printf(" mnt_cred = { uid=%u ruid=%u",
2941 (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid);
2942 if (jailed(mp->mnt_cred))
2943 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id);
2945 db_printf(" mnt_ref = %d\n", mp->mnt_ref);
2946 db_printf(" mnt_gen = %d\n", mp->mnt_gen);
2947 db_printf(" mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize);
2948 db_printf(" mnt_writeopcount = %d\n", mp->mnt_writeopcount);
2949 db_printf(" mnt_noasync = %u\n", mp->mnt_noasync);
2950 db_printf(" mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen);
2951 db_printf(" mnt_iosize_max = %d\n", mp->mnt_iosize_max);
2952 db_printf(" mnt_hashseed = %u\n", mp->mnt_hashseed);
2953 db_printf(" mnt_secondary_writes = %d\n", mp->mnt_secondary_writes);
2954 db_printf(" mnt_secondary_accwrites = %d\n",
2955 mp->mnt_secondary_accwrites);
2956 db_printf(" mnt_gjprovider = %s\n",
2957 mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL");
2960 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
2961 if (vp->v_type != VMARKER) {
2962 vn_printf(vp, "vnode ");
2971 * Fill in a struct xvfsconf based on a struct vfsconf.
2974 vfsconf2x(struct vfsconf *vfsp, struct xvfsconf *xvfsp)
2977 strcpy(xvfsp->vfc_name, vfsp->vfc_name);
2978 xvfsp->vfc_typenum = vfsp->vfc_typenum;
2979 xvfsp->vfc_refcount = vfsp->vfc_refcount;
2980 xvfsp->vfc_flags = vfsp->vfc_flags;
2982 * These are unused in userland, we keep them
2983 * to not break binary compatibility.
2985 xvfsp->vfc_vfsops = NULL;
2986 xvfsp->vfc_next = NULL;
2990 * Top level filesystem related information gathering.
2993 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS)
2995 struct vfsconf *vfsp;
2996 struct xvfsconf xvfsp;
3000 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3001 bzero(&xvfsp, sizeof(xvfsp));
3002 vfsconf2x(vfsp, &xvfsp);
3003 error = SYSCTL_OUT(req, &xvfsp, sizeof xvfsp);
3010 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLFLAG_RD, NULL, 0, sysctl_vfs_conflist,
3011 "S,xvfsconf", "List of all configured filesystems");
3013 #ifndef BURN_BRIDGES
3014 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS);
3017 vfs_sysctl(SYSCTL_HANDLER_ARGS)
3019 int *name = (int *)arg1 - 1; /* XXX */
3020 u_int namelen = arg2 + 1; /* XXX */
3021 struct vfsconf *vfsp;
3022 struct xvfsconf xvfsp;
3024 printf("WARNING: userland calling deprecated sysctl, "
3025 "please rebuild world\n");
3027 #if 1 || defined(COMPAT_PRELITE2)
3028 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
3030 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
3034 case VFS_MAXTYPENUM:
3037 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
3040 return (ENOTDIR); /* overloaded */
3041 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list)
3042 if (vfsp->vfc_typenum == name[2])
3045 return (EOPNOTSUPP);
3046 bzero(&xvfsp, sizeof(xvfsp));
3047 vfsconf2x(vfsp, &xvfsp);
3048 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3050 return (EOPNOTSUPP);
3053 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP,
3054 vfs_sysctl, "Generic filesystem");
3056 #if 1 || defined(COMPAT_PRELITE2)
3059 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
3062 struct vfsconf *vfsp;
3063 struct ovfsconf ovfs;
3065 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3066 bzero(&ovfs, sizeof(ovfs));
3067 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
3068 strcpy(ovfs.vfc_name, vfsp->vfc_name);
3069 ovfs.vfc_index = vfsp->vfc_typenum;
3070 ovfs.vfc_refcount = vfsp->vfc_refcount;
3071 ovfs.vfc_flags = vfsp->vfc_flags;
3072 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
3079 #endif /* 1 || COMPAT_PRELITE2 */
3080 #endif /* !BURN_BRIDGES */
3082 #define KINFO_VNODESLOP 10
3085 * Dump vnode list (via sysctl).
3089 sysctl_vnode(SYSCTL_HANDLER_ARGS)
3097 * Stale numvnodes access is not fatal here.
3100 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn;
3102 /* Make an estimate */
3103 return (SYSCTL_OUT(req, 0, len));
3105 error = sysctl_wire_old_buffer(req, 0);
3108 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK);
3110 mtx_lock(&mountlist_mtx);
3111 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3112 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK))
3115 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3119 xvn[n].xv_size = sizeof *xvn;
3120 xvn[n].xv_vnode = vp;
3121 xvn[n].xv_id = 0; /* XXX compat */
3122 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field
3124 XV_COPY(writecount);
3130 xvn[n].xv_flag = vp->v_vflag;
3132 switch (vp->v_type) {
3139 if (vp->v_rdev == NULL) {
3143 xvn[n].xv_dev = dev2udev(vp->v_rdev);
3146 xvn[n].xv_socket = vp->v_socket;
3149 xvn[n].xv_fifo = vp->v_fifoinfo;
3154 /* shouldn't happen? */
3162 mtx_lock(&mountlist_mtx);
3167 mtx_unlock(&mountlist_mtx);
3169 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn);
3174 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD,
3175 0, 0, sysctl_vnode, "S,xvnode", "");
3179 * Unmount all filesystems. The list is traversed in reverse order
3180 * of mounting to avoid dependencies.
3183 vfs_unmountall(void)
3189 KASSERT(curthread != NULL, ("vfs_unmountall: NULL curthread"));
3190 CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__);
3194 * Since this only runs when rebooting, it is not interlocked.
3196 while(!TAILQ_EMPTY(&mountlist)) {
3197 mp = TAILQ_LAST(&mountlist, mntlist);
3198 error = dounmount(mp, MNT_FORCE, td);
3200 TAILQ_REMOVE(&mountlist, mp, mnt_list);
3202 * XXX: Due to the way in which we mount the root
3203 * file system off of devfs, devfs will generate a
3204 * "busy" warning when we try to unmount it before
3205 * the root. Don't print a warning as a result in
3206 * order to avoid false positive errors that may
3207 * cause needless upset.
3209 if (strcmp(mp->mnt_vfc->vfc_name, "devfs") != 0) {
3210 printf("unmount of %s failed (",
3211 mp->mnt_stat.f_mntonname);
3215 printf("%d)\n", error);
3218 /* The unmount has removed mp from the mountlist */
3224 * perform msync on all vnodes under a mount point
3225 * the mount point must be locked.
3228 vfs_msync(struct mount *mp, int flags)
3230 struct vnode *vp, *mvp;
3231 struct vm_object *obj;
3233 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
3235 MNT_VNODE_FOREACH(vp, mp, mvp) {
3238 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0 &&
3239 (flags == MNT_WAIT || VOP_ISLOCKED(vp) == 0)) {
3242 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK,
3244 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */
3252 VM_OBJECT_LOCK(obj);
3253 vm_object_page_clean(obj, 0, 0,
3255 OBJPC_SYNC : OBJPC_NOSYNC);
3256 VM_OBJECT_UNLOCK(obj);
3268 * Mark a vnode as free, putting it up for recycling.
3271 vfree(struct vnode *vp)
3274 ASSERT_VI_LOCKED(vp, "vfree");
3275 mtx_lock(&vnode_free_list_mtx);
3276 VNASSERT(vp->v_op != NULL, vp, ("vfree: vnode already reclaimed."));
3277 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp, ("vnode already free"));
3278 VNASSERT(VSHOULDFREE(vp), vp, ("vfree: freeing when we shouldn't"));
3279 VNASSERT((vp->v_iflag & VI_DOOMED) == 0, vp,
3280 ("vfree: Freeing doomed vnode"));
3281 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3282 if (vp->v_iflag & VI_AGE) {
3283 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
3285 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
3288 vp->v_iflag &= ~VI_AGE;
3289 vp->v_iflag |= VI_FREE;
3290 mtx_unlock(&vnode_free_list_mtx);
3294 * Opposite of vfree() - mark a vnode as in use.
3297 vbusy(struct vnode *vp)
3299 ASSERT_VI_LOCKED(vp, "vbusy");
3300 VNASSERT((vp->v_iflag & VI_FREE) != 0, vp, ("vnode not free"));
3301 VNASSERT(vp->v_op != NULL, vp, ("vbusy: vnode already reclaimed."));
3302 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3304 mtx_lock(&vnode_free_list_mtx);
3305 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
3307 vp->v_iflag &= ~(VI_FREE|VI_AGE);
3308 mtx_unlock(&vnode_free_list_mtx);
3312 destroy_vpollinfo(struct vpollinfo *vi)
3314 seldrain(&vi->vpi_selinfo);
3315 knlist_destroy(&vi->vpi_selinfo.si_note);
3316 mtx_destroy(&vi->vpi_lock);
3317 uma_zfree(vnodepoll_zone, vi);
3321 * Initalize per-vnode helper structure to hold poll-related state.
3324 v_addpollinfo(struct vnode *vp)
3326 struct vpollinfo *vi;
3328 if (vp->v_pollinfo != NULL)
3330 vi = uma_zalloc(vnodepoll_zone, M_WAITOK);
3331 mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF);
3332 knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock,
3333 vfs_knlunlock, vfs_knl_assert_locked, vfs_knl_assert_unlocked);
3335 if (vp->v_pollinfo != NULL) {
3337 destroy_vpollinfo(vi);
3340 vp->v_pollinfo = vi;
3345 * Record a process's interest in events which might happen to
3346 * a vnode. Because poll uses the historic select-style interface
3347 * internally, this routine serves as both the ``check for any
3348 * pending events'' and the ``record my interest in future events''
3349 * functions. (These are done together, while the lock is held,
3350 * to avoid race conditions.)
3353 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
3357 mtx_lock(&vp->v_pollinfo->vpi_lock);
3358 if (vp->v_pollinfo->vpi_revents & events) {
3360 * This leaves events we are not interested
3361 * in available for the other process which
3362 * which presumably had requested them
3363 * (otherwise they would never have been
3366 events &= vp->v_pollinfo->vpi_revents;
3367 vp->v_pollinfo->vpi_revents &= ~events;
3369 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3372 vp->v_pollinfo->vpi_events |= events;
3373 selrecord(td, &vp->v_pollinfo->vpi_selinfo);
3374 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3379 * Routine to create and manage a filesystem syncer vnode.
3381 #define sync_close ((int (*)(struct vop_close_args *))nullop)
3382 static int sync_fsync(struct vop_fsync_args *);
3383 static int sync_inactive(struct vop_inactive_args *);
3384 static int sync_reclaim(struct vop_reclaim_args *);
3386 static struct vop_vector sync_vnodeops = {
3387 .vop_bypass = VOP_EOPNOTSUPP,
3388 .vop_close = sync_close, /* close */
3389 .vop_fsync = sync_fsync, /* fsync */
3390 .vop_inactive = sync_inactive, /* inactive */
3391 .vop_reclaim = sync_reclaim, /* reclaim */
3392 .vop_lock1 = vop_stdlock, /* lock */
3393 .vop_unlock = vop_stdunlock, /* unlock */
3394 .vop_islocked = vop_stdislocked, /* islocked */
3398 * Create a new filesystem syncer vnode for the specified mount point.
3401 vfs_allocate_syncvnode(struct mount *mp)
3405 static long start, incr, next;
3408 /* Allocate a new vnode */
3409 if ((error = getnewvnode("syncer", mp, &sync_vnodeops, &vp)) != 0) {
3410 mp->mnt_syncer = NULL;
3414 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3415 vp->v_vflag |= VV_FORCEINSMQ;
3416 error = insmntque(vp, mp);
3418 panic("vfs_allocate_syncvnode: insmntque failed");
3419 vp->v_vflag &= ~VV_FORCEINSMQ;
3422 * Place the vnode onto the syncer worklist. We attempt to
3423 * scatter them about on the list so that they will go off
3424 * at evenly distributed times even if all the filesystems
3425 * are mounted at once.
3428 if (next == 0 || next > syncer_maxdelay) {
3432 start = syncer_maxdelay / 2;
3433 incr = syncer_maxdelay;
3439 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0);
3440 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */
3441 mtx_lock(&sync_mtx);
3443 mtx_unlock(&sync_mtx);
3445 mp->mnt_syncer = vp;
3450 * Do a lazy sync of the filesystem.
3453 sync_fsync(struct vop_fsync_args *ap)
3455 struct vnode *syncvp = ap->a_vp;
3456 struct mount *mp = syncvp->v_mount;
3461 * We only need to do something if this is a lazy evaluation.
3463 if (ap->a_waitfor != MNT_LAZY)
3467 * Move ourselves to the back of the sync list.
3469 bo = &syncvp->v_bufobj;
3471 vn_syncer_add_to_worklist(bo, syncdelay);
3475 * Walk the list of vnodes pushing all that are dirty and
3476 * not already on the sync list.
3478 mtx_lock(&mountlist_mtx);
3479 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
3480 mtx_unlock(&mountlist_mtx);
3483 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) {
3489 mp->mnt_kern_flag &= ~MNTK_ASYNC;
3491 vfs_msync(mp, MNT_NOWAIT);
3492 error = VFS_SYNC(mp, MNT_LAZY);
3495 if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0)
3496 mp->mnt_kern_flag |= MNTK_ASYNC;
3498 vn_finished_write(mp);
3504 * The syncer vnode is no referenced.
3507 sync_inactive(struct vop_inactive_args *ap)
3515 * The syncer vnode is no longer needed and is being decommissioned.
3517 * Modifications to the worklist must be protected by sync_mtx.
3520 sync_reclaim(struct vop_reclaim_args *ap)
3522 struct vnode *vp = ap->a_vp;
3527 vp->v_mount->mnt_syncer = NULL;
3528 if (bo->bo_flag & BO_ONWORKLST) {
3529 mtx_lock(&sync_mtx);
3530 LIST_REMOVE(bo, bo_synclist);
3531 syncer_worklist_len--;
3533 mtx_unlock(&sync_mtx);
3534 bo->bo_flag &= ~BO_ONWORKLST;
3542 * Check if vnode represents a disk device
3545 vn_isdisk(struct vnode *vp, int *errp)
3551 if (vp->v_type != VCHR)
3553 else if (vp->v_rdev == NULL)
3555 else if (vp->v_rdev->si_devsw == NULL)
3557 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK))
3562 return (error == 0);
3566 * Common filesystem object access control check routine. Accepts a
3567 * vnode's type, "mode", uid and gid, requested access mode, credentials,
3568 * and optional call-by-reference privused argument allowing vaccess()
3569 * to indicate to the caller whether privilege was used to satisfy the
3570 * request (obsoleted). Returns 0 on success, or an errno on failure.
3573 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid,
3574 accmode_t accmode, struct ucred *cred, int *privused)
3576 accmode_t dac_granted;
3577 accmode_t priv_granted;
3579 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0,
3580 ("invalid bit in accmode"));
3583 * Look for a normal, non-privileged way to access the file/directory
3584 * as requested. If it exists, go with that.
3587 if (privused != NULL)
3592 /* Check the owner. */
3593 if (cred->cr_uid == file_uid) {
3594 dac_granted |= VADMIN;
3595 if (file_mode & S_IXUSR)
3596 dac_granted |= VEXEC;
3597 if (file_mode & S_IRUSR)
3598 dac_granted |= VREAD;
3599 if (file_mode & S_IWUSR)
3600 dac_granted |= (VWRITE | VAPPEND);
3602 if ((accmode & dac_granted) == accmode)
3608 /* Otherwise, check the groups (first match) */
3609 if (groupmember(file_gid, cred)) {
3610 if (file_mode & S_IXGRP)
3611 dac_granted |= VEXEC;
3612 if (file_mode & S_IRGRP)
3613 dac_granted |= VREAD;
3614 if (file_mode & S_IWGRP)
3615 dac_granted |= (VWRITE | VAPPEND);
3617 if ((accmode & dac_granted) == accmode)
3623 /* Otherwise, check everyone else. */
3624 if (file_mode & S_IXOTH)
3625 dac_granted |= VEXEC;
3626 if (file_mode & S_IROTH)
3627 dac_granted |= VREAD;
3628 if (file_mode & S_IWOTH)
3629 dac_granted |= (VWRITE | VAPPEND);
3630 if ((accmode & dac_granted) == accmode)
3635 * Build a privilege mask to determine if the set of privileges
3636 * satisfies the requirements when combined with the granted mask
3637 * from above. For each privilege, if the privilege is required,
3638 * bitwise or the request type onto the priv_granted mask.
3644 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC
3645 * requests, instead of PRIV_VFS_EXEC.
3647 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3648 !priv_check_cred(cred, PRIV_VFS_LOOKUP, 0))
3649 priv_granted |= VEXEC;
3651 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3652 !priv_check_cred(cred, PRIV_VFS_EXEC, 0))
3653 priv_granted |= VEXEC;
3656 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) &&
3657 !priv_check_cred(cred, PRIV_VFS_READ, 0))
3658 priv_granted |= VREAD;
3660 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) &&
3661 !priv_check_cred(cred, PRIV_VFS_WRITE, 0))
3662 priv_granted |= (VWRITE | VAPPEND);
3664 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) &&
3665 !priv_check_cred(cred, PRIV_VFS_ADMIN, 0))
3666 priv_granted |= VADMIN;
3668 if ((accmode & (priv_granted | dac_granted)) == accmode) {
3669 /* XXX audit: privilege used */
3670 if (privused != NULL)
3675 return ((accmode & VADMIN) ? EPERM : EACCES);
3679 * Credential check based on process requesting service, and per-attribute
3683 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred,
3684 struct thread *td, accmode_t accmode)
3688 * Kernel-invoked always succeeds.
3694 * Do not allow privileged processes in jail to directly manipulate
3695 * system attributes.
3697 switch (attrnamespace) {
3698 case EXTATTR_NAMESPACE_SYSTEM:
3699 /* Potentially should be: return (EPERM); */
3700 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM, 0));
3701 case EXTATTR_NAMESPACE_USER:
3702 return (VOP_ACCESS(vp, accmode, cred, td));
3708 #ifdef DEBUG_VFS_LOCKS
3710 * This only exists to supress warnings from unlocked specfs accesses. It is
3711 * no longer ok to have an unlocked VFS.
3713 #define IGNORE_LOCK(vp) (panicstr != NULL || (vp) == NULL || \
3714 (vp)->v_type == VCHR || (vp)->v_type == VBAD)
3716 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */
3717 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0,
3718 "Drop into debugger on lock violation");
3720 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */
3721 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex,
3722 0, "Check for interlock across VOPs");
3724 int vfs_badlock_print = 1; /* Print lock violations. */
3725 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print,
3726 0, "Print lock violations");
3729 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */
3730 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW,
3731 &vfs_badlock_backtrace, 0, "Print backtrace at lock violations");
3735 vfs_badlock(const char *msg, const char *str, struct vnode *vp)
3739 if (vfs_badlock_backtrace)
3742 if (vfs_badlock_print)
3743 printf("%s: %p %s\n", str, (void *)vp, msg);
3744 if (vfs_badlock_ddb)
3745 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
3749 assert_vi_locked(struct vnode *vp, const char *str)
3752 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp)))
3753 vfs_badlock("interlock is not locked but should be", str, vp);
3757 assert_vi_unlocked(struct vnode *vp, const char *str)
3760 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp)))
3761 vfs_badlock("interlock is locked but should not be", str, vp);
3765 assert_vop_locked(struct vnode *vp, const char *str)
3768 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == 0)
3769 vfs_badlock("is not locked but should be", str, vp);
3773 assert_vop_unlocked(struct vnode *vp, const char *str)
3776 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
3777 vfs_badlock("is locked but should not be", str, vp);
3781 assert_vop_elocked(struct vnode *vp, const char *str)
3784 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
3785 vfs_badlock("is not exclusive locked but should be", str, vp);
3790 assert_vop_elocked_other(struct vnode *vp, const char *str)
3793 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLOTHER)
3794 vfs_badlock("is not exclusive locked by another thread",
3799 assert_vop_slocked(struct vnode *vp, const char *str)
3802 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_SHARED)
3803 vfs_badlock("is not locked shared but should be", str, vp);
3806 #endif /* DEBUG_VFS_LOCKS */
3809 vop_rename_fail(struct vop_rename_args *ap)
3812 if (ap->a_tvp != NULL)
3814 if (ap->a_tdvp == ap->a_tvp)
3823 vop_rename_pre(void *ap)
3825 struct vop_rename_args *a = ap;
3827 #ifdef DEBUG_VFS_LOCKS
3829 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME");
3830 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME");
3831 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME");
3832 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME");
3834 /* Check the source (from). */
3835 if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock &&
3836 (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock))
3837 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked");
3838 if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock)
3839 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked");
3841 /* Check the target. */
3843 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked");
3844 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked");
3846 if (a->a_tdvp != a->a_fdvp)
3848 if (a->a_tvp != a->a_fvp)
3856 vop_strategy_pre(void *ap)
3858 #ifdef DEBUG_VFS_LOCKS
3859 struct vop_strategy_args *a;
3866 * Cluster ops lock their component buffers but not the IO container.
3868 if ((bp->b_flags & B_CLUSTER) != 0)
3871 if (panicstr == NULL && !BUF_ISLOCKED(bp)) {
3872 if (vfs_badlock_print)
3874 "VOP_STRATEGY: bp is not locked but should be\n");
3875 if (vfs_badlock_ddb)
3876 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
3882 vop_lookup_pre(void *ap)
3884 #ifdef DEBUG_VFS_LOCKS
3885 struct vop_lookup_args *a;
3890 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP");
3891 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP");
3896 vop_lookup_post(void *ap, int rc)
3898 #ifdef DEBUG_VFS_LOCKS
3899 struct vop_lookup_args *a;
3907 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP");
3908 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP");
3911 ASSERT_VOP_LOCKED(vp, "VOP_LOOKUP (child)");
3916 vop_lock_pre(void *ap)
3918 #ifdef DEBUG_VFS_LOCKS
3919 struct vop_lock1_args *a = ap;
3921 if ((a->a_flags & LK_INTERLOCK) == 0)
3922 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
3924 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK");
3929 vop_lock_post(void *ap, int rc)
3931 #ifdef DEBUG_VFS_LOCKS
3932 struct vop_lock1_args *a = ap;
3934 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
3936 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK");
3941 vop_unlock_pre(void *ap)
3943 #ifdef DEBUG_VFS_LOCKS
3944 struct vop_unlock_args *a = ap;
3946 if (a->a_flags & LK_INTERLOCK)
3947 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK");
3948 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK");
3953 vop_unlock_post(void *ap, int rc)
3955 #ifdef DEBUG_VFS_LOCKS
3956 struct vop_unlock_args *a = ap;
3958 if (a->a_flags & LK_INTERLOCK)
3959 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK");
3964 vop_create_post(void *ap, int rc)
3966 struct vop_create_args *a = ap;
3969 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
3973 vop_link_post(void *ap, int rc)
3975 struct vop_link_args *a = ap;
3978 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK);
3979 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE);
3984 vop_mkdir_post(void *ap, int rc)
3986 struct vop_mkdir_args *a = ap;
3989 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
3993 vop_mknod_post(void *ap, int rc)
3995 struct vop_mknod_args *a = ap;
3998 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4002 vop_remove_post(void *ap, int rc)
4004 struct vop_remove_args *a = ap;
4007 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4008 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4013 vop_rename_post(void *ap, int rc)
4015 struct vop_rename_args *a = ap;
4018 VFS_KNOTE_UNLOCKED(a->a_fdvp, NOTE_WRITE);
4019 VFS_KNOTE_UNLOCKED(a->a_tdvp, NOTE_WRITE);
4020 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME);
4022 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE);
4024 if (a->a_tdvp != a->a_fdvp)
4026 if (a->a_tvp != a->a_fvp)
4034 vop_rmdir_post(void *ap, int rc)
4036 struct vop_rmdir_args *a = ap;
4039 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4040 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4045 vop_setattr_post(void *ap, int rc)
4047 struct vop_setattr_args *a = ap;
4050 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4054 vop_symlink_post(void *ap, int rc)
4056 struct vop_symlink_args *a = ap;
4059 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4062 static struct knlist fs_knlist;
4065 vfs_event_init(void *arg)
4067 knlist_init_mtx(&fs_knlist, NULL);
4069 /* XXX - correct order? */
4070 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL);
4073 vfs_event_signal(fsid_t *fsid, u_int32_t event, intptr_t data __unused)
4076 KNOTE_UNLOCKED(&fs_knlist, event);
4079 static int filt_fsattach(struct knote *kn);
4080 static void filt_fsdetach(struct knote *kn);
4081 static int filt_fsevent(struct knote *kn, long hint);
4083 struct filterops fs_filtops =
4084 { 0, filt_fsattach, filt_fsdetach, filt_fsevent };
4087 filt_fsattach(struct knote *kn)
4090 kn->kn_flags |= EV_CLEAR;
4091 knlist_add(&fs_knlist, kn, 0);
4096 filt_fsdetach(struct knote *kn)
4099 knlist_remove(&fs_knlist, kn, 0);
4103 filt_fsevent(struct knote *kn, long hint)
4106 kn->kn_fflags |= hint;
4107 return (kn->kn_fflags != 0);
4111 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS)
4117 error = SYSCTL_IN(req, &vc, sizeof(vc));
4120 if (vc.vc_vers != VFS_CTL_VERS1)
4122 mp = vfs_getvfs(&vc.vc_fsid);
4125 /* ensure that a specific sysctl goes to the right filesystem. */
4126 if (strcmp(vc.vc_fstypename, "*") != 0 &&
4127 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) {
4131 VCTLTOREQ(&vc, req);
4132 error = VFS_SYSCTL(mp, vc.vc_op, req);
4137 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLFLAG_WR, NULL, 0, sysctl_vfs_ctl, "",
4141 * Function to initialize a va_filerev field sensibly.
4142 * XXX: Wouldn't a random number make a lot more sense ??
4145 init_va_filerev(void)
4150 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL));
4153 static int filt_vfsread(struct knote *kn, long hint);
4154 static int filt_vfswrite(struct knote *kn, long hint);
4155 static int filt_vfsvnode(struct knote *kn, long hint);
4156 static void filt_vfsdetach(struct knote *kn);
4157 static struct filterops vfsread_filtops =
4158 { 1, NULL, filt_vfsdetach, filt_vfsread };
4159 static struct filterops vfswrite_filtops =
4160 { 1, NULL, filt_vfsdetach, filt_vfswrite };
4161 static struct filterops vfsvnode_filtops =
4162 { 1, NULL, filt_vfsdetach, filt_vfsvnode };
4165 vfs_knllock(void *arg)
4167 struct vnode *vp = arg;
4169 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4173 vfs_knlunlock(void *arg)
4175 struct vnode *vp = arg;
4181 vfs_knl_assert_locked(void *arg)
4183 #ifdef DEBUG_VFS_LOCKS
4184 struct vnode *vp = arg;
4186 ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked");
4191 vfs_knl_assert_unlocked(void *arg)
4193 #ifdef DEBUG_VFS_LOCKS
4194 struct vnode *vp = arg;
4196 ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked");
4201 vfs_kqfilter(struct vop_kqfilter_args *ap)
4203 struct vnode *vp = ap->a_vp;
4204 struct knote *kn = ap->a_kn;
4207 switch (kn->kn_filter) {
4209 kn->kn_fop = &vfsread_filtops;
4212 kn->kn_fop = &vfswrite_filtops;
4215 kn->kn_fop = &vfsvnode_filtops;
4221 kn->kn_hook = (caddr_t)vp;
4224 if (vp->v_pollinfo == NULL)
4226 knl = &vp->v_pollinfo->vpi_selinfo.si_note;
4227 knlist_add(knl, kn, 0);
4233 * Detach knote from vnode
4236 filt_vfsdetach(struct knote *kn)
4238 struct vnode *vp = (struct vnode *)kn->kn_hook;
4240 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo"));
4241 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
4246 filt_vfsread(struct knote *kn, long hint)
4248 struct vnode *vp = (struct vnode *)kn->kn_hook;
4253 * filesystem is gone, so set the EOF flag and schedule
4254 * the knote for deletion.
4256 if (hint == NOTE_REVOKE) {
4258 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4263 if (VOP_GETATTR(vp, &va, curthread->td_ucred))
4267 kn->kn_data = va.va_size - kn->kn_fp->f_offset;
4268 res = (kn->kn_data != 0);
4275 filt_vfswrite(struct knote *kn, long hint)
4277 struct vnode *vp = (struct vnode *)kn->kn_hook;
4282 * filesystem is gone, so set the EOF flag and schedule
4283 * the knote for deletion.
4285 if (hint == NOTE_REVOKE)
4286 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4294 filt_vfsvnode(struct knote *kn, long hint)
4296 struct vnode *vp = (struct vnode *)kn->kn_hook;
4300 if (kn->kn_sfflags & hint)
4301 kn->kn_fflags |= hint;
4302 if (hint == NOTE_REVOKE) {
4303 kn->kn_flags |= EV_EOF;
4307 res = (kn->kn_fflags != 0);
4313 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off)
4317 if (dp->d_reclen > ap->a_uio->uio_resid)
4318 return (ENAMETOOLONG);
4319 error = uiomove(dp, dp->d_reclen, ap->a_uio);
4321 if (ap->a_ncookies != NULL) {
4322 if (ap->a_cookies != NULL)
4323 free(ap->a_cookies, M_TEMP);
4324 ap->a_cookies = NULL;
4325 *ap->a_ncookies = 0;
4329 if (ap->a_ncookies == NULL)
4332 KASSERT(ap->a_cookies,
4333 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!"));
4335 *ap->a_cookies = realloc(*ap->a_cookies,
4336 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO);
4337 (*ap->a_cookies)[*ap->a_ncookies] = off;
4342 * Mark for update the access time of the file if the filesystem
4343 * supports VOP_MARKATIME. This functionality is used by execve and
4344 * mmap, so we want to avoid the I/O implied by directly setting
4345 * va_atime for the sake of efficiency.
4348 vfs_mark_atime(struct vnode *vp, struct ucred *cred)
4353 VFS_ASSERT_GIANT(mp);
4354 ASSERT_VOP_LOCKED(vp, "vfs_mark_atime");
4355 if (mp != NULL && (mp->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
4356 (void)VOP_MARKATIME(vp);
4360 * The purpose of this routine is to remove granularity from accmode_t,
4361 * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE,
4362 * VADMIN and VAPPEND.
4364 * If it returns 0, the caller is supposed to continue with the usual
4365 * access checks using 'accmode' as modified by this routine. If it
4366 * returns nonzero value, the caller is supposed to return that value
4369 * Note that after this routine runs, accmode may be zero.
4372 vfs_unixify_accmode(accmode_t *accmode)
4375 * There is no way to specify explicit "deny" rule using
4376 * file mode or POSIX.1e ACLs.
4378 if (*accmode & VEXPLICIT_DENY) {
4384 * None of these can be translated into usual access bits.
4385 * Also, the common case for NFSv4 ACLs is to not contain
4386 * either of these bits. Caller should check for VWRITE
4387 * on the containing directory instead.
4389 if (*accmode & (VDELETE_CHILD | VDELETE))
4392 if (*accmode & VADMIN_PERMS) {
4393 *accmode &= ~VADMIN_PERMS;
4398 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL
4399 * or VSYNCHRONIZE using file mode or POSIX.1e ACL.
4401 *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE);