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$");
46 #include <sys/param.h>
47 #include <sys/systm.h>
50 #include <sys/condvar.h>
52 #include <sys/dirent.h>
53 #include <sys/event.h>
54 #include <sys/eventhandler.h>
55 #include <sys/extattr.h>
57 #include <sys/fcntl.h>
60 #include <sys/kernel.h>
61 #include <sys/kthread.h>
62 #include <sys/lockf.h>
63 #include <sys/malloc.h>
64 #include <sys/mount.h>
65 #include <sys/namei.h>
67 #include <sys/reboot.h>
68 #include <sys/sleepqueue.h>
70 #include <sys/sysctl.h>
71 #include <sys/syslog.h>
72 #include <sys/vmmeter.h>
73 #include <sys/vnode.h>
75 #include <machine/stdarg.h>
77 #include <security/mac/mac_framework.h>
80 #include <vm/vm_object.h>
81 #include <vm/vm_extern.h>
83 #include <vm/vm_map.h>
84 #include <vm/vm_page.h>
85 #include <vm/vm_kern.h>
95 static MALLOC_DEFINE(M_NETADDR, "subr_export_host", "Export host address structure");
97 static void delmntque(struct vnode *vp);
98 static int flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo,
99 int slpflag, int slptimeo);
100 static void syncer_shutdown(void *arg, int howto);
101 static int vtryrecycle(struct vnode *vp);
102 static void vbusy(struct vnode *vp);
103 static void vinactive(struct vnode *, struct thread *);
104 static void v_incr_usecount(struct vnode *);
105 static void v_decr_usecount(struct vnode *);
106 static void v_decr_useonly(struct vnode *);
107 static void v_upgrade_usecount(struct vnode *);
108 static void vfree(struct vnode *);
109 static void vnlru_free(int);
110 static void vgonel(struct vnode *);
111 static void vfs_knllock(void *arg);
112 static void vfs_knlunlock(void *arg);
113 static void vfs_knl_assert_locked(void *arg);
114 static void vfs_knl_assert_unlocked(void *arg);
115 static void destroy_vpollinfo(struct vpollinfo *vi);
118 * Number of vnodes in existence. Increased whenever getnewvnode()
119 * allocates a new vnode, decreased on vdestroy() called on VI_DOOMed
122 static unsigned long numvnodes;
124 SYSCTL_LONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0,
125 "Number of vnodes in existence");
128 * Conversion tables for conversion from vnode types to inode formats
131 enum vtype iftovt_tab[16] = {
132 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
133 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
135 int vttoif_tab[10] = {
136 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
137 S_IFSOCK, S_IFIFO, S_IFMT, S_IFMT
141 * List of vnodes that are ready for recycling.
143 static TAILQ_HEAD(freelst, vnode) vnode_free_list;
146 * Free vnode target. Free vnodes may simply be files which have been stat'd
147 * but not read. This is somewhat common, and a small cache of such files
148 * should be kept to avoid recreation costs.
150 static u_long wantfreevnodes;
151 SYSCTL_LONG(_vfs, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, "");
152 /* Number of vnodes in the free list. */
153 static u_long freevnodes;
154 SYSCTL_LONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0,
155 "Number of vnodes in the free list");
157 static int vlru_allow_cache_src;
158 SYSCTL_INT(_vfs, OID_AUTO, vlru_allow_cache_src, CTLFLAG_RW,
159 &vlru_allow_cache_src, 0, "Allow vlru to reclaim source vnode");
162 * Various variables used for debugging the new implementation of
164 * XXX these are probably of (very) limited utility now.
166 static int reassignbufcalls;
167 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0,
168 "Number of calls to reassignbuf");
171 * Cache for the mount type id assigned to NFS. This is used for
172 * special checks in nfs/nfs_nqlease.c and vm/vnode_pager.c.
174 int nfs_mount_type = -1;
176 /* To keep more than one thread at a time from running vfs_getnewfsid */
177 static struct mtx mntid_mtx;
180 * Lock for any access to the following:
185 static struct mtx vnode_free_list_mtx;
187 /* Publicly exported FS */
188 struct nfs_public nfs_pub;
190 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */
191 static uma_zone_t vnode_zone;
192 static uma_zone_t vnodepoll_zone;
194 /* Set to 1 to print out reclaim of active vnodes */
198 * The workitem queue.
200 * It is useful to delay writes of file data and filesystem metadata
201 * for tens of seconds so that quickly created and deleted files need
202 * not waste disk bandwidth being created and removed. To realize this,
203 * we append vnodes to a "workitem" queue. When running with a soft
204 * updates implementation, most pending metadata dependencies should
205 * not wait for more than a few seconds. Thus, mounted on block devices
206 * are delayed only about a half the time that file data is delayed.
207 * Similarly, directory updates are more critical, so are only delayed
208 * about a third the time that file data is delayed. Thus, there are
209 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
210 * one each second (driven off the filesystem syncer process). The
211 * syncer_delayno variable indicates the next queue that is to be processed.
212 * Items that need to be processed soon are placed in this queue:
214 * syncer_workitem_pending[syncer_delayno]
216 * A delay of fifteen seconds is done by placing the request fifteen
217 * entries later in the queue:
219 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
222 static int syncer_delayno;
223 static long syncer_mask;
224 LIST_HEAD(synclist, bufobj);
225 static struct synclist *syncer_workitem_pending[2];
227 * The sync_mtx protects:
232 * syncer_workitem_pending
233 * syncer_worklist_len
236 static struct mtx sync_mtx;
237 static struct cv sync_wakeup;
239 #define SYNCER_MAXDELAY 32
240 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
241 static int syncdelay = 30; /* max time to delay syncing data */
242 static int filedelay = 30; /* time to delay syncing files */
243 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0,
244 "Time to delay syncing files (in seconds)");
245 static int dirdelay = 29; /* time to delay syncing directories */
246 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0,
247 "Time to delay syncing directories (in seconds)");
248 static int metadelay = 28; /* time to delay syncing metadata */
249 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0,
250 "Time to delay syncing metadata (in seconds)");
251 static int rushjob; /* number of slots to run ASAP */
252 static int stat_rush_requests; /* number of times I/O speeded up */
253 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0,
254 "Number of times I/O speeded up (rush requests)");
257 * When shutting down the syncer, run it at four times normal speed.
259 #define SYNCER_SHUTDOWN_SPEEDUP 4
260 static int sync_vnode_count;
261 static int syncer_worklist_len;
262 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY }
266 * Number of vnodes we want to exist at any one time. This is mostly used
267 * to size hash tables in vnode-related code. It is normally not used in
268 * getnewvnode(), as wantfreevnodes is normally nonzero.)
270 * XXX desiredvnodes is historical cruft and should not exist.
273 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
274 &desiredvnodes, 0, "Maximum number of vnodes");
275 SYSCTL_INT(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
276 &wantfreevnodes, 0, "Minimum number of vnodes (legacy)");
277 static int vnlru_nowhere;
278 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW,
279 &vnlru_nowhere, 0, "Number of times the vnlru process ran without success");
282 * Macros to control when a vnode is freed and recycled. All require
283 * the vnode interlock.
285 #define VCANRECYCLE(vp) (((vp)->v_iflag & VI_FREE) && !(vp)->v_holdcnt)
286 #define VSHOULDFREE(vp) (!((vp)->v_iflag & VI_FREE) && !(vp)->v_holdcnt)
287 #define VSHOULDBUSY(vp) (((vp)->v_iflag & VI_FREE) && (vp)->v_holdcnt)
291 * Initialize the vnode management data structures.
293 * Reevaluate the following cap on the number of vnodes after the physical
294 * memory size exceeds 512GB. In the limit, as the physical memory size
295 * grows, the ratio of physical pages to vnodes approaches sixteen to one.
297 #ifndef MAXVNODES_MAX
298 #define MAXVNODES_MAX (512 * (1024 * 1024 * 1024 / (int)PAGE_SIZE / 16))
301 vntblinit(void *dummy __unused)
303 int physvnodes, virtvnodes;
306 * Desiredvnodes is a function of the physical memory size and the
307 * kernel's heap size. Generally speaking, it scales with the
308 * physical memory size. The ratio of desiredvnodes to physical pages
309 * is one to four until desiredvnodes exceeds 98,304. Thereafter, the
310 * marginal ratio of desiredvnodes to physical pages is one to
311 * sixteen. However, desiredvnodes is limited by the kernel's heap
312 * size. The memory required by desiredvnodes vnodes and vm objects
313 * may not exceed one seventh of the kernel's heap size.
315 physvnodes = maxproc + cnt.v_page_count / 16 + 3 * min(98304 * 4,
316 cnt.v_page_count) / 16;
317 virtvnodes = vm_kmem_size / (7 * (sizeof(struct vm_object) +
318 sizeof(struct vnode)));
319 desiredvnodes = min(physvnodes, virtvnodes);
320 if (desiredvnodes > MAXVNODES_MAX) {
322 printf("Reducing kern.maxvnodes %d -> %d\n",
323 desiredvnodes, MAXVNODES_MAX);
324 desiredvnodes = MAXVNODES_MAX;
326 wantfreevnodes = desiredvnodes / 4;
327 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF);
328 TAILQ_INIT(&vnode_free_list);
329 mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF);
330 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL,
331 NULL, NULL, UMA_ALIGN_PTR, 0);
332 vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo),
333 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
335 * Initialize the filesystem syncer.
337 syncer_workitem_pending[WI_MPSAFEQ] = hashinit(syncer_maxdelay, M_VNODE,
339 syncer_workitem_pending[WI_GIANTQ] = hashinit(syncer_maxdelay, M_VNODE,
341 syncer_maxdelay = syncer_mask + 1;
342 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF);
343 cv_init(&sync_wakeup, "syncer");
345 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL);
349 * Mark a mount point as busy. Used to synchronize access and to delay
350 * unmounting. Eventually, mountlist_mtx is not released on failure.
353 vfs_busy(struct mount *mp, int flags)
356 MPASS((flags & ~MBF_MASK) == 0);
357 CTR3(KTR_VFS, "%s: mp %p with flags %d", __func__, mp, flags);
362 * If mount point is currenly being unmounted, sleep until the
363 * mount point fate is decided. If thread doing the unmounting fails,
364 * it will clear MNTK_UNMOUNT flag before waking us up, indicating
365 * that this mount point has survived the unmount attempt and vfs_busy
366 * should retry. Otherwise the unmounter thread will set MNTK_REFEXPIRE
367 * flag in addition to MNTK_UNMOUNT, indicating that mount point is
368 * about to be really destroyed. vfs_busy needs to release its
369 * reference on the mount point in this case and return with ENOENT,
370 * telling the caller that mount mount it tried to busy is no longer
373 while (mp->mnt_kern_flag & MNTK_UNMOUNT) {
374 if (flags & MBF_NOWAIT || mp->mnt_kern_flag & MNTK_REFEXPIRE) {
377 CTR1(KTR_VFS, "%s: failed busying before sleeping",
381 if (flags & MBF_MNTLSTLOCK)
382 mtx_unlock(&mountlist_mtx);
383 mp->mnt_kern_flag |= MNTK_MWAIT;
384 msleep(mp, MNT_MTX(mp), PVFS, "vfs_busy", 0);
385 if (flags & MBF_MNTLSTLOCK)
386 mtx_lock(&mountlist_mtx);
388 if (flags & MBF_MNTLSTLOCK)
389 mtx_unlock(&mountlist_mtx);
396 * Free a busy filesystem.
399 vfs_unbusy(struct mount *mp)
402 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
405 KASSERT(mp->mnt_lockref > 0, ("negative mnt_lockref"));
407 if (mp->mnt_lockref == 0 && (mp->mnt_kern_flag & MNTK_DRAINING) != 0) {
408 MPASS(mp->mnt_kern_flag & MNTK_UNMOUNT);
409 CTR1(KTR_VFS, "%s: waking up waiters", __func__);
410 mp->mnt_kern_flag &= ~MNTK_DRAINING;
411 wakeup(&mp->mnt_lockref);
417 * Lookup a mount point by filesystem identifier.
420 vfs_getvfs(fsid_t *fsid)
424 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
425 mtx_lock(&mountlist_mtx);
426 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
427 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
428 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
430 mtx_unlock(&mountlist_mtx);
434 mtx_unlock(&mountlist_mtx);
435 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
436 return ((struct mount *) 0);
440 * Lookup a mount point by filesystem identifier, busying it before
444 vfs_busyfs(fsid_t *fsid)
449 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
450 mtx_lock(&mountlist_mtx);
451 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
452 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
453 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
454 error = vfs_busy(mp, MBF_MNTLSTLOCK);
456 mtx_unlock(&mountlist_mtx);
462 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
463 mtx_unlock(&mountlist_mtx);
464 return ((struct mount *) 0);
468 * Check if a user can access privileged mount options.
471 vfs_suser(struct mount *mp, struct thread *td)
476 * If the thread is jailed, but this is not a jail-friendly file
477 * system, deny immediately.
479 if (!(mp->mnt_vfc->vfc_flags & VFCF_JAIL) && jailed(td->td_ucred))
483 * If the file system was mounted outside the jail of the calling
484 * thread, deny immediately.
486 if (prison_check(td->td_ucred, mp->mnt_cred) != 0)
490 * If file system supports delegated administration, we don't check
491 * for the PRIV_VFS_MOUNT_OWNER privilege - it will be better verified
492 * by the file system itself.
493 * If this is not the user that did original mount, we check for
494 * the PRIV_VFS_MOUNT_OWNER privilege.
496 if (!(mp->mnt_vfc->vfc_flags & VFCF_DELEGADMIN) &&
497 mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
498 if ((error = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 0)
505 * Get a new unique fsid. Try to make its val[0] unique, since this value
506 * will be used to create fake device numbers for stat(). Also try (but
507 * not so hard) make its val[0] unique mod 2^16, since some emulators only
508 * support 16-bit device numbers. We end up with unique val[0]'s for the
509 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
511 * Keep in mind that several mounts may be running in parallel. Starting
512 * the search one past where the previous search terminated is both a
513 * micro-optimization and a defense against returning the same fsid to
517 vfs_getnewfsid(struct mount *mp)
519 static u_int16_t mntid_base;
524 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
525 mtx_lock(&mntid_mtx);
526 mtype = mp->mnt_vfc->vfc_typenum;
527 tfsid.val[1] = mtype;
528 mtype = (mtype & 0xFF) << 24;
530 tfsid.val[0] = makedev(255,
531 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
533 if ((nmp = vfs_getvfs(&tfsid)) == NULL)
537 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
538 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
539 mtx_unlock(&mntid_mtx);
543 * Knob to control the precision of file timestamps:
545 * 0 = seconds only; nanoseconds zeroed.
546 * 1 = seconds and nanoseconds, accurate within 1/HZ.
547 * 2 = seconds and nanoseconds, truncated to microseconds.
548 * >=3 = seconds and nanoseconds, maximum precision.
550 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
552 static int timestamp_precision = TSP_SEC;
553 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
554 ×tamp_precision, 0, "File timestamp precision (0: seconds, "
555 "1: sec + ns accurate to 1/HZ, 2: sec + ns truncated to ms, "
556 "3+: sec + ns (max. precision))");
559 * Get a current timestamp.
562 vfs_timestamp(struct timespec *tsp)
566 switch (timestamp_precision) {
568 tsp->tv_sec = time_second;
576 TIMEVAL_TO_TIMESPEC(&tv, tsp);
586 * Set vnode attributes to VNOVAL
589 vattr_null(struct vattr *vap)
593 vap->va_size = VNOVAL;
594 vap->va_bytes = VNOVAL;
595 vap->va_mode = VNOVAL;
596 vap->va_nlink = VNOVAL;
597 vap->va_uid = VNOVAL;
598 vap->va_gid = VNOVAL;
599 vap->va_fsid = VNOVAL;
600 vap->va_fileid = VNOVAL;
601 vap->va_blocksize = VNOVAL;
602 vap->va_rdev = VNOVAL;
603 vap->va_atime.tv_sec = VNOVAL;
604 vap->va_atime.tv_nsec = VNOVAL;
605 vap->va_mtime.tv_sec = VNOVAL;
606 vap->va_mtime.tv_nsec = VNOVAL;
607 vap->va_ctime.tv_sec = VNOVAL;
608 vap->va_ctime.tv_nsec = VNOVAL;
609 vap->va_birthtime.tv_sec = VNOVAL;
610 vap->va_birthtime.tv_nsec = VNOVAL;
611 vap->va_flags = VNOVAL;
612 vap->va_gen = VNOVAL;
617 * This routine is called when we have too many vnodes. It attempts
618 * to free <count> vnodes and will potentially free vnodes that still
619 * have VM backing store (VM backing store is typically the cause
620 * of a vnode blowout so we want to do this). Therefore, this operation
621 * is not considered cheap.
623 * A number of conditions may prevent a vnode from being reclaimed.
624 * the buffer cache may have references on the vnode, a directory
625 * vnode may still have references due to the namei cache representing
626 * underlying files, or the vnode may be in active use. It is not
627 * desireable to reuse such vnodes. These conditions may cause the
628 * number of vnodes to reach some minimum value regardless of what
629 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
632 vlrureclaim(struct mount *mp)
641 * Calculate the trigger point, don't allow user
642 * screwups to blow us up. This prevents us from
643 * recycling vnodes with lots of resident pages. We
644 * aren't trying to free memory, we are trying to
647 usevnodes = desiredvnodes;
650 trigger = cnt.v_page_count * 2 / usevnodes;
652 vn_start_write(NULL, &mp, V_WAIT);
654 count = mp->mnt_nvnodelistsize / 10 + 1;
656 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
657 while (vp != NULL && vp->v_type == VMARKER)
658 vp = TAILQ_NEXT(vp, v_nmntvnodes);
661 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
662 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
667 * If it's been deconstructed already, it's still
668 * referenced, or it exceeds the trigger, skip it.
670 if (vp->v_usecount ||
671 (!vlru_allow_cache_src &&
672 !LIST_EMPTY(&(vp)->v_cache_src)) ||
673 (vp->v_iflag & VI_DOOMED) != 0 || (vp->v_object != NULL &&
674 vp->v_object->resident_page_count > trigger)) {
680 if (VOP_LOCK(vp, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT)) {
682 goto next_iter_mntunlocked;
686 * v_usecount may have been bumped after VOP_LOCK() dropped
687 * the vnode interlock and before it was locked again.
689 * It is not necessary to recheck VI_DOOMED because it can
690 * only be set by another thread that holds both the vnode
691 * lock and vnode interlock. If another thread has the
692 * vnode lock before we get to VOP_LOCK() and obtains the
693 * vnode interlock after VOP_LOCK() drops the vnode
694 * interlock, the other thread will be unable to drop the
695 * vnode lock before our VOP_LOCK() call fails.
697 if (vp->v_usecount ||
698 (!vlru_allow_cache_src &&
699 !LIST_EMPTY(&(vp)->v_cache_src)) ||
700 (vp->v_object != NULL &&
701 vp->v_object->resident_page_count > trigger)) {
702 VOP_UNLOCK(vp, LK_INTERLOCK);
703 goto next_iter_mntunlocked;
705 KASSERT((vp->v_iflag & VI_DOOMED) == 0,
706 ("VI_DOOMED unexpectedly detected in vlrureclaim()"));
711 next_iter_mntunlocked:
712 if ((count % 256) != 0)
716 if ((count % 256) != 0)
725 vn_finished_write(mp);
730 * Attempt to keep the free list at wantfreevnodes length.
733 vnlru_free(int count)
738 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
739 for (; count > 0; count--) {
740 vp = TAILQ_FIRST(&vnode_free_list);
742 * The list can be modified while the free_list_mtx
743 * has been dropped and vp could be NULL here.
747 VNASSERT(vp->v_op != NULL, vp,
748 ("vnlru_free: vnode already reclaimed."));
749 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
751 * Don't recycle if we can't get the interlock.
753 if (!VI_TRYLOCK(vp)) {
754 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
757 VNASSERT(VCANRECYCLE(vp), vp,
758 ("vp inconsistent on freelist"));
760 vp->v_iflag &= ~VI_FREE;
762 mtx_unlock(&vnode_free_list_mtx);
764 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
766 VFS_UNLOCK_GIANT(vfslocked);
768 * If the recycled succeeded this vdrop will actually free
769 * the vnode. If not it will simply place it back on
773 mtx_lock(&vnode_free_list_mtx);
777 * Attempt to recycle vnodes in a context that is always safe to block.
778 * Calling vlrurecycle() from the bowels of filesystem code has some
779 * interesting deadlock problems.
781 static struct proc *vnlruproc;
782 static int vnlruproc_sig;
787 struct mount *mp, *nmp;
789 struct proc *p = vnlruproc;
791 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, p,
795 kproc_suspend_check(p);
796 mtx_lock(&vnode_free_list_mtx);
797 if (freevnodes > wantfreevnodes)
798 vnlru_free(freevnodes - wantfreevnodes);
799 if (numvnodes <= desiredvnodes * 9 / 10) {
801 wakeup(&vnlruproc_sig);
802 msleep(vnlruproc, &vnode_free_list_mtx,
803 PVFS|PDROP, "vlruwt", hz);
806 mtx_unlock(&vnode_free_list_mtx);
808 mtx_lock(&mountlist_mtx);
809 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
810 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
811 nmp = TAILQ_NEXT(mp, mnt_list);
814 vfslocked = VFS_LOCK_GIANT(mp);
815 done += vlrureclaim(mp);
816 VFS_UNLOCK_GIANT(vfslocked);
817 mtx_lock(&mountlist_mtx);
818 nmp = TAILQ_NEXT(mp, mnt_list);
821 mtx_unlock(&mountlist_mtx);
824 /* These messages are temporary debugging aids */
825 if (vnlru_nowhere < 5)
826 printf("vnlru process getting nowhere..\n");
827 else if (vnlru_nowhere == 5)
828 printf("vnlru process messages stopped.\n");
831 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
837 static struct kproc_desc vnlru_kp = {
842 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start,
846 * Routines having to do with the management of the vnode table.
850 vdestroy(struct vnode *vp)
854 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
855 mtx_lock(&vnode_free_list_mtx);
857 mtx_unlock(&vnode_free_list_mtx);
859 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
860 ("cleaned vnode still on the free list."));
861 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't"));
862 VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count"));
863 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count"));
864 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count"));
865 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's"));
866 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0"));
867 VNASSERT(bo->bo_clean.bv_root == NULL, vp, ("cleanblkroot not NULL"));
868 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0"));
869 VNASSERT(bo->bo_dirty.bv_root == NULL, vp, ("dirtyblkroot not NULL"));
870 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst"));
871 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src"));
872 VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for .."));
875 mac_vnode_destroy(vp);
877 if (vp->v_pollinfo != NULL)
878 destroy_vpollinfo(vp->v_pollinfo);
880 /* XXX Elsewhere we can detect an already freed vnode via NULL v_op. */
883 lockdestroy(vp->v_vnlock);
884 mtx_destroy(&vp->v_interlock);
885 mtx_destroy(BO_MTX(bo));
886 uma_zfree(vnode_zone, vp);
890 * Try to recycle a freed vnode. We abort if anyone picks up a reference
891 * before we actually vgone(). This function must be called with the vnode
892 * held to prevent the vnode from being returned to the free list midway
896 vtryrecycle(struct vnode *vp)
900 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
901 VNASSERT(vp->v_holdcnt, vp,
902 ("vtryrecycle: Recycling vp %p without a reference.", vp));
904 * This vnode may found and locked via some other list, if so we
905 * can't recycle it yet.
907 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
909 "%s: impossible to recycle, vp %p lock is already held",
911 return (EWOULDBLOCK);
914 * Don't recycle if its filesystem is being suspended.
916 if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) {
919 "%s: impossible to recycle, cannot start the write for %p",
924 * If we got this far, we need to acquire the interlock and see if
925 * anyone picked up this vnode from another list. If not, we will
926 * mark it with DOOMED via vgonel() so that anyone who does find it
930 if (vp->v_usecount) {
931 VOP_UNLOCK(vp, LK_INTERLOCK);
932 vn_finished_write(vnmp);
934 "%s: impossible to recycle, %p is already referenced",
938 if ((vp->v_iflag & VI_DOOMED) == 0)
940 VOP_UNLOCK(vp, LK_INTERLOCK);
941 vn_finished_write(vnmp);
946 * Return the next vnode from the free list.
949 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops,
952 struct vnode *vp = NULL;
955 CTR3(KTR_VFS, "%s: mp %p with tag %s", __func__, mp, tag);
956 mtx_lock(&vnode_free_list_mtx);
958 * Lend our context to reclaim vnodes if they've exceeded the max.
960 if (freevnodes > wantfreevnodes)
963 * Wait for available vnodes.
965 if (numvnodes > desiredvnodes) {
966 if (mp != NULL && (mp->mnt_kern_flag & MNTK_SUSPEND)) {
968 * File system is beeing suspended, we cannot risk a
969 * deadlock here, so allocate new vnode anyway.
971 if (freevnodes > wantfreevnodes)
972 vnlru_free(freevnodes - wantfreevnodes);
975 if (vnlruproc_sig == 0) {
976 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
979 msleep(&vnlruproc_sig, &vnode_free_list_mtx, PVFS,
981 #if 0 /* XXX Not all VFS_VGET/ffs_vget callers check returns. */
982 if (numvnodes > desiredvnodes) {
983 mtx_unlock(&vnode_free_list_mtx);
990 mtx_unlock(&vnode_free_list_mtx);
991 vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK|M_ZERO);
995 vp->v_vnlock = &vp->v_lock;
996 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF);
998 * By default, don't allow shared locks unless filesystems
1001 lockinit(vp->v_vnlock, PVFS, tag, VLKTIMEOUT, LK_NOSHARE);
1003 * Initialize bufobj.
1006 bo->__bo_vnode = vp;
1007 mtx_init(BO_MTX(bo), "bufobj interlock", NULL, MTX_DEF);
1008 bo->bo_ops = &buf_ops_bio;
1009 bo->bo_private = vp;
1010 TAILQ_INIT(&bo->bo_clean.bv_hd);
1011 TAILQ_INIT(&bo->bo_dirty.bv_hd);
1013 * Initialize namecache.
1015 LIST_INIT(&vp->v_cache_src);
1016 TAILQ_INIT(&vp->v_cache_dst);
1018 * Finalize various vnode identity bits.
1023 v_incr_usecount(vp);
1027 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0)
1028 mac_vnode_associate_singlelabel(mp, vp);
1029 else if (mp == NULL && vops != &dead_vnodeops)
1030 printf("NULL mp in getnewvnode()\n");
1033 bo->bo_bsize = mp->mnt_stat.f_iosize;
1034 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0)
1035 vp->v_vflag |= VV_NOKNOTE;
1043 * Delete from old mount point vnode list, if on one.
1046 delmntque(struct vnode *vp)
1055 VNASSERT(mp->mnt_nvnodelistsize > 0, vp,
1056 ("bad mount point vnode list size"));
1057 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1058 mp->mnt_nvnodelistsize--;
1064 insmntque_stddtr(struct vnode *vp, void *dtr_arg)
1068 vp->v_op = &dead_vnodeops;
1069 /* XXX non mp-safe fs may still call insmntque with vnode
1071 if (!VOP_ISLOCKED(vp))
1072 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1078 * Insert into list of vnodes for the new mount point, if available.
1081 insmntque1(struct vnode *vp, struct mount *mp,
1082 void (*dtr)(struct vnode *, void *), void *dtr_arg)
1086 KASSERT(vp->v_mount == NULL,
1087 ("insmntque: vnode already on per mount vnode list"));
1088 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)"));
1089 #ifdef DEBUG_VFS_LOCKS
1090 if (!VFS_NEEDSGIANT(mp))
1091 ASSERT_VOP_ELOCKED(vp,
1092 "insmntque: mp-safe fs and non-locked vp");
1095 if ((mp->mnt_kern_flag & MNTK_NOINSMNTQ) != 0 &&
1096 ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 ||
1097 mp->mnt_nvnodelistsize == 0)) {
1098 locked = VOP_ISLOCKED(vp);
1099 if (!locked || (locked == LK_EXCLUSIVE &&
1100 (vp->v_vflag & VV_FORCEINSMQ) == 0)) {
1109 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1110 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp,
1111 ("neg mount point vnode list size"));
1112 mp->mnt_nvnodelistsize++;
1118 insmntque(struct vnode *vp, struct mount *mp)
1121 return (insmntque1(vp, mp, insmntque_stddtr, NULL));
1125 * Flush out and invalidate all buffers associated with a bufobj
1126 * Called with the underlying object locked.
1129 bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo)
1134 if (flags & V_SAVE) {
1135 error = bufobj_wwait(bo, slpflag, slptimeo);
1140 if (bo->bo_dirty.bv_cnt > 0) {
1142 if ((error = BO_SYNC(bo, MNT_WAIT)) != 0)
1145 * XXX We could save a lock/unlock if this was only
1146 * enabled under INVARIANTS
1149 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)
1150 panic("vinvalbuf: dirty bufs");
1154 * If you alter this loop please notice that interlock is dropped and
1155 * reacquired in flushbuflist. Special care is needed to ensure that
1156 * no race conditions occur from this.
1159 error = flushbuflist(&bo->bo_clean,
1160 flags, bo, slpflag, slptimeo);
1162 error = flushbuflist(&bo->bo_dirty,
1163 flags, bo, slpflag, slptimeo);
1164 if (error != 0 && error != EAGAIN) {
1168 } while (error != 0);
1171 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
1172 * have write I/O in-progress but if there is a VM object then the
1173 * VM object can also have read-I/O in-progress.
1176 bufobj_wwait(bo, 0, 0);
1178 if (bo->bo_object != NULL) {
1179 VM_OBJECT_LOCK(bo->bo_object);
1180 vm_object_pip_wait(bo->bo_object, "bovlbx");
1181 VM_OBJECT_UNLOCK(bo->bo_object);
1184 } while (bo->bo_numoutput > 0);
1188 * Destroy the copy in the VM cache, too.
1190 if (bo->bo_object != NULL && (flags & (V_ALT | V_NORMAL)) == 0) {
1191 VM_OBJECT_LOCK(bo->bo_object);
1192 vm_object_page_remove(bo->bo_object, 0, 0,
1193 (flags & V_SAVE) ? TRUE : FALSE);
1194 VM_OBJECT_UNLOCK(bo->bo_object);
1199 if ((flags & (V_ALT | V_NORMAL)) == 0 &&
1200 (bo->bo_dirty.bv_cnt > 0 || bo->bo_clean.bv_cnt > 0))
1201 panic("vinvalbuf: flush failed");
1208 * Flush out and invalidate all buffers associated with a vnode.
1209 * Called with the underlying object locked.
1212 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
1215 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
1216 ASSERT_VOP_LOCKED(vp, "vinvalbuf");
1217 return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo));
1221 * Flush out buffers on the specified list.
1225 flushbuflist( struct bufv *bufv, int flags, struct bufobj *bo, int slpflag,
1228 struct buf *bp, *nbp;
1233 ASSERT_BO_LOCKED(bo);
1236 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) {
1237 if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) ||
1238 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) {
1244 lblkno = nbp->b_lblkno;
1245 xflags = nbp->b_xflags &
1246 (BX_BKGRDMARKER | BX_VNDIRTY | BX_VNCLEAN);
1249 error = BUF_TIMELOCK(bp,
1250 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_MTX(bo),
1251 "flushbuf", slpflag, slptimeo);
1254 return (error != ENOLCK ? error : EAGAIN);
1256 KASSERT(bp->b_bufobj == bo,
1257 ("bp %p wrong b_bufobj %p should be %p",
1258 bp, bp->b_bufobj, bo));
1259 if (bp->b_bufobj != bo) { /* XXX: necessary ? */
1265 * XXX Since there are no node locks for NFS, I
1266 * believe there is a slight chance that a delayed
1267 * write will occur while sleeping just above, so
1270 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
1275 bp->b_flags |= B_ASYNC;
1278 return (EAGAIN); /* XXX: why not loop ? */
1283 bp->b_flags |= (B_INVAL | B_RELBUF);
1284 bp->b_flags &= ~B_ASYNC;
1288 (nbp->b_bufobj != bo ||
1289 nbp->b_lblkno != lblkno ||
1291 (BX_BKGRDMARKER | BX_VNDIRTY | BX_VNCLEAN)) != xflags))
1292 break; /* nbp invalid */
1298 * Truncate a file's buffer and pages to a specified length. This
1299 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
1303 vtruncbuf(struct vnode *vp, struct ucred *cred, struct thread *td,
1304 off_t length, int blksize)
1306 struct buf *bp, *nbp;
1311 CTR5(KTR_VFS, "%s: vp %p with cred %p and block %d:%ju", __func__,
1312 vp, cred, blksize, (uintmax_t)length);
1315 * Round up to the *next* lbn.
1317 trunclbn = (length + blksize - 1) / blksize;
1319 ASSERT_VOP_LOCKED(vp, "vtruncbuf");
1326 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) {
1327 if (bp->b_lblkno < trunclbn)
1330 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1331 BO_MTX(bo)) == ENOLCK)
1337 bp->b_flags |= (B_INVAL | B_RELBUF);
1338 bp->b_flags &= ~B_ASYNC;
1343 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
1344 (nbp->b_vp != vp) ||
1345 (nbp->b_flags & B_DELWRI))) {
1351 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1352 if (bp->b_lblkno < trunclbn)
1355 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1356 BO_MTX(bo)) == ENOLCK)
1361 bp->b_flags |= (B_INVAL | B_RELBUF);
1362 bp->b_flags &= ~B_ASYNC;
1366 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
1367 (nbp->b_vp != vp) ||
1368 (nbp->b_flags & B_DELWRI) == 0)) {
1377 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1378 if (bp->b_lblkno > 0)
1381 * Since we hold the vnode lock this should only
1382 * fail if we're racing with the buf daemon.
1385 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1386 BO_MTX(bo)) == ENOLCK) {
1389 VNASSERT((bp->b_flags & B_DELWRI), vp,
1390 ("buf(%p) on dirty queue without DELWRI", bp));
1401 bufobj_wwait(bo, 0, 0);
1403 vnode_pager_setsize(vp, length);
1409 * buf_splay() - splay tree core for the clean/dirty list of buffers in
1412 * NOTE: We have to deal with the special case of a background bitmap
1413 * buffer, a situation where two buffers will have the same logical
1414 * block offset. We want (1) only the foreground buffer to be accessed
1415 * in a lookup and (2) must differentiate between the foreground and
1416 * background buffer in the splay tree algorithm because the splay
1417 * tree cannot normally handle multiple entities with the same 'index'.
1418 * We accomplish this by adding differentiating flags to the splay tree's
1423 buf_splay(daddr_t lblkno, b_xflags_t xflags, struct buf *root)
1426 struct buf *lefttreemax, *righttreemin, *y;
1430 lefttreemax = righttreemin = &dummy;
1432 if (lblkno < root->b_lblkno ||
1433 (lblkno == root->b_lblkno &&
1434 (xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
1435 if ((y = root->b_left) == NULL)
1437 if (lblkno < y->b_lblkno) {
1439 root->b_left = y->b_right;
1442 if ((y = root->b_left) == NULL)
1445 /* Link into the new root's right tree. */
1446 righttreemin->b_left = root;
1447 righttreemin = root;
1448 } else if (lblkno > root->b_lblkno ||
1449 (lblkno == root->b_lblkno &&
1450 (xflags & BX_BKGRDMARKER) > (root->b_xflags & BX_BKGRDMARKER))) {
1451 if ((y = root->b_right) == NULL)
1453 if (lblkno > y->b_lblkno) {
1455 root->b_right = y->b_left;
1458 if ((y = root->b_right) == NULL)
1461 /* Link into the new root's left tree. */
1462 lefttreemax->b_right = root;
1469 /* Assemble the new root. */
1470 lefttreemax->b_right = root->b_left;
1471 righttreemin->b_left = root->b_right;
1472 root->b_left = dummy.b_right;
1473 root->b_right = dummy.b_left;
1478 buf_vlist_remove(struct buf *bp)
1483 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1484 ASSERT_BO_LOCKED(bp->b_bufobj);
1485 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) !=
1486 (BX_VNDIRTY|BX_VNCLEAN),
1487 ("buf_vlist_remove: Buf %p is on two lists", bp));
1488 if (bp->b_xflags & BX_VNDIRTY)
1489 bv = &bp->b_bufobj->bo_dirty;
1491 bv = &bp->b_bufobj->bo_clean;
1492 if (bp != bv->bv_root) {
1493 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root);
1494 KASSERT(root == bp, ("splay lookup failed in remove"));
1496 if (bp->b_left == NULL) {
1499 root = buf_splay(bp->b_lblkno, bp->b_xflags, bp->b_left);
1500 root->b_right = bp->b_right;
1503 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs);
1505 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1509 * Add the buffer to the sorted clean or dirty block list using a
1510 * splay tree algorithm.
1512 * NOTE: xflags is passed as a constant, optimizing this inline function!
1515 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags)
1520 ASSERT_BO_LOCKED(bo);
1521 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0,
1522 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags));
1523 bp->b_xflags |= xflags;
1524 if (xflags & BX_VNDIRTY)
1529 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root);
1533 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs);
1534 } else if (bp->b_lblkno < root->b_lblkno ||
1535 (bp->b_lblkno == root->b_lblkno &&
1536 (bp->b_xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
1537 bp->b_left = root->b_left;
1539 root->b_left = NULL;
1540 TAILQ_INSERT_BEFORE(root, bp, b_bobufs);
1542 bp->b_right = root->b_right;
1544 root->b_right = NULL;
1545 TAILQ_INSERT_AFTER(&bv->bv_hd, root, bp, b_bobufs);
1552 * Lookup a buffer using the splay tree. Note that we specifically avoid
1553 * shadow buffers used in background bitmap writes.
1555 * This code isn't quite efficient as it could be because we are maintaining
1556 * two sorted lists and do not know which list the block resides in.
1558 * During a "make buildworld" the desired buffer is found at one of
1559 * the roots more than 60% of the time. Thus, checking both roots
1560 * before performing either splay eliminates unnecessary splays on the
1561 * first tree splayed.
1564 gbincore(struct bufobj *bo, daddr_t lblkno)
1568 ASSERT_BO_LOCKED(bo);
1569 if ((bp = bo->bo_clean.bv_root) != NULL &&
1570 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1572 if ((bp = bo->bo_dirty.bv_root) != NULL &&
1573 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1575 if ((bp = bo->bo_clean.bv_root) != NULL) {
1576 bo->bo_clean.bv_root = bp = buf_splay(lblkno, 0, bp);
1577 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1580 if ((bp = bo->bo_dirty.bv_root) != NULL) {
1581 bo->bo_dirty.bv_root = bp = buf_splay(lblkno, 0, bp);
1582 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1589 * Associate a buffer with a vnode.
1592 bgetvp(struct vnode *vp, struct buf *bp)
1597 ASSERT_BO_LOCKED(bo);
1598 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free"));
1600 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags);
1601 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp,
1602 ("bgetvp: bp already attached! %p", bp));
1605 if (VFS_NEEDSGIANT(vp->v_mount) || bo->bo_flag & BO_NEEDSGIANT)
1606 bp->b_flags |= B_NEEDSGIANT;
1610 * Insert onto list for new vnode.
1612 buf_vlist_add(bp, bo, BX_VNCLEAN);
1616 * Disassociate a buffer from a vnode.
1619 brelvp(struct buf *bp)
1624 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1625 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1628 * Delete from old vnode list, if on one.
1630 vp = bp->b_vp; /* XXX */
1633 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1634 buf_vlist_remove(bp);
1636 panic("brelvp: Buffer %p not on queue.", bp);
1637 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
1638 bo->bo_flag &= ~BO_ONWORKLST;
1639 mtx_lock(&sync_mtx);
1640 LIST_REMOVE(bo, bo_synclist);
1641 syncer_worklist_len--;
1642 mtx_unlock(&sync_mtx);
1644 bp->b_flags &= ~B_NEEDSGIANT;
1646 bp->b_bufobj = NULL;
1652 * Add an item to the syncer work queue.
1655 vn_syncer_add_to_worklist(struct bufobj *bo, int delay)
1659 ASSERT_BO_LOCKED(bo);
1661 mtx_lock(&sync_mtx);
1662 if (bo->bo_flag & BO_ONWORKLST)
1663 LIST_REMOVE(bo, bo_synclist);
1665 bo->bo_flag |= BO_ONWORKLST;
1666 syncer_worklist_len++;
1669 if (delay > syncer_maxdelay - 2)
1670 delay = syncer_maxdelay - 2;
1671 slot = (syncer_delayno + delay) & syncer_mask;
1673 queue = VFS_NEEDSGIANT(bo->__bo_vnode->v_mount) ? WI_GIANTQ :
1675 LIST_INSERT_HEAD(&syncer_workitem_pending[queue][slot], bo,
1677 mtx_unlock(&sync_mtx);
1681 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS)
1685 mtx_lock(&sync_mtx);
1686 len = syncer_worklist_len - sync_vnode_count;
1687 mtx_unlock(&sync_mtx);
1688 error = SYSCTL_OUT(req, &len, sizeof(len));
1692 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, CTLTYPE_INT | CTLFLAG_RD, NULL, 0,
1693 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length");
1695 static struct proc *updateproc;
1696 static void sched_sync(void);
1697 static struct kproc_desc up_kp = {
1702 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp);
1705 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td)
1710 *bo = LIST_FIRST(slp);
1713 vp = (*bo)->__bo_vnode; /* XXX */
1714 if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0)
1717 * We use vhold in case the vnode does not
1718 * successfully sync. vhold prevents the vnode from
1719 * going away when we unlock the sync_mtx so that
1720 * we can acquire the vnode interlock.
1723 mtx_unlock(&sync_mtx);
1725 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1727 mtx_lock(&sync_mtx);
1728 return (*bo == LIST_FIRST(slp));
1730 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1731 (void) VOP_FSYNC(vp, MNT_LAZY, td);
1733 vn_finished_write(mp);
1735 if (((*bo)->bo_flag & BO_ONWORKLST) != 0) {
1737 * Put us back on the worklist. The worklist
1738 * routine will remove us from our current
1739 * position and then add us back in at a later
1742 vn_syncer_add_to_worklist(*bo, syncdelay);
1746 mtx_lock(&sync_mtx);
1751 * System filesystem synchronizer daemon.
1756 struct synclist *gnext, *next;
1757 struct synclist *gslp, *slp;
1760 struct thread *td = curthread;
1762 int net_worklist_len;
1763 int syncer_final_iter;
1768 syncer_final_iter = 0;
1770 syncer_state = SYNCER_RUNNING;
1771 starttime = time_uptime;
1772 td->td_pflags |= TDP_NORUNNINGBUF;
1774 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc,
1777 mtx_lock(&sync_mtx);
1779 if (syncer_state == SYNCER_FINAL_DELAY &&
1780 syncer_final_iter == 0) {
1781 mtx_unlock(&sync_mtx);
1782 kproc_suspend_check(td->td_proc);
1783 mtx_lock(&sync_mtx);
1785 net_worklist_len = syncer_worklist_len - sync_vnode_count;
1786 if (syncer_state != SYNCER_RUNNING &&
1787 starttime != time_uptime) {
1789 printf("\nSyncing disks, vnodes remaining...");
1792 printf("%d ", net_worklist_len);
1794 starttime = time_uptime;
1797 * Push files whose dirty time has expired. Be careful
1798 * of interrupt race on slp queue.
1800 * Skip over empty worklist slots when shutting down.
1803 slp = &syncer_workitem_pending[WI_MPSAFEQ][syncer_delayno];
1804 gslp = &syncer_workitem_pending[WI_GIANTQ][syncer_delayno];
1805 syncer_delayno += 1;
1806 if (syncer_delayno == syncer_maxdelay)
1808 next = &syncer_workitem_pending[WI_MPSAFEQ][syncer_delayno];
1809 gnext = &syncer_workitem_pending[WI_GIANTQ][syncer_delayno];
1811 * If the worklist has wrapped since the
1812 * it was emptied of all but syncer vnodes,
1813 * switch to the FINAL_DELAY state and run
1814 * for one more second.
1816 if (syncer_state == SYNCER_SHUTTING_DOWN &&
1817 net_worklist_len == 0 &&
1818 last_work_seen == syncer_delayno) {
1819 syncer_state = SYNCER_FINAL_DELAY;
1820 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP;
1822 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) &&
1823 LIST_EMPTY(gslp) && syncer_worklist_len > 0);
1826 * Keep track of the last time there was anything
1827 * on the worklist other than syncer vnodes.
1828 * Return to the SHUTTING_DOWN state if any
1831 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING)
1832 last_work_seen = syncer_delayno;
1833 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY)
1834 syncer_state = SYNCER_SHUTTING_DOWN;
1835 while (!LIST_EMPTY(slp)) {
1836 error = sync_vnode(slp, &bo, td);
1838 LIST_REMOVE(bo, bo_synclist);
1839 LIST_INSERT_HEAD(next, bo, bo_synclist);
1843 if (!LIST_EMPTY(gslp)) {
1844 mtx_unlock(&sync_mtx);
1846 mtx_lock(&sync_mtx);
1847 while (!LIST_EMPTY(gslp)) {
1848 error = sync_vnode(gslp, &bo, td);
1850 LIST_REMOVE(bo, bo_synclist);
1851 LIST_INSERT_HEAD(gnext, bo,
1858 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0)
1859 syncer_final_iter--;
1861 * The variable rushjob allows the kernel to speed up the
1862 * processing of the filesystem syncer process. A rushjob
1863 * value of N tells the filesystem syncer to process the next
1864 * N seconds worth of work on its queue ASAP. Currently rushjob
1865 * is used by the soft update code to speed up the filesystem
1866 * syncer process when the incore state is getting so far
1867 * ahead of the disk that the kernel memory pool is being
1868 * threatened with exhaustion.
1875 * Just sleep for a short period of time between
1876 * iterations when shutting down to allow some I/O
1879 * If it has taken us less than a second to process the
1880 * current work, then wait. Otherwise start right over
1881 * again. We can still lose time if any single round
1882 * takes more than two seconds, but it does not really
1883 * matter as we are just trying to generally pace the
1884 * filesystem activity.
1886 if (syncer_state != SYNCER_RUNNING)
1887 cv_timedwait(&sync_wakeup, &sync_mtx,
1888 hz / SYNCER_SHUTDOWN_SPEEDUP);
1889 else if (time_uptime == starttime)
1890 cv_timedwait(&sync_wakeup, &sync_mtx, hz);
1895 * Request the syncer daemon to speed up its work.
1896 * We never push it to speed up more than half of its
1897 * normal turn time, otherwise it could take over the cpu.
1900 speedup_syncer(void)
1904 mtx_lock(&sync_mtx);
1905 if (rushjob < syncdelay / 2) {
1907 stat_rush_requests += 1;
1910 mtx_unlock(&sync_mtx);
1911 cv_broadcast(&sync_wakeup);
1916 * Tell the syncer to speed up its work and run though its work
1917 * list several times, then tell it to shut down.
1920 syncer_shutdown(void *arg, int howto)
1923 if (howto & RB_NOSYNC)
1925 mtx_lock(&sync_mtx);
1926 syncer_state = SYNCER_SHUTTING_DOWN;
1928 mtx_unlock(&sync_mtx);
1929 cv_broadcast(&sync_wakeup);
1930 kproc_shutdown(arg, howto);
1934 * Reassign a buffer from one vnode to another.
1935 * Used to assign file specific control information
1936 * (indirect blocks) to the vnode to which they belong.
1939 reassignbuf(struct buf *bp)
1952 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X",
1953 bp, bp->b_vp, bp->b_flags);
1955 * B_PAGING flagged buffers cannot be reassigned because their vp
1956 * is not fully linked in.
1958 if (bp->b_flags & B_PAGING)
1959 panic("cannot reassign paging buffer");
1962 * Delete from old vnode list, if on one.
1965 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1966 buf_vlist_remove(bp);
1968 panic("reassignbuf: Buffer %p not on queue.", bp);
1970 * If dirty, put on list of dirty buffers; otherwise insert onto list
1973 if (bp->b_flags & B_DELWRI) {
1974 if ((bo->bo_flag & BO_ONWORKLST) == 0) {
1975 switch (vp->v_type) {
1985 vn_syncer_add_to_worklist(bo, delay);
1987 buf_vlist_add(bp, bo, BX_VNDIRTY);
1989 buf_vlist_add(bp, bo, BX_VNCLEAN);
1991 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
1992 mtx_lock(&sync_mtx);
1993 LIST_REMOVE(bo, bo_synclist);
1994 syncer_worklist_len--;
1995 mtx_unlock(&sync_mtx);
1996 bo->bo_flag &= ~BO_ONWORKLST;
2001 bp = TAILQ_FIRST(&bv->bv_hd);
2002 KASSERT(bp == NULL || bp->b_bufobj == bo,
2003 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2004 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2005 KASSERT(bp == NULL || bp->b_bufobj == bo,
2006 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2008 bp = TAILQ_FIRST(&bv->bv_hd);
2009 KASSERT(bp == NULL || bp->b_bufobj == bo,
2010 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2011 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2012 KASSERT(bp == NULL || bp->b_bufobj == bo,
2013 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2019 * Increment the use and hold counts on the vnode, taking care to reference
2020 * the driver's usecount if this is a chardev. The vholdl() will remove
2021 * the vnode from the free list if it is presently free. Requires the
2022 * vnode interlock and returns with it held.
2025 v_incr_usecount(struct vnode *vp)
2028 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2030 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2032 vp->v_rdev->si_usecount++;
2039 * Turn a holdcnt into a use+holdcnt such that only one call to
2040 * v_decr_usecount is needed.
2043 v_upgrade_usecount(struct vnode *vp)
2046 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2048 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2050 vp->v_rdev->si_usecount++;
2056 * Decrement the vnode use and hold count along with the driver's usecount
2057 * if this is a chardev. The vdropl() below releases the vnode interlock
2058 * as it may free the vnode.
2061 v_decr_usecount(struct vnode *vp)
2064 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2065 VNASSERT(vp->v_usecount > 0, vp,
2066 ("v_decr_usecount: negative usecount"));
2067 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2069 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2071 vp->v_rdev->si_usecount--;
2078 * Decrement only the use count and driver use count. This is intended to
2079 * be paired with a follow on vdropl() to release the remaining hold count.
2080 * In this way we may vgone() a vnode with a 0 usecount without risk of
2081 * having it end up on a free list because the hold count is kept above 0.
2084 v_decr_useonly(struct vnode *vp)
2087 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2088 VNASSERT(vp->v_usecount > 0, vp,
2089 ("v_decr_useonly: negative usecount"));
2090 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2092 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2094 vp->v_rdev->si_usecount--;
2100 * Grab a particular vnode from the free list, increment its
2101 * reference count and lock it. VI_DOOMED is set if the vnode
2102 * is being destroyed. Only callers who specify LK_RETRY will
2103 * see doomed vnodes. If inactive processing was delayed in
2104 * vput try to do it here.
2107 vget(struct vnode *vp, int flags, struct thread *td)
2112 VFS_ASSERT_GIANT(vp->v_mount);
2113 VNASSERT((flags & LK_TYPE_MASK) != 0, vp,
2114 ("vget: invalid lock operation"));
2115 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
2117 if ((flags & LK_INTERLOCK) == 0)
2120 if ((error = vn_lock(vp, flags | LK_INTERLOCK)) != 0) {
2122 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__,
2126 if (vp->v_iflag & VI_DOOMED && (flags & LK_RETRY) == 0)
2127 panic("vget: vn_lock failed to return ENOENT\n");
2129 /* Upgrade our holdcnt to a usecount. */
2130 v_upgrade_usecount(vp);
2132 * We don't guarantee that any particular close will
2133 * trigger inactive processing so just make a best effort
2134 * here at preventing a reference to a removed file. If
2135 * we don't succeed no harm is done.
2137 if (vp->v_iflag & VI_OWEINACT) {
2138 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE &&
2139 (flags & LK_NOWAIT) == 0)
2141 vp->v_iflag &= ~VI_OWEINACT;
2148 * Increase the reference count of a vnode.
2151 vref(struct vnode *vp)
2154 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2156 v_incr_usecount(vp);
2161 * Return reference count of a vnode.
2163 * The results of this call are only guaranteed when some mechanism other
2164 * than the VI lock is used to stop other processes from gaining references
2165 * to the vnode. This may be the case if the caller holds the only reference.
2166 * This is also useful when stale data is acceptable as race conditions may
2167 * be accounted for by some other means.
2170 vrefcnt(struct vnode *vp)
2175 usecnt = vp->v_usecount;
2181 #define VPUTX_VRELE 1
2182 #define VPUTX_VPUT 2
2183 #define VPUTX_VUNREF 3
2186 vputx(struct vnode *vp, int func)
2190 KASSERT(vp != NULL, ("vputx: null vp"));
2191 if (func == VPUTX_VUNREF)
2192 ASSERT_VOP_LOCKED(vp, "vunref");
2193 else if (func == VPUTX_VPUT)
2194 ASSERT_VOP_LOCKED(vp, "vput");
2196 KASSERT(func == VPUTX_VRELE, ("vputx: wrong func"));
2197 VFS_ASSERT_GIANT(vp->v_mount);
2198 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2201 /* Skip this v_writecount check if we're going to panic below. */
2202 VNASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, vp,
2203 ("vputx: missed vn_close"));
2206 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) &&
2207 vp->v_usecount == 1)) {
2208 if (func == VPUTX_VPUT)
2210 v_decr_usecount(vp);
2214 if (vp->v_usecount != 1) {
2215 vprint("vputx: negative ref count", vp);
2216 panic("vputx: negative ref cnt");
2218 CTR2(KTR_VFS, "%s: return vnode %p to the freelist", __func__, vp);
2220 * We want to hold the vnode until the inactive finishes to
2221 * prevent vgone() races. We drop the use count here and the
2222 * hold count below when we're done.
2226 * We must call VOP_INACTIVE with the node locked. Mark
2227 * as VI_DOINGINACT to avoid recursion.
2229 vp->v_iflag |= VI_OWEINACT;
2232 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
2236 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2237 error = VOP_LOCK(vp, LK_UPGRADE | LK_INTERLOCK |
2243 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
2247 if (vp->v_usecount > 0)
2248 vp->v_iflag &= ~VI_OWEINACT;
2250 if (vp->v_iflag & VI_OWEINACT)
2251 vinactive(vp, curthread);
2252 if (func != VPUTX_VUNREF)
2259 * Vnode put/release.
2260 * If count drops to zero, call inactive routine and return to freelist.
2263 vrele(struct vnode *vp)
2266 vputx(vp, VPUTX_VRELE);
2270 * Release an already locked vnode. This give the same effects as
2271 * unlock+vrele(), but takes less time and avoids releasing and
2272 * re-aquiring the lock (as vrele() acquires the lock internally.)
2275 vput(struct vnode *vp)
2278 vputx(vp, VPUTX_VPUT);
2282 * Release an exclusively locked vnode. Do not unlock the vnode lock.
2285 vunref(struct vnode *vp)
2288 vputx(vp, VPUTX_VUNREF);
2292 * Somebody doesn't want the vnode recycled.
2295 vhold(struct vnode *vp)
2304 vholdl(struct vnode *vp)
2307 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2309 if (VSHOULDBUSY(vp))
2314 * Note that there is one less who cares about this vnode. vdrop() is the
2315 * opposite of vhold().
2318 vdrop(struct vnode *vp)
2326 * Drop the hold count of the vnode. If this is the last reference to
2327 * the vnode we will free it if it has been vgone'd otherwise it is
2328 * placed on the free list.
2331 vdropl(struct vnode *vp)
2334 ASSERT_VI_LOCKED(vp, "vdropl");
2335 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2336 if (vp->v_holdcnt <= 0)
2337 panic("vdrop: holdcnt %d", vp->v_holdcnt);
2339 if (vp->v_holdcnt == 0) {
2340 if (vp->v_iflag & VI_DOOMED) {
2341 CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__,
2352 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT
2353 * flags. DOINGINACT prevents us from recursing in calls to vinactive.
2354 * OWEINACT tracks whether a vnode missed a call to inactive due to a
2355 * failed lock upgrade.
2358 vinactive(struct vnode *vp, struct thread *td)
2361 ASSERT_VOP_ELOCKED(vp, "vinactive");
2362 ASSERT_VI_LOCKED(vp, "vinactive");
2363 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
2364 ("vinactive: recursed on VI_DOINGINACT"));
2365 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2366 vp->v_iflag |= VI_DOINGINACT;
2367 vp->v_iflag &= ~VI_OWEINACT;
2369 VOP_INACTIVE(vp, td);
2371 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
2372 ("vinactive: lost VI_DOINGINACT"));
2373 vp->v_iflag &= ~VI_DOINGINACT;
2377 * Remove any vnodes in the vnode table belonging to mount point mp.
2379 * If FORCECLOSE is not specified, there should not be any active ones,
2380 * return error if any are found (nb: this is a user error, not a
2381 * system error). If FORCECLOSE is specified, detach any active vnodes
2384 * If WRITECLOSE is set, only flush out regular file vnodes open for
2387 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
2389 * `rootrefs' specifies the base reference count for the root vnode
2390 * of this filesystem. The root vnode is considered busy if its
2391 * v_usecount exceeds this value. On a successful return, vflush(, td)
2392 * will call vrele() on the root vnode exactly rootrefs times.
2393 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
2397 static int busyprt = 0; /* print out busy vnodes */
2398 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "Print out busy vnodes");
2402 vflush( struct mount *mp, int rootrefs, int flags, struct thread *td)
2404 struct vnode *vp, *mvp, *rootvp = NULL;
2406 int busy = 0, error;
2408 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp,
2411 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
2412 ("vflush: bad args"));
2414 * Get the filesystem root vnode. We can vput() it
2415 * immediately, since with rootrefs > 0, it won't go away.
2417 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) {
2418 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d",
2427 MNT_VNODE_FOREACH(vp, mp, mvp) {
2432 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE);
2436 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
2440 * Skip over a vnodes marked VV_SYSTEM.
2442 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
2449 * If WRITECLOSE is set, flush out unlinked but still open
2450 * files (even if open only for reading) and regular file
2451 * vnodes open for writing.
2453 if (flags & WRITECLOSE) {
2454 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
2457 if ((vp->v_type == VNON ||
2458 (error == 0 && vattr.va_nlink > 0)) &&
2459 (vp->v_writecount == 0 || vp->v_type != VREG)) {
2468 * With v_usecount == 0, all we need to do is clear out the
2469 * vnode data structures and we are done.
2471 * If FORCECLOSE is set, forcibly close the vnode.
2473 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) {
2474 VNASSERT(vp->v_usecount == 0 ||
2475 (vp->v_type != VCHR && vp->v_type != VBLK), vp,
2476 ("device VNODE %p is FORCECLOSED", vp));
2482 vprint("vflush: busy vnode", vp);
2490 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
2492 * If just the root vnode is busy, and if its refcount
2493 * is equal to `rootrefs', then go ahead and kill it.
2496 KASSERT(busy > 0, ("vflush: not busy"));
2497 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp,
2498 ("vflush: usecount %d < rootrefs %d",
2499 rootvp->v_usecount, rootrefs));
2500 if (busy == 1 && rootvp->v_usecount == rootrefs) {
2501 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK);
2503 VOP_UNLOCK(rootvp, 0);
2509 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__,
2513 for (; rootrefs > 0; rootrefs--)
2519 * Recycle an unused vnode to the front of the free list.
2522 vrecycle(struct vnode *vp, struct thread *td)
2526 ASSERT_VOP_ELOCKED(vp, "vrecycle");
2527 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2530 if (vp->v_usecount == 0) {
2539 * Eliminate all activity associated with a vnode
2540 * in preparation for reuse.
2543 vgone(struct vnode *vp)
2551 * vgone, with the vp interlock held.
2554 vgonel(struct vnode *vp)
2561 ASSERT_VOP_ELOCKED(vp, "vgonel");
2562 ASSERT_VI_LOCKED(vp, "vgonel");
2563 VNASSERT(vp->v_holdcnt, vp,
2564 ("vgonel: vp %p has no reference.", vp));
2565 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2569 * Don't vgonel if we're already doomed.
2571 if (vp->v_iflag & VI_DOOMED)
2573 vp->v_iflag |= VI_DOOMED;
2575 * Check to see if the vnode is in use. If so, we have to call
2576 * VOP_CLOSE() and VOP_INACTIVE().
2578 active = vp->v_usecount;
2579 oweinact = (vp->v_iflag & VI_OWEINACT);
2582 * Clean out any buffers associated with the vnode.
2583 * If the flush fails, just toss the buffers.
2586 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd))
2587 (void) vn_start_secondary_write(vp, &mp, V_WAIT);
2588 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0)
2589 vinvalbuf(vp, 0, 0, 0);
2592 * If purging an active vnode, it must be closed and
2593 * deactivated before being reclaimed.
2596 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
2597 if (oweinact || active) {
2599 if ((vp->v_iflag & VI_DOINGINACT) == 0)
2604 * Reclaim the vnode.
2606 if (VOP_RECLAIM(vp, td))
2607 panic("vgone: cannot reclaim");
2609 vn_finished_secondary_write(mp);
2610 VNASSERT(vp->v_object == NULL, vp,
2611 ("vop_reclaim left v_object vp=%p, tag=%s", vp, vp->v_tag));
2613 * Clear the advisory locks and wake up waiting threads.
2615 lf_purgelocks(vp, &(vp->v_lockf));
2617 * Delete from old mount point vnode list.
2622 * Done with purge, reset to the standard lock and invalidate
2626 vp->v_vnlock = &vp->v_lock;
2627 vp->v_op = &dead_vnodeops;
2633 * Calculate the total number of references to a special device.
2636 vcount(struct vnode *vp)
2641 count = vp->v_rdev->si_usecount;
2647 * Same as above, but using the struct cdev *as argument
2650 count_dev(struct cdev *dev)
2655 count = dev->si_usecount;
2661 * Print out a description of a vnode.
2663 static char *typename[] =
2664 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD",
2668 vn_printf(struct vnode *vp, const char *fmt, ...)
2671 char buf[256], buf2[16];
2677 printf("%p: ", (void *)vp);
2678 printf("tag %s, type %s\n", vp->v_tag, typename[vp->v_type]);
2679 printf(" usecount %d, writecount %d, refcount %d mountedhere %p\n",
2680 vp->v_usecount, vp->v_writecount, vp->v_holdcnt, vp->v_mountedhere);
2683 if (vp->v_vflag & VV_ROOT)
2684 strlcat(buf, "|VV_ROOT", sizeof(buf));
2685 if (vp->v_vflag & VV_ISTTY)
2686 strlcat(buf, "|VV_ISTTY", sizeof(buf));
2687 if (vp->v_vflag & VV_NOSYNC)
2688 strlcat(buf, "|VV_NOSYNC", sizeof(buf));
2689 if (vp->v_vflag & VV_CACHEDLABEL)
2690 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf));
2691 if (vp->v_vflag & VV_TEXT)
2692 strlcat(buf, "|VV_TEXT", sizeof(buf));
2693 if (vp->v_vflag & VV_COPYONWRITE)
2694 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf));
2695 if (vp->v_vflag & VV_SYSTEM)
2696 strlcat(buf, "|VV_SYSTEM", sizeof(buf));
2697 if (vp->v_vflag & VV_PROCDEP)
2698 strlcat(buf, "|VV_PROCDEP", sizeof(buf));
2699 if (vp->v_vflag & VV_NOKNOTE)
2700 strlcat(buf, "|VV_NOKNOTE", sizeof(buf));
2701 if (vp->v_vflag & VV_DELETED)
2702 strlcat(buf, "|VV_DELETED", sizeof(buf));
2703 if (vp->v_vflag & VV_MD)
2704 strlcat(buf, "|VV_MD", sizeof(buf));
2705 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC |
2706 VV_CACHEDLABEL | VV_TEXT | VV_COPYONWRITE | VV_SYSTEM | VV_PROCDEP |
2707 VV_NOKNOTE | VV_DELETED | VV_MD);
2709 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags);
2710 strlcat(buf, buf2, sizeof(buf));
2712 if (vp->v_iflag & VI_MOUNT)
2713 strlcat(buf, "|VI_MOUNT", sizeof(buf));
2714 if (vp->v_iflag & VI_AGE)
2715 strlcat(buf, "|VI_AGE", sizeof(buf));
2716 if (vp->v_iflag & VI_DOOMED)
2717 strlcat(buf, "|VI_DOOMED", sizeof(buf));
2718 if (vp->v_iflag & VI_FREE)
2719 strlcat(buf, "|VI_FREE", sizeof(buf));
2720 if (vp->v_iflag & VI_DOINGINACT)
2721 strlcat(buf, "|VI_DOINGINACT", sizeof(buf));
2722 if (vp->v_iflag & VI_OWEINACT)
2723 strlcat(buf, "|VI_OWEINACT", sizeof(buf));
2724 flags = vp->v_iflag & ~(VI_MOUNT | VI_AGE | VI_DOOMED | VI_FREE |
2725 VI_DOINGINACT | VI_OWEINACT);
2727 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags);
2728 strlcat(buf, buf2, sizeof(buf));
2730 printf(" flags (%s)\n", buf + 1);
2731 if (mtx_owned(VI_MTX(vp)))
2732 printf(" VI_LOCKed");
2733 if (vp->v_object != NULL)
2734 printf(" v_object %p ref %d pages %d\n",
2735 vp->v_object, vp->v_object->ref_count,
2736 vp->v_object->resident_page_count);
2738 lockmgr_printinfo(vp->v_vnlock);
2739 if (vp->v_data != NULL)
2745 * List all of the locked vnodes in the system.
2746 * Called when debugging the kernel.
2748 DB_SHOW_COMMAND(lockedvnods, lockedvnodes)
2750 struct mount *mp, *nmp;
2754 * Note: because this is DDB, we can't obey the locking semantics
2755 * for these structures, which means we could catch an inconsistent
2756 * state and dereference a nasty pointer. Not much to be done
2759 db_printf("Locked vnodes\n");
2760 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2761 nmp = TAILQ_NEXT(mp, mnt_list);
2762 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
2763 if (vp->v_type != VMARKER &&
2767 nmp = TAILQ_NEXT(mp, mnt_list);
2772 * Show details about the given vnode.
2774 DB_SHOW_COMMAND(vnode, db_show_vnode)
2780 vp = (struct vnode *)addr;
2781 vn_printf(vp, "vnode ");
2785 * Show details about the given mount point.
2787 DB_SHOW_COMMAND(mount, db_show_mount)
2797 /* No address given, print short info about all mount points. */
2798 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2799 db_printf("%p %s on %s (%s)\n", mp,
2800 mp->mnt_stat.f_mntfromname,
2801 mp->mnt_stat.f_mntonname,
2802 mp->mnt_stat.f_fstypename);
2806 db_printf("\nMore info: show mount <addr>\n");
2810 mp = (struct mount *)addr;
2811 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname,
2812 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename);
2815 flags = mp->mnt_flag;
2816 #define MNT_FLAG(flag) do { \
2817 if (flags & (flag)) { \
2818 if (buf[0] != '\0') \
2819 strlcat(buf, ", ", sizeof(buf)); \
2820 strlcat(buf, (#flag) + 4, sizeof(buf)); \
2824 MNT_FLAG(MNT_RDONLY);
2825 MNT_FLAG(MNT_SYNCHRONOUS);
2826 MNT_FLAG(MNT_NOEXEC);
2827 MNT_FLAG(MNT_NOSUID);
2828 MNT_FLAG(MNT_UNION);
2829 MNT_FLAG(MNT_ASYNC);
2830 MNT_FLAG(MNT_SUIDDIR);
2831 MNT_FLAG(MNT_SOFTDEP);
2832 MNT_FLAG(MNT_NOSYMFOLLOW);
2833 MNT_FLAG(MNT_GJOURNAL);
2834 MNT_FLAG(MNT_MULTILABEL);
2836 MNT_FLAG(MNT_NOATIME);
2837 MNT_FLAG(MNT_NOCLUSTERR);
2838 MNT_FLAG(MNT_NOCLUSTERW);
2839 MNT_FLAG(MNT_NFS4ACLS);
2840 MNT_FLAG(MNT_EXRDONLY);
2841 MNT_FLAG(MNT_EXPORTED);
2842 MNT_FLAG(MNT_DEFEXPORTED);
2843 MNT_FLAG(MNT_EXPORTANON);
2844 MNT_FLAG(MNT_EXKERB);
2845 MNT_FLAG(MNT_EXPUBLIC);
2846 MNT_FLAG(MNT_LOCAL);
2847 MNT_FLAG(MNT_QUOTA);
2848 MNT_FLAG(MNT_ROOTFS);
2850 MNT_FLAG(MNT_IGNORE);
2851 MNT_FLAG(MNT_UPDATE);
2852 MNT_FLAG(MNT_DELEXPORT);
2853 MNT_FLAG(MNT_RELOAD);
2854 MNT_FLAG(MNT_FORCE);
2855 MNT_FLAG(MNT_SNAPSHOT);
2856 MNT_FLAG(MNT_BYFSID);
2860 strlcat(buf, ", ", sizeof(buf));
2861 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
2864 db_printf(" mnt_flag = %s\n", buf);
2867 flags = mp->mnt_kern_flag;
2868 #define MNT_KERN_FLAG(flag) do { \
2869 if (flags & (flag)) { \
2870 if (buf[0] != '\0') \
2871 strlcat(buf, ", ", sizeof(buf)); \
2872 strlcat(buf, (#flag) + 5, sizeof(buf)); \
2876 MNT_KERN_FLAG(MNTK_UNMOUNTF);
2877 MNT_KERN_FLAG(MNTK_ASYNC);
2878 MNT_KERN_FLAG(MNTK_SOFTDEP);
2879 MNT_KERN_FLAG(MNTK_NOINSMNTQ);
2880 MNT_KERN_FLAG(MNTK_UNMOUNT);
2881 MNT_KERN_FLAG(MNTK_MWAIT);
2882 MNT_KERN_FLAG(MNTK_SUSPEND);
2883 MNT_KERN_FLAG(MNTK_SUSPEND2);
2884 MNT_KERN_FLAG(MNTK_SUSPENDED);
2885 MNT_KERN_FLAG(MNTK_MPSAFE);
2886 MNT_KERN_FLAG(MNTK_NOKNOTE);
2887 MNT_KERN_FLAG(MNTK_LOOKUP_SHARED);
2888 #undef MNT_KERN_FLAG
2891 strlcat(buf, ", ", sizeof(buf));
2892 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
2895 db_printf(" mnt_kern_flag = %s\n", buf);
2897 db_printf(" mnt_opt = ");
2898 opt = TAILQ_FIRST(mp->mnt_opt);
2900 db_printf("%s", opt->name);
2901 opt = TAILQ_NEXT(opt, link);
2902 while (opt != NULL) {
2903 db_printf(", %s", opt->name);
2904 opt = TAILQ_NEXT(opt, link);
2910 db_printf(" mnt_stat = { version=%u type=%u flags=0x%016jx "
2911 "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju "
2912 "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju "
2913 "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n",
2914 (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags,
2915 (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize,
2916 (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree,
2917 (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files,
2918 (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites,
2919 (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads,
2920 (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax,
2921 (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]);
2923 db_printf(" mnt_cred = { uid=%u ruid=%u",
2924 (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid);
2925 if (jailed(mp->mnt_cred))
2926 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id);
2928 db_printf(" mnt_ref = %d\n", mp->mnt_ref);
2929 db_printf(" mnt_gen = %d\n", mp->mnt_gen);
2930 db_printf(" mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize);
2931 db_printf(" mnt_writeopcount = %d\n", mp->mnt_writeopcount);
2932 db_printf(" mnt_noasync = %u\n", mp->mnt_noasync);
2933 db_printf(" mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen);
2934 db_printf(" mnt_iosize_max = %d\n", mp->mnt_iosize_max);
2935 db_printf(" mnt_hashseed = %u\n", mp->mnt_hashseed);
2936 db_printf(" mnt_secondary_writes = %d\n", mp->mnt_secondary_writes);
2937 db_printf(" mnt_secondary_accwrites = %d\n",
2938 mp->mnt_secondary_accwrites);
2939 db_printf(" mnt_gjprovider = %s\n",
2940 mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL");
2943 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
2944 if (vp->v_type != VMARKER) {
2945 vn_printf(vp, "vnode ");
2954 * Fill in a struct xvfsconf based on a struct vfsconf.
2957 vfsconf2x(struct vfsconf *vfsp, struct xvfsconf *xvfsp)
2960 strcpy(xvfsp->vfc_name, vfsp->vfc_name);
2961 xvfsp->vfc_typenum = vfsp->vfc_typenum;
2962 xvfsp->vfc_refcount = vfsp->vfc_refcount;
2963 xvfsp->vfc_flags = vfsp->vfc_flags;
2965 * These are unused in userland, we keep them
2966 * to not break binary compatibility.
2968 xvfsp->vfc_vfsops = NULL;
2969 xvfsp->vfc_next = NULL;
2973 * Top level filesystem related information gathering.
2976 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS)
2978 struct vfsconf *vfsp;
2979 struct xvfsconf xvfsp;
2983 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
2984 bzero(&xvfsp, sizeof(xvfsp));
2985 vfsconf2x(vfsp, &xvfsp);
2986 error = SYSCTL_OUT(req, &xvfsp, sizeof xvfsp);
2993 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLFLAG_RD, NULL, 0, sysctl_vfs_conflist,
2994 "S,xvfsconf", "List of all configured filesystems");
2996 #ifndef BURN_BRIDGES
2997 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS);
3000 vfs_sysctl(SYSCTL_HANDLER_ARGS)
3002 int *name = (int *)arg1 - 1; /* XXX */
3003 u_int namelen = arg2 + 1; /* XXX */
3004 struct vfsconf *vfsp;
3005 struct xvfsconf xvfsp;
3007 printf("WARNING: userland calling deprecated sysctl, "
3008 "please rebuild world\n");
3010 #if 1 || defined(COMPAT_PRELITE2)
3011 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
3013 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
3017 case VFS_MAXTYPENUM:
3020 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
3023 return (ENOTDIR); /* overloaded */
3024 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list)
3025 if (vfsp->vfc_typenum == name[2])
3028 return (EOPNOTSUPP);
3029 bzero(&xvfsp, sizeof(xvfsp));
3030 vfsconf2x(vfsp, &xvfsp);
3031 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3033 return (EOPNOTSUPP);
3036 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP,
3037 vfs_sysctl, "Generic filesystem");
3039 #if 1 || defined(COMPAT_PRELITE2)
3042 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
3045 struct vfsconf *vfsp;
3046 struct ovfsconf ovfs;
3048 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3049 bzero(&ovfs, sizeof(ovfs));
3050 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
3051 strcpy(ovfs.vfc_name, vfsp->vfc_name);
3052 ovfs.vfc_index = vfsp->vfc_typenum;
3053 ovfs.vfc_refcount = vfsp->vfc_refcount;
3054 ovfs.vfc_flags = vfsp->vfc_flags;
3055 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
3062 #endif /* 1 || COMPAT_PRELITE2 */
3063 #endif /* !BURN_BRIDGES */
3065 #define KINFO_VNODESLOP 10
3068 * Dump vnode list (via sysctl).
3072 sysctl_vnode(SYSCTL_HANDLER_ARGS)
3080 * Stale numvnodes access is not fatal here.
3083 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn;
3085 /* Make an estimate */
3086 return (SYSCTL_OUT(req, 0, len));
3088 error = sysctl_wire_old_buffer(req, 0);
3091 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK);
3093 mtx_lock(&mountlist_mtx);
3094 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3095 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK))
3098 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3102 xvn[n].xv_size = sizeof *xvn;
3103 xvn[n].xv_vnode = vp;
3104 xvn[n].xv_id = 0; /* XXX compat */
3105 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field
3107 XV_COPY(writecount);
3113 xvn[n].xv_flag = vp->v_vflag;
3115 switch (vp->v_type) {
3122 if (vp->v_rdev == NULL) {
3126 xvn[n].xv_dev = dev2udev(vp->v_rdev);
3129 xvn[n].xv_socket = vp->v_socket;
3132 xvn[n].xv_fifo = vp->v_fifoinfo;
3137 /* shouldn't happen? */
3145 mtx_lock(&mountlist_mtx);
3150 mtx_unlock(&mountlist_mtx);
3152 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn);
3157 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD,
3158 0, 0, sysctl_vnode, "S,xvnode", "");
3162 * Unmount all filesystems. The list is traversed in reverse order
3163 * of mounting to avoid dependencies.
3166 vfs_unmountall(void)
3172 KASSERT(curthread != NULL, ("vfs_unmountall: NULL curthread"));
3173 CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__);
3177 * Since this only runs when rebooting, it is not interlocked.
3179 while(!TAILQ_EMPTY(&mountlist)) {
3180 mp = TAILQ_LAST(&mountlist, mntlist);
3181 error = dounmount(mp, MNT_FORCE, td);
3183 TAILQ_REMOVE(&mountlist, mp, mnt_list);
3185 * XXX: Due to the way in which we mount the root
3186 * file system off of devfs, devfs will generate a
3187 * "busy" warning when we try to unmount it before
3188 * the root. Don't print a warning as a result in
3189 * order to avoid false positive errors that may
3190 * cause needless upset.
3192 if (strcmp(mp->mnt_vfc->vfc_name, "devfs") != 0) {
3193 printf("unmount of %s failed (",
3194 mp->mnt_stat.f_mntonname);
3198 printf("%d)\n", error);
3201 /* The unmount has removed mp from the mountlist */
3207 * perform msync on all vnodes under a mount point
3208 * the mount point must be locked.
3211 vfs_msync(struct mount *mp, int flags)
3213 struct vnode *vp, *mvp;
3214 struct vm_object *obj;
3216 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
3218 MNT_VNODE_FOREACH(vp, mp, mvp) {
3221 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0 &&
3222 (flags == MNT_WAIT || VOP_ISLOCKED(vp) == 0)) {
3225 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK,
3227 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */
3235 VM_OBJECT_LOCK(obj);
3236 vm_object_page_clean(obj, 0, 0,
3238 OBJPC_SYNC : OBJPC_NOSYNC);
3239 VM_OBJECT_UNLOCK(obj);
3251 * Mark a vnode as free, putting it up for recycling.
3254 vfree(struct vnode *vp)
3257 ASSERT_VI_LOCKED(vp, "vfree");
3258 mtx_lock(&vnode_free_list_mtx);
3259 VNASSERT(vp->v_op != NULL, vp, ("vfree: vnode already reclaimed."));
3260 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp, ("vnode already free"));
3261 VNASSERT(VSHOULDFREE(vp), vp, ("vfree: freeing when we shouldn't"));
3262 VNASSERT((vp->v_iflag & VI_DOOMED) == 0, vp,
3263 ("vfree: Freeing doomed vnode"));
3264 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3265 if (vp->v_iflag & VI_AGE) {
3266 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
3268 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
3271 vp->v_iflag &= ~VI_AGE;
3272 vp->v_iflag |= VI_FREE;
3273 mtx_unlock(&vnode_free_list_mtx);
3277 * Opposite of vfree() - mark a vnode as in use.
3280 vbusy(struct vnode *vp)
3282 ASSERT_VI_LOCKED(vp, "vbusy");
3283 VNASSERT((vp->v_iflag & VI_FREE) != 0, vp, ("vnode not free"));
3284 VNASSERT(vp->v_op != NULL, vp, ("vbusy: vnode already reclaimed."));
3285 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3287 mtx_lock(&vnode_free_list_mtx);
3288 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
3290 vp->v_iflag &= ~(VI_FREE|VI_AGE);
3291 mtx_unlock(&vnode_free_list_mtx);
3295 destroy_vpollinfo(struct vpollinfo *vi)
3297 knlist_destroy(&vi->vpi_selinfo.si_note);
3298 mtx_destroy(&vi->vpi_lock);
3299 uma_zfree(vnodepoll_zone, vi);
3303 * Initalize per-vnode helper structure to hold poll-related state.
3306 v_addpollinfo(struct vnode *vp)
3308 struct vpollinfo *vi;
3310 if (vp->v_pollinfo != NULL)
3312 vi = uma_zalloc(vnodepoll_zone, M_WAITOK);
3313 mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF);
3314 knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock,
3315 vfs_knlunlock, vfs_knl_assert_locked, vfs_knl_assert_unlocked);
3317 if (vp->v_pollinfo != NULL) {
3319 destroy_vpollinfo(vi);
3322 vp->v_pollinfo = vi;
3327 * Record a process's interest in events which might happen to
3328 * a vnode. Because poll uses the historic select-style interface
3329 * internally, this routine serves as both the ``check for any
3330 * pending events'' and the ``record my interest in future events''
3331 * functions. (These are done together, while the lock is held,
3332 * to avoid race conditions.)
3335 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
3339 mtx_lock(&vp->v_pollinfo->vpi_lock);
3340 if (vp->v_pollinfo->vpi_revents & events) {
3342 * This leaves events we are not interested
3343 * in available for the other process which
3344 * which presumably had requested them
3345 * (otherwise they would never have been
3348 events &= vp->v_pollinfo->vpi_revents;
3349 vp->v_pollinfo->vpi_revents &= ~events;
3351 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3354 vp->v_pollinfo->vpi_events |= events;
3355 selrecord(td, &vp->v_pollinfo->vpi_selinfo);
3356 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3361 * Routine to create and manage a filesystem syncer vnode.
3363 #define sync_close ((int (*)(struct vop_close_args *))nullop)
3364 static int sync_fsync(struct vop_fsync_args *);
3365 static int sync_inactive(struct vop_inactive_args *);
3366 static int sync_reclaim(struct vop_reclaim_args *);
3368 static struct vop_vector sync_vnodeops = {
3369 .vop_bypass = VOP_EOPNOTSUPP,
3370 .vop_close = sync_close, /* close */
3371 .vop_fsync = sync_fsync, /* fsync */
3372 .vop_inactive = sync_inactive, /* inactive */
3373 .vop_reclaim = sync_reclaim, /* reclaim */
3374 .vop_lock1 = vop_stdlock, /* lock */
3375 .vop_unlock = vop_stdunlock, /* unlock */
3376 .vop_islocked = vop_stdislocked, /* islocked */
3380 * Create a new filesystem syncer vnode for the specified mount point.
3383 vfs_allocate_syncvnode(struct mount *mp)
3387 static long start, incr, next;
3390 /* Allocate a new vnode */
3391 if ((error = getnewvnode("syncer", mp, &sync_vnodeops, &vp)) != 0) {
3392 mp->mnt_syncer = NULL;
3396 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3397 vp->v_vflag |= VV_FORCEINSMQ;
3398 error = insmntque(vp, mp);
3400 panic("vfs_allocate_syncvnode: insmntque failed");
3401 vp->v_vflag &= ~VV_FORCEINSMQ;
3404 * Place the vnode onto the syncer worklist. We attempt to
3405 * scatter them about on the list so that they will go off
3406 * at evenly distributed times even if all the filesystems
3407 * are mounted at once.
3410 if (next == 0 || next > syncer_maxdelay) {
3414 start = syncer_maxdelay / 2;
3415 incr = syncer_maxdelay;
3421 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0);
3422 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */
3423 mtx_lock(&sync_mtx);
3425 mtx_unlock(&sync_mtx);
3427 mp->mnt_syncer = vp;
3432 * Do a lazy sync of the filesystem.
3435 sync_fsync(struct vop_fsync_args *ap)
3437 struct vnode *syncvp = ap->a_vp;
3438 struct mount *mp = syncvp->v_mount;
3443 * We only need to do something if this is a lazy evaluation.
3445 if (ap->a_waitfor != MNT_LAZY)
3449 * Move ourselves to the back of the sync list.
3451 bo = &syncvp->v_bufobj;
3453 vn_syncer_add_to_worklist(bo, syncdelay);
3457 * Walk the list of vnodes pushing all that are dirty and
3458 * not already on the sync list.
3460 mtx_lock(&mountlist_mtx);
3461 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
3462 mtx_unlock(&mountlist_mtx);
3465 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) {
3471 mp->mnt_kern_flag &= ~MNTK_ASYNC;
3473 vfs_msync(mp, MNT_NOWAIT);
3474 error = VFS_SYNC(mp, MNT_LAZY);
3477 if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0)
3478 mp->mnt_kern_flag |= MNTK_ASYNC;
3480 vn_finished_write(mp);
3486 * The syncer vnode is no referenced.
3489 sync_inactive(struct vop_inactive_args *ap)
3497 * The syncer vnode is no longer needed and is being decommissioned.
3499 * Modifications to the worklist must be protected by sync_mtx.
3502 sync_reclaim(struct vop_reclaim_args *ap)
3504 struct vnode *vp = ap->a_vp;
3509 vp->v_mount->mnt_syncer = NULL;
3510 if (bo->bo_flag & BO_ONWORKLST) {
3511 mtx_lock(&sync_mtx);
3512 LIST_REMOVE(bo, bo_synclist);
3513 syncer_worklist_len--;
3515 mtx_unlock(&sync_mtx);
3516 bo->bo_flag &= ~BO_ONWORKLST;
3524 * Check if vnode represents a disk device
3527 vn_isdisk(struct vnode *vp, int *errp)
3533 if (vp->v_type != VCHR)
3535 else if (vp->v_rdev == NULL)
3537 else if (vp->v_rdev->si_devsw == NULL)
3539 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK))
3544 return (error == 0);
3548 * Common filesystem object access control check routine. Accepts a
3549 * vnode's type, "mode", uid and gid, requested access mode, credentials,
3550 * and optional call-by-reference privused argument allowing vaccess()
3551 * to indicate to the caller whether privilege was used to satisfy the
3552 * request (obsoleted). Returns 0 on success, or an errno on failure.
3554 * The ifdef'd CAPABILITIES version is here for reference, but is not
3558 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid,
3559 accmode_t accmode, struct ucred *cred, int *privused)
3561 accmode_t dac_granted;
3562 accmode_t priv_granted;
3564 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0,
3565 ("invalid bit in accmode"));
3568 * Look for a normal, non-privileged way to access the file/directory
3569 * as requested. If it exists, go with that.
3572 if (privused != NULL)
3577 /* Check the owner. */
3578 if (cred->cr_uid == file_uid) {
3579 dac_granted |= VADMIN;
3580 if (file_mode & S_IXUSR)
3581 dac_granted |= VEXEC;
3582 if (file_mode & S_IRUSR)
3583 dac_granted |= VREAD;
3584 if (file_mode & S_IWUSR)
3585 dac_granted |= (VWRITE | VAPPEND);
3587 if ((accmode & dac_granted) == accmode)
3593 /* Otherwise, check the groups (first match) */
3594 if (groupmember(file_gid, cred)) {
3595 if (file_mode & S_IXGRP)
3596 dac_granted |= VEXEC;
3597 if (file_mode & S_IRGRP)
3598 dac_granted |= VREAD;
3599 if (file_mode & S_IWGRP)
3600 dac_granted |= (VWRITE | VAPPEND);
3602 if ((accmode & dac_granted) == accmode)
3608 /* Otherwise, check everyone else. */
3609 if (file_mode & S_IXOTH)
3610 dac_granted |= VEXEC;
3611 if (file_mode & S_IROTH)
3612 dac_granted |= VREAD;
3613 if (file_mode & S_IWOTH)
3614 dac_granted |= (VWRITE | VAPPEND);
3615 if ((accmode & dac_granted) == accmode)
3620 * Build a privilege mask to determine if the set of privileges
3621 * satisfies the requirements when combined with the granted mask
3622 * from above. For each privilege, if the privilege is required,
3623 * bitwise or the request type onto the priv_granted mask.
3629 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC
3630 * requests, instead of PRIV_VFS_EXEC.
3632 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3633 !priv_check_cred(cred, PRIV_VFS_LOOKUP, 0))
3634 priv_granted |= VEXEC;
3636 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3637 !priv_check_cred(cred, PRIV_VFS_EXEC, 0))
3638 priv_granted |= VEXEC;
3641 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) &&
3642 !priv_check_cred(cred, PRIV_VFS_READ, 0))
3643 priv_granted |= VREAD;
3645 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) &&
3646 !priv_check_cred(cred, PRIV_VFS_WRITE, 0))
3647 priv_granted |= (VWRITE | VAPPEND);
3649 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) &&
3650 !priv_check_cred(cred, PRIV_VFS_ADMIN, 0))
3651 priv_granted |= VADMIN;
3653 if ((accmode & (priv_granted | dac_granted)) == accmode) {
3654 /* XXX audit: privilege used */
3655 if (privused != NULL)
3660 return ((accmode & VADMIN) ? EPERM : EACCES);
3664 * Credential check based on process requesting service, and per-attribute
3668 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred,
3669 struct thread *td, accmode_t accmode)
3673 * Kernel-invoked always succeeds.
3679 * Do not allow privileged processes in jail to directly manipulate
3680 * system attributes.
3682 switch (attrnamespace) {
3683 case EXTATTR_NAMESPACE_SYSTEM:
3684 /* Potentially should be: return (EPERM); */
3685 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM, 0));
3686 case EXTATTR_NAMESPACE_USER:
3687 return (VOP_ACCESS(vp, accmode, cred, td));
3693 #ifdef DEBUG_VFS_LOCKS
3695 * This only exists to supress warnings from unlocked specfs accesses. It is
3696 * no longer ok to have an unlocked VFS.
3698 #define IGNORE_LOCK(vp) (panicstr != NULL || (vp) == NULL || \
3699 (vp)->v_type == VCHR || (vp)->v_type == VBAD)
3701 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */
3702 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0,
3703 "Drop into debugger on lock violation");
3705 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */
3706 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex,
3707 0, "Check for interlock across VOPs");
3709 int vfs_badlock_print = 1; /* Print lock violations. */
3710 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print,
3711 0, "Print lock violations");
3714 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */
3715 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW,
3716 &vfs_badlock_backtrace, 0, "Print backtrace at lock violations");
3720 vfs_badlock(const char *msg, const char *str, struct vnode *vp)
3724 if (vfs_badlock_backtrace)
3727 if (vfs_badlock_print)
3728 printf("%s: %p %s\n", str, (void *)vp, msg);
3729 if (vfs_badlock_ddb)
3730 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
3734 assert_vi_locked(struct vnode *vp, const char *str)
3737 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp)))
3738 vfs_badlock("interlock is not locked but should be", str, vp);
3742 assert_vi_unlocked(struct vnode *vp, const char *str)
3745 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp)))
3746 vfs_badlock("interlock is locked but should not be", str, vp);
3750 assert_vop_locked(struct vnode *vp, const char *str)
3753 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == 0)
3754 vfs_badlock("is not locked but should be", str, vp);
3758 assert_vop_unlocked(struct vnode *vp, const char *str)
3761 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
3762 vfs_badlock("is locked but should not be", str, vp);
3766 assert_vop_elocked(struct vnode *vp, const char *str)
3769 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
3770 vfs_badlock("is not exclusive locked but should be", str, vp);
3775 assert_vop_elocked_other(struct vnode *vp, const char *str)
3778 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLOTHER)
3779 vfs_badlock("is not exclusive locked by another thread",
3784 assert_vop_slocked(struct vnode *vp, const char *str)
3787 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_SHARED)
3788 vfs_badlock("is not locked shared but should be", str, vp);
3791 #endif /* DEBUG_VFS_LOCKS */
3794 vop_rename_fail(struct vop_rename_args *ap)
3797 if (ap->a_tvp != NULL)
3799 if (ap->a_tdvp == ap->a_tvp)
3808 vop_rename_pre(void *ap)
3810 struct vop_rename_args *a = ap;
3812 #ifdef DEBUG_VFS_LOCKS
3814 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME");
3815 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME");
3816 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME");
3817 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME");
3819 /* Check the source (from). */
3820 if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock &&
3821 (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock))
3822 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked");
3823 if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock)
3824 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked");
3826 /* Check the target. */
3828 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked");
3829 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked");
3831 if (a->a_tdvp != a->a_fdvp)
3833 if (a->a_tvp != a->a_fvp)
3841 vop_strategy_pre(void *ap)
3843 #ifdef DEBUG_VFS_LOCKS
3844 struct vop_strategy_args *a;
3851 * Cluster ops lock their component buffers but not the IO container.
3853 if ((bp->b_flags & B_CLUSTER) != 0)
3856 if (!BUF_ISLOCKED(bp)) {
3857 if (vfs_badlock_print)
3859 "VOP_STRATEGY: bp is not locked but should be\n");
3860 if (vfs_badlock_ddb)
3861 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
3867 vop_lookup_pre(void *ap)
3869 #ifdef DEBUG_VFS_LOCKS
3870 struct vop_lookup_args *a;
3875 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP");
3876 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP");
3881 vop_lookup_post(void *ap, int rc)
3883 #ifdef DEBUG_VFS_LOCKS
3884 struct vop_lookup_args *a;
3892 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP");
3893 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP");
3896 ASSERT_VOP_LOCKED(vp, "VOP_LOOKUP (child)");
3901 vop_lock_pre(void *ap)
3903 #ifdef DEBUG_VFS_LOCKS
3904 struct vop_lock1_args *a = ap;
3906 if ((a->a_flags & LK_INTERLOCK) == 0)
3907 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
3909 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK");
3914 vop_lock_post(void *ap, int rc)
3916 #ifdef DEBUG_VFS_LOCKS
3917 struct vop_lock1_args *a = ap;
3919 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
3921 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK");
3926 vop_unlock_pre(void *ap)
3928 #ifdef DEBUG_VFS_LOCKS
3929 struct vop_unlock_args *a = ap;
3931 if (a->a_flags & LK_INTERLOCK)
3932 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK");
3933 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK");
3938 vop_unlock_post(void *ap, int rc)
3940 #ifdef DEBUG_VFS_LOCKS
3941 struct vop_unlock_args *a = ap;
3943 if (a->a_flags & LK_INTERLOCK)
3944 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK");
3949 vop_create_post(void *ap, int rc)
3951 struct vop_create_args *a = ap;
3954 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
3958 vop_link_post(void *ap, int rc)
3960 struct vop_link_args *a = ap;
3963 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK);
3964 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE);
3969 vop_mkdir_post(void *ap, int rc)
3971 struct vop_mkdir_args *a = ap;
3974 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
3978 vop_mknod_post(void *ap, int rc)
3980 struct vop_mknod_args *a = ap;
3983 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
3987 vop_remove_post(void *ap, int rc)
3989 struct vop_remove_args *a = ap;
3992 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
3993 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
3998 vop_rename_post(void *ap, int rc)
4000 struct vop_rename_args *a = ap;
4003 VFS_KNOTE_UNLOCKED(a->a_fdvp, NOTE_WRITE);
4004 VFS_KNOTE_UNLOCKED(a->a_tdvp, NOTE_WRITE);
4005 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME);
4007 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE);
4009 if (a->a_tdvp != a->a_fdvp)
4011 if (a->a_tvp != a->a_fvp)
4019 vop_rmdir_post(void *ap, int rc)
4021 struct vop_rmdir_args *a = ap;
4024 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4025 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4030 vop_setattr_post(void *ap, int rc)
4032 struct vop_setattr_args *a = ap;
4035 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4039 vop_symlink_post(void *ap, int rc)
4041 struct vop_symlink_args *a = ap;
4044 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4047 static struct knlist fs_knlist;
4050 vfs_event_init(void *arg)
4052 knlist_init_mtx(&fs_knlist, NULL);
4054 /* XXX - correct order? */
4055 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL);
4058 vfs_event_signal(fsid_t *fsid, u_int32_t event, intptr_t data __unused)
4061 KNOTE_UNLOCKED(&fs_knlist, event);
4064 static int filt_fsattach(struct knote *kn);
4065 static void filt_fsdetach(struct knote *kn);
4066 static int filt_fsevent(struct knote *kn, long hint);
4068 struct filterops fs_filtops =
4069 { 0, filt_fsattach, filt_fsdetach, filt_fsevent };
4072 filt_fsattach(struct knote *kn)
4075 kn->kn_flags |= EV_CLEAR;
4076 knlist_add(&fs_knlist, kn, 0);
4081 filt_fsdetach(struct knote *kn)
4084 knlist_remove(&fs_knlist, kn, 0);
4088 filt_fsevent(struct knote *kn, long hint)
4091 kn->kn_fflags |= hint;
4092 return (kn->kn_fflags != 0);
4096 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS)
4102 error = SYSCTL_IN(req, &vc, sizeof(vc));
4105 if (vc.vc_vers != VFS_CTL_VERS1)
4107 mp = vfs_getvfs(&vc.vc_fsid);
4110 /* ensure that a specific sysctl goes to the right filesystem. */
4111 if (strcmp(vc.vc_fstypename, "*") != 0 &&
4112 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) {
4116 VCTLTOREQ(&vc, req);
4117 error = VFS_SYSCTL(mp, vc.vc_op, req);
4122 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLFLAG_WR, NULL, 0, sysctl_vfs_ctl, "",
4126 * Function to initialize a va_filerev field sensibly.
4127 * XXX: Wouldn't a random number make a lot more sense ??
4130 init_va_filerev(void)
4135 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL));
4138 static int filt_vfsread(struct knote *kn, long hint);
4139 static int filt_vfswrite(struct knote *kn, long hint);
4140 static int filt_vfsvnode(struct knote *kn, long hint);
4141 static void filt_vfsdetach(struct knote *kn);
4142 static struct filterops vfsread_filtops =
4143 { 1, NULL, filt_vfsdetach, filt_vfsread };
4144 static struct filterops vfswrite_filtops =
4145 { 1, NULL, filt_vfsdetach, filt_vfswrite };
4146 static struct filterops vfsvnode_filtops =
4147 { 1, NULL, filt_vfsdetach, filt_vfsvnode };
4150 vfs_knllock(void *arg)
4152 struct vnode *vp = arg;
4154 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4158 vfs_knlunlock(void *arg)
4160 struct vnode *vp = arg;
4166 vfs_knl_assert_locked(void *arg)
4168 #ifdef DEBUG_VFS_LOCKS
4169 struct vnode *vp = arg;
4171 ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked");
4176 vfs_knl_assert_unlocked(void *arg)
4178 #ifdef DEBUG_VFS_LOCKS
4179 struct vnode *vp = arg;
4181 ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked");
4186 vfs_kqfilter(struct vop_kqfilter_args *ap)
4188 struct vnode *vp = ap->a_vp;
4189 struct knote *kn = ap->a_kn;
4192 switch (kn->kn_filter) {
4194 kn->kn_fop = &vfsread_filtops;
4197 kn->kn_fop = &vfswrite_filtops;
4200 kn->kn_fop = &vfsvnode_filtops;
4206 kn->kn_hook = (caddr_t)vp;
4209 if (vp->v_pollinfo == NULL)
4211 knl = &vp->v_pollinfo->vpi_selinfo.si_note;
4212 knlist_add(knl, kn, 0);
4218 * Detach knote from vnode
4221 filt_vfsdetach(struct knote *kn)
4223 struct vnode *vp = (struct vnode *)kn->kn_hook;
4225 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo"));
4226 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
4231 filt_vfsread(struct knote *kn, long hint)
4233 struct vnode *vp = (struct vnode *)kn->kn_hook;
4238 * filesystem is gone, so set the EOF flag and schedule
4239 * the knote for deletion.
4241 if (hint == NOTE_REVOKE) {
4243 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4248 if (VOP_GETATTR(vp, &va, curthread->td_ucred))
4252 kn->kn_data = va.va_size - kn->kn_fp->f_offset;
4253 res = (kn->kn_data != 0);
4260 filt_vfswrite(struct knote *kn, long hint)
4262 struct vnode *vp = (struct vnode *)kn->kn_hook;
4267 * filesystem is gone, so set the EOF flag and schedule
4268 * the knote for deletion.
4270 if (hint == NOTE_REVOKE)
4271 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4279 filt_vfsvnode(struct knote *kn, long hint)
4281 struct vnode *vp = (struct vnode *)kn->kn_hook;
4285 if (kn->kn_sfflags & hint)
4286 kn->kn_fflags |= hint;
4287 if (hint == NOTE_REVOKE) {
4288 kn->kn_flags |= EV_EOF;
4292 res = (kn->kn_fflags != 0);
4298 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off)
4302 if (dp->d_reclen > ap->a_uio->uio_resid)
4303 return (ENAMETOOLONG);
4304 error = uiomove(dp, dp->d_reclen, ap->a_uio);
4306 if (ap->a_ncookies != NULL) {
4307 if (ap->a_cookies != NULL)
4308 free(ap->a_cookies, M_TEMP);
4309 ap->a_cookies = NULL;
4310 *ap->a_ncookies = 0;
4314 if (ap->a_ncookies == NULL)
4317 KASSERT(ap->a_cookies,
4318 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!"));
4320 *ap->a_cookies = realloc(*ap->a_cookies,
4321 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO);
4322 (*ap->a_cookies)[*ap->a_ncookies] = off;
4327 * Mark for update the access time of the file if the filesystem
4328 * supports VOP_MARKATIME. This functionality is used by execve and
4329 * mmap, so we want to avoid the I/O implied by directly setting
4330 * va_atime for the sake of efficiency.
4333 vfs_mark_atime(struct vnode *vp, struct ucred *cred)
4338 VFS_ASSERT_GIANT(mp);
4339 ASSERT_VOP_LOCKED(vp, "vfs_mark_atime");
4340 if (mp != NULL && (mp->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
4341 (void)VOP_MARKATIME(vp);
4345 * The purpose of this routine is to remove granularity from accmode_t,
4346 * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE,
4347 * VADMIN and VAPPEND.
4349 * If it returns 0, the caller is supposed to continue with the usual
4350 * access checks using 'accmode' as modified by this routine. If it
4351 * returns nonzero value, the caller is supposed to return that value
4354 * Note that after this routine runs, accmode may be zero.
4357 vfs_unixify_accmode(accmode_t *accmode)
4360 * There is no way to specify explicit "deny" rule using
4361 * file mode or POSIX.1e ACLs.
4363 if (*accmode & VEXPLICIT_DENY) {
4369 * None of these can be translated into usual access bits.
4370 * Also, the common case for NFSv4 ACLs is to not contain
4371 * either of these bits. Caller should check for VWRITE
4372 * on the containing directory instead.
4374 if (*accmode & (VDELETE_CHILD | VDELETE))
4377 if (*accmode & VADMIN_PERMS) {
4378 *accmode &= ~VADMIN_PERMS;
4383 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL
4384 * or VSYNCHRONIZE using file mode or POSIX.1e ACL.
4386 *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE);