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;
195 * The workitem queue.
197 * It is useful to delay writes of file data and filesystem metadata
198 * for tens of seconds so that quickly created and deleted files need
199 * not waste disk bandwidth being created and removed. To realize this,
200 * we append vnodes to a "workitem" queue. When running with a soft
201 * updates implementation, most pending metadata dependencies should
202 * not wait for more than a few seconds. Thus, mounted on block devices
203 * are delayed only about a half the time that file data is delayed.
204 * Similarly, directory updates are more critical, so are only delayed
205 * about a third the time that file data is delayed. Thus, there are
206 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
207 * one each second (driven off the filesystem syncer process). The
208 * syncer_delayno variable indicates the next queue that is to be processed.
209 * Items that need to be processed soon are placed in this queue:
211 * syncer_workitem_pending[syncer_delayno]
213 * A delay of fifteen seconds is done by placing the request fifteen
214 * entries later in the queue:
216 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
219 static int syncer_delayno;
220 static long syncer_mask;
221 LIST_HEAD(synclist, bufobj);
222 static struct synclist *syncer_workitem_pending[2];
224 * The sync_mtx protects:
229 * syncer_workitem_pending
230 * syncer_worklist_len
233 static struct mtx sync_mtx;
234 static struct cv sync_wakeup;
236 #define SYNCER_MAXDELAY 32
237 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
238 static int syncdelay = 30; /* max time to delay syncing data */
239 static int filedelay = 30; /* time to delay syncing files */
240 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0,
241 "Time to delay syncing files (in seconds)");
242 static int dirdelay = 29; /* time to delay syncing directories */
243 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0,
244 "Time to delay syncing directories (in seconds)");
245 static int metadelay = 28; /* time to delay syncing metadata */
246 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0,
247 "Time to delay syncing metadata (in seconds)");
248 static int rushjob; /* number of slots to run ASAP */
249 static int stat_rush_requests; /* number of times I/O speeded up */
250 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0,
251 "Number of times I/O speeded up (rush requests)");
254 * When shutting down the syncer, run it at four times normal speed.
256 #define SYNCER_SHUTDOWN_SPEEDUP 4
257 static int sync_vnode_count;
258 static int syncer_worklist_len;
259 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY }
263 * Number of vnodes we want to exist at any one time. This is mostly used
264 * to size hash tables in vnode-related code. It is normally not used in
265 * getnewvnode(), as wantfreevnodes is normally nonzero.)
267 * XXX desiredvnodes is historical cruft and should not exist.
270 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
271 &desiredvnodes, 0, "Maximum number of vnodes");
272 SYSCTL_INT(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
273 &wantfreevnodes, 0, "Minimum number of vnodes (legacy)");
274 static int vnlru_nowhere;
275 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW,
276 &vnlru_nowhere, 0, "Number of times the vnlru process ran without success");
279 * Macros to control when a vnode is freed and recycled. All require
280 * the vnode interlock.
282 #define VCANRECYCLE(vp) (((vp)->v_iflag & VI_FREE) && !(vp)->v_holdcnt)
283 #define VSHOULDFREE(vp) (!((vp)->v_iflag & VI_FREE) && !(vp)->v_holdcnt)
284 #define VSHOULDBUSY(vp) (((vp)->v_iflag & VI_FREE) && (vp)->v_holdcnt)
288 * Initialize the vnode management data structures.
290 * Reevaluate the following cap on the number of vnodes after the physical
291 * memory size exceeds 512GB. In the limit, as the physical memory size
292 * grows, the ratio of physical pages to vnodes approaches sixteen to one.
294 #ifndef MAXVNODES_MAX
295 #define MAXVNODES_MAX (512 * (1024 * 1024 * 1024 / (int)PAGE_SIZE / 16))
298 vntblinit(void *dummy __unused)
300 int physvnodes, virtvnodes;
303 * Desiredvnodes is a function of the physical memory size and the
304 * kernel's heap size. Generally speaking, it scales with the
305 * physical memory size. The ratio of desiredvnodes to physical pages
306 * is one to four until desiredvnodes exceeds 98,304. Thereafter, the
307 * marginal ratio of desiredvnodes to physical pages is one to
308 * sixteen. However, desiredvnodes is limited by the kernel's heap
309 * size. The memory required by desiredvnodes vnodes and vm objects
310 * may not exceed one seventh of the kernel's heap size.
312 physvnodes = maxproc + cnt.v_page_count / 16 + 3 * min(98304 * 4,
313 cnt.v_page_count) / 16;
314 virtvnodes = vm_kmem_size / (7 * (sizeof(struct vm_object) +
315 sizeof(struct vnode)));
316 desiredvnodes = min(physvnodes, virtvnodes);
317 if (desiredvnodes > MAXVNODES_MAX) {
319 printf("Reducing kern.maxvnodes %d -> %d\n",
320 desiredvnodes, MAXVNODES_MAX);
321 desiredvnodes = MAXVNODES_MAX;
323 wantfreevnodes = desiredvnodes / 4;
324 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF);
325 TAILQ_INIT(&vnode_free_list);
326 mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF);
327 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL,
328 NULL, NULL, UMA_ALIGN_PTR, 0);
329 vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo),
330 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
332 * Initialize the filesystem syncer.
334 syncer_workitem_pending[WI_MPSAFEQ] = hashinit(syncer_maxdelay, M_VNODE,
336 syncer_workitem_pending[WI_GIANTQ] = hashinit(syncer_maxdelay, M_VNODE,
338 syncer_maxdelay = syncer_mask + 1;
339 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF);
340 cv_init(&sync_wakeup, "syncer");
342 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL);
346 * Mark a mount point as busy. Used to synchronize access and to delay
347 * unmounting. Eventually, mountlist_mtx is not released on failure.
350 vfs_busy(struct mount *mp, int flags)
353 MPASS((flags & ~MBF_MASK) == 0);
354 CTR3(KTR_VFS, "%s: mp %p with flags %d", __func__, mp, flags);
359 * If mount point is currenly being unmounted, sleep until the
360 * mount point fate is decided. If thread doing the unmounting fails,
361 * it will clear MNTK_UNMOUNT flag before waking us up, indicating
362 * that this mount point has survived the unmount attempt and vfs_busy
363 * should retry. Otherwise the unmounter thread will set MNTK_REFEXPIRE
364 * flag in addition to MNTK_UNMOUNT, indicating that mount point is
365 * about to be really destroyed. vfs_busy needs to release its
366 * reference on the mount point in this case and return with ENOENT,
367 * telling the caller that mount mount it tried to busy is no longer
370 while (mp->mnt_kern_flag & MNTK_UNMOUNT) {
371 if (flags & MBF_NOWAIT || mp->mnt_kern_flag & MNTK_REFEXPIRE) {
374 CTR1(KTR_VFS, "%s: failed busying before sleeping",
378 if (flags & MBF_MNTLSTLOCK)
379 mtx_unlock(&mountlist_mtx);
380 mp->mnt_kern_flag |= MNTK_MWAIT;
381 msleep(mp, MNT_MTX(mp), PVFS, "vfs_busy", 0);
382 if (flags & MBF_MNTLSTLOCK)
383 mtx_lock(&mountlist_mtx);
385 if (flags & MBF_MNTLSTLOCK)
386 mtx_unlock(&mountlist_mtx);
393 * Free a busy filesystem.
396 vfs_unbusy(struct mount *mp)
399 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
402 KASSERT(mp->mnt_lockref > 0, ("negative mnt_lockref"));
404 if (mp->mnt_lockref == 0 && (mp->mnt_kern_flag & MNTK_DRAINING) != 0) {
405 MPASS(mp->mnt_kern_flag & MNTK_UNMOUNT);
406 CTR1(KTR_VFS, "%s: waking up waiters", __func__);
407 mp->mnt_kern_flag &= ~MNTK_DRAINING;
408 wakeup(&mp->mnt_lockref);
414 * Lookup a mount point by filesystem identifier.
417 vfs_getvfs(fsid_t *fsid)
421 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
422 mtx_lock(&mountlist_mtx);
423 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
424 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
425 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
427 mtx_unlock(&mountlist_mtx);
431 mtx_unlock(&mountlist_mtx);
432 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
433 return ((struct mount *) 0);
437 * Lookup a mount point by filesystem identifier, busying it before
441 vfs_busyfs(fsid_t *fsid)
446 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
447 mtx_lock(&mountlist_mtx);
448 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
449 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
450 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
451 error = vfs_busy(mp, MBF_MNTLSTLOCK);
453 mtx_unlock(&mountlist_mtx);
459 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
460 mtx_unlock(&mountlist_mtx);
461 return ((struct mount *) 0);
465 * Check if a user can access privileged mount options.
468 vfs_suser(struct mount *mp, struct thread *td)
473 * If the thread is jailed, but this is not a jail-friendly file
474 * system, deny immediately.
476 if (!(mp->mnt_vfc->vfc_flags & VFCF_JAIL) && jailed(td->td_ucred))
480 * If the file system was mounted outside the jail of the calling
481 * thread, deny immediately.
483 if (prison_check(td->td_ucred, mp->mnt_cred) != 0)
487 * If file system supports delegated administration, we don't check
488 * for the PRIV_VFS_MOUNT_OWNER privilege - it will be better verified
489 * by the file system itself.
490 * If this is not the user that did original mount, we check for
491 * the PRIV_VFS_MOUNT_OWNER privilege.
493 if (!(mp->mnt_vfc->vfc_flags & VFCF_DELEGADMIN) &&
494 mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
495 if ((error = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 0)
502 * Get a new unique fsid. Try to make its val[0] unique, since this value
503 * will be used to create fake device numbers for stat(). Also try (but
504 * not so hard) make its val[0] unique mod 2^16, since some emulators only
505 * support 16-bit device numbers. We end up with unique val[0]'s for the
506 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
508 * Keep in mind that several mounts may be running in parallel. Starting
509 * the search one past where the previous search terminated is both a
510 * micro-optimization and a defense against returning the same fsid to
514 vfs_getnewfsid(struct mount *mp)
516 static uint16_t mntid_base;
521 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
522 mtx_lock(&mntid_mtx);
523 mtype = mp->mnt_vfc->vfc_typenum;
524 tfsid.val[1] = mtype;
525 mtype = (mtype & 0xFF) << 24;
527 tfsid.val[0] = makedev(255,
528 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
530 if ((nmp = vfs_getvfs(&tfsid)) == NULL)
534 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
535 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
536 mtx_unlock(&mntid_mtx);
540 * Knob to control the precision of file timestamps:
542 * 0 = seconds only; nanoseconds zeroed.
543 * 1 = seconds and nanoseconds, accurate within 1/HZ.
544 * 2 = seconds and nanoseconds, truncated to microseconds.
545 * >=3 = seconds and nanoseconds, maximum precision.
547 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
549 static int timestamp_precision = TSP_SEC;
550 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
551 ×tamp_precision, 0, "File timestamp precision (0: seconds, "
552 "1: sec + ns accurate to 1/HZ, 2: sec + ns truncated to ms, "
553 "3+: sec + ns (max. precision))");
556 * Get a current timestamp.
559 vfs_timestamp(struct timespec *tsp)
563 switch (timestamp_precision) {
565 tsp->tv_sec = time_second;
573 TIMEVAL_TO_TIMESPEC(&tv, tsp);
583 * Set vnode attributes to VNOVAL
586 vattr_null(struct vattr *vap)
590 vap->va_size = VNOVAL;
591 vap->va_bytes = VNOVAL;
592 vap->va_mode = VNOVAL;
593 vap->va_nlink = VNOVAL;
594 vap->va_uid = VNOVAL;
595 vap->va_gid = VNOVAL;
596 vap->va_fsid = VNOVAL;
597 vap->va_fileid = VNOVAL;
598 vap->va_blocksize = VNOVAL;
599 vap->va_rdev = VNOVAL;
600 vap->va_atime.tv_sec = VNOVAL;
601 vap->va_atime.tv_nsec = VNOVAL;
602 vap->va_mtime.tv_sec = VNOVAL;
603 vap->va_mtime.tv_nsec = VNOVAL;
604 vap->va_ctime.tv_sec = VNOVAL;
605 vap->va_ctime.tv_nsec = VNOVAL;
606 vap->va_birthtime.tv_sec = VNOVAL;
607 vap->va_birthtime.tv_nsec = VNOVAL;
608 vap->va_flags = VNOVAL;
609 vap->va_gen = VNOVAL;
614 * This routine is called when we have too many vnodes. It attempts
615 * to free <count> vnodes and will potentially free vnodes that still
616 * have VM backing store (VM backing store is typically the cause
617 * of a vnode blowout so we want to do this). Therefore, this operation
618 * is not considered cheap.
620 * A number of conditions may prevent a vnode from being reclaimed.
621 * the buffer cache may have references on the vnode, a directory
622 * vnode may still have references due to the namei cache representing
623 * underlying files, or the vnode may be in active use. It is not
624 * desireable to reuse such vnodes. These conditions may cause the
625 * number of vnodes to reach some minimum value regardless of what
626 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
629 vlrureclaim(struct mount *mp)
638 * Calculate the trigger point, don't allow user
639 * screwups to blow us up. This prevents us from
640 * recycling vnodes with lots of resident pages. We
641 * aren't trying to free memory, we are trying to
644 usevnodes = desiredvnodes;
647 trigger = cnt.v_page_count * 2 / usevnodes;
649 vn_start_write(NULL, &mp, V_WAIT);
651 count = mp->mnt_nvnodelistsize / 10 + 1;
653 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
654 while (vp != NULL && vp->v_type == VMARKER)
655 vp = TAILQ_NEXT(vp, v_nmntvnodes);
658 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
659 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
664 * If it's been deconstructed already, it's still
665 * referenced, or it exceeds the trigger, skip it.
667 if (vp->v_usecount ||
668 (!vlru_allow_cache_src &&
669 !LIST_EMPTY(&(vp)->v_cache_src)) ||
670 (vp->v_iflag & VI_DOOMED) != 0 || (vp->v_object != NULL &&
671 vp->v_object->resident_page_count > trigger)) {
677 if (VOP_LOCK(vp, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT)) {
679 goto next_iter_mntunlocked;
683 * v_usecount may have been bumped after VOP_LOCK() dropped
684 * the vnode interlock and before it was locked again.
686 * It is not necessary to recheck VI_DOOMED because it can
687 * only be set by another thread that holds both the vnode
688 * lock and vnode interlock. If another thread has the
689 * vnode lock before we get to VOP_LOCK() and obtains the
690 * vnode interlock after VOP_LOCK() drops the vnode
691 * interlock, the other thread will be unable to drop the
692 * vnode lock before our VOP_LOCK() call fails.
694 if (vp->v_usecount ||
695 (!vlru_allow_cache_src &&
696 !LIST_EMPTY(&(vp)->v_cache_src)) ||
697 (vp->v_object != NULL &&
698 vp->v_object->resident_page_count > trigger)) {
699 VOP_UNLOCK(vp, LK_INTERLOCK);
700 goto next_iter_mntunlocked;
702 KASSERT((vp->v_iflag & VI_DOOMED) == 0,
703 ("VI_DOOMED unexpectedly detected in vlrureclaim()"));
708 next_iter_mntunlocked:
709 if ((count % 256) != 0)
713 if ((count % 256) != 0)
722 vn_finished_write(mp);
727 * Attempt to keep the free list at wantfreevnodes length.
730 vnlru_free(int count)
735 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
736 for (; count > 0; count--) {
737 vp = TAILQ_FIRST(&vnode_free_list);
739 * The list can be modified while the free_list_mtx
740 * has been dropped and vp could be NULL here.
744 VNASSERT(vp->v_op != NULL, vp,
745 ("vnlru_free: vnode already reclaimed."));
746 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
748 * Don't recycle if we can't get the interlock.
750 if (!VI_TRYLOCK(vp)) {
751 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
754 VNASSERT(VCANRECYCLE(vp), vp,
755 ("vp inconsistent on freelist"));
757 vp->v_iflag &= ~VI_FREE;
759 mtx_unlock(&vnode_free_list_mtx);
761 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
763 VFS_UNLOCK_GIANT(vfslocked);
765 * If the recycled succeeded this vdrop will actually free
766 * the vnode. If not it will simply place it back on
770 mtx_lock(&vnode_free_list_mtx);
774 * Attempt to recycle vnodes in a context that is always safe to block.
775 * Calling vlrurecycle() from the bowels of filesystem code has some
776 * interesting deadlock problems.
778 static struct proc *vnlruproc;
779 static int vnlruproc_sig;
784 struct mount *mp, *nmp;
786 struct proc *p = vnlruproc;
788 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, p,
792 kproc_suspend_check(p);
793 mtx_lock(&vnode_free_list_mtx);
794 if (freevnodes > wantfreevnodes)
795 vnlru_free(freevnodes - wantfreevnodes);
796 if (numvnodes <= desiredvnodes * 9 / 10) {
798 wakeup(&vnlruproc_sig);
799 msleep(vnlruproc, &vnode_free_list_mtx,
800 PVFS|PDROP, "vlruwt", hz);
803 mtx_unlock(&vnode_free_list_mtx);
805 mtx_lock(&mountlist_mtx);
806 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
807 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
808 nmp = TAILQ_NEXT(mp, mnt_list);
811 vfslocked = VFS_LOCK_GIANT(mp);
812 done += vlrureclaim(mp);
813 VFS_UNLOCK_GIANT(vfslocked);
814 mtx_lock(&mountlist_mtx);
815 nmp = TAILQ_NEXT(mp, mnt_list);
818 mtx_unlock(&mountlist_mtx);
821 /* These messages are temporary debugging aids */
822 if (vnlru_nowhere < 5)
823 printf("vnlru process getting nowhere..\n");
824 else if (vnlru_nowhere == 5)
825 printf("vnlru process messages stopped.\n");
828 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
834 static struct kproc_desc vnlru_kp = {
839 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start,
843 * Routines having to do with the management of the vnode table.
847 vdestroy(struct vnode *vp)
851 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
852 mtx_lock(&vnode_free_list_mtx);
854 mtx_unlock(&vnode_free_list_mtx);
856 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
857 ("cleaned vnode still on the free list."));
858 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't"));
859 VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count"));
860 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count"));
861 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count"));
862 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's"));
863 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0"));
864 VNASSERT(bo->bo_clean.bv_root == NULL, vp, ("cleanblkroot not NULL"));
865 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0"));
866 VNASSERT(bo->bo_dirty.bv_root == NULL, vp, ("dirtyblkroot not NULL"));
867 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst"));
868 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src"));
869 VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for .."));
872 mac_vnode_destroy(vp);
874 if (vp->v_pollinfo != NULL)
875 destroy_vpollinfo(vp->v_pollinfo);
877 /* XXX Elsewhere we can detect an already freed vnode via NULL v_op. */
880 lockdestroy(vp->v_vnlock);
881 mtx_destroy(&vp->v_interlock);
882 mtx_destroy(BO_MTX(bo));
883 uma_zfree(vnode_zone, vp);
887 * Try to recycle a freed vnode. We abort if anyone picks up a reference
888 * before we actually vgone(). This function must be called with the vnode
889 * held to prevent the vnode from being returned to the free list midway
893 vtryrecycle(struct vnode *vp)
897 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
898 VNASSERT(vp->v_holdcnt, vp,
899 ("vtryrecycle: Recycling vp %p without a reference.", vp));
901 * This vnode may found and locked via some other list, if so we
902 * can't recycle it yet.
904 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
906 "%s: impossible to recycle, vp %p lock is already held",
908 return (EWOULDBLOCK);
911 * Don't recycle if its filesystem is being suspended.
913 if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) {
916 "%s: impossible to recycle, cannot start the write for %p",
921 * If we got this far, we need to acquire the interlock and see if
922 * anyone picked up this vnode from another list. If not, we will
923 * mark it with DOOMED via vgonel() so that anyone who does find it
927 if (vp->v_usecount) {
928 VOP_UNLOCK(vp, LK_INTERLOCK);
929 vn_finished_write(vnmp);
931 "%s: impossible to recycle, %p is already referenced",
935 if ((vp->v_iflag & VI_DOOMED) == 0)
937 VOP_UNLOCK(vp, LK_INTERLOCK);
938 vn_finished_write(vnmp);
943 * Return the next vnode from the free list.
946 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops,
949 struct vnode *vp = NULL;
952 CTR3(KTR_VFS, "%s: mp %p with tag %s", __func__, mp, tag);
953 mtx_lock(&vnode_free_list_mtx);
955 * Lend our context to reclaim vnodes if they've exceeded the max.
957 if (freevnodes > wantfreevnodes)
960 * Wait for available vnodes.
962 if (numvnodes > desiredvnodes) {
963 if (mp != NULL && (mp->mnt_kern_flag & MNTK_SUSPEND)) {
965 * File system is beeing suspended, we cannot risk a
966 * deadlock here, so allocate new vnode anyway.
968 if (freevnodes > wantfreevnodes)
969 vnlru_free(freevnodes - wantfreevnodes);
972 if (vnlruproc_sig == 0) {
973 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
976 msleep(&vnlruproc_sig, &vnode_free_list_mtx, PVFS,
978 #if 0 /* XXX Not all VFS_VGET/ffs_vget callers check returns. */
979 if (numvnodes > desiredvnodes) {
980 mtx_unlock(&vnode_free_list_mtx);
987 mtx_unlock(&vnode_free_list_mtx);
988 vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK|M_ZERO);
992 vp->v_vnlock = &vp->v_lock;
993 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF);
995 * By default, don't allow shared locks unless filesystems
998 lockinit(vp->v_vnlock, PVFS, tag, VLKTIMEOUT, LK_NOSHARE);
1000 * Initialize bufobj.
1003 bo->__bo_vnode = vp;
1004 mtx_init(BO_MTX(bo), "bufobj interlock", NULL, MTX_DEF);
1005 bo->bo_ops = &buf_ops_bio;
1006 bo->bo_private = vp;
1007 TAILQ_INIT(&bo->bo_clean.bv_hd);
1008 TAILQ_INIT(&bo->bo_dirty.bv_hd);
1010 * Initialize namecache.
1012 LIST_INIT(&vp->v_cache_src);
1013 TAILQ_INIT(&vp->v_cache_dst);
1015 * Finalize various vnode identity bits.
1020 v_incr_usecount(vp);
1024 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0)
1025 mac_vnode_associate_singlelabel(mp, vp);
1026 else if (mp == NULL && vops != &dead_vnodeops)
1027 printf("NULL mp in getnewvnode()\n");
1030 bo->bo_bsize = mp->mnt_stat.f_iosize;
1031 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0)
1032 vp->v_vflag |= VV_NOKNOTE;
1040 * Delete from old mount point vnode list, if on one.
1043 delmntque(struct vnode *vp)
1052 VNASSERT(mp->mnt_nvnodelistsize > 0, vp,
1053 ("bad mount point vnode list size"));
1054 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1055 mp->mnt_nvnodelistsize--;
1061 insmntque_stddtr(struct vnode *vp, void *dtr_arg)
1065 vp->v_op = &dead_vnodeops;
1066 /* XXX non mp-safe fs may still call insmntque with vnode
1068 if (!VOP_ISLOCKED(vp))
1069 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1075 * Insert into list of vnodes for the new mount point, if available.
1078 insmntque1(struct vnode *vp, struct mount *mp,
1079 void (*dtr)(struct vnode *, void *), void *dtr_arg)
1083 KASSERT(vp->v_mount == NULL,
1084 ("insmntque: vnode already on per mount vnode list"));
1085 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)"));
1086 #ifdef DEBUG_VFS_LOCKS
1087 if (!VFS_NEEDSGIANT(mp))
1088 ASSERT_VOP_ELOCKED(vp,
1089 "insmntque: mp-safe fs and non-locked vp");
1092 if ((mp->mnt_kern_flag & MNTK_NOINSMNTQ) != 0 &&
1093 ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 ||
1094 mp->mnt_nvnodelistsize == 0)) {
1095 locked = VOP_ISLOCKED(vp);
1096 if (!locked || (locked == LK_EXCLUSIVE &&
1097 (vp->v_vflag & VV_FORCEINSMQ) == 0)) {
1106 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1107 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp,
1108 ("neg mount point vnode list size"));
1109 mp->mnt_nvnodelistsize++;
1115 insmntque(struct vnode *vp, struct mount *mp)
1118 return (insmntque1(vp, mp, insmntque_stddtr, NULL));
1122 * Flush out and invalidate all buffers associated with a bufobj
1123 * Called with the underlying object locked.
1126 bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo)
1131 if (flags & V_SAVE) {
1132 error = bufobj_wwait(bo, slpflag, slptimeo);
1137 if (bo->bo_dirty.bv_cnt > 0) {
1139 if ((error = BO_SYNC(bo, MNT_WAIT)) != 0)
1142 * XXX We could save a lock/unlock if this was only
1143 * enabled under INVARIANTS
1146 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)
1147 panic("vinvalbuf: dirty bufs");
1151 * If you alter this loop please notice that interlock is dropped and
1152 * reacquired in flushbuflist. Special care is needed to ensure that
1153 * no race conditions occur from this.
1156 error = flushbuflist(&bo->bo_clean,
1157 flags, bo, slpflag, slptimeo);
1159 error = flushbuflist(&bo->bo_dirty,
1160 flags, bo, slpflag, slptimeo);
1161 if (error != 0 && error != EAGAIN) {
1165 } while (error != 0);
1168 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
1169 * have write I/O in-progress but if there is a VM object then the
1170 * VM object can also have read-I/O in-progress.
1173 bufobj_wwait(bo, 0, 0);
1175 if (bo->bo_object != NULL) {
1176 VM_OBJECT_LOCK(bo->bo_object);
1177 vm_object_pip_wait(bo->bo_object, "bovlbx");
1178 VM_OBJECT_UNLOCK(bo->bo_object);
1181 } while (bo->bo_numoutput > 0);
1185 * Destroy the copy in the VM cache, too.
1187 if (bo->bo_object != NULL && (flags & (V_ALT | V_NORMAL)) == 0) {
1188 VM_OBJECT_LOCK(bo->bo_object);
1189 vm_object_page_remove(bo->bo_object, 0, 0,
1190 (flags & V_SAVE) ? TRUE : FALSE);
1191 VM_OBJECT_UNLOCK(bo->bo_object);
1196 if ((flags & (V_ALT | V_NORMAL)) == 0 &&
1197 (bo->bo_dirty.bv_cnt > 0 || bo->bo_clean.bv_cnt > 0))
1198 panic("vinvalbuf: flush failed");
1205 * Flush out and invalidate all buffers associated with a vnode.
1206 * Called with the underlying object locked.
1209 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
1212 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
1213 ASSERT_VOP_LOCKED(vp, "vinvalbuf");
1214 return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo));
1218 * Flush out buffers on the specified list.
1222 flushbuflist( struct bufv *bufv, int flags, struct bufobj *bo, int slpflag,
1225 struct buf *bp, *nbp;
1230 ASSERT_BO_LOCKED(bo);
1233 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) {
1234 if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) ||
1235 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) {
1241 lblkno = nbp->b_lblkno;
1242 xflags = nbp->b_xflags &
1243 (BX_BKGRDMARKER | BX_VNDIRTY | BX_VNCLEAN);
1246 error = BUF_TIMELOCK(bp,
1247 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_MTX(bo),
1248 "flushbuf", slpflag, slptimeo);
1251 return (error != ENOLCK ? error : EAGAIN);
1253 KASSERT(bp->b_bufobj == bo,
1254 ("bp %p wrong b_bufobj %p should be %p",
1255 bp, bp->b_bufobj, bo));
1256 if (bp->b_bufobj != bo) { /* XXX: necessary ? */
1262 * XXX Since there are no node locks for NFS, I
1263 * believe there is a slight chance that a delayed
1264 * write will occur while sleeping just above, so
1267 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
1272 bp->b_flags |= B_ASYNC;
1275 return (EAGAIN); /* XXX: why not loop ? */
1280 bp->b_flags |= (B_INVAL | B_RELBUF);
1281 bp->b_flags &= ~B_ASYNC;
1285 (nbp->b_bufobj != bo ||
1286 nbp->b_lblkno != lblkno ||
1288 (BX_BKGRDMARKER | BX_VNDIRTY | BX_VNCLEAN)) != xflags))
1289 break; /* nbp invalid */
1295 * Truncate a file's buffer and pages to a specified length. This
1296 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
1300 vtruncbuf(struct vnode *vp, struct ucred *cred, struct thread *td,
1301 off_t length, int blksize)
1303 struct buf *bp, *nbp;
1308 CTR5(KTR_VFS, "%s: vp %p with cred %p and block %d:%ju", __func__,
1309 vp, cred, blksize, (uintmax_t)length);
1312 * Round up to the *next* lbn.
1314 trunclbn = (length + blksize - 1) / blksize;
1316 ASSERT_VOP_LOCKED(vp, "vtruncbuf");
1323 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) {
1324 if (bp->b_lblkno < trunclbn)
1327 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1328 BO_MTX(bo)) == ENOLCK)
1334 bp->b_flags |= (B_INVAL | B_RELBUF);
1335 bp->b_flags &= ~B_ASYNC;
1340 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
1341 (nbp->b_vp != vp) ||
1342 (nbp->b_flags & B_DELWRI))) {
1348 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1349 if (bp->b_lblkno < trunclbn)
1352 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1353 BO_MTX(bo)) == ENOLCK)
1358 bp->b_flags |= (B_INVAL | B_RELBUF);
1359 bp->b_flags &= ~B_ASYNC;
1363 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
1364 (nbp->b_vp != vp) ||
1365 (nbp->b_flags & B_DELWRI) == 0)) {
1374 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1375 if (bp->b_lblkno > 0)
1378 * Since we hold the vnode lock this should only
1379 * fail if we're racing with the buf daemon.
1382 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1383 BO_MTX(bo)) == ENOLCK) {
1386 VNASSERT((bp->b_flags & B_DELWRI), vp,
1387 ("buf(%p) on dirty queue without DELWRI", bp));
1398 bufobj_wwait(bo, 0, 0);
1400 vnode_pager_setsize(vp, length);
1406 * buf_splay() - splay tree core for the clean/dirty list of buffers in
1409 * NOTE: We have to deal with the special case of a background bitmap
1410 * buffer, a situation where two buffers will have the same logical
1411 * block offset. We want (1) only the foreground buffer to be accessed
1412 * in a lookup and (2) must differentiate between the foreground and
1413 * background buffer in the splay tree algorithm because the splay
1414 * tree cannot normally handle multiple entities with the same 'index'.
1415 * We accomplish this by adding differentiating flags to the splay tree's
1420 buf_splay(daddr_t lblkno, b_xflags_t xflags, struct buf *root)
1423 struct buf *lefttreemax, *righttreemin, *y;
1427 lefttreemax = righttreemin = &dummy;
1429 if (lblkno < root->b_lblkno ||
1430 (lblkno == root->b_lblkno &&
1431 (xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
1432 if ((y = root->b_left) == NULL)
1434 if (lblkno < y->b_lblkno) {
1436 root->b_left = y->b_right;
1439 if ((y = root->b_left) == NULL)
1442 /* Link into the new root's right tree. */
1443 righttreemin->b_left = root;
1444 righttreemin = root;
1445 } else if (lblkno > root->b_lblkno ||
1446 (lblkno == root->b_lblkno &&
1447 (xflags & BX_BKGRDMARKER) > (root->b_xflags & BX_BKGRDMARKER))) {
1448 if ((y = root->b_right) == NULL)
1450 if (lblkno > y->b_lblkno) {
1452 root->b_right = y->b_left;
1455 if ((y = root->b_right) == NULL)
1458 /* Link into the new root's left tree. */
1459 lefttreemax->b_right = root;
1466 /* Assemble the new root. */
1467 lefttreemax->b_right = root->b_left;
1468 righttreemin->b_left = root->b_right;
1469 root->b_left = dummy.b_right;
1470 root->b_right = dummy.b_left;
1475 buf_vlist_remove(struct buf *bp)
1480 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1481 ASSERT_BO_LOCKED(bp->b_bufobj);
1482 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) !=
1483 (BX_VNDIRTY|BX_VNCLEAN),
1484 ("buf_vlist_remove: Buf %p is on two lists", bp));
1485 if (bp->b_xflags & BX_VNDIRTY)
1486 bv = &bp->b_bufobj->bo_dirty;
1488 bv = &bp->b_bufobj->bo_clean;
1489 if (bp != bv->bv_root) {
1490 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root);
1491 KASSERT(root == bp, ("splay lookup failed in remove"));
1493 if (bp->b_left == NULL) {
1496 root = buf_splay(bp->b_lblkno, bp->b_xflags, bp->b_left);
1497 root->b_right = bp->b_right;
1500 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs);
1502 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1506 * Add the buffer to the sorted clean or dirty block list using a
1507 * splay tree algorithm.
1509 * NOTE: xflags is passed as a constant, optimizing this inline function!
1512 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags)
1517 ASSERT_BO_LOCKED(bo);
1518 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0,
1519 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags));
1520 bp->b_xflags |= xflags;
1521 if (xflags & BX_VNDIRTY)
1526 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root);
1530 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs);
1531 } else if (bp->b_lblkno < root->b_lblkno ||
1532 (bp->b_lblkno == root->b_lblkno &&
1533 (bp->b_xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
1534 bp->b_left = root->b_left;
1536 root->b_left = NULL;
1537 TAILQ_INSERT_BEFORE(root, bp, b_bobufs);
1539 bp->b_right = root->b_right;
1541 root->b_right = NULL;
1542 TAILQ_INSERT_AFTER(&bv->bv_hd, root, bp, b_bobufs);
1549 * Lookup a buffer using the splay tree. Note that we specifically avoid
1550 * shadow buffers used in background bitmap writes.
1552 * This code isn't quite efficient as it could be because we are maintaining
1553 * two sorted lists and do not know which list the block resides in.
1555 * During a "make buildworld" the desired buffer is found at one of
1556 * the roots more than 60% of the time. Thus, checking both roots
1557 * before performing either splay eliminates unnecessary splays on the
1558 * first tree splayed.
1561 gbincore(struct bufobj *bo, daddr_t lblkno)
1565 ASSERT_BO_LOCKED(bo);
1566 if ((bp = bo->bo_clean.bv_root) != NULL &&
1567 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1569 if ((bp = bo->bo_dirty.bv_root) != NULL &&
1570 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1572 if ((bp = bo->bo_clean.bv_root) != NULL) {
1573 bo->bo_clean.bv_root = bp = buf_splay(lblkno, 0, bp);
1574 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1577 if ((bp = bo->bo_dirty.bv_root) != NULL) {
1578 bo->bo_dirty.bv_root = bp = buf_splay(lblkno, 0, bp);
1579 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1586 * Associate a buffer with a vnode.
1589 bgetvp(struct vnode *vp, struct buf *bp)
1594 ASSERT_BO_LOCKED(bo);
1595 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free"));
1597 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags);
1598 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp,
1599 ("bgetvp: bp already attached! %p", bp));
1602 if (VFS_NEEDSGIANT(vp->v_mount) || bo->bo_flag & BO_NEEDSGIANT)
1603 bp->b_flags |= B_NEEDSGIANT;
1607 * Insert onto list for new vnode.
1609 buf_vlist_add(bp, bo, BX_VNCLEAN);
1613 * Disassociate a buffer from a vnode.
1616 brelvp(struct buf *bp)
1621 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1622 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1625 * Delete from old vnode list, if on one.
1627 vp = bp->b_vp; /* XXX */
1630 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1631 buf_vlist_remove(bp);
1633 panic("brelvp: Buffer %p not on queue.", bp);
1634 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
1635 bo->bo_flag &= ~BO_ONWORKLST;
1636 mtx_lock(&sync_mtx);
1637 LIST_REMOVE(bo, bo_synclist);
1638 syncer_worklist_len--;
1639 mtx_unlock(&sync_mtx);
1641 bp->b_flags &= ~B_NEEDSGIANT;
1643 bp->b_bufobj = NULL;
1649 * Add an item to the syncer work queue.
1652 vn_syncer_add_to_worklist(struct bufobj *bo, int delay)
1656 ASSERT_BO_LOCKED(bo);
1658 mtx_lock(&sync_mtx);
1659 if (bo->bo_flag & BO_ONWORKLST)
1660 LIST_REMOVE(bo, bo_synclist);
1662 bo->bo_flag |= BO_ONWORKLST;
1663 syncer_worklist_len++;
1666 if (delay > syncer_maxdelay - 2)
1667 delay = syncer_maxdelay - 2;
1668 slot = (syncer_delayno + delay) & syncer_mask;
1670 queue = VFS_NEEDSGIANT(bo->__bo_vnode->v_mount) ? WI_GIANTQ :
1672 LIST_INSERT_HEAD(&syncer_workitem_pending[queue][slot], bo,
1674 mtx_unlock(&sync_mtx);
1678 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS)
1682 mtx_lock(&sync_mtx);
1683 len = syncer_worklist_len - sync_vnode_count;
1684 mtx_unlock(&sync_mtx);
1685 error = SYSCTL_OUT(req, &len, sizeof(len));
1689 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, CTLTYPE_INT | CTLFLAG_RD, NULL, 0,
1690 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length");
1692 static struct proc *updateproc;
1693 static void sched_sync(void);
1694 static struct kproc_desc up_kp = {
1699 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp);
1702 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td)
1707 *bo = LIST_FIRST(slp);
1710 vp = (*bo)->__bo_vnode; /* XXX */
1711 if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0)
1714 * We use vhold in case the vnode does not
1715 * successfully sync. vhold prevents the vnode from
1716 * going away when we unlock the sync_mtx so that
1717 * we can acquire the vnode interlock.
1720 mtx_unlock(&sync_mtx);
1722 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1724 mtx_lock(&sync_mtx);
1725 return (*bo == LIST_FIRST(slp));
1727 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1728 (void) VOP_FSYNC(vp, MNT_LAZY, td);
1730 vn_finished_write(mp);
1732 if (((*bo)->bo_flag & BO_ONWORKLST) != 0) {
1734 * Put us back on the worklist. The worklist
1735 * routine will remove us from our current
1736 * position and then add us back in at a later
1739 vn_syncer_add_to_worklist(*bo, syncdelay);
1743 mtx_lock(&sync_mtx);
1748 * System filesystem synchronizer daemon.
1753 struct synclist *gnext, *next;
1754 struct synclist *gslp, *slp;
1757 struct thread *td = curthread;
1759 int net_worklist_len;
1760 int syncer_final_iter;
1765 syncer_final_iter = 0;
1767 syncer_state = SYNCER_RUNNING;
1768 starttime = time_uptime;
1769 td->td_pflags |= TDP_NORUNNINGBUF;
1771 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc,
1774 mtx_lock(&sync_mtx);
1776 if (syncer_state == SYNCER_FINAL_DELAY &&
1777 syncer_final_iter == 0) {
1778 mtx_unlock(&sync_mtx);
1779 kproc_suspend_check(td->td_proc);
1780 mtx_lock(&sync_mtx);
1782 net_worklist_len = syncer_worklist_len - sync_vnode_count;
1783 if (syncer_state != SYNCER_RUNNING &&
1784 starttime != time_uptime) {
1786 printf("\nSyncing disks, vnodes remaining...");
1789 printf("%d ", net_worklist_len);
1791 starttime = time_uptime;
1794 * Push files whose dirty time has expired. Be careful
1795 * of interrupt race on slp queue.
1797 * Skip over empty worklist slots when shutting down.
1800 slp = &syncer_workitem_pending[WI_MPSAFEQ][syncer_delayno];
1801 gslp = &syncer_workitem_pending[WI_GIANTQ][syncer_delayno];
1802 syncer_delayno += 1;
1803 if (syncer_delayno == syncer_maxdelay)
1805 next = &syncer_workitem_pending[WI_MPSAFEQ][syncer_delayno];
1806 gnext = &syncer_workitem_pending[WI_GIANTQ][syncer_delayno];
1808 * If the worklist has wrapped since the
1809 * it was emptied of all but syncer vnodes,
1810 * switch to the FINAL_DELAY state and run
1811 * for one more second.
1813 if (syncer_state == SYNCER_SHUTTING_DOWN &&
1814 net_worklist_len == 0 &&
1815 last_work_seen == syncer_delayno) {
1816 syncer_state = SYNCER_FINAL_DELAY;
1817 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP;
1819 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) &&
1820 LIST_EMPTY(gslp) && syncer_worklist_len > 0);
1823 * Keep track of the last time there was anything
1824 * on the worklist other than syncer vnodes.
1825 * Return to the SHUTTING_DOWN state if any
1828 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING)
1829 last_work_seen = syncer_delayno;
1830 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY)
1831 syncer_state = SYNCER_SHUTTING_DOWN;
1832 while (!LIST_EMPTY(slp)) {
1833 error = sync_vnode(slp, &bo, td);
1835 LIST_REMOVE(bo, bo_synclist);
1836 LIST_INSERT_HEAD(next, bo, bo_synclist);
1840 if (!LIST_EMPTY(gslp)) {
1841 mtx_unlock(&sync_mtx);
1843 mtx_lock(&sync_mtx);
1844 while (!LIST_EMPTY(gslp)) {
1845 error = sync_vnode(gslp, &bo, td);
1847 LIST_REMOVE(bo, bo_synclist);
1848 LIST_INSERT_HEAD(gnext, bo,
1855 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0)
1856 syncer_final_iter--;
1858 * The variable rushjob allows the kernel to speed up the
1859 * processing of the filesystem syncer process. A rushjob
1860 * value of N tells the filesystem syncer to process the next
1861 * N seconds worth of work on its queue ASAP. Currently rushjob
1862 * is used by the soft update code to speed up the filesystem
1863 * syncer process when the incore state is getting so far
1864 * ahead of the disk that the kernel memory pool is being
1865 * threatened with exhaustion.
1872 * Just sleep for a short period of time between
1873 * iterations when shutting down to allow some I/O
1876 * If it has taken us less than a second to process the
1877 * current work, then wait. Otherwise start right over
1878 * again. We can still lose time if any single round
1879 * takes more than two seconds, but it does not really
1880 * matter as we are just trying to generally pace the
1881 * filesystem activity.
1883 if (syncer_state != SYNCER_RUNNING)
1884 cv_timedwait(&sync_wakeup, &sync_mtx,
1885 hz / SYNCER_SHUTDOWN_SPEEDUP);
1886 else if (time_uptime == starttime)
1887 cv_timedwait(&sync_wakeup, &sync_mtx, hz);
1892 * Request the syncer daemon to speed up its work.
1893 * We never push it to speed up more than half of its
1894 * normal turn time, otherwise it could take over the cpu.
1897 speedup_syncer(void)
1901 mtx_lock(&sync_mtx);
1902 if (rushjob < syncdelay / 2) {
1904 stat_rush_requests += 1;
1907 mtx_unlock(&sync_mtx);
1908 cv_broadcast(&sync_wakeup);
1913 * Tell the syncer to speed up its work and run though its work
1914 * list several times, then tell it to shut down.
1917 syncer_shutdown(void *arg, int howto)
1920 if (howto & RB_NOSYNC)
1922 mtx_lock(&sync_mtx);
1923 syncer_state = SYNCER_SHUTTING_DOWN;
1925 mtx_unlock(&sync_mtx);
1926 cv_broadcast(&sync_wakeup);
1927 kproc_shutdown(arg, howto);
1931 * Reassign a buffer from one vnode to another.
1932 * Used to assign file specific control information
1933 * (indirect blocks) to the vnode to which they belong.
1936 reassignbuf(struct buf *bp)
1949 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X",
1950 bp, bp->b_vp, bp->b_flags);
1952 * B_PAGING flagged buffers cannot be reassigned because their vp
1953 * is not fully linked in.
1955 if (bp->b_flags & B_PAGING)
1956 panic("cannot reassign paging buffer");
1959 * Delete from old vnode list, if on one.
1962 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1963 buf_vlist_remove(bp);
1965 panic("reassignbuf: Buffer %p not on queue.", bp);
1967 * If dirty, put on list of dirty buffers; otherwise insert onto list
1970 if (bp->b_flags & B_DELWRI) {
1971 if ((bo->bo_flag & BO_ONWORKLST) == 0) {
1972 switch (vp->v_type) {
1982 vn_syncer_add_to_worklist(bo, delay);
1984 buf_vlist_add(bp, bo, BX_VNDIRTY);
1986 buf_vlist_add(bp, bo, BX_VNCLEAN);
1988 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
1989 mtx_lock(&sync_mtx);
1990 LIST_REMOVE(bo, bo_synclist);
1991 syncer_worklist_len--;
1992 mtx_unlock(&sync_mtx);
1993 bo->bo_flag &= ~BO_ONWORKLST;
1998 bp = TAILQ_FIRST(&bv->bv_hd);
1999 KASSERT(bp == NULL || bp->b_bufobj == bo,
2000 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2001 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2002 KASSERT(bp == NULL || bp->b_bufobj == bo,
2003 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2005 bp = TAILQ_FIRST(&bv->bv_hd);
2006 KASSERT(bp == NULL || bp->b_bufobj == bo,
2007 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2008 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2009 KASSERT(bp == NULL || bp->b_bufobj == bo,
2010 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2016 * Increment the use and hold counts on the vnode, taking care to reference
2017 * the driver's usecount if this is a chardev. The vholdl() will remove
2018 * the vnode from the free list if it is presently free. Requires the
2019 * vnode interlock and returns with it held.
2022 v_incr_usecount(struct vnode *vp)
2025 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2027 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2029 vp->v_rdev->si_usecount++;
2036 * Turn a holdcnt into a use+holdcnt such that only one call to
2037 * v_decr_usecount is needed.
2040 v_upgrade_usecount(struct vnode *vp)
2043 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2045 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2047 vp->v_rdev->si_usecount++;
2053 * Decrement the vnode use and hold count along with the driver's usecount
2054 * if this is a chardev. The vdropl() below releases the vnode interlock
2055 * as it may free the vnode.
2058 v_decr_usecount(struct vnode *vp)
2061 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2062 VNASSERT(vp->v_usecount > 0, vp,
2063 ("v_decr_usecount: negative usecount"));
2064 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2066 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2068 vp->v_rdev->si_usecount--;
2075 * Decrement only the use count and driver use count. This is intended to
2076 * be paired with a follow on vdropl() to release the remaining hold count.
2077 * In this way we may vgone() a vnode with a 0 usecount without risk of
2078 * having it end up on a free list because the hold count is kept above 0.
2081 v_decr_useonly(struct vnode *vp)
2084 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2085 VNASSERT(vp->v_usecount > 0, vp,
2086 ("v_decr_useonly: negative usecount"));
2087 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2089 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2091 vp->v_rdev->si_usecount--;
2097 * Grab a particular vnode from the free list, increment its
2098 * reference count and lock it. VI_DOOMED is set if the vnode
2099 * is being destroyed. Only callers who specify LK_RETRY will
2100 * see doomed vnodes. If inactive processing was delayed in
2101 * vput try to do it here.
2104 vget(struct vnode *vp, int flags, struct thread *td)
2109 VFS_ASSERT_GIANT(vp->v_mount);
2110 VNASSERT((flags & LK_TYPE_MASK) != 0, vp,
2111 ("vget: invalid lock operation"));
2112 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
2114 if ((flags & LK_INTERLOCK) == 0)
2117 if ((error = vn_lock(vp, flags | LK_INTERLOCK)) != 0) {
2119 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__,
2123 if (vp->v_iflag & VI_DOOMED && (flags & LK_RETRY) == 0)
2124 panic("vget: vn_lock failed to return ENOENT\n");
2126 /* Upgrade our holdcnt to a usecount. */
2127 v_upgrade_usecount(vp);
2129 * We don't guarantee that any particular close will
2130 * trigger inactive processing so just make a best effort
2131 * here at preventing a reference to a removed file. If
2132 * we don't succeed no harm is done.
2134 if (vp->v_iflag & VI_OWEINACT) {
2135 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE &&
2136 (flags & LK_NOWAIT) == 0)
2138 vp->v_iflag &= ~VI_OWEINACT;
2145 * Increase the reference count of a vnode.
2148 vref(struct vnode *vp)
2151 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2153 v_incr_usecount(vp);
2158 * Return reference count of a vnode.
2160 * The results of this call are only guaranteed when some mechanism other
2161 * than the VI lock is used to stop other processes from gaining references
2162 * to the vnode. This may be the case if the caller holds the only reference.
2163 * This is also useful when stale data is acceptable as race conditions may
2164 * be accounted for by some other means.
2167 vrefcnt(struct vnode *vp)
2172 usecnt = vp->v_usecount;
2178 #define VPUTX_VRELE 1
2179 #define VPUTX_VPUT 2
2180 #define VPUTX_VUNREF 3
2183 vputx(struct vnode *vp, int func)
2187 KASSERT(vp != NULL, ("vputx: null vp"));
2188 if (func == VPUTX_VUNREF)
2189 ASSERT_VOP_LOCKED(vp, "vunref");
2190 else if (func == VPUTX_VPUT)
2191 ASSERT_VOP_LOCKED(vp, "vput");
2193 KASSERT(func == VPUTX_VRELE, ("vputx: wrong func"));
2194 VFS_ASSERT_GIANT(vp->v_mount);
2195 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2198 /* Skip this v_writecount check if we're going to panic below. */
2199 VNASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, vp,
2200 ("vputx: missed vn_close"));
2203 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) &&
2204 vp->v_usecount == 1)) {
2205 if (func == VPUTX_VPUT)
2207 v_decr_usecount(vp);
2211 if (vp->v_usecount != 1) {
2212 vprint("vputx: negative ref count", vp);
2213 panic("vputx: negative ref cnt");
2215 CTR2(KTR_VFS, "%s: return vnode %p to the freelist", __func__, vp);
2217 * We want to hold the vnode until the inactive finishes to
2218 * prevent vgone() races. We drop the use count here and the
2219 * hold count below when we're done.
2223 * We must call VOP_INACTIVE with the node locked. Mark
2224 * as VI_DOINGINACT to avoid recursion.
2226 vp->v_iflag |= VI_OWEINACT;
2229 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
2233 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2234 error = VOP_LOCK(vp, LK_UPGRADE | LK_INTERLOCK |
2240 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
2244 if (vp->v_usecount > 0)
2245 vp->v_iflag &= ~VI_OWEINACT;
2247 if (vp->v_iflag & VI_OWEINACT)
2248 vinactive(vp, curthread);
2249 if (func != VPUTX_VUNREF)
2256 * Vnode put/release.
2257 * If count drops to zero, call inactive routine and return to freelist.
2260 vrele(struct vnode *vp)
2263 vputx(vp, VPUTX_VRELE);
2267 * Release an already locked vnode. This give the same effects as
2268 * unlock+vrele(), but takes less time and avoids releasing and
2269 * re-aquiring the lock (as vrele() acquires the lock internally.)
2272 vput(struct vnode *vp)
2275 vputx(vp, VPUTX_VPUT);
2279 * Release an exclusively locked vnode. Do not unlock the vnode lock.
2282 vunref(struct vnode *vp)
2285 vputx(vp, VPUTX_VUNREF);
2289 * Somebody doesn't want the vnode recycled.
2292 vhold(struct vnode *vp)
2301 vholdl(struct vnode *vp)
2304 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2306 if (VSHOULDBUSY(vp))
2311 * Note that there is one less who cares about this vnode. vdrop() is the
2312 * opposite of vhold().
2315 vdrop(struct vnode *vp)
2323 * Drop the hold count of the vnode. If this is the last reference to
2324 * the vnode we will free it if it has been vgone'd otherwise it is
2325 * placed on the free list.
2328 vdropl(struct vnode *vp)
2331 ASSERT_VI_LOCKED(vp, "vdropl");
2332 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2333 if (vp->v_holdcnt <= 0)
2334 panic("vdrop: holdcnt %d", vp->v_holdcnt);
2336 if (vp->v_holdcnt == 0) {
2337 if (vp->v_iflag & VI_DOOMED) {
2338 CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__,
2349 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT
2350 * flags. DOINGINACT prevents us from recursing in calls to vinactive.
2351 * OWEINACT tracks whether a vnode missed a call to inactive due to a
2352 * failed lock upgrade.
2355 vinactive(struct vnode *vp, struct thread *td)
2358 ASSERT_VOP_ELOCKED(vp, "vinactive");
2359 ASSERT_VI_LOCKED(vp, "vinactive");
2360 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
2361 ("vinactive: recursed on VI_DOINGINACT"));
2362 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2363 vp->v_iflag |= VI_DOINGINACT;
2364 vp->v_iflag &= ~VI_OWEINACT;
2366 VOP_INACTIVE(vp, td);
2368 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
2369 ("vinactive: lost VI_DOINGINACT"));
2370 vp->v_iflag &= ~VI_DOINGINACT;
2374 * Remove any vnodes in the vnode table belonging to mount point mp.
2376 * If FORCECLOSE is not specified, there should not be any active ones,
2377 * return error if any are found (nb: this is a user error, not a
2378 * system error). If FORCECLOSE is specified, detach any active vnodes
2381 * If WRITECLOSE is set, only flush out regular file vnodes open for
2384 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
2386 * `rootrefs' specifies the base reference count for the root vnode
2387 * of this filesystem. The root vnode is considered busy if its
2388 * v_usecount exceeds this value. On a successful return, vflush(, td)
2389 * will call vrele() on the root vnode exactly rootrefs times.
2390 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
2394 static int busyprt = 0; /* print out busy vnodes */
2395 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "Print out busy vnodes");
2399 vflush(struct mount *mp, int rootrefs, int flags, struct thread *td)
2401 struct vnode *vp, *mvp, *rootvp = NULL;
2403 int busy = 0, error;
2405 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp,
2408 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
2409 ("vflush: bad args"));
2411 * Get the filesystem root vnode. We can vput() it
2412 * immediately, since with rootrefs > 0, it won't go away.
2414 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) {
2415 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d",
2423 MNT_VNODE_FOREACH(vp, mp, mvp) {
2427 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE);
2431 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
2435 * Skip over a vnodes marked VV_SYSTEM.
2437 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
2444 * If WRITECLOSE is set, flush out unlinked but still open
2445 * files (even if open only for reading) and regular file
2446 * vnodes open for writing.
2448 if (flags & WRITECLOSE) {
2449 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
2452 if ((vp->v_type == VNON ||
2453 (error == 0 && vattr.va_nlink > 0)) &&
2454 (vp->v_writecount == 0 || vp->v_type != VREG)) {
2463 * With v_usecount == 0, all we need to do is clear out the
2464 * vnode data structures and we are done.
2466 * If FORCECLOSE is set, forcibly close the vnode.
2468 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) {
2469 VNASSERT(vp->v_usecount == 0 ||
2470 (vp->v_type != VCHR && vp->v_type != VBLK), vp,
2471 ("device VNODE %p is FORCECLOSED", vp));
2477 vprint("vflush: busy vnode", vp);
2485 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
2487 * If just the root vnode is busy, and if its refcount
2488 * is equal to `rootrefs', then go ahead and kill it.
2491 KASSERT(busy > 0, ("vflush: not busy"));
2492 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp,
2493 ("vflush: usecount %d < rootrefs %d",
2494 rootvp->v_usecount, rootrefs));
2495 if (busy == 1 && rootvp->v_usecount == rootrefs) {
2496 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK);
2498 VOP_UNLOCK(rootvp, 0);
2504 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__,
2508 for (; rootrefs > 0; rootrefs--)
2514 * Recycle an unused vnode to the front of the free list.
2517 vrecycle(struct vnode *vp, struct thread *td)
2521 ASSERT_VOP_ELOCKED(vp, "vrecycle");
2522 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2525 if (vp->v_usecount == 0) {
2534 * Eliminate all activity associated with a vnode
2535 * in preparation for reuse.
2538 vgone(struct vnode *vp)
2546 * vgone, with the vp interlock held.
2549 vgonel(struct vnode *vp)
2556 ASSERT_VOP_ELOCKED(vp, "vgonel");
2557 ASSERT_VI_LOCKED(vp, "vgonel");
2558 VNASSERT(vp->v_holdcnt, vp,
2559 ("vgonel: vp %p has no reference.", vp));
2560 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2564 * Don't vgonel if we're already doomed.
2566 if (vp->v_iflag & VI_DOOMED)
2568 vp->v_iflag |= VI_DOOMED;
2570 * Check to see if the vnode is in use. If so, we have to call
2571 * VOP_CLOSE() and VOP_INACTIVE().
2573 active = vp->v_usecount;
2574 oweinact = (vp->v_iflag & VI_OWEINACT);
2577 * Clean out any buffers associated with the vnode.
2578 * If the flush fails, just toss the buffers.
2581 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd))
2582 (void) vn_start_secondary_write(vp, &mp, V_WAIT);
2583 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0)
2584 vinvalbuf(vp, 0, 0, 0);
2587 * If purging an active vnode, it must be closed and
2588 * deactivated before being reclaimed.
2591 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
2592 if (oweinact || active) {
2594 if ((vp->v_iflag & VI_DOINGINACT) == 0)
2599 * Reclaim the vnode.
2601 if (VOP_RECLAIM(vp, td))
2602 panic("vgone: cannot reclaim");
2604 vn_finished_secondary_write(mp);
2605 VNASSERT(vp->v_object == NULL, vp,
2606 ("vop_reclaim left v_object vp=%p, tag=%s", vp, vp->v_tag));
2608 * Clear the advisory locks and wake up waiting threads.
2610 (void)VOP_ADVLOCKPURGE(vp);
2612 * Delete from old mount point vnode list.
2617 * Done with purge, reset to the standard lock and invalidate
2621 vp->v_vnlock = &vp->v_lock;
2622 vp->v_op = &dead_vnodeops;
2628 * Calculate the total number of references to a special device.
2631 vcount(struct vnode *vp)
2636 count = vp->v_rdev->si_usecount;
2642 * Same as above, but using the struct cdev *as argument
2645 count_dev(struct cdev *dev)
2650 count = dev->si_usecount;
2656 * Print out a description of a vnode.
2658 static char *typename[] =
2659 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD",
2663 vn_printf(struct vnode *vp, const char *fmt, ...)
2666 char buf[256], buf2[16];
2672 printf("%p: ", (void *)vp);
2673 printf("tag %s, type %s\n", vp->v_tag, typename[vp->v_type]);
2674 printf(" usecount %d, writecount %d, refcount %d mountedhere %p\n",
2675 vp->v_usecount, vp->v_writecount, vp->v_holdcnt, vp->v_mountedhere);
2678 if (vp->v_vflag & VV_ROOT)
2679 strlcat(buf, "|VV_ROOT", sizeof(buf));
2680 if (vp->v_vflag & VV_ISTTY)
2681 strlcat(buf, "|VV_ISTTY", sizeof(buf));
2682 if (vp->v_vflag & VV_NOSYNC)
2683 strlcat(buf, "|VV_NOSYNC", sizeof(buf));
2684 if (vp->v_vflag & VV_CACHEDLABEL)
2685 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf));
2686 if (vp->v_vflag & VV_TEXT)
2687 strlcat(buf, "|VV_TEXT", sizeof(buf));
2688 if (vp->v_vflag & VV_COPYONWRITE)
2689 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf));
2690 if (vp->v_vflag & VV_SYSTEM)
2691 strlcat(buf, "|VV_SYSTEM", sizeof(buf));
2692 if (vp->v_vflag & VV_PROCDEP)
2693 strlcat(buf, "|VV_PROCDEP", sizeof(buf));
2694 if (vp->v_vflag & VV_NOKNOTE)
2695 strlcat(buf, "|VV_NOKNOTE", sizeof(buf));
2696 if (vp->v_vflag & VV_DELETED)
2697 strlcat(buf, "|VV_DELETED", sizeof(buf));
2698 if (vp->v_vflag & VV_MD)
2699 strlcat(buf, "|VV_MD", sizeof(buf));
2700 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC |
2701 VV_CACHEDLABEL | VV_TEXT | VV_COPYONWRITE | VV_SYSTEM | VV_PROCDEP |
2702 VV_NOKNOTE | VV_DELETED | VV_MD);
2704 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags);
2705 strlcat(buf, buf2, sizeof(buf));
2707 if (vp->v_iflag & VI_MOUNT)
2708 strlcat(buf, "|VI_MOUNT", sizeof(buf));
2709 if (vp->v_iflag & VI_AGE)
2710 strlcat(buf, "|VI_AGE", sizeof(buf));
2711 if (vp->v_iflag & VI_DOOMED)
2712 strlcat(buf, "|VI_DOOMED", sizeof(buf));
2713 if (vp->v_iflag & VI_FREE)
2714 strlcat(buf, "|VI_FREE", sizeof(buf));
2715 if (vp->v_iflag & VI_DOINGINACT)
2716 strlcat(buf, "|VI_DOINGINACT", sizeof(buf));
2717 if (vp->v_iflag & VI_OWEINACT)
2718 strlcat(buf, "|VI_OWEINACT", sizeof(buf));
2719 flags = vp->v_iflag & ~(VI_MOUNT | VI_AGE | VI_DOOMED | VI_FREE |
2720 VI_DOINGINACT | VI_OWEINACT);
2722 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags);
2723 strlcat(buf, buf2, sizeof(buf));
2725 printf(" flags (%s)\n", buf + 1);
2726 if (mtx_owned(VI_MTX(vp)))
2727 printf(" VI_LOCKed");
2728 if (vp->v_object != NULL)
2729 printf(" v_object %p ref %d pages %d\n",
2730 vp->v_object, vp->v_object->ref_count,
2731 vp->v_object->resident_page_count);
2733 lockmgr_printinfo(vp->v_vnlock);
2734 if (vp->v_data != NULL)
2740 * List all of the locked vnodes in the system.
2741 * Called when debugging the kernel.
2743 DB_SHOW_COMMAND(lockedvnods, lockedvnodes)
2745 struct mount *mp, *nmp;
2749 * Note: because this is DDB, we can't obey the locking semantics
2750 * for these structures, which means we could catch an inconsistent
2751 * state and dereference a nasty pointer. Not much to be done
2754 db_printf("Locked vnodes\n");
2755 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2756 nmp = TAILQ_NEXT(mp, mnt_list);
2757 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
2758 if (vp->v_type != VMARKER &&
2762 nmp = TAILQ_NEXT(mp, mnt_list);
2767 * Show details about the given vnode.
2769 DB_SHOW_COMMAND(vnode, db_show_vnode)
2775 vp = (struct vnode *)addr;
2776 vn_printf(vp, "vnode ");
2780 * Show details about the given mount point.
2782 DB_SHOW_COMMAND(mount, db_show_mount)
2792 /* No address given, print short info about all mount points. */
2793 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2794 db_printf("%p %s on %s (%s)\n", mp,
2795 mp->mnt_stat.f_mntfromname,
2796 mp->mnt_stat.f_mntonname,
2797 mp->mnt_stat.f_fstypename);
2801 db_printf("\nMore info: show mount <addr>\n");
2805 mp = (struct mount *)addr;
2806 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname,
2807 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename);
2810 flags = mp->mnt_flag;
2811 #define MNT_FLAG(flag) do { \
2812 if (flags & (flag)) { \
2813 if (buf[0] != '\0') \
2814 strlcat(buf, ", ", sizeof(buf)); \
2815 strlcat(buf, (#flag) + 4, sizeof(buf)); \
2819 MNT_FLAG(MNT_RDONLY);
2820 MNT_FLAG(MNT_SYNCHRONOUS);
2821 MNT_FLAG(MNT_NOEXEC);
2822 MNT_FLAG(MNT_NOSUID);
2823 MNT_FLAG(MNT_UNION);
2824 MNT_FLAG(MNT_ASYNC);
2825 MNT_FLAG(MNT_SUIDDIR);
2826 MNT_FLAG(MNT_SOFTDEP);
2827 MNT_FLAG(MNT_NOSYMFOLLOW);
2828 MNT_FLAG(MNT_GJOURNAL);
2829 MNT_FLAG(MNT_MULTILABEL);
2831 MNT_FLAG(MNT_NOATIME);
2832 MNT_FLAG(MNT_NOCLUSTERR);
2833 MNT_FLAG(MNT_NOCLUSTERW);
2834 MNT_FLAG(MNT_NFS4ACLS);
2835 MNT_FLAG(MNT_EXRDONLY);
2836 MNT_FLAG(MNT_EXPORTED);
2837 MNT_FLAG(MNT_DEFEXPORTED);
2838 MNT_FLAG(MNT_EXPORTANON);
2839 MNT_FLAG(MNT_EXKERB);
2840 MNT_FLAG(MNT_EXPUBLIC);
2841 MNT_FLAG(MNT_LOCAL);
2842 MNT_FLAG(MNT_QUOTA);
2843 MNT_FLAG(MNT_ROOTFS);
2845 MNT_FLAG(MNT_IGNORE);
2846 MNT_FLAG(MNT_UPDATE);
2847 MNT_FLAG(MNT_DELEXPORT);
2848 MNT_FLAG(MNT_RELOAD);
2849 MNT_FLAG(MNT_FORCE);
2850 MNT_FLAG(MNT_SNAPSHOT);
2851 MNT_FLAG(MNT_BYFSID);
2852 MNT_FLAG(MNT_SOFTDEP);
2856 strlcat(buf, ", ", sizeof(buf));
2857 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
2860 db_printf(" mnt_flag = %s\n", buf);
2863 flags = mp->mnt_kern_flag;
2864 #define MNT_KERN_FLAG(flag) do { \
2865 if (flags & (flag)) { \
2866 if (buf[0] != '\0') \
2867 strlcat(buf, ", ", sizeof(buf)); \
2868 strlcat(buf, (#flag) + 5, sizeof(buf)); \
2872 MNT_KERN_FLAG(MNTK_UNMOUNTF);
2873 MNT_KERN_FLAG(MNTK_ASYNC);
2874 MNT_KERN_FLAG(MNTK_SOFTDEP);
2875 MNT_KERN_FLAG(MNTK_NOINSMNTQ);
2876 MNT_KERN_FLAG(MNTK_DRAINING);
2877 MNT_KERN_FLAG(MNTK_REFEXPIRE);
2878 MNT_KERN_FLAG(MNTK_EXTENDED_SHARED);
2879 MNT_KERN_FLAG(MNTK_SHARED_WRITES);
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_LOOKUP_SHARED);
2887 MNT_KERN_FLAG(MNTK_NOKNOTE);
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 error = getnewvnode("syncer", mp, &sync_vnodeops, &vp);
3393 panic("vfs_allocate_syncvnode: getnewvnode() failed");
3395 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3396 vp->v_vflag |= VV_FORCEINSMQ;
3397 error = insmntque(vp, mp);
3399 panic("vfs_allocate_syncvnode: insmntque() failed");
3400 vp->v_vflag &= ~VV_FORCEINSMQ;
3403 * Place the vnode onto the syncer worklist. We attempt to
3404 * scatter them about on the list so that they will go off
3405 * at evenly distributed times even if all the filesystems
3406 * are mounted at once.
3409 if (next == 0 || next > syncer_maxdelay) {
3413 start = syncer_maxdelay / 2;
3414 incr = syncer_maxdelay;
3420 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0);
3421 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */
3422 mtx_lock(&sync_mtx);
3424 if (mp->mnt_syncer == NULL) {
3425 mp->mnt_syncer = vp;
3428 mtx_unlock(&sync_mtx);
3431 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3438 vfs_deallocate_syncvnode(struct mount *mp)
3442 mtx_lock(&sync_mtx);
3443 vp = mp->mnt_syncer;
3445 mp->mnt_syncer = NULL;
3446 mtx_unlock(&sync_mtx);
3452 * Do a lazy sync of the filesystem.
3455 sync_fsync(struct vop_fsync_args *ap)
3457 struct vnode *syncvp = ap->a_vp;
3458 struct mount *mp = syncvp->v_mount;
3463 * We only need to do something if this is a lazy evaluation.
3465 if (ap->a_waitfor != MNT_LAZY)
3469 * Move ourselves to the back of the sync list.
3471 bo = &syncvp->v_bufobj;
3473 vn_syncer_add_to_worklist(bo, syncdelay);
3477 * Walk the list of vnodes pushing all that are dirty and
3478 * not already on the sync list.
3480 mtx_lock(&mountlist_mtx);
3481 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
3482 mtx_unlock(&mountlist_mtx);
3485 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) {
3491 mp->mnt_kern_flag &= ~MNTK_ASYNC;
3493 vfs_msync(mp, MNT_NOWAIT);
3494 error = VFS_SYNC(mp, MNT_LAZY);
3497 if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0)
3498 mp->mnt_kern_flag |= MNTK_ASYNC;
3500 vn_finished_write(mp);
3506 * The syncer vnode is no referenced.
3509 sync_inactive(struct vop_inactive_args *ap)
3517 * The syncer vnode is no longer needed and is being decommissioned.
3519 * Modifications to the worklist must be protected by sync_mtx.
3522 sync_reclaim(struct vop_reclaim_args *ap)
3524 struct vnode *vp = ap->a_vp;
3529 mtx_lock(&sync_mtx);
3530 if (vp->v_mount->mnt_syncer == vp)
3531 vp->v_mount->mnt_syncer = NULL;
3532 if (bo->bo_flag & BO_ONWORKLST) {
3533 LIST_REMOVE(bo, bo_synclist);
3534 syncer_worklist_len--;
3536 bo->bo_flag &= ~BO_ONWORKLST;
3538 mtx_unlock(&sync_mtx);
3545 * Check if vnode represents a disk device
3548 vn_isdisk(struct vnode *vp, int *errp)
3554 if (vp->v_type != VCHR)
3556 else if (vp->v_rdev == NULL)
3558 else if (vp->v_rdev->si_devsw == NULL)
3560 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK))
3565 return (error == 0);
3569 * Common filesystem object access control check routine. Accepts a
3570 * vnode's type, "mode", uid and gid, requested access mode, credentials,
3571 * and optional call-by-reference privused argument allowing vaccess()
3572 * to indicate to the caller whether privilege was used to satisfy the
3573 * request (obsoleted). Returns 0 on success, or an errno on failure.
3575 * The ifdef'd CAPABILITIES version is here for reference, but is not
3579 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid,
3580 accmode_t accmode, struct ucred *cred, int *privused)
3582 accmode_t dac_granted;
3583 accmode_t priv_granted;
3585 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0,
3586 ("invalid bit in accmode"));
3587 KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE),
3588 ("VAPPEND without VWRITE"));
3591 * Look for a normal, non-privileged way to access the file/directory
3592 * as requested. If it exists, go with that.
3595 if (privused != NULL)
3600 /* Check the owner. */
3601 if (cred->cr_uid == file_uid) {
3602 dac_granted |= VADMIN;
3603 if (file_mode & S_IXUSR)
3604 dac_granted |= VEXEC;
3605 if (file_mode & S_IRUSR)
3606 dac_granted |= VREAD;
3607 if (file_mode & S_IWUSR)
3608 dac_granted |= (VWRITE | VAPPEND);
3610 if ((accmode & dac_granted) == accmode)
3616 /* Otherwise, check the groups (first match) */
3617 if (groupmember(file_gid, cred)) {
3618 if (file_mode & S_IXGRP)
3619 dac_granted |= VEXEC;
3620 if (file_mode & S_IRGRP)
3621 dac_granted |= VREAD;
3622 if (file_mode & S_IWGRP)
3623 dac_granted |= (VWRITE | VAPPEND);
3625 if ((accmode & dac_granted) == accmode)
3631 /* Otherwise, check everyone else. */
3632 if (file_mode & S_IXOTH)
3633 dac_granted |= VEXEC;
3634 if (file_mode & S_IROTH)
3635 dac_granted |= VREAD;
3636 if (file_mode & S_IWOTH)
3637 dac_granted |= (VWRITE | VAPPEND);
3638 if ((accmode & dac_granted) == accmode)
3643 * Build a privilege mask to determine if the set of privileges
3644 * satisfies the requirements when combined with the granted mask
3645 * from above. For each privilege, if the privilege is required,
3646 * bitwise or the request type onto the priv_granted mask.
3652 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC
3653 * requests, instead of PRIV_VFS_EXEC.
3655 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3656 !priv_check_cred(cred, PRIV_VFS_LOOKUP, 0))
3657 priv_granted |= VEXEC;
3660 * Ensure that at least one execute bit is on. Otherwise,
3661 * a privileged user will always succeed, and we don't want
3662 * this to happen unless the file really is executable.
3664 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3665 (file_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 &&
3666 !priv_check_cred(cred, PRIV_VFS_EXEC, 0))
3667 priv_granted |= VEXEC;
3670 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) &&
3671 !priv_check_cred(cred, PRIV_VFS_READ, 0))
3672 priv_granted |= VREAD;
3674 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) &&
3675 !priv_check_cred(cred, PRIV_VFS_WRITE, 0))
3676 priv_granted |= (VWRITE | VAPPEND);
3678 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) &&
3679 !priv_check_cred(cred, PRIV_VFS_ADMIN, 0))
3680 priv_granted |= VADMIN;
3682 if ((accmode & (priv_granted | dac_granted)) == accmode) {
3683 /* XXX audit: privilege used */
3684 if (privused != NULL)
3689 return ((accmode & VADMIN) ? EPERM : EACCES);
3693 * Credential check based on process requesting service, and per-attribute
3697 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred,
3698 struct thread *td, accmode_t accmode)
3702 * Kernel-invoked always succeeds.
3708 * Do not allow privileged processes in jail to directly manipulate
3709 * system attributes.
3711 switch (attrnamespace) {
3712 case EXTATTR_NAMESPACE_SYSTEM:
3713 /* Potentially should be: return (EPERM); */
3714 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM, 0));
3715 case EXTATTR_NAMESPACE_USER:
3716 return (VOP_ACCESS(vp, accmode, cred, td));
3722 #ifdef DEBUG_VFS_LOCKS
3724 * This only exists to supress warnings from unlocked specfs accesses. It is
3725 * no longer ok to have an unlocked VFS.
3727 #define IGNORE_LOCK(vp) (panicstr != NULL || (vp) == NULL || \
3728 (vp)->v_type == VCHR || (vp)->v_type == VBAD)
3730 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */
3731 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0,
3732 "Drop into debugger on lock violation");
3734 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */
3735 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex,
3736 0, "Check for interlock across VOPs");
3738 int vfs_badlock_print = 1; /* Print lock violations. */
3739 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print,
3740 0, "Print lock violations");
3743 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */
3744 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW,
3745 &vfs_badlock_backtrace, 0, "Print backtrace at lock violations");
3749 vfs_badlock(const char *msg, const char *str, struct vnode *vp)
3753 if (vfs_badlock_backtrace)
3756 if (vfs_badlock_print)
3757 printf("%s: %p %s\n", str, (void *)vp, msg);
3758 if (vfs_badlock_ddb)
3759 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
3763 assert_vi_locked(struct vnode *vp, const char *str)
3766 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp)))
3767 vfs_badlock("interlock is not locked but should be", str, vp);
3771 assert_vi_unlocked(struct vnode *vp, const char *str)
3774 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp)))
3775 vfs_badlock("interlock is locked but should not be", str, vp);
3779 assert_vop_locked(struct vnode *vp, const char *str)
3782 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == 0)
3783 vfs_badlock("is not locked but should be", str, vp);
3787 assert_vop_unlocked(struct vnode *vp, const char *str)
3790 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
3791 vfs_badlock("is locked but should not be", str, vp);
3795 assert_vop_elocked(struct vnode *vp, const char *str)
3798 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
3799 vfs_badlock("is not exclusive locked but should be", str, vp);
3804 assert_vop_elocked_other(struct vnode *vp, const char *str)
3807 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLOTHER)
3808 vfs_badlock("is not exclusive locked by another thread",
3813 assert_vop_slocked(struct vnode *vp, const char *str)
3816 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_SHARED)
3817 vfs_badlock("is not locked shared but should be", str, vp);
3820 #endif /* DEBUG_VFS_LOCKS */
3823 vop_rename_fail(struct vop_rename_args *ap)
3826 if (ap->a_tvp != NULL)
3828 if (ap->a_tdvp == ap->a_tvp)
3837 vop_rename_pre(void *ap)
3839 struct vop_rename_args *a = ap;
3841 #ifdef DEBUG_VFS_LOCKS
3843 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME");
3844 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME");
3845 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME");
3846 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME");
3848 /* Check the source (from). */
3849 if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock &&
3850 (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock))
3851 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked");
3852 if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock)
3853 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked");
3855 /* Check the target. */
3857 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked");
3858 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked");
3860 if (a->a_tdvp != a->a_fdvp)
3862 if (a->a_tvp != a->a_fvp)
3870 vop_strategy_pre(void *ap)
3872 #ifdef DEBUG_VFS_LOCKS
3873 struct vop_strategy_args *a;
3880 * Cluster ops lock their component buffers but not the IO container.
3882 if ((bp->b_flags & B_CLUSTER) != 0)
3885 if (!BUF_ISLOCKED(bp)) {
3886 if (vfs_badlock_print)
3888 "VOP_STRATEGY: bp is not locked but should be\n");
3889 if (vfs_badlock_ddb)
3890 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
3896 vop_lookup_pre(void *ap)
3898 #ifdef DEBUG_VFS_LOCKS
3899 struct vop_lookup_args *a;
3904 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP");
3905 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP");
3910 vop_lookup_post(void *ap, int rc)
3912 #ifdef DEBUG_VFS_LOCKS
3913 struct vop_lookup_args *a;
3921 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP");
3922 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP");
3925 ASSERT_VOP_LOCKED(vp, "VOP_LOOKUP (child)");
3930 vop_lock_pre(void *ap)
3932 #ifdef DEBUG_VFS_LOCKS
3933 struct vop_lock1_args *a = ap;
3935 if ((a->a_flags & LK_INTERLOCK) == 0)
3936 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
3938 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK");
3943 vop_lock_post(void *ap, int rc)
3945 #ifdef DEBUG_VFS_LOCKS
3946 struct vop_lock1_args *a = ap;
3948 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
3950 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK");
3955 vop_unlock_pre(void *ap)
3957 #ifdef DEBUG_VFS_LOCKS
3958 struct vop_unlock_args *a = ap;
3960 if (a->a_flags & LK_INTERLOCK)
3961 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK");
3962 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK");
3967 vop_unlock_post(void *ap, int rc)
3969 #ifdef DEBUG_VFS_LOCKS
3970 struct vop_unlock_args *a = ap;
3972 if (a->a_flags & LK_INTERLOCK)
3973 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK");
3978 vop_create_post(void *ap, int rc)
3980 struct vop_create_args *a = ap;
3983 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
3987 vop_link_post(void *ap, int rc)
3989 struct vop_link_args *a = ap;
3992 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK);
3993 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE);
3998 vop_mkdir_post(void *ap, int rc)
4000 struct vop_mkdir_args *a = ap;
4003 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4007 vop_mknod_post(void *ap, int rc)
4009 struct vop_mknod_args *a = ap;
4012 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4016 vop_remove_post(void *ap, int rc)
4018 struct vop_remove_args *a = ap;
4021 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4022 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4027 vop_rename_post(void *ap, int rc)
4029 struct vop_rename_args *a = ap;
4032 VFS_KNOTE_UNLOCKED(a->a_fdvp, NOTE_WRITE);
4033 VFS_KNOTE_UNLOCKED(a->a_tdvp, NOTE_WRITE);
4034 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME);
4036 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE);
4038 if (a->a_tdvp != a->a_fdvp)
4040 if (a->a_tvp != a->a_fvp)
4048 vop_rmdir_post(void *ap, int rc)
4050 struct vop_rmdir_args *a = ap;
4053 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4054 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4059 vop_setattr_post(void *ap, int rc)
4061 struct vop_setattr_args *a = ap;
4064 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4068 vop_symlink_post(void *ap, int rc)
4070 struct vop_symlink_args *a = ap;
4073 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4076 static struct knlist fs_knlist;
4079 vfs_event_init(void *arg)
4081 knlist_init_mtx(&fs_knlist, NULL);
4083 /* XXX - correct order? */
4084 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL);
4087 vfs_event_signal(fsid_t *fsid, uint32_t event, intptr_t data __unused)
4090 KNOTE_UNLOCKED(&fs_knlist, event);
4093 static int filt_fsattach(struct knote *kn);
4094 static void filt_fsdetach(struct knote *kn);
4095 static int filt_fsevent(struct knote *kn, long hint);
4097 struct filterops fs_filtops = {
4099 .f_attach = filt_fsattach,
4100 .f_detach = filt_fsdetach,
4101 .f_event = filt_fsevent
4105 filt_fsattach(struct knote *kn)
4108 kn->kn_flags |= EV_CLEAR;
4109 knlist_add(&fs_knlist, kn, 0);
4114 filt_fsdetach(struct knote *kn)
4117 knlist_remove(&fs_knlist, kn, 0);
4121 filt_fsevent(struct knote *kn, long hint)
4124 kn->kn_fflags |= hint;
4125 return (kn->kn_fflags != 0);
4129 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS)
4135 error = SYSCTL_IN(req, &vc, sizeof(vc));
4138 if (vc.vc_vers != VFS_CTL_VERS1)
4140 mp = vfs_getvfs(&vc.vc_fsid);
4143 /* ensure that a specific sysctl goes to the right filesystem. */
4144 if (strcmp(vc.vc_fstypename, "*") != 0 &&
4145 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) {
4149 VCTLTOREQ(&vc, req);
4150 error = VFS_SYSCTL(mp, vc.vc_op, req);
4155 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLFLAG_WR, NULL, 0, sysctl_vfs_ctl, "",
4159 * Function to initialize a va_filerev field sensibly.
4160 * XXX: Wouldn't a random number make a lot more sense ??
4163 init_va_filerev(void)
4168 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL));
4171 static int filt_vfsread(struct knote *kn, long hint);
4172 static int filt_vfswrite(struct knote *kn, long hint);
4173 static int filt_vfsvnode(struct knote *kn, long hint);
4174 static void filt_vfsdetach(struct knote *kn);
4175 static struct filterops vfsread_filtops = {
4177 .f_detach = filt_vfsdetach,
4178 .f_event = filt_vfsread
4180 static struct filterops vfswrite_filtops = {
4182 .f_detach = filt_vfsdetach,
4183 .f_event = filt_vfswrite
4185 static struct filterops vfsvnode_filtops = {
4187 .f_detach = filt_vfsdetach,
4188 .f_event = filt_vfsvnode
4192 vfs_knllock(void *arg)
4194 struct vnode *vp = arg;
4196 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4200 vfs_knlunlock(void *arg)
4202 struct vnode *vp = arg;
4208 vfs_knl_assert_locked(void *arg)
4210 #ifdef DEBUG_VFS_LOCKS
4211 struct vnode *vp = arg;
4213 ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked");
4218 vfs_knl_assert_unlocked(void *arg)
4220 #ifdef DEBUG_VFS_LOCKS
4221 struct vnode *vp = arg;
4223 ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked");
4228 vfs_kqfilter(struct vop_kqfilter_args *ap)
4230 struct vnode *vp = ap->a_vp;
4231 struct knote *kn = ap->a_kn;
4234 switch (kn->kn_filter) {
4236 kn->kn_fop = &vfsread_filtops;
4239 kn->kn_fop = &vfswrite_filtops;
4242 kn->kn_fop = &vfsvnode_filtops;
4248 kn->kn_hook = (caddr_t)vp;
4251 if (vp->v_pollinfo == NULL)
4253 knl = &vp->v_pollinfo->vpi_selinfo.si_note;
4254 knlist_add(knl, kn, 0);
4260 * Detach knote from vnode
4263 filt_vfsdetach(struct knote *kn)
4265 struct vnode *vp = (struct vnode *)kn->kn_hook;
4267 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo"));
4268 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
4273 filt_vfsread(struct knote *kn, long hint)
4275 struct vnode *vp = (struct vnode *)kn->kn_hook;
4280 * filesystem is gone, so set the EOF flag and schedule
4281 * the knote for deletion.
4283 if (hint == NOTE_REVOKE) {
4285 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4290 if (VOP_GETATTR(vp, &va, curthread->td_ucred))
4294 kn->kn_data = va.va_size - kn->kn_fp->f_offset;
4295 res = (kn->kn_data != 0);
4302 filt_vfswrite(struct knote *kn, long hint)
4304 struct vnode *vp = (struct vnode *)kn->kn_hook;
4309 * filesystem is gone, so set the EOF flag and schedule
4310 * the knote for deletion.
4312 if (hint == NOTE_REVOKE)
4313 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4321 filt_vfsvnode(struct knote *kn, long hint)
4323 struct vnode *vp = (struct vnode *)kn->kn_hook;
4327 if (kn->kn_sfflags & hint)
4328 kn->kn_fflags |= hint;
4329 if (hint == NOTE_REVOKE) {
4330 kn->kn_flags |= EV_EOF;
4334 res = (kn->kn_fflags != 0);
4340 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off)
4344 if (dp->d_reclen > ap->a_uio->uio_resid)
4345 return (ENAMETOOLONG);
4346 error = uiomove(dp, dp->d_reclen, ap->a_uio);
4348 if (ap->a_ncookies != NULL) {
4349 if (ap->a_cookies != NULL)
4350 free(ap->a_cookies, M_TEMP);
4351 ap->a_cookies = NULL;
4352 *ap->a_ncookies = 0;
4356 if (ap->a_ncookies == NULL)
4359 KASSERT(ap->a_cookies,
4360 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!"));
4362 *ap->a_cookies = realloc(*ap->a_cookies,
4363 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO);
4364 (*ap->a_cookies)[*ap->a_ncookies] = off;
4369 * Mark for update the access time of the file if the filesystem
4370 * supports VOP_MARKATIME. This functionality is used by execve and
4371 * mmap, so we want to avoid the I/O implied by directly setting
4372 * va_atime for the sake of efficiency.
4375 vfs_mark_atime(struct vnode *vp, struct ucred *cred)
4380 VFS_ASSERT_GIANT(mp);
4381 ASSERT_VOP_LOCKED(vp, "vfs_mark_atime");
4382 if (mp != NULL && (mp->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
4383 (void)VOP_MARKATIME(vp);
4387 * The purpose of this routine is to remove granularity from accmode_t,
4388 * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE,
4389 * VADMIN and VAPPEND.
4391 * If it returns 0, the caller is supposed to continue with the usual
4392 * access checks using 'accmode' as modified by this routine. If it
4393 * returns nonzero value, the caller is supposed to return that value
4396 * Note that after this routine runs, accmode may be zero.
4399 vfs_unixify_accmode(accmode_t *accmode)
4402 * There is no way to specify explicit "deny" rule using
4403 * file mode or POSIX.1e ACLs.
4405 if (*accmode & VEXPLICIT_DENY) {
4411 * None of these can be translated into usual access bits.
4412 * Also, the common case for NFSv4 ACLs is to not contain
4413 * either of these bits. Caller should check for VWRITE
4414 * on the containing directory instead.
4416 if (*accmode & (VDELETE_CHILD | VDELETE))
4419 if (*accmode & VADMIN_PERMS) {
4420 *accmode &= ~VADMIN_PERMS;
4425 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL
4426 * or VSYNCHRONIZE using file mode or POSIX.1e ACL.
4428 *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE);