2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95
38 * External virtual filesystem routines
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
45 #include "opt_watchdog.h"
47 #include <sys/param.h>
48 #include <sys/systm.h>
51 #include <sys/condvar.h>
53 #include <sys/dirent.h>
54 #include <sys/event.h>
55 #include <sys/eventhandler.h>
56 #include <sys/extattr.h>
58 #include <sys/fcntl.h>
61 #include <sys/kernel.h>
62 #include <sys/kthread.h>
63 #include <sys/lockf.h>
64 #include <sys/malloc.h>
65 #include <sys/mount.h>
66 #include <sys/namei.h>
68 #include <sys/reboot.h>
69 #include <sys/sched.h>
70 #include <sys/sleepqueue.h>
72 #include <sys/sysctl.h>
73 #include <sys/syslog.h>
74 #include <sys/vmmeter.h>
75 #include <sys/vnode.h>
77 #include <sys/watchdog.h>
80 #include <machine/stdarg.h>
82 #include <security/mac/mac_framework.h>
85 #include <vm/vm_object.h>
86 #include <vm/vm_extern.h>
88 #include <vm/vm_map.h>
89 #include <vm/vm_page.h>
90 #include <vm/vm_kern.h>
100 static void delmntque(struct vnode *vp);
101 static int flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo,
102 int slpflag, int slptimeo);
103 static void syncer_shutdown(void *arg, int howto);
104 static int vtryrecycle(struct vnode *vp);
105 static void vbusy(struct vnode *vp);
106 static void v_incr_usecount(struct vnode *);
107 static void v_decr_usecount(struct vnode *);
108 static void v_decr_useonly(struct vnode *);
109 static void v_upgrade_usecount(struct vnode *);
110 static void vfree(struct vnode *);
111 static void vnlru_free(int);
112 static void vgonel(struct vnode *);
113 static void vfs_knllock(void *arg);
114 static void vfs_knlunlock(void *arg);
115 static void vfs_knl_assert_locked(void *arg);
116 static void vfs_knl_assert_unlocked(void *arg);
117 static void destroy_vpollinfo(struct vpollinfo *vi);
120 * Number of vnodes in existence. Increased whenever getnewvnode()
121 * allocates a new vnode, decreased on vdestroy() called on VI_DOOMed
124 static unsigned long numvnodes;
126 SYSCTL_LONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0,
127 "Number of vnodes in existence");
130 * Conversion tables for conversion from vnode types to inode formats
133 enum vtype iftovt_tab[16] = {
134 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
135 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
137 int vttoif_tab[10] = {
138 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
139 S_IFSOCK, S_IFIFO, S_IFMT, S_IFMT
143 * List of vnodes that are ready for recycling.
145 static TAILQ_HEAD(freelst, vnode) vnode_free_list;
148 * Free vnode target. Free vnodes may simply be files which have been stat'd
149 * but not read. This is somewhat common, and a small cache of such files
150 * should be kept to avoid recreation costs.
152 static u_long wantfreevnodes;
153 SYSCTL_LONG(_vfs, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, "");
154 /* Number of vnodes in the free list. */
155 static u_long freevnodes;
156 SYSCTL_LONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0,
157 "Number of vnodes in the free list");
159 static int vlru_allow_cache_src;
160 SYSCTL_INT(_vfs, OID_AUTO, vlru_allow_cache_src, CTLFLAG_RW,
161 &vlru_allow_cache_src, 0, "Allow vlru to reclaim source vnode");
164 * Various variables used for debugging the new implementation of
166 * XXX these are probably of (very) limited utility now.
168 static int reassignbufcalls;
169 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0,
170 "Number of calls to reassignbuf");
173 * Cache for the mount type id assigned to NFS. This is used for
174 * special checks in nfs/nfs_nqlease.c and vm/vnode_pager.c.
176 int nfs_mount_type = -1;
178 /* To keep more than one thread at a time from running vfs_getnewfsid */
179 static struct mtx mntid_mtx;
182 * Lock for any access to the following:
187 static struct mtx vnode_free_list_mtx;
189 /* Publicly exported FS */
190 struct nfs_public nfs_pub;
192 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */
193 static uma_zone_t vnode_zone;
194 static uma_zone_t vnodepoll_zone;
196 /* Set to 1 to print out reclaim of active vnodes */
200 * The workitem queue.
202 * It is useful to delay writes of file data and filesystem metadata
203 * for tens of seconds so that quickly created and deleted files need
204 * not waste disk bandwidth being created and removed. To realize this,
205 * we append vnodes to a "workitem" queue. When running with a soft
206 * updates implementation, most pending metadata dependencies should
207 * not wait for more than a few seconds. Thus, mounted on block devices
208 * are delayed only about a half the time that file data is delayed.
209 * Similarly, directory updates are more critical, so are only delayed
210 * about a third the time that file data is delayed. Thus, there are
211 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
212 * one each second (driven off the filesystem syncer process). The
213 * syncer_delayno variable indicates the next queue that is to be processed.
214 * Items that need to be processed soon are placed in this queue:
216 * syncer_workitem_pending[syncer_delayno]
218 * A delay of fifteen seconds is done by placing the request fifteen
219 * entries later in the queue:
221 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
224 static int syncer_delayno;
225 static long syncer_mask;
226 LIST_HEAD(synclist, bufobj);
227 static struct synclist *syncer_workitem_pending[2];
229 * The sync_mtx protects:
234 * syncer_workitem_pending
235 * syncer_worklist_len
238 static struct mtx sync_mtx;
239 static struct cv sync_wakeup;
241 #define SYNCER_MAXDELAY 32
242 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
243 static int syncdelay = 30; /* max time to delay syncing data */
244 static int filedelay = 30; /* time to delay syncing files */
245 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0,
246 "Time to delay syncing files (in seconds)");
247 static int dirdelay = 29; /* time to delay syncing directories */
248 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0,
249 "Time to delay syncing directories (in seconds)");
250 static int metadelay = 28; /* time to delay syncing metadata */
251 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0,
252 "Time to delay syncing metadata (in seconds)");
253 static int rushjob; /* number of slots to run ASAP */
254 static int stat_rush_requests; /* number of times I/O speeded up */
255 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0,
256 "Number of times I/O speeded up (rush requests)");
259 * When shutting down the syncer, run it at four times normal speed.
261 #define SYNCER_SHUTDOWN_SPEEDUP 4
262 static int sync_vnode_count;
263 static int syncer_worklist_len;
264 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY }
268 * Number of vnodes we want to exist at any one time. This is mostly used
269 * to size hash tables in vnode-related code. It is normally not used in
270 * getnewvnode(), as wantfreevnodes is normally nonzero.)
272 * XXX desiredvnodes is historical cruft and should not exist.
275 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
276 &desiredvnodes, 0, "Maximum number of vnodes");
277 SYSCTL_INT(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
278 &wantfreevnodes, 0, "Minimum number of vnodes (legacy)");
279 static int vnlru_nowhere;
280 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW,
281 &vnlru_nowhere, 0, "Number of times the vnlru process ran without success");
284 * Macros to control when a vnode is freed and recycled. All require
285 * the vnode interlock.
287 #define VCANRECYCLE(vp) (((vp)->v_iflag & VI_FREE) && !(vp)->v_holdcnt)
288 #define VSHOULDFREE(vp) (!((vp)->v_iflag & VI_FREE) && !(vp)->v_holdcnt)
289 #define VSHOULDBUSY(vp) (((vp)->v_iflag & VI_FREE) && (vp)->v_holdcnt)
293 * Initialize the vnode management data structures.
295 * Reevaluate the following cap on the number of vnodes after the physical
296 * memory size exceeds 512GB. In the limit, as the physical memory size
297 * grows, the ratio of physical pages to vnodes approaches sixteen to one.
299 #ifndef MAXVNODES_MAX
300 #define MAXVNODES_MAX (512 * (1024 * 1024 * 1024 / (int)PAGE_SIZE / 16))
303 vntblinit(void *dummy __unused)
305 int physvnodes, virtvnodes;
308 * Desiredvnodes is a function of the physical memory size and the
309 * kernel's heap size. Generally speaking, it scales with the
310 * physical memory size. The ratio of desiredvnodes to physical pages
311 * is one to four until desiredvnodes exceeds 98,304. Thereafter, the
312 * marginal ratio of desiredvnodes to physical pages is one to
313 * sixteen. However, desiredvnodes is limited by the kernel's heap
314 * size. The memory required by desiredvnodes vnodes and vm objects
315 * may not exceed one seventh of the kernel's heap size.
317 physvnodes = maxproc + cnt.v_page_count / 16 + 3 * min(98304 * 4,
318 cnt.v_page_count) / 16;
319 virtvnodes = vm_kmem_size / (7 * (sizeof(struct vm_object) +
320 sizeof(struct vnode)));
321 desiredvnodes = min(physvnodes, virtvnodes);
322 if (desiredvnodes > MAXVNODES_MAX) {
324 printf("Reducing kern.maxvnodes %d -> %d\n",
325 desiredvnodes, MAXVNODES_MAX);
326 desiredvnodes = MAXVNODES_MAX;
328 wantfreevnodes = desiredvnodes / 4;
329 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF);
330 TAILQ_INIT(&vnode_free_list);
331 mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF);
332 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL,
333 NULL, NULL, UMA_ALIGN_PTR, 0);
334 vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo),
335 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
337 * Initialize the filesystem syncer.
339 syncer_workitem_pending[WI_MPSAFEQ] = hashinit(syncer_maxdelay, M_VNODE,
341 syncer_workitem_pending[WI_GIANTQ] = hashinit(syncer_maxdelay, M_VNODE,
343 syncer_maxdelay = syncer_mask + 1;
344 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF);
345 cv_init(&sync_wakeup, "syncer");
347 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL);
351 * Mark a mount point as busy. Used to synchronize access and to delay
352 * unmounting. Eventually, mountlist_mtx is not released on failure.
354 * vfs_busy() is a custom lock, it can block the caller.
355 * vfs_busy() only sleeps if the unmount is active on the mount point.
356 * For a mountpoint mp, vfs_busy-enforced lock is before lock of any
357 * vnode belonging to mp.
359 * Lookup uses vfs_busy() to traverse mount points.
361 * / vnode lock A / vnode lock (/var) D
362 * /var vnode lock B /log vnode lock(/var/log) E
363 * vfs_busy lock C vfs_busy lock F
365 * Within each file system, the lock order is C->A->B and F->D->E.
367 * When traversing across mounts, the system follows that lock order:
373 * The lookup() process for namei("/var") illustrates the process:
374 * VOP_LOOKUP() obtains B while A is held
375 * vfs_busy() obtains a shared lock on F while A and B are held
376 * vput() releases lock on B
377 * vput() releases lock on A
378 * VFS_ROOT() obtains lock on D while shared lock on F is held
379 * vfs_unbusy() releases shared lock on F
380 * vn_lock() obtains lock on deadfs vnode vp_crossmp instead of A.
381 * Attempt to lock A (instead of vp_crossmp) while D is held would
382 * violate the global order, causing deadlocks.
384 * dounmount() locks B while F is drained.
387 vfs_busy(struct mount *mp, int flags)
390 MPASS((flags & ~MBF_MASK) == 0);
391 CTR3(KTR_VFS, "%s: mp %p with flags %d", __func__, mp, flags);
396 * If mount point is currenly being unmounted, sleep until the
397 * mount point fate is decided. If thread doing the unmounting fails,
398 * it will clear MNTK_UNMOUNT flag before waking us up, indicating
399 * that this mount point has survived the unmount attempt and vfs_busy
400 * should retry. Otherwise the unmounter thread will set MNTK_REFEXPIRE
401 * flag in addition to MNTK_UNMOUNT, indicating that mount point is
402 * about to be really destroyed. vfs_busy needs to release its
403 * reference on the mount point in this case and return with ENOENT,
404 * telling the caller that mount mount it tried to busy is no longer
407 while (mp->mnt_kern_flag & MNTK_UNMOUNT) {
408 if (flags & MBF_NOWAIT || mp->mnt_kern_flag & MNTK_REFEXPIRE) {
411 CTR1(KTR_VFS, "%s: failed busying before sleeping",
415 if (flags & MBF_MNTLSTLOCK)
416 mtx_unlock(&mountlist_mtx);
417 mp->mnt_kern_flag |= MNTK_MWAIT;
418 msleep(mp, MNT_MTX(mp), PVFS | PDROP, "vfs_busy", 0);
419 if (flags & MBF_MNTLSTLOCK)
420 mtx_lock(&mountlist_mtx);
423 if (flags & MBF_MNTLSTLOCK)
424 mtx_unlock(&mountlist_mtx);
431 * Free a busy filesystem.
434 vfs_unbusy(struct mount *mp)
437 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
440 KASSERT(mp->mnt_lockref > 0, ("negative mnt_lockref"));
442 if (mp->mnt_lockref == 0 && (mp->mnt_kern_flag & MNTK_DRAINING) != 0) {
443 MPASS(mp->mnt_kern_flag & MNTK_UNMOUNT);
444 CTR1(KTR_VFS, "%s: waking up waiters", __func__);
445 mp->mnt_kern_flag &= ~MNTK_DRAINING;
446 wakeup(&mp->mnt_lockref);
452 * Lookup a mount point by filesystem identifier.
455 vfs_getvfs(fsid_t *fsid)
459 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
460 mtx_lock(&mountlist_mtx);
461 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
462 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
463 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
465 mtx_unlock(&mountlist_mtx);
469 mtx_unlock(&mountlist_mtx);
470 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
471 return ((struct mount *) 0);
475 * Lookup a mount point by filesystem identifier, busying it before
479 vfs_busyfs(fsid_t *fsid)
484 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
485 mtx_lock(&mountlist_mtx);
486 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
487 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
488 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
489 error = vfs_busy(mp, MBF_MNTLSTLOCK);
491 mtx_unlock(&mountlist_mtx);
497 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
498 mtx_unlock(&mountlist_mtx);
499 return ((struct mount *) 0);
503 * Check if a user can access privileged mount options.
506 vfs_suser(struct mount *mp, struct thread *td)
511 * If the thread is jailed, but this is not a jail-friendly file
512 * system, deny immediately.
514 if (!(mp->mnt_vfc->vfc_flags & VFCF_JAIL) && jailed(td->td_ucred))
518 * If the file system was mounted outside the jail of the calling
519 * thread, deny immediately.
521 if (prison_check(td->td_ucred, mp->mnt_cred) != 0)
525 * If file system supports delegated administration, we don't check
526 * for the PRIV_VFS_MOUNT_OWNER privilege - it will be better verified
527 * by the file system itself.
528 * If this is not the user that did original mount, we check for
529 * the PRIV_VFS_MOUNT_OWNER privilege.
531 if (!(mp->mnt_vfc->vfc_flags & VFCF_DELEGADMIN) &&
532 mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
533 if ((error = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 0)
540 * Get a new unique fsid. Try to make its val[0] unique, since this value
541 * will be used to create fake device numbers for stat(). Also try (but
542 * not so hard) make its val[0] unique mod 2^16, since some emulators only
543 * support 16-bit device numbers. We end up with unique val[0]'s for the
544 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
546 * Keep in mind that several mounts may be running in parallel. Starting
547 * the search one past where the previous search terminated is both a
548 * micro-optimization and a defense against returning the same fsid to
552 vfs_getnewfsid(struct mount *mp)
554 static u_int16_t mntid_base;
559 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
560 mtx_lock(&mntid_mtx);
561 mtype = mp->mnt_vfc->vfc_typenum;
562 tfsid.val[1] = mtype;
563 mtype = (mtype & 0xFF) << 24;
565 tfsid.val[0] = makedev(255,
566 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
568 if ((nmp = vfs_getvfs(&tfsid)) == NULL)
572 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
573 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
574 mtx_unlock(&mntid_mtx);
578 * Knob to control the precision of file timestamps:
580 * 0 = seconds only; nanoseconds zeroed.
581 * 1 = seconds and nanoseconds, accurate within 1/HZ.
582 * 2 = seconds and nanoseconds, truncated to microseconds.
583 * >=3 = seconds and nanoseconds, maximum precision.
585 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
587 static int timestamp_precision = TSP_SEC;
588 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
589 ×tamp_precision, 0, "File timestamp precision (0: seconds, "
590 "1: sec + ns accurate to 1/HZ, 2: sec + ns truncated to ms, "
591 "3+: sec + ns (max. precision))");
594 * Get a current timestamp.
597 vfs_timestamp(struct timespec *tsp)
601 switch (timestamp_precision) {
603 tsp->tv_sec = time_second;
611 TIMEVAL_TO_TIMESPEC(&tv, tsp);
621 * Set vnode attributes to VNOVAL
624 vattr_null(struct vattr *vap)
628 vap->va_size = VNOVAL;
629 vap->va_bytes = VNOVAL;
630 vap->va_mode = VNOVAL;
631 vap->va_nlink = VNOVAL;
632 vap->va_uid = VNOVAL;
633 vap->va_gid = VNOVAL;
634 vap->va_fsid = VNOVAL;
635 vap->va_fileid = VNOVAL;
636 vap->va_blocksize = VNOVAL;
637 vap->va_rdev = VNOVAL;
638 vap->va_atime.tv_sec = VNOVAL;
639 vap->va_atime.tv_nsec = VNOVAL;
640 vap->va_mtime.tv_sec = VNOVAL;
641 vap->va_mtime.tv_nsec = VNOVAL;
642 vap->va_ctime.tv_sec = VNOVAL;
643 vap->va_ctime.tv_nsec = VNOVAL;
644 vap->va_birthtime.tv_sec = VNOVAL;
645 vap->va_birthtime.tv_nsec = VNOVAL;
646 vap->va_flags = VNOVAL;
647 vap->va_gen = VNOVAL;
652 * This routine is called when we have too many vnodes. It attempts
653 * to free <count> vnodes and will potentially free vnodes that still
654 * have VM backing store (VM backing store is typically the cause
655 * of a vnode blowout so we want to do this). Therefore, this operation
656 * is not considered cheap.
658 * A number of conditions may prevent a vnode from being reclaimed.
659 * the buffer cache may have references on the vnode, a directory
660 * vnode may still have references due to the namei cache representing
661 * underlying files, or the vnode may be in active use. It is not
662 * desireable to reuse such vnodes. These conditions may cause the
663 * number of vnodes to reach some minimum value regardless of what
664 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
667 vlrureclaim(struct mount *mp)
676 * Calculate the trigger point, don't allow user
677 * screwups to blow us up. This prevents us from
678 * recycling vnodes with lots of resident pages. We
679 * aren't trying to free memory, we are trying to
682 usevnodes = desiredvnodes;
685 trigger = cnt.v_page_count * 2 / usevnodes;
687 vn_start_write(NULL, &mp, V_WAIT);
689 count = mp->mnt_nvnodelistsize / 10 + 1;
691 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
692 while (vp != NULL && vp->v_type == VMARKER)
693 vp = TAILQ_NEXT(vp, v_nmntvnodes);
696 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
697 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
702 * If it's been deconstructed already, it's still
703 * referenced, or it exceeds the trigger, skip it.
705 if (vp->v_usecount ||
706 (!vlru_allow_cache_src &&
707 !LIST_EMPTY(&(vp)->v_cache_src)) ||
708 (vp->v_iflag & VI_DOOMED) != 0 || (vp->v_object != NULL &&
709 vp->v_object->resident_page_count > trigger)) {
715 if (VOP_LOCK(vp, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT)) {
717 goto next_iter_mntunlocked;
721 * v_usecount may have been bumped after VOP_LOCK() dropped
722 * the vnode interlock and before it was locked again.
724 * It is not necessary to recheck VI_DOOMED because it can
725 * only be set by another thread that holds both the vnode
726 * lock and vnode interlock. If another thread has the
727 * vnode lock before we get to VOP_LOCK() and obtains the
728 * vnode interlock after VOP_LOCK() drops the vnode
729 * interlock, the other thread will be unable to drop the
730 * vnode lock before our VOP_LOCK() call fails.
732 if (vp->v_usecount ||
733 (!vlru_allow_cache_src &&
734 !LIST_EMPTY(&(vp)->v_cache_src)) ||
735 (vp->v_object != NULL &&
736 vp->v_object->resident_page_count > trigger)) {
737 VOP_UNLOCK(vp, LK_INTERLOCK);
738 goto next_iter_mntunlocked;
740 KASSERT((vp->v_iflag & VI_DOOMED) == 0,
741 ("VI_DOOMED unexpectedly detected in vlrureclaim()"));
746 next_iter_mntunlocked:
747 if ((count % 256) != 0)
751 if ((count % 256) != 0)
760 vn_finished_write(mp);
765 * Attempt to keep the free list at wantfreevnodes length.
768 vnlru_free(int count)
773 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
774 for (; count > 0; count--) {
775 vp = TAILQ_FIRST(&vnode_free_list);
777 * The list can be modified while the free_list_mtx
778 * has been dropped and vp could be NULL here.
782 VNASSERT(vp->v_op != NULL, vp,
783 ("vnlru_free: vnode already reclaimed."));
784 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
786 * Don't recycle if we can't get the interlock.
788 if (!VI_TRYLOCK(vp)) {
789 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
792 VNASSERT(VCANRECYCLE(vp), vp,
793 ("vp inconsistent on freelist"));
795 vp->v_iflag &= ~VI_FREE;
797 mtx_unlock(&vnode_free_list_mtx);
799 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
801 VFS_UNLOCK_GIANT(vfslocked);
803 * If the recycled succeeded this vdrop will actually free
804 * the vnode. If not it will simply place it back on
808 mtx_lock(&vnode_free_list_mtx);
812 * Attempt to recycle vnodes in a context that is always safe to block.
813 * Calling vlrurecycle() from the bowels of filesystem code has some
814 * interesting deadlock problems.
816 static struct proc *vnlruproc;
817 static int vnlruproc_sig;
822 struct mount *mp, *nmp;
824 struct proc *p = vnlruproc;
826 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, p,
830 kproc_suspend_check(p);
831 mtx_lock(&vnode_free_list_mtx);
832 if (freevnodes > wantfreevnodes)
833 vnlru_free(freevnodes - wantfreevnodes);
834 if (numvnodes <= desiredvnodes * 9 / 10) {
836 wakeup(&vnlruproc_sig);
837 msleep(vnlruproc, &vnode_free_list_mtx,
838 PVFS|PDROP, "vlruwt", hz);
841 mtx_unlock(&vnode_free_list_mtx);
843 mtx_lock(&mountlist_mtx);
844 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
845 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
846 nmp = TAILQ_NEXT(mp, mnt_list);
849 vfslocked = VFS_LOCK_GIANT(mp);
850 done += vlrureclaim(mp);
851 VFS_UNLOCK_GIANT(vfslocked);
852 mtx_lock(&mountlist_mtx);
853 nmp = TAILQ_NEXT(mp, mnt_list);
856 mtx_unlock(&mountlist_mtx);
859 /* These messages are temporary debugging aids */
860 if (vnlru_nowhere < 5)
861 printf("vnlru process getting nowhere..\n");
862 else if (vnlru_nowhere == 5)
863 printf("vnlru process messages stopped.\n");
866 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
872 static struct kproc_desc vnlru_kp = {
877 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start,
881 * Routines having to do with the management of the vnode table.
885 vdestroy(struct vnode *vp)
889 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
890 mtx_lock(&vnode_free_list_mtx);
892 mtx_unlock(&vnode_free_list_mtx);
894 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
895 ("cleaned vnode still on the free list."));
896 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't"));
897 VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count"));
898 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count"));
899 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count"));
900 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's"));
901 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0"));
902 VNASSERT(bo->bo_clean.bv_root == NULL, vp, ("cleanblkroot not NULL"));
903 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0"));
904 VNASSERT(bo->bo_dirty.bv_root == NULL, vp, ("dirtyblkroot not NULL"));
905 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst"));
906 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src"));
907 VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for .."));
910 mac_vnode_destroy(vp);
912 if (vp->v_pollinfo != NULL)
913 destroy_vpollinfo(vp->v_pollinfo);
915 /* XXX Elsewhere we can detect an already freed vnode via NULL v_op. */
918 lockdestroy(vp->v_vnlock);
919 mtx_destroy(&vp->v_interlock);
920 mtx_destroy(BO_MTX(bo));
921 uma_zfree(vnode_zone, vp);
925 * Try to recycle a freed vnode. We abort if anyone picks up a reference
926 * before we actually vgone(). This function must be called with the vnode
927 * held to prevent the vnode from being returned to the free list midway
931 vtryrecycle(struct vnode *vp)
935 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
936 VNASSERT(vp->v_holdcnt, vp,
937 ("vtryrecycle: Recycling vp %p without a reference.", vp));
939 * This vnode may found and locked via some other list, if so we
940 * can't recycle it yet.
942 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
944 "%s: impossible to recycle, vp %p lock is already held",
946 return (EWOULDBLOCK);
949 * Don't recycle if its filesystem is being suspended.
951 if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) {
954 "%s: impossible to recycle, cannot start the write for %p",
959 * If we got this far, we need to acquire the interlock and see if
960 * anyone picked up this vnode from another list. If not, we will
961 * mark it with DOOMED via vgonel() so that anyone who does find it
965 if (vp->v_usecount) {
966 VOP_UNLOCK(vp, LK_INTERLOCK);
967 vn_finished_write(vnmp);
969 "%s: impossible to recycle, %p is already referenced",
973 if ((vp->v_iflag & VI_DOOMED) == 0)
975 VOP_UNLOCK(vp, LK_INTERLOCK);
976 vn_finished_write(vnmp);
981 * Return the next vnode from the free list.
984 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops,
987 struct vnode *vp = NULL;
990 CTR3(KTR_VFS, "%s: mp %p with tag %s", __func__, mp, tag);
991 mtx_lock(&vnode_free_list_mtx);
993 * Lend our context to reclaim vnodes if they've exceeded the max.
995 if (freevnodes > wantfreevnodes)
998 * Wait for available vnodes.
1000 if (numvnodes > desiredvnodes) {
1001 if (mp != NULL && (mp->mnt_kern_flag & MNTK_SUSPEND)) {
1003 * File system is beeing suspended, we cannot risk a
1004 * deadlock here, so allocate new vnode anyway.
1006 if (freevnodes > wantfreevnodes)
1007 vnlru_free(freevnodes - wantfreevnodes);
1010 if (vnlruproc_sig == 0) {
1011 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
1014 msleep(&vnlruproc_sig, &vnode_free_list_mtx, PVFS,
1016 #if 0 /* XXX Not all VFS_VGET/ffs_vget callers check returns. */
1017 if (numvnodes > desiredvnodes) {
1018 mtx_unlock(&vnode_free_list_mtx);
1025 mtx_unlock(&vnode_free_list_mtx);
1026 vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK|M_ZERO);
1030 vp->v_vnlock = &vp->v_lock;
1031 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF);
1033 * By default, don't allow shared locks unless filesystems
1036 lockinit(vp->v_vnlock, PVFS, tag, VLKTIMEOUT, LK_NOSHARE);
1038 * Initialize bufobj.
1041 bo->__bo_vnode = vp;
1042 mtx_init(BO_MTX(bo), "bufobj interlock", NULL, MTX_DEF);
1043 bo->bo_ops = &buf_ops_bio;
1044 bo->bo_private = vp;
1045 TAILQ_INIT(&bo->bo_clean.bv_hd);
1046 TAILQ_INIT(&bo->bo_dirty.bv_hd);
1048 * Initialize namecache.
1050 LIST_INIT(&vp->v_cache_src);
1051 TAILQ_INIT(&vp->v_cache_dst);
1053 * Finalize various vnode identity bits.
1058 v_incr_usecount(vp);
1062 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0)
1063 mac_vnode_associate_singlelabel(mp, vp);
1064 else if (mp == NULL && vops != &dead_vnodeops)
1065 printf("NULL mp in getnewvnode()\n");
1068 bo->bo_bsize = mp->mnt_stat.f_iosize;
1069 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0)
1070 vp->v_vflag |= VV_NOKNOTE;
1078 * Delete from old mount point vnode list, if on one.
1081 delmntque(struct vnode *vp)
1090 VNASSERT(mp->mnt_nvnodelistsize > 0, vp,
1091 ("bad mount point vnode list size"));
1092 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1093 mp->mnt_nvnodelistsize--;
1099 insmntque_stddtr(struct vnode *vp, void *dtr_arg)
1103 vp->v_op = &dead_vnodeops;
1104 /* XXX non mp-safe fs may still call insmntque with vnode
1106 if (!VOP_ISLOCKED(vp))
1107 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1113 * Insert into list of vnodes for the new mount point, if available.
1116 insmntque1(struct vnode *vp, struct mount *mp,
1117 void (*dtr)(struct vnode *, void *), void *dtr_arg)
1121 KASSERT(vp->v_mount == NULL,
1122 ("insmntque: vnode already on per mount vnode list"));
1123 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)"));
1124 #ifdef DEBUG_VFS_LOCKS
1125 if (!VFS_NEEDSGIANT(mp))
1126 ASSERT_VOP_ELOCKED(vp,
1127 "insmntque: mp-safe fs and non-locked vp");
1130 if ((mp->mnt_kern_flag & MNTK_NOINSMNTQ) != 0 &&
1131 ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 ||
1132 mp->mnt_nvnodelistsize == 0)) {
1133 locked = VOP_ISLOCKED(vp);
1134 if (!locked || (locked == LK_EXCLUSIVE &&
1135 (vp->v_vflag & VV_FORCEINSMQ) == 0)) {
1144 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1145 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp,
1146 ("neg mount point vnode list size"));
1147 mp->mnt_nvnodelistsize++;
1153 insmntque(struct vnode *vp, struct mount *mp)
1156 return (insmntque1(vp, mp, insmntque_stddtr, NULL));
1160 * Flush out and invalidate all buffers associated with a bufobj
1161 * Called with the underlying object locked.
1164 bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo)
1169 if (flags & V_SAVE) {
1170 error = bufobj_wwait(bo, slpflag, slptimeo);
1175 if (bo->bo_dirty.bv_cnt > 0) {
1177 if ((error = BO_SYNC(bo, MNT_WAIT)) != 0)
1180 * XXX We could save a lock/unlock if this was only
1181 * enabled under INVARIANTS
1184 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)
1185 panic("vinvalbuf: dirty bufs");
1189 * If you alter this loop please notice that interlock is dropped and
1190 * reacquired in flushbuflist. Special care is needed to ensure that
1191 * no race conditions occur from this.
1194 error = flushbuflist(&bo->bo_clean,
1195 flags, bo, slpflag, slptimeo);
1196 if (error == 0 && !(flags & V_CLEANONLY))
1197 error = flushbuflist(&bo->bo_dirty,
1198 flags, bo, slpflag, slptimeo);
1199 if (error != 0 && error != EAGAIN) {
1203 } while (error != 0);
1206 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
1207 * have write I/O in-progress but if there is a VM object then the
1208 * VM object can also have read-I/O in-progress.
1211 bufobj_wwait(bo, 0, 0);
1213 if (bo->bo_object != NULL) {
1214 VM_OBJECT_LOCK(bo->bo_object);
1215 vm_object_pip_wait(bo->bo_object, "bovlbx");
1216 VM_OBJECT_UNLOCK(bo->bo_object);
1219 } while (bo->bo_numoutput > 0);
1223 * Destroy the copy in the VM cache, too.
1225 if (bo->bo_object != NULL &&
1226 (flags & (V_ALT | V_NORMAL | V_CLEANONLY)) == 0) {
1227 VM_OBJECT_LOCK(bo->bo_object);
1228 vm_object_page_remove(bo->bo_object, 0, 0,
1229 (flags & V_SAVE) ? TRUE : FALSE);
1230 VM_OBJECT_UNLOCK(bo->bo_object);
1235 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY)) == 0 &&
1236 (bo->bo_dirty.bv_cnt > 0 || bo->bo_clean.bv_cnt > 0))
1237 panic("vinvalbuf: flush failed");
1244 * Flush out and invalidate all buffers associated with a vnode.
1245 * Called with the underlying object locked.
1248 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
1251 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
1252 ASSERT_VOP_LOCKED(vp, "vinvalbuf");
1253 return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo));
1257 * Flush out buffers on the specified list.
1261 flushbuflist( struct bufv *bufv, int flags, struct bufobj *bo, int slpflag,
1264 struct buf *bp, *nbp;
1269 ASSERT_BO_LOCKED(bo);
1272 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) {
1273 if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) ||
1274 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) {
1280 lblkno = nbp->b_lblkno;
1281 xflags = nbp->b_xflags &
1282 (BX_BKGRDMARKER | BX_VNDIRTY | BX_VNCLEAN);
1285 error = BUF_TIMELOCK(bp,
1286 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_MTX(bo),
1287 "flushbuf", slpflag, slptimeo);
1290 return (error != ENOLCK ? error : EAGAIN);
1292 KASSERT(bp->b_bufobj == bo,
1293 ("bp %p wrong b_bufobj %p should be %p",
1294 bp, bp->b_bufobj, bo));
1295 if (bp->b_bufobj != bo) { /* XXX: necessary ? */
1301 * XXX Since there are no node locks for NFS, I
1302 * believe there is a slight chance that a delayed
1303 * write will occur while sleeping just above, so
1306 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
1311 bp->b_flags |= B_ASYNC;
1314 return (EAGAIN); /* XXX: why not loop ? */
1319 bp->b_flags |= (B_INVAL | B_RELBUF);
1320 bp->b_flags &= ~B_ASYNC;
1324 (nbp->b_bufobj != bo ||
1325 nbp->b_lblkno != lblkno ||
1327 (BX_BKGRDMARKER | BX_VNDIRTY | BX_VNCLEAN)) != xflags))
1328 break; /* nbp invalid */
1334 * Truncate a file's buffer and pages to a specified length. This
1335 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
1339 vtruncbuf(struct vnode *vp, struct ucred *cred, struct thread *td,
1340 off_t length, int blksize)
1342 struct buf *bp, *nbp;
1347 CTR5(KTR_VFS, "%s: vp %p with cred %p and block %d:%ju", __func__,
1348 vp, cred, blksize, (uintmax_t)length);
1351 * Round up to the *next* lbn.
1353 trunclbn = (length + blksize - 1) / blksize;
1355 ASSERT_VOP_LOCKED(vp, "vtruncbuf");
1362 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) {
1363 if (bp->b_lblkno < trunclbn)
1366 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1367 BO_MTX(bo)) == ENOLCK)
1373 bp->b_flags |= (B_INVAL | B_RELBUF);
1374 bp->b_flags &= ~B_ASYNC;
1380 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
1381 (nbp->b_vp != vp) ||
1382 (nbp->b_flags & B_DELWRI))) {
1388 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1389 if (bp->b_lblkno < trunclbn)
1392 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1393 BO_MTX(bo)) == ENOLCK)
1398 bp->b_flags |= (B_INVAL | B_RELBUF);
1399 bp->b_flags &= ~B_ASYNC;
1405 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
1406 (nbp->b_vp != vp) ||
1407 (nbp->b_flags & B_DELWRI) == 0)) {
1416 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1417 if (bp->b_lblkno > 0)
1420 * Since we hold the vnode lock this should only
1421 * fail if we're racing with the buf daemon.
1424 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1425 BO_MTX(bo)) == ENOLCK) {
1428 VNASSERT((bp->b_flags & B_DELWRI), vp,
1429 ("buf(%p) on dirty queue without DELWRI", bp));
1440 bufobj_wwait(bo, 0, 0);
1442 vnode_pager_setsize(vp, length);
1448 * buf_splay() - splay tree core for the clean/dirty list of buffers in
1451 * NOTE: We have to deal with the special case of a background bitmap
1452 * buffer, a situation where two buffers will have the same logical
1453 * block offset. We want (1) only the foreground buffer to be accessed
1454 * in a lookup and (2) must differentiate between the foreground and
1455 * background buffer in the splay tree algorithm because the splay
1456 * tree cannot normally handle multiple entities with the same 'index'.
1457 * We accomplish this by adding differentiating flags to the splay tree's
1462 buf_splay(daddr_t lblkno, b_xflags_t xflags, struct buf *root)
1465 struct buf *lefttreemax, *righttreemin, *y;
1469 lefttreemax = righttreemin = &dummy;
1471 if (lblkno < root->b_lblkno ||
1472 (lblkno == root->b_lblkno &&
1473 (xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
1474 if ((y = root->b_left) == NULL)
1476 if (lblkno < y->b_lblkno) {
1478 root->b_left = y->b_right;
1481 if ((y = root->b_left) == NULL)
1484 /* Link into the new root's right tree. */
1485 righttreemin->b_left = root;
1486 righttreemin = root;
1487 } else if (lblkno > root->b_lblkno ||
1488 (lblkno == root->b_lblkno &&
1489 (xflags & BX_BKGRDMARKER) > (root->b_xflags & BX_BKGRDMARKER))) {
1490 if ((y = root->b_right) == NULL)
1492 if (lblkno > y->b_lblkno) {
1494 root->b_right = y->b_left;
1497 if ((y = root->b_right) == NULL)
1500 /* Link into the new root's left tree. */
1501 lefttreemax->b_right = root;
1508 /* Assemble the new root. */
1509 lefttreemax->b_right = root->b_left;
1510 righttreemin->b_left = root->b_right;
1511 root->b_left = dummy.b_right;
1512 root->b_right = dummy.b_left;
1517 buf_vlist_remove(struct buf *bp)
1522 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1523 ASSERT_BO_LOCKED(bp->b_bufobj);
1524 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) !=
1525 (BX_VNDIRTY|BX_VNCLEAN),
1526 ("buf_vlist_remove: Buf %p is on two lists", bp));
1527 if (bp->b_xflags & BX_VNDIRTY)
1528 bv = &bp->b_bufobj->bo_dirty;
1530 bv = &bp->b_bufobj->bo_clean;
1531 if (bp != bv->bv_root) {
1532 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root);
1533 KASSERT(root == bp, ("splay lookup failed in remove"));
1535 if (bp->b_left == NULL) {
1538 root = buf_splay(bp->b_lblkno, bp->b_xflags, bp->b_left);
1539 root->b_right = bp->b_right;
1542 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs);
1544 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1548 * Add the buffer to the sorted clean or dirty block list using a
1549 * splay tree algorithm.
1551 * NOTE: xflags is passed as a constant, optimizing this inline function!
1554 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags)
1559 ASSERT_BO_LOCKED(bo);
1560 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0,
1561 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags));
1562 bp->b_xflags |= xflags;
1563 if (xflags & BX_VNDIRTY)
1568 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root);
1572 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs);
1573 } else if (bp->b_lblkno < root->b_lblkno ||
1574 (bp->b_lblkno == root->b_lblkno &&
1575 (bp->b_xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
1576 bp->b_left = root->b_left;
1578 root->b_left = NULL;
1579 TAILQ_INSERT_BEFORE(root, bp, b_bobufs);
1581 bp->b_right = root->b_right;
1583 root->b_right = NULL;
1584 TAILQ_INSERT_AFTER(&bv->bv_hd, root, bp, b_bobufs);
1591 * Lookup a buffer using the splay tree. Note that we specifically avoid
1592 * shadow buffers used in background bitmap writes.
1594 * This code isn't quite efficient as it could be because we are maintaining
1595 * two sorted lists and do not know which list the block resides in.
1597 * During a "make buildworld" the desired buffer is found at one of
1598 * the roots more than 60% of the time. Thus, checking both roots
1599 * before performing either splay eliminates unnecessary splays on the
1600 * first tree splayed.
1603 gbincore(struct bufobj *bo, daddr_t lblkno)
1607 ASSERT_BO_LOCKED(bo);
1608 if ((bp = bo->bo_clean.bv_root) != NULL &&
1609 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1611 if ((bp = bo->bo_dirty.bv_root) != NULL &&
1612 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1614 if ((bp = bo->bo_clean.bv_root) != NULL) {
1615 bo->bo_clean.bv_root = bp = buf_splay(lblkno, 0, bp);
1616 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1619 if ((bp = bo->bo_dirty.bv_root) != NULL) {
1620 bo->bo_dirty.bv_root = bp = buf_splay(lblkno, 0, bp);
1621 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1628 * Associate a buffer with a vnode.
1631 bgetvp(struct vnode *vp, struct buf *bp)
1636 ASSERT_BO_LOCKED(bo);
1637 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free"));
1639 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags);
1640 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp,
1641 ("bgetvp: bp already attached! %p", bp));
1644 if (VFS_NEEDSGIANT(vp->v_mount) || bo->bo_flag & BO_NEEDSGIANT)
1645 bp->b_flags |= B_NEEDSGIANT;
1649 * Insert onto list for new vnode.
1651 buf_vlist_add(bp, bo, BX_VNCLEAN);
1655 * Disassociate a buffer from a vnode.
1658 brelvp(struct buf *bp)
1663 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1664 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1667 * Delete from old vnode list, if on one.
1669 vp = bp->b_vp; /* XXX */
1672 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1673 buf_vlist_remove(bp);
1675 panic("brelvp: Buffer %p not on queue.", bp);
1676 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
1677 bo->bo_flag &= ~BO_ONWORKLST;
1678 mtx_lock(&sync_mtx);
1679 LIST_REMOVE(bo, bo_synclist);
1680 syncer_worklist_len--;
1681 mtx_unlock(&sync_mtx);
1683 bp->b_flags &= ~B_NEEDSGIANT;
1685 bp->b_bufobj = NULL;
1691 * Add an item to the syncer work queue.
1694 vn_syncer_add_to_worklist(struct bufobj *bo, int delay)
1698 ASSERT_BO_LOCKED(bo);
1700 mtx_lock(&sync_mtx);
1701 if (bo->bo_flag & BO_ONWORKLST)
1702 LIST_REMOVE(bo, bo_synclist);
1704 bo->bo_flag |= BO_ONWORKLST;
1705 syncer_worklist_len++;
1708 if (delay > syncer_maxdelay - 2)
1709 delay = syncer_maxdelay - 2;
1710 slot = (syncer_delayno + delay) & syncer_mask;
1712 queue = VFS_NEEDSGIANT(bo->__bo_vnode->v_mount) ? WI_GIANTQ :
1714 LIST_INSERT_HEAD(&syncer_workitem_pending[queue][slot], bo,
1716 mtx_unlock(&sync_mtx);
1720 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS)
1724 mtx_lock(&sync_mtx);
1725 len = syncer_worklist_len - sync_vnode_count;
1726 mtx_unlock(&sync_mtx);
1727 error = SYSCTL_OUT(req, &len, sizeof(len));
1731 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, CTLTYPE_INT | CTLFLAG_RD, NULL, 0,
1732 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length");
1734 static struct proc *updateproc;
1735 static void sched_sync(void);
1736 static struct kproc_desc up_kp = {
1741 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp);
1744 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td)
1749 *bo = LIST_FIRST(slp);
1752 vp = (*bo)->__bo_vnode; /* XXX */
1753 if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0)
1756 * We use vhold in case the vnode does not
1757 * successfully sync. vhold prevents the vnode from
1758 * going away when we unlock the sync_mtx so that
1759 * we can acquire the vnode interlock.
1762 mtx_unlock(&sync_mtx);
1764 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1766 mtx_lock(&sync_mtx);
1767 return (*bo == LIST_FIRST(slp));
1769 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1770 (void) VOP_FSYNC(vp, MNT_LAZY, td);
1772 vn_finished_write(mp);
1774 if (((*bo)->bo_flag & BO_ONWORKLST) != 0) {
1776 * Put us back on the worklist. The worklist
1777 * routine will remove us from our current
1778 * position and then add us back in at a later
1781 vn_syncer_add_to_worklist(*bo, syncdelay);
1785 mtx_lock(&sync_mtx);
1790 * System filesystem synchronizer daemon.
1795 struct synclist *gnext, *next;
1796 struct synclist *gslp, *slp;
1799 struct thread *td = curthread;
1801 int net_worklist_len;
1802 int syncer_final_iter;
1807 syncer_final_iter = 0;
1809 syncer_state = SYNCER_RUNNING;
1810 starttime = time_uptime;
1811 td->td_pflags |= TDP_NORUNNINGBUF;
1813 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc,
1816 mtx_lock(&sync_mtx);
1818 if (syncer_state == SYNCER_FINAL_DELAY &&
1819 syncer_final_iter == 0) {
1820 mtx_unlock(&sync_mtx);
1821 kproc_suspend_check(td->td_proc);
1822 mtx_lock(&sync_mtx);
1824 net_worklist_len = syncer_worklist_len - sync_vnode_count;
1825 if (syncer_state != SYNCER_RUNNING &&
1826 starttime != time_uptime) {
1828 printf("\nSyncing disks, vnodes remaining...");
1831 printf("%d ", net_worklist_len);
1833 starttime = time_uptime;
1836 * Push files whose dirty time has expired. Be careful
1837 * of interrupt race on slp queue.
1839 * Skip over empty worklist slots when shutting down.
1842 slp = &syncer_workitem_pending[WI_MPSAFEQ][syncer_delayno];
1843 gslp = &syncer_workitem_pending[WI_GIANTQ][syncer_delayno];
1844 syncer_delayno += 1;
1845 if (syncer_delayno == syncer_maxdelay)
1847 next = &syncer_workitem_pending[WI_MPSAFEQ][syncer_delayno];
1848 gnext = &syncer_workitem_pending[WI_GIANTQ][syncer_delayno];
1850 * If the worklist has wrapped since the
1851 * it was emptied of all but syncer vnodes,
1852 * switch to the FINAL_DELAY state and run
1853 * for one more second.
1855 if (syncer_state == SYNCER_SHUTTING_DOWN &&
1856 net_worklist_len == 0 &&
1857 last_work_seen == syncer_delayno) {
1858 syncer_state = SYNCER_FINAL_DELAY;
1859 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP;
1861 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) &&
1862 LIST_EMPTY(gslp) && syncer_worklist_len > 0);
1865 * Keep track of the last time there was anything
1866 * on the worklist other than syncer vnodes.
1867 * Return to the SHUTTING_DOWN state if any
1870 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING)
1871 last_work_seen = syncer_delayno;
1872 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY)
1873 syncer_state = SYNCER_SHUTTING_DOWN;
1874 while (!LIST_EMPTY(slp)) {
1875 error = sync_vnode(slp, &bo, td);
1877 LIST_REMOVE(bo, bo_synclist);
1878 LIST_INSERT_HEAD(next, bo, bo_synclist);
1882 if (first_printf == 0)
1883 wdog_kern_pat(WD_LASTVAL);
1886 if (!LIST_EMPTY(gslp)) {
1887 mtx_unlock(&sync_mtx);
1889 mtx_lock(&sync_mtx);
1890 while (!LIST_EMPTY(gslp)) {
1891 error = sync_vnode(gslp, &bo, td);
1893 LIST_REMOVE(bo, bo_synclist);
1894 LIST_INSERT_HEAD(gnext, bo,
1901 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0)
1902 syncer_final_iter--;
1904 * The variable rushjob allows the kernel to speed up the
1905 * processing of the filesystem syncer process. A rushjob
1906 * value of N tells the filesystem syncer to process the next
1907 * N seconds worth of work on its queue ASAP. Currently rushjob
1908 * is used by the soft update code to speed up the filesystem
1909 * syncer process when the incore state is getting so far
1910 * ahead of the disk that the kernel memory pool is being
1911 * threatened with exhaustion.
1918 * Just sleep for a short period of time between
1919 * iterations when shutting down to allow some I/O
1922 * If it has taken us less than a second to process the
1923 * current work, then wait. Otherwise start right over
1924 * again. We can still lose time if any single round
1925 * takes more than two seconds, but it does not really
1926 * matter as we are just trying to generally pace the
1927 * filesystem activity.
1929 if (syncer_state != SYNCER_RUNNING ||
1930 time_uptime == starttime) {
1932 sched_prio(td, PPAUSE);
1935 if (syncer_state != SYNCER_RUNNING)
1936 cv_timedwait(&sync_wakeup, &sync_mtx,
1937 hz / SYNCER_SHUTDOWN_SPEEDUP);
1938 else if (time_uptime == starttime)
1939 cv_timedwait(&sync_wakeup, &sync_mtx, hz);
1944 * Request the syncer daemon to speed up its work.
1945 * We never push it to speed up more than half of its
1946 * normal turn time, otherwise it could take over the cpu.
1949 speedup_syncer(void)
1953 mtx_lock(&sync_mtx);
1954 if (rushjob < syncdelay / 2) {
1956 stat_rush_requests += 1;
1959 mtx_unlock(&sync_mtx);
1960 cv_broadcast(&sync_wakeup);
1965 * Tell the syncer to speed up its work and run though its work
1966 * list several times, then tell it to shut down.
1969 syncer_shutdown(void *arg, int howto)
1972 if (howto & RB_NOSYNC)
1974 mtx_lock(&sync_mtx);
1975 syncer_state = SYNCER_SHUTTING_DOWN;
1977 mtx_unlock(&sync_mtx);
1978 cv_broadcast(&sync_wakeup);
1979 kproc_shutdown(arg, howto);
1983 * Reassign a buffer from one vnode to another.
1984 * Used to assign file specific control information
1985 * (indirect blocks) to the vnode to which they belong.
1988 reassignbuf(struct buf *bp)
2001 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X",
2002 bp, bp->b_vp, bp->b_flags);
2004 * B_PAGING flagged buffers cannot be reassigned because their vp
2005 * is not fully linked in.
2007 if (bp->b_flags & B_PAGING)
2008 panic("cannot reassign paging buffer");
2011 * Delete from old vnode list, if on one.
2014 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
2015 buf_vlist_remove(bp);
2017 panic("reassignbuf: Buffer %p not on queue.", bp);
2019 * If dirty, put on list of dirty buffers; otherwise insert onto list
2022 if (bp->b_flags & B_DELWRI) {
2023 if ((bo->bo_flag & BO_ONWORKLST) == 0) {
2024 switch (vp->v_type) {
2034 vn_syncer_add_to_worklist(bo, delay);
2036 buf_vlist_add(bp, bo, BX_VNDIRTY);
2038 buf_vlist_add(bp, bo, BX_VNCLEAN);
2040 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
2041 mtx_lock(&sync_mtx);
2042 LIST_REMOVE(bo, bo_synclist);
2043 syncer_worklist_len--;
2044 mtx_unlock(&sync_mtx);
2045 bo->bo_flag &= ~BO_ONWORKLST;
2050 bp = TAILQ_FIRST(&bv->bv_hd);
2051 KASSERT(bp == NULL || bp->b_bufobj == bo,
2052 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2053 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2054 KASSERT(bp == NULL || bp->b_bufobj == bo,
2055 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2057 bp = TAILQ_FIRST(&bv->bv_hd);
2058 KASSERT(bp == NULL || bp->b_bufobj == bo,
2059 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2060 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2061 KASSERT(bp == NULL || bp->b_bufobj == bo,
2062 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2068 * Increment the use and hold counts on the vnode, taking care to reference
2069 * the driver's usecount if this is a chardev. The vholdl() will remove
2070 * the vnode from the free list if it is presently free. Requires the
2071 * vnode interlock and returns with it held.
2074 v_incr_usecount(struct vnode *vp)
2077 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2079 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2081 vp->v_rdev->si_usecount++;
2088 * Turn a holdcnt into a use+holdcnt such that only one call to
2089 * v_decr_usecount is needed.
2092 v_upgrade_usecount(struct vnode *vp)
2095 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2097 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2099 vp->v_rdev->si_usecount++;
2105 * Decrement the vnode use and hold count along with the driver's usecount
2106 * if this is a chardev. The vdropl() below releases the vnode interlock
2107 * as it may free the vnode.
2110 v_decr_usecount(struct vnode *vp)
2113 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2114 VNASSERT(vp->v_usecount > 0, vp,
2115 ("v_decr_usecount: negative usecount"));
2116 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2118 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2120 vp->v_rdev->si_usecount--;
2127 * Decrement only the use count and driver use count. This is intended to
2128 * be paired with a follow on vdropl() to release the remaining hold count.
2129 * In this way we may vgone() a vnode with a 0 usecount without risk of
2130 * having it end up on a free list because the hold count is kept above 0.
2133 v_decr_useonly(struct vnode *vp)
2136 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2137 VNASSERT(vp->v_usecount > 0, vp,
2138 ("v_decr_useonly: negative usecount"));
2139 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2141 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2143 vp->v_rdev->si_usecount--;
2149 * Grab a particular vnode from the free list, increment its
2150 * reference count and lock it. VI_DOOMED is set if the vnode
2151 * is being destroyed. Only callers who specify LK_RETRY will
2152 * see doomed vnodes. If inactive processing was delayed in
2153 * vput try to do it here.
2156 vget(struct vnode *vp, int flags, struct thread *td)
2161 VFS_ASSERT_GIANT(vp->v_mount);
2162 VNASSERT((flags & LK_TYPE_MASK) != 0, vp,
2163 ("vget: invalid lock operation"));
2164 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
2166 if ((flags & LK_INTERLOCK) == 0)
2169 if ((error = vn_lock(vp, flags | LK_INTERLOCK)) != 0) {
2171 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__,
2175 if (vp->v_iflag & VI_DOOMED && (flags & LK_RETRY) == 0)
2176 panic("vget: vn_lock failed to return ENOENT\n");
2178 /* Upgrade our holdcnt to a usecount. */
2179 v_upgrade_usecount(vp);
2181 * We don't guarantee that any particular close will
2182 * trigger inactive processing so just make a best effort
2183 * here at preventing a reference to a removed file. If
2184 * we don't succeed no harm is done.
2186 if (vp->v_iflag & VI_OWEINACT) {
2187 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE &&
2188 (flags & LK_NOWAIT) == 0)
2190 vp->v_iflag &= ~VI_OWEINACT;
2197 * Increase the reference count of a vnode.
2200 vref(struct vnode *vp)
2203 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2205 v_incr_usecount(vp);
2210 * Return reference count of a vnode.
2212 * The results of this call are only guaranteed when some mechanism other
2213 * than the VI lock is used to stop other processes from gaining references
2214 * to the vnode. This may be the case if the caller holds the only reference.
2215 * This is also useful when stale data is acceptable as race conditions may
2216 * be accounted for by some other means.
2219 vrefcnt(struct vnode *vp)
2224 usecnt = vp->v_usecount;
2230 #define VPUTX_VRELE 1
2231 #define VPUTX_VPUT 2
2232 #define VPUTX_VUNREF 3
2235 vputx(struct vnode *vp, int func)
2239 KASSERT(vp != NULL, ("vputx: null vp"));
2240 if (func == VPUTX_VUNREF)
2241 ASSERT_VOP_LOCKED(vp, "vunref");
2242 else if (func == VPUTX_VPUT)
2243 ASSERT_VOP_LOCKED(vp, "vput");
2245 KASSERT(func == VPUTX_VRELE, ("vputx: wrong func"));
2246 VFS_ASSERT_GIANT(vp->v_mount);
2247 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2250 /* Skip this v_writecount check if we're going to panic below. */
2251 VNASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, vp,
2252 ("vputx: missed vn_close"));
2255 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) &&
2256 vp->v_usecount == 1)) {
2257 if (func == VPUTX_VPUT)
2259 v_decr_usecount(vp);
2263 if (vp->v_usecount != 1) {
2264 vprint("vputx: negative ref count", vp);
2265 panic("vputx: negative ref cnt");
2267 CTR2(KTR_VFS, "%s: return vnode %p to the freelist", __func__, vp);
2269 * We want to hold the vnode until the inactive finishes to
2270 * prevent vgone() races. We drop the use count here and the
2271 * hold count below when we're done.
2275 * We must call VOP_INACTIVE with the node locked. Mark
2276 * as VI_DOINGINACT to avoid recursion.
2278 vp->v_iflag |= VI_OWEINACT;
2281 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
2285 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2286 error = VOP_LOCK(vp, LK_UPGRADE | LK_INTERLOCK |
2292 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
2296 if (vp->v_usecount > 0)
2297 vp->v_iflag &= ~VI_OWEINACT;
2299 if (vp->v_iflag & VI_OWEINACT)
2300 vinactive(vp, curthread);
2301 if (func != VPUTX_VUNREF)
2308 * Vnode put/release.
2309 * If count drops to zero, call inactive routine and return to freelist.
2312 vrele(struct vnode *vp)
2315 vputx(vp, VPUTX_VRELE);
2319 * Release an already locked vnode. This give the same effects as
2320 * unlock+vrele(), but takes less time and avoids releasing and
2321 * re-aquiring the lock (as vrele() acquires the lock internally.)
2324 vput(struct vnode *vp)
2327 vputx(vp, VPUTX_VPUT);
2331 * Release an exclusively locked vnode. Do not unlock the vnode lock.
2334 vunref(struct vnode *vp)
2337 vputx(vp, VPUTX_VUNREF);
2341 * Somebody doesn't want the vnode recycled.
2344 vhold(struct vnode *vp)
2353 vholdl(struct vnode *vp)
2356 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2358 if (VSHOULDBUSY(vp))
2363 * Note that there is one less who cares about this vnode. vdrop() is the
2364 * opposite of vhold().
2367 vdrop(struct vnode *vp)
2375 * Drop the hold count of the vnode. If this is the last reference to
2376 * the vnode we will free it if it has been vgone'd otherwise it is
2377 * placed on the free list.
2380 vdropl(struct vnode *vp)
2383 ASSERT_VI_LOCKED(vp, "vdropl");
2384 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2385 if (vp->v_holdcnt <= 0)
2386 panic("vdrop: holdcnt %d", vp->v_holdcnt);
2388 if (vp->v_holdcnt == 0) {
2389 if (vp->v_iflag & VI_DOOMED) {
2390 CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__,
2401 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT
2402 * flags. DOINGINACT prevents us from recursing in calls to vinactive.
2403 * OWEINACT tracks whether a vnode missed a call to inactive due to a
2404 * failed lock upgrade.
2407 vinactive(struct vnode *vp, struct thread *td)
2410 ASSERT_VOP_ELOCKED(vp, "vinactive");
2411 ASSERT_VI_LOCKED(vp, "vinactive");
2412 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
2413 ("vinactive: recursed on VI_DOINGINACT"));
2414 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2415 vp->v_iflag |= VI_DOINGINACT;
2416 vp->v_iflag &= ~VI_OWEINACT;
2418 VOP_INACTIVE(vp, td);
2420 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
2421 ("vinactive: lost VI_DOINGINACT"));
2422 vp->v_iflag &= ~VI_DOINGINACT;
2426 * Remove any vnodes in the vnode table belonging to mount point mp.
2428 * If FORCECLOSE is not specified, there should not be any active ones,
2429 * return error if any are found (nb: this is a user error, not a
2430 * system error). If FORCECLOSE is specified, detach any active vnodes
2433 * If WRITECLOSE is set, only flush out regular file vnodes open for
2436 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
2438 * `rootrefs' specifies the base reference count for the root vnode
2439 * of this filesystem. The root vnode is considered busy if its
2440 * v_usecount exceeds this value. On a successful return, vflush(, td)
2441 * will call vrele() on the root vnode exactly rootrefs times.
2442 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
2446 static int busyprt = 0; /* print out busy vnodes */
2447 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "Print out busy vnodes");
2451 vflush( struct mount *mp, int rootrefs, int flags, struct thread *td)
2453 struct vnode *vp, *mvp, *rootvp = NULL;
2455 int busy = 0, error;
2457 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp,
2460 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
2461 ("vflush: bad args"));
2463 * Get the filesystem root vnode. We can vput() it
2464 * immediately, since with rootrefs > 0, it won't go away.
2466 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) {
2467 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d",
2476 MNT_VNODE_FOREACH(vp, mp, mvp) {
2481 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE);
2485 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
2489 * Skip over a vnodes marked VV_SYSTEM.
2491 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
2498 * If WRITECLOSE is set, flush out unlinked but still open
2499 * files (even if open only for reading) and regular file
2500 * vnodes open for writing.
2502 if (flags & WRITECLOSE) {
2503 if (vp->v_object != NULL) {
2504 VM_OBJECT_LOCK(vp->v_object);
2505 vm_object_page_clean(vp->v_object, 0, 0, 0);
2506 VM_OBJECT_UNLOCK(vp->v_object);
2508 error = VOP_FSYNC(vp, MNT_WAIT, td);
2512 MNT_VNODE_FOREACH_ABORT(mp, mvp);
2515 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
2518 if ((vp->v_type == VNON ||
2519 (error == 0 && vattr.va_nlink > 0)) &&
2520 (vp->v_writecount == 0 || vp->v_type != VREG)) {
2529 * With v_usecount == 0, all we need to do is clear out the
2530 * vnode data structures and we are done.
2532 * If FORCECLOSE is set, forcibly close the vnode.
2534 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) {
2535 VNASSERT(vp->v_usecount == 0 ||
2536 (vp->v_type != VCHR && vp->v_type != VBLK), vp,
2537 ("device VNODE %p is FORCECLOSED", vp));
2543 vprint("vflush: busy vnode", vp);
2551 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
2553 * If just the root vnode is busy, and if its refcount
2554 * is equal to `rootrefs', then go ahead and kill it.
2557 KASSERT(busy > 0, ("vflush: not busy"));
2558 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp,
2559 ("vflush: usecount %d < rootrefs %d",
2560 rootvp->v_usecount, rootrefs));
2561 if (busy == 1 && rootvp->v_usecount == rootrefs) {
2562 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK);
2564 VOP_UNLOCK(rootvp, 0);
2570 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__,
2574 for (; rootrefs > 0; rootrefs--)
2580 * Recycle an unused vnode to the front of the free list.
2583 vrecycle(struct vnode *vp, struct thread *td)
2587 ASSERT_VOP_ELOCKED(vp, "vrecycle");
2588 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2591 if (vp->v_usecount == 0) {
2600 * Eliminate all activity associated with a vnode
2601 * in preparation for reuse.
2604 vgone(struct vnode *vp)
2612 * vgone, with the vp interlock held.
2615 vgonel(struct vnode *vp)
2622 ASSERT_VOP_ELOCKED(vp, "vgonel");
2623 ASSERT_VI_LOCKED(vp, "vgonel");
2624 VNASSERT(vp->v_holdcnt, vp,
2625 ("vgonel: vp %p has no reference.", vp));
2626 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2630 * Don't vgonel if we're already doomed.
2632 if (vp->v_iflag & VI_DOOMED)
2634 vp->v_iflag |= VI_DOOMED;
2636 * Check to see if the vnode is in use. If so, we have to call
2637 * VOP_CLOSE() and VOP_INACTIVE().
2639 active = vp->v_usecount;
2640 oweinact = (vp->v_iflag & VI_OWEINACT);
2643 * Clean out any buffers associated with the vnode.
2644 * If the flush fails, just toss the buffers.
2647 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd))
2648 (void) vn_start_secondary_write(vp, &mp, V_WAIT);
2649 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0)
2650 vinvalbuf(vp, 0, 0, 0);
2653 * If purging an active vnode, it must be closed and
2654 * deactivated before being reclaimed.
2657 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
2658 if (oweinact || active) {
2660 if ((vp->v_iflag & VI_DOINGINACT) == 0)
2665 * Reclaim the vnode.
2667 if (VOP_RECLAIM(vp, td))
2668 panic("vgone: cannot reclaim");
2670 vn_finished_secondary_write(mp);
2671 VNASSERT(vp->v_object == NULL, vp,
2672 ("vop_reclaim left v_object vp=%p, tag=%s", vp, vp->v_tag));
2674 * Clear the advisory locks and wake up waiting threads.
2676 lf_purgelocks(vp, &(vp->v_lockf));
2678 * Delete from old mount point vnode list.
2683 * Done with purge, reset to the standard lock and invalidate
2687 vp->v_vnlock = &vp->v_lock;
2688 vp->v_op = &dead_vnodeops;
2694 * Calculate the total number of references to a special device.
2697 vcount(struct vnode *vp)
2702 count = vp->v_rdev->si_usecount;
2708 * Same as above, but using the struct cdev *as argument
2711 count_dev(struct cdev *dev)
2716 count = dev->si_usecount;
2722 * Print out a description of a vnode.
2724 static char *typename[] =
2725 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD",
2729 vn_printf(struct vnode *vp, const char *fmt, ...)
2732 char buf[256], buf2[16];
2738 printf("%p: ", (void *)vp);
2739 printf("tag %s, type %s\n", vp->v_tag, typename[vp->v_type]);
2740 printf(" usecount %d, writecount %d, refcount %d mountedhere %p\n",
2741 vp->v_usecount, vp->v_writecount, vp->v_holdcnt, vp->v_mountedhere);
2744 if (vp->v_vflag & VV_ROOT)
2745 strlcat(buf, "|VV_ROOT", sizeof(buf));
2746 if (vp->v_vflag & VV_ISTTY)
2747 strlcat(buf, "|VV_ISTTY", sizeof(buf));
2748 if (vp->v_vflag & VV_NOSYNC)
2749 strlcat(buf, "|VV_NOSYNC", sizeof(buf));
2750 if (vp->v_vflag & VV_CACHEDLABEL)
2751 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf));
2752 if (vp->v_vflag & VV_TEXT)
2753 strlcat(buf, "|VV_TEXT", sizeof(buf));
2754 if (vp->v_vflag & VV_COPYONWRITE)
2755 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf));
2756 if (vp->v_vflag & VV_SYSTEM)
2757 strlcat(buf, "|VV_SYSTEM", sizeof(buf));
2758 if (vp->v_vflag & VV_PROCDEP)
2759 strlcat(buf, "|VV_PROCDEP", sizeof(buf));
2760 if (vp->v_vflag & VV_NOKNOTE)
2761 strlcat(buf, "|VV_NOKNOTE", sizeof(buf));
2762 if (vp->v_vflag & VV_DELETED)
2763 strlcat(buf, "|VV_DELETED", sizeof(buf));
2764 if (vp->v_vflag & VV_MD)
2765 strlcat(buf, "|VV_MD", sizeof(buf));
2766 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC |
2767 VV_CACHEDLABEL | VV_TEXT | VV_COPYONWRITE | VV_SYSTEM | VV_PROCDEP |
2768 VV_NOKNOTE | VV_DELETED | VV_MD);
2770 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags);
2771 strlcat(buf, buf2, sizeof(buf));
2773 if (vp->v_iflag & VI_MOUNT)
2774 strlcat(buf, "|VI_MOUNT", sizeof(buf));
2775 if (vp->v_iflag & VI_AGE)
2776 strlcat(buf, "|VI_AGE", sizeof(buf));
2777 if (vp->v_iflag & VI_DOOMED)
2778 strlcat(buf, "|VI_DOOMED", sizeof(buf));
2779 if (vp->v_iflag & VI_FREE)
2780 strlcat(buf, "|VI_FREE", sizeof(buf));
2781 if (vp->v_iflag & VI_DOINGINACT)
2782 strlcat(buf, "|VI_DOINGINACT", sizeof(buf));
2783 if (vp->v_iflag & VI_OWEINACT)
2784 strlcat(buf, "|VI_OWEINACT", sizeof(buf));
2785 flags = vp->v_iflag & ~(VI_MOUNT | VI_AGE | VI_DOOMED | VI_FREE |
2786 VI_DOINGINACT | VI_OWEINACT);
2788 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags);
2789 strlcat(buf, buf2, sizeof(buf));
2791 printf(" flags (%s)\n", buf + 1);
2792 if (mtx_owned(VI_MTX(vp)))
2793 printf(" VI_LOCKed");
2794 if (vp->v_object != NULL)
2795 printf(" v_object %p ref %d pages %d\n",
2796 vp->v_object, vp->v_object->ref_count,
2797 vp->v_object->resident_page_count);
2799 lockmgr_printinfo(vp->v_vnlock);
2800 if (vp->v_data != NULL)
2806 * List all of the locked vnodes in the system.
2807 * Called when debugging the kernel.
2809 DB_SHOW_COMMAND(lockedvnods, lockedvnodes)
2811 struct mount *mp, *nmp;
2815 * Note: because this is DDB, we can't obey the locking semantics
2816 * for these structures, which means we could catch an inconsistent
2817 * state and dereference a nasty pointer. Not much to be done
2820 db_printf("Locked vnodes\n");
2821 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2822 nmp = TAILQ_NEXT(mp, mnt_list);
2823 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
2824 if (vp->v_type != VMARKER &&
2828 nmp = TAILQ_NEXT(mp, mnt_list);
2833 * Show details about the given vnode.
2835 DB_SHOW_COMMAND(vnode, db_show_vnode)
2841 vp = (struct vnode *)addr;
2842 vn_printf(vp, "vnode ");
2846 * Show details about the given mount point.
2848 DB_SHOW_COMMAND(mount, db_show_mount)
2858 /* No address given, print short info about all mount points. */
2859 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2860 db_printf("%p %s on %s (%s)\n", mp,
2861 mp->mnt_stat.f_mntfromname,
2862 mp->mnt_stat.f_mntonname,
2863 mp->mnt_stat.f_fstypename);
2867 db_printf("\nMore info: show mount <addr>\n");
2871 mp = (struct mount *)addr;
2872 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname,
2873 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename);
2876 flags = mp->mnt_flag;
2877 #define MNT_FLAG(flag) do { \
2878 if (flags & (flag)) { \
2879 if (buf[0] != '\0') \
2880 strlcat(buf, ", ", sizeof(buf)); \
2881 strlcat(buf, (#flag) + 4, sizeof(buf)); \
2885 MNT_FLAG(MNT_RDONLY);
2886 MNT_FLAG(MNT_SYNCHRONOUS);
2887 MNT_FLAG(MNT_NOEXEC);
2888 MNT_FLAG(MNT_NOSUID);
2889 MNT_FLAG(MNT_UNION);
2890 MNT_FLAG(MNT_ASYNC);
2891 MNT_FLAG(MNT_SUIDDIR);
2892 MNT_FLAG(MNT_SOFTDEP);
2893 MNT_FLAG(MNT_NOSYMFOLLOW);
2894 MNT_FLAG(MNT_GJOURNAL);
2895 MNT_FLAG(MNT_MULTILABEL);
2897 MNT_FLAG(MNT_NOATIME);
2898 MNT_FLAG(MNT_NOCLUSTERR);
2899 MNT_FLAG(MNT_NOCLUSTERW);
2900 MNT_FLAG(MNT_NFS4ACLS);
2901 MNT_FLAG(MNT_EXRDONLY);
2902 MNT_FLAG(MNT_EXPORTED);
2903 MNT_FLAG(MNT_DEFEXPORTED);
2904 MNT_FLAG(MNT_EXPORTANON);
2905 MNT_FLAG(MNT_EXKERB);
2906 MNT_FLAG(MNT_EXPUBLIC);
2907 MNT_FLAG(MNT_LOCAL);
2908 MNT_FLAG(MNT_QUOTA);
2909 MNT_FLAG(MNT_ROOTFS);
2911 MNT_FLAG(MNT_IGNORE);
2912 MNT_FLAG(MNT_UPDATE);
2913 MNT_FLAG(MNT_DELEXPORT);
2914 MNT_FLAG(MNT_RELOAD);
2915 MNT_FLAG(MNT_FORCE);
2916 MNT_FLAG(MNT_SNAPSHOT);
2917 MNT_FLAG(MNT_BYFSID);
2921 strlcat(buf, ", ", sizeof(buf));
2922 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
2925 db_printf(" mnt_flag = %s\n", buf);
2928 flags = mp->mnt_kern_flag;
2929 #define MNT_KERN_FLAG(flag) do { \
2930 if (flags & (flag)) { \
2931 if (buf[0] != '\0') \
2932 strlcat(buf, ", ", sizeof(buf)); \
2933 strlcat(buf, (#flag) + 5, sizeof(buf)); \
2937 MNT_KERN_FLAG(MNTK_UNMOUNTF);
2938 MNT_KERN_FLAG(MNTK_ASYNC);
2939 MNT_KERN_FLAG(MNTK_SOFTDEP);
2940 MNT_KERN_FLAG(MNTK_NOINSMNTQ);
2941 MNT_KERN_FLAG(MNTK_UNMOUNT);
2942 MNT_KERN_FLAG(MNTK_MWAIT);
2943 MNT_KERN_FLAG(MNTK_SUSPEND);
2944 MNT_KERN_FLAG(MNTK_SUSPEND2);
2945 MNT_KERN_FLAG(MNTK_SUSPENDED);
2946 MNT_KERN_FLAG(MNTK_MPSAFE);
2947 MNT_KERN_FLAG(MNTK_NOKNOTE);
2948 MNT_KERN_FLAG(MNTK_LOOKUP_SHARED);
2949 #undef MNT_KERN_FLAG
2952 strlcat(buf, ", ", sizeof(buf));
2953 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
2956 db_printf(" mnt_kern_flag = %s\n", buf);
2958 db_printf(" mnt_opt = ");
2959 opt = TAILQ_FIRST(mp->mnt_opt);
2961 db_printf("%s", opt->name);
2962 opt = TAILQ_NEXT(opt, link);
2963 while (opt != NULL) {
2964 db_printf(", %s", opt->name);
2965 opt = TAILQ_NEXT(opt, link);
2971 db_printf(" mnt_stat = { version=%u type=%u flags=0x%016jx "
2972 "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju "
2973 "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju "
2974 "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n",
2975 (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags,
2976 (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize,
2977 (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree,
2978 (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files,
2979 (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites,
2980 (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads,
2981 (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax,
2982 (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]);
2984 db_printf(" mnt_cred = { uid=%u ruid=%u",
2985 (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid);
2986 if (jailed(mp->mnt_cred))
2987 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id);
2989 db_printf(" mnt_ref = %d\n", mp->mnt_ref);
2990 db_printf(" mnt_gen = %d\n", mp->mnt_gen);
2991 db_printf(" mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize);
2992 db_printf(" mnt_writeopcount = %d\n", mp->mnt_writeopcount);
2993 db_printf(" mnt_noasync = %u\n", mp->mnt_noasync);
2994 db_printf(" mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen);
2995 db_printf(" mnt_iosize_max = %d\n", mp->mnt_iosize_max);
2996 db_printf(" mnt_hashseed = %u\n", mp->mnt_hashseed);
2997 db_printf(" mnt_secondary_writes = %d\n", mp->mnt_secondary_writes);
2998 db_printf(" mnt_secondary_accwrites = %d\n",
2999 mp->mnt_secondary_accwrites);
3000 db_printf(" mnt_gjprovider = %s\n",
3001 mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL");
3004 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3005 if (vp->v_type != VMARKER) {
3006 vn_printf(vp, "vnode ");
3015 * Fill in a struct xvfsconf based on a struct vfsconf.
3018 vfsconf2x(struct vfsconf *vfsp, struct xvfsconf *xvfsp)
3021 strcpy(xvfsp->vfc_name, vfsp->vfc_name);
3022 xvfsp->vfc_typenum = vfsp->vfc_typenum;
3023 xvfsp->vfc_refcount = vfsp->vfc_refcount;
3024 xvfsp->vfc_flags = vfsp->vfc_flags;
3026 * These are unused in userland, we keep them
3027 * to not break binary compatibility.
3029 xvfsp->vfc_vfsops = NULL;
3030 xvfsp->vfc_next = NULL;
3034 * Top level filesystem related information gathering.
3037 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS)
3039 struct vfsconf *vfsp;
3040 struct xvfsconf xvfsp;
3044 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3045 bzero(&xvfsp, sizeof(xvfsp));
3046 vfsconf2x(vfsp, &xvfsp);
3047 error = SYSCTL_OUT(req, &xvfsp, sizeof xvfsp);
3054 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLFLAG_RD, NULL, 0, sysctl_vfs_conflist,
3055 "S,xvfsconf", "List of all configured filesystems");
3057 #ifndef BURN_BRIDGES
3058 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS);
3061 vfs_sysctl(SYSCTL_HANDLER_ARGS)
3063 int *name = (int *)arg1 - 1; /* XXX */
3064 u_int namelen = arg2 + 1; /* XXX */
3065 struct vfsconf *vfsp;
3066 struct xvfsconf xvfsp;
3068 printf("WARNING: userland calling deprecated sysctl, "
3069 "please rebuild world\n");
3071 #if 1 || defined(COMPAT_PRELITE2)
3072 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
3074 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
3078 case VFS_MAXTYPENUM:
3081 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
3084 return (ENOTDIR); /* overloaded */
3085 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list)
3086 if (vfsp->vfc_typenum == name[2])
3089 return (EOPNOTSUPP);
3090 bzero(&xvfsp, sizeof(xvfsp));
3091 vfsconf2x(vfsp, &xvfsp);
3092 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3094 return (EOPNOTSUPP);
3097 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP,
3098 vfs_sysctl, "Generic filesystem");
3100 #if 1 || defined(COMPAT_PRELITE2)
3103 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
3106 struct vfsconf *vfsp;
3107 struct ovfsconf ovfs;
3109 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3110 bzero(&ovfs, sizeof(ovfs));
3111 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
3112 strcpy(ovfs.vfc_name, vfsp->vfc_name);
3113 ovfs.vfc_index = vfsp->vfc_typenum;
3114 ovfs.vfc_refcount = vfsp->vfc_refcount;
3115 ovfs.vfc_flags = vfsp->vfc_flags;
3116 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
3123 #endif /* 1 || COMPAT_PRELITE2 */
3124 #endif /* !BURN_BRIDGES */
3126 #define KINFO_VNODESLOP 10
3129 * Dump vnode list (via sysctl).
3133 sysctl_vnode(SYSCTL_HANDLER_ARGS)
3141 * Stale numvnodes access is not fatal here.
3144 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn;
3146 /* Make an estimate */
3147 return (SYSCTL_OUT(req, 0, len));
3149 error = sysctl_wire_old_buffer(req, 0);
3152 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK);
3154 mtx_lock(&mountlist_mtx);
3155 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3156 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK))
3159 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3163 xvn[n].xv_size = sizeof *xvn;
3164 xvn[n].xv_vnode = vp;
3165 xvn[n].xv_id = 0; /* XXX compat */
3166 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field
3168 XV_COPY(writecount);
3174 xvn[n].xv_flag = vp->v_vflag;
3176 switch (vp->v_type) {
3183 if (vp->v_rdev == NULL) {
3187 xvn[n].xv_dev = dev2udev(vp->v_rdev);
3190 xvn[n].xv_socket = vp->v_socket;
3193 xvn[n].xv_fifo = vp->v_fifoinfo;
3198 /* shouldn't happen? */
3206 mtx_lock(&mountlist_mtx);
3211 mtx_unlock(&mountlist_mtx);
3213 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn);
3218 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD,
3219 0, 0, sysctl_vnode, "S,xvnode", "");
3223 * Unmount all filesystems. The list is traversed in reverse order
3224 * of mounting to avoid dependencies.
3227 vfs_unmountall(void)
3233 KASSERT(curthread != NULL, ("vfs_unmountall: NULL curthread"));
3234 CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__);
3238 * Since this only runs when rebooting, it is not interlocked.
3240 while(!TAILQ_EMPTY(&mountlist)) {
3241 mp = TAILQ_LAST(&mountlist, mntlist);
3242 error = dounmount(mp, MNT_FORCE, td);
3244 TAILQ_REMOVE(&mountlist, mp, mnt_list);
3246 * XXX: Due to the way in which we mount the root
3247 * file system off of devfs, devfs will generate a
3248 * "busy" warning when we try to unmount it before
3249 * the root. Don't print a warning as a result in
3250 * order to avoid false positive errors that may
3251 * cause needless upset.
3253 if (strcmp(mp->mnt_vfc->vfc_name, "devfs") != 0) {
3254 printf("unmount of %s failed (",
3255 mp->mnt_stat.f_mntonname);
3259 printf("%d)\n", error);
3262 /* The unmount has removed mp from the mountlist */
3268 * perform msync on all vnodes under a mount point
3269 * the mount point must be locked.
3272 vfs_msync(struct mount *mp, int flags)
3274 struct vnode *vp, *mvp;
3275 struct vm_object *obj;
3277 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
3279 MNT_VNODE_FOREACH(vp, mp, mvp) {
3282 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0 &&
3283 (flags == MNT_WAIT || VOP_ISLOCKED(vp) == 0)) {
3286 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK,
3288 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */
3296 VM_OBJECT_LOCK(obj);
3297 vm_object_page_clean(obj, 0, 0,
3299 OBJPC_SYNC : OBJPC_NOSYNC);
3300 VM_OBJECT_UNLOCK(obj);
3312 * Mark a vnode as free, putting it up for recycling.
3315 vfree(struct vnode *vp)
3318 ASSERT_VI_LOCKED(vp, "vfree");
3319 mtx_lock(&vnode_free_list_mtx);
3320 VNASSERT(vp->v_op != NULL, vp, ("vfree: vnode already reclaimed."));
3321 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp, ("vnode already free"));
3322 VNASSERT(VSHOULDFREE(vp), vp, ("vfree: freeing when we shouldn't"));
3323 VNASSERT((vp->v_iflag & VI_DOOMED) == 0, vp,
3324 ("vfree: Freeing doomed vnode"));
3325 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3326 if (vp->v_iflag & VI_AGE) {
3327 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
3329 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
3332 vp->v_iflag &= ~VI_AGE;
3333 vp->v_iflag |= VI_FREE;
3334 mtx_unlock(&vnode_free_list_mtx);
3338 * Opposite of vfree() - mark a vnode as in use.
3341 vbusy(struct vnode *vp)
3343 ASSERT_VI_LOCKED(vp, "vbusy");
3344 VNASSERT((vp->v_iflag & VI_FREE) != 0, vp, ("vnode not free"));
3345 VNASSERT(vp->v_op != NULL, vp, ("vbusy: vnode already reclaimed."));
3346 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3348 mtx_lock(&vnode_free_list_mtx);
3349 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
3351 vp->v_iflag &= ~(VI_FREE|VI_AGE);
3352 mtx_unlock(&vnode_free_list_mtx);
3356 destroy_vpollinfo(struct vpollinfo *vi)
3358 seldrain(&vi->vpi_selinfo);
3359 knlist_destroy(&vi->vpi_selinfo.si_note);
3360 mtx_destroy(&vi->vpi_lock);
3361 uma_zfree(vnodepoll_zone, vi);
3365 * Initalize per-vnode helper structure to hold poll-related state.
3368 v_addpollinfo(struct vnode *vp)
3370 struct vpollinfo *vi;
3372 if (vp->v_pollinfo != NULL)
3374 vi = uma_zalloc(vnodepoll_zone, M_WAITOK);
3375 mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF);
3376 knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock,
3377 vfs_knlunlock, vfs_knl_assert_locked, vfs_knl_assert_unlocked);
3379 if (vp->v_pollinfo != NULL) {
3381 destroy_vpollinfo(vi);
3384 vp->v_pollinfo = vi;
3389 * Record a process's interest in events which might happen to
3390 * a vnode. Because poll uses the historic select-style interface
3391 * internally, this routine serves as both the ``check for any
3392 * pending events'' and the ``record my interest in future events''
3393 * functions. (These are done together, while the lock is held,
3394 * to avoid race conditions.)
3397 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
3401 mtx_lock(&vp->v_pollinfo->vpi_lock);
3402 if (vp->v_pollinfo->vpi_revents & events) {
3404 * This leaves events we are not interested
3405 * in available for the other process which
3406 * which presumably had requested them
3407 * (otherwise they would never have been
3410 events &= vp->v_pollinfo->vpi_revents;
3411 vp->v_pollinfo->vpi_revents &= ~events;
3413 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3416 vp->v_pollinfo->vpi_events |= events;
3417 selrecord(td, &vp->v_pollinfo->vpi_selinfo);
3418 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3423 * Routine to create and manage a filesystem syncer vnode.
3425 #define sync_close ((int (*)(struct vop_close_args *))nullop)
3426 static int sync_fsync(struct vop_fsync_args *);
3427 static int sync_inactive(struct vop_inactive_args *);
3428 static int sync_reclaim(struct vop_reclaim_args *);
3430 static struct vop_vector sync_vnodeops = {
3431 .vop_bypass = VOP_EOPNOTSUPP,
3432 .vop_close = sync_close, /* close */
3433 .vop_fsync = sync_fsync, /* fsync */
3434 .vop_inactive = sync_inactive, /* inactive */
3435 .vop_reclaim = sync_reclaim, /* reclaim */
3436 .vop_lock1 = vop_stdlock, /* lock */
3437 .vop_unlock = vop_stdunlock, /* unlock */
3438 .vop_islocked = vop_stdislocked, /* islocked */
3442 * Create a new filesystem syncer vnode for the specified mount point.
3445 vfs_allocate_syncvnode(struct mount *mp)
3449 static long start, incr, next;
3452 /* Allocate a new vnode */
3453 if ((error = getnewvnode("syncer", mp, &sync_vnodeops, &vp)) != 0) {
3454 mp->mnt_syncer = NULL;
3458 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3459 vp->v_vflag |= VV_FORCEINSMQ;
3460 error = insmntque(vp, mp);
3462 panic("vfs_allocate_syncvnode: insmntque failed");
3463 vp->v_vflag &= ~VV_FORCEINSMQ;
3466 * Place the vnode onto the syncer worklist. We attempt to
3467 * scatter them about on the list so that they will go off
3468 * at evenly distributed times even if all the filesystems
3469 * are mounted at once.
3472 if (next == 0 || next > syncer_maxdelay) {
3476 start = syncer_maxdelay / 2;
3477 incr = syncer_maxdelay;
3483 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0);
3484 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */
3485 mtx_lock(&sync_mtx);
3487 mtx_unlock(&sync_mtx);
3489 mp->mnt_syncer = vp;
3494 * Do a lazy sync of the filesystem.
3497 sync_fsync(struct vop_fsync_args *ap)
3499 struct vnode *syncvp = ap->a_vp;
3500 struct mount *mp = syncvp->v_mount;
3505 * We only need to do something if this is a lazy evaluation.
3507 if (ap->a_waitfor != MNT_LAZY)
3511 * Move ourselves to the back of the sync list.
3513 bo = &syncvp->v_bufobj;
3515 vn_syncer_add_to_worklist(bo, syncdelay);
3519 * Walk the list of vnodes pushing all that are dirty and
3520 * not already on the sync list.
3522 mtx_lock(&mountlist_mtx);
3523 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
3524 mtx_unlock(&mountlist_mtx);
3527 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) {
3531 save = curthread_pflags_set(TDP_SYNCIO);
3532 vfs_msync(mp, MNT_NOWAIT);
3533 error = VFS_SYNC(mp, MNT_LAZY);
3534 curthread_pflags_restore(save);
3535 vn_finished_write(mp);
3541 * The syncer vnode is no referenced.
3544 sync_inactive(struct vop_inactive_args *ap)
3552 * The syncer vnode is no longer needed and is being decommissioned.
3554 * Modifications to the worklist must be protected by sync_mtx.
3557 sync_reclaim(struct vop_reclaim_args *ap)
3559 struct vnode *vp = ap->a_vp;
3564 vp->v_mount->mnt_syncer = NULL;
3565 if (bo->bo_flag & BO_ONWORKLST) {
3566 mtx_lock(&sync_mtx);
3567 LIST_REMOVE(bo, bo_synclist);
3568 syncer_worklist_len--;
3570 mtx_unlock(&sync_mtx);
3571 bo->bo_flag &= ~BO_ONWORKLST;
3579 * Check if vnode represents a disk device
3582 vn_isdisk(struct vnode *vp, int *errp)
3588 if (vp->v_type != VCHR)
3590 else if (vp->v_rdev == NULL)
3592 else if (vp->v_rdev->si_devsw == NULL)
3594 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK))
3599 return (error == 0);
3603 * Common filesystem object access control check routine. Accepts a
3604 * vnode's type, "mode", uid and gid, requested access mode, credentials,
3605 * and optional call-by-reference privused argument allowing vaccess()
3606 * to indicate to the caller whether privilege was used to satisfy the
3607 * request (obsoleted). Returns 0 on success, or an errno on failure.
3610 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid,
3611 accmode_t accmode, struct ucred *cred, int *privused)
3613 accmode_t dac_granted;
3614 accmode_t priv_granted;
3616 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0,
3617 ("invalid bit in accmode"));
3620 * Look for a normal, non-privileged way to access the file/directory
3621 * as requested. If it exists, go with that.
3624 if (privused != NULL)
3629 /* Check the owner. */
3630 if (cred->cr_uid == file_uid) {
3631 dac_granted |= VADMIN;
3632 if (file_mode & S_IXUSR)
3633 dac_granted |= VEXEC;
3634 if (file_mode & S_IRUSR)
3635 dac_granted |= VREAD;
3636 if (file_mode & S_IWUSR)
3637 dac_granted |= (VWRITE | VAPPEND);
3639 if ((accmode & dac_granted) == accmode)
3645 /* Otherwise, check the groups (first match) */
3646 if (groupmember(file_gid, cred)) {
3647 if (file_mode & S_IXGRP)
3648 dac_granted |= VEXEC;
3649 if (file_mode & S_IRGRP)
3650 dac_granted |= VREAD;
3651 if (file_mode & S_IWGRP)
3652 dac_granted |= (VWRITE | VAPPEND);
3654 if ((accmode & dac_granted) == accmode)
3660 /* Otherwise, check everyone else. */
3661 if (file_mode & S_IXOTH)
3662 dac_granted |= VEXEC;
3663 if (file_mode & S_IROTH)
3664 dac_granted |= VREAD;
3665 if (file_mode & S_IWOTH)
3666 dac_granted |= (VWRITE | VAPPEND);
3667 if ((accmode & dac_granted) == accmode)
3672 * Build a privilege mask to determine if the set of privileges
3673 * satisfies the requirements when combined with the granted mask
3674 * from above. For each privilege, if the privilege is required,
3675 * bitwise or the request type onto the priv_granted mask.
3681 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC
3682 * requests, instead of PRIV_VFS_EXEC.
3684 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3685 !priv_check_cred(cred, PRIV_VFS_LOOKUP, 0))
3686 priv_granted |= VEXEC;
3688 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3689 !priv_check_cred(cred, PRIV_VFS_EXEC, 0))
3690 priv_granted |= VEXEC;
3693 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) &&
3694 !priv_check_cred(cred, PRIV_VFS_READ, 0))
3695 priv_granted |= VREAD;
3697 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) &&
3698 !priv_check_cred(cred, PRIV_VFS_WRITE, 0))
3699 priv_granted |= (VWRITE | VAPPEND);
3701 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) &&
3702 !priv_check_cred(cred, PRIV_VFS_ADMIN, 0))
3703 priv_granted |= VADMIN;
3705 if ((accmode & (priv_granted | dac_granted)) == accmode) {
3706 /* XXX audit: privilege used */
3707 if (privused != NULL)
3712 return ((accmode & VADMIN) ? EPERM : EACCES);
3716 * Credential check based on process requesting service, and per-attribute
3720 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred,
3721 struct thread *td, accmode_t accmode)
3725 * Kernel-invoked always succeeds.
3731 * Do not allow privileged processes in jail to directly manipulate
3732 * system attributes.
3734 switch (attrnamespace) {
3735 case EXTATTR_NAMESPACE_SYSTEM:
3736 /* Potentially should be: return (EPERM); */
3737 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM, 0));
3738 case EXTATTR_NAMESPACE_USER:
3739 return (VOP_ACCESS(vp, accmode, cred, td));
3745 #ifdef DEBUG_VFS_LOCKS
3747 * This only exists to supress warnings from unlocked specfs accesses. It is
3748 * no longer ok to have an unlocked VFS.
3750 #define IGNORE_LOCK(vp) (panicstr != NULL || (vp) == NULL || \
3751 (vp)->v_type == VCHR || (vp)->v_type == VBAD)
3753 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */
3754 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0,
3755 "Drop into debugger on lock violation");
3757 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */
3758 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex,
3759 0, "Check for interlock across VOPs");
3761 int vfs_badlock_print = 1; /* Print lock violations. */
3762 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print,
3763 0, "Print lock violations");
3766 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */
3767 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW,
3768 &vfs_badlock_backtrace, 0, "Print backtrace at lock violations");
3772 vfs_badlock(const char *msg, const char *str, struct vnode *vp)
3776 if (vfs_badlock_backtrace)
3779 if (vfs_badlock_print)
3780 printf("%s: %p %s\n", str, (void *)vp, msg);
3781 if (vfs_badlock_ddb)
3782 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
3786 assert_vi_locked(struct vnode *vp, const char *str)
3789 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp)))
3790 vfs_badlock("interlock is not locked but should be", str, vp);
3794 assert_vi_unlocked(struct vnode *vp, const char *str)
3797 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp)))
3798 vfs_badlock("interlock is locked but should not be", str, vp);
3802 assert_vop_locked(struct vnode *vp, const char *str)
3805 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == 0)
3806 vfs_badlock("is not locked but should be", str, vp);
3810 assert_vop_unlocked(struct vnode *vp, const char *str)
3813 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
3814 vfs_badlock("is locked but should not be", str, vp);
3818 assert_vop_elocked(struct vnode *vp, const char *str)
3821 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
3822 vfs_badlock("is not exclusive locked but should be", str, vp);
3827 assert_vop_elocked_other(struct vnode *vp, const char *str)
3830 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLOTHER)
3831 vfs_badlock("is not exclusive locked by another thread",
3836 assert_vop_slocked(struct vnode *vp, const char *str)
3839 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_SHARED)
3840 vfs_badlock("is not locked shared but should be", str, vp);
3843 #endif /* DEBUG_VFS_LOCKS */
3846 vop_rename_fail(struct vop_rename_args *ap)
3849 if (ap->a_tvp != NULL)
3851 if (ap->a_tdvp == ap->a_tvp)
3860 vop_rename_pre(void *ap)
3862 struct vop_rename_args *a = ap;
3864 #ifdef DEBUG_VFS_LOCKS
3866 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME");
3867 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME");
3868 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME");
3869 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME");
3871 /* Check the source (from). */
3872 if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock &&
3873 (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock))
3874 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked");
3875 if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock)
3876 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked");
3878 /* Check the target. */
3880 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked");
3881 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked");
3883 if (a->a_tdvp != a->a_fdvp)
3885 if (a->a_tvp != a->a_fvp)
3893 vop_strategy_pre(void *ap)
3895 #ifdef DEBUG_VFS_LOCKS
3896 struct vop_strategy_args *a;
3903 * Cluster ops lock their component buffers but not the IO container.
3905 if ((bp->b_flags & B_CLUSTER) != 0)
3908 if (panicstr == NULL && !BUF_ISLOCKED(bp)) {
3909 if (vfs_badlock_print)
3911 "VOP_STRATEGY: bp is not locked but should be\n");
3912 if (vfs_badlock_ddb)
3913 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
3919 vop_lookup_pre(void *ap)
3921 #ifdef DEBUG_VFS_LOCKS
3922 struct vop_lookup_args *a;
3927 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP");
3928 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP");
3933 vop_lookup_post(void *ap, int rc)
3935 #ifdef DEBUG_VFS_LOCKS
3936 struct vop_lookup_args *a;
3944 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP");
3945 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP");
3948 ASSERT_VOP_LOCKED(vp, "VOP_LOOKUP (child)");
3953 vop_lock_pre(void *ap)
3955 #ifdef DEBUG_VFS_LOCKS
3956 struct vop_lock1_args *a = ap;
3958 if ((a->a_flags & LK_INTERLOCK) == 0)
3959 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
3961 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK");
3966 vop_lock_post(void *ap, int rc)
3968 #ifdef DEBUG_VFS_LOCKS
3969 struct vop_lock1_args *a = ap;
3971 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
3973 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK");
3978 vop_unlock_pre(void *ap)
3980 #ifdef DEBUG_VFS_LOCKS
3981 struct vop_unlock_args *a = ap;
3983 if (a->a_flags & LK_INTERLOCK)
3984 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK");
3985 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK");
3990 vop_unlock_post(void *ap, int rc)
3992 #ifdef DEBUG_VFS_LOCKS
3993 struct vop_unlock_args *a = ap;
3995 if (a->a_flags & LK_INTERLOCK)
3996 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK");
4001 vop_create_post(void *ap, int rc)
4003 struct vop_create_args *a = ap;
4006 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4010 vop_deleteextattr_post(void *ap, int rc)
4012 struct vop_deleteextattr_args *a = ap;
4015 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4019 vop_link_post(void *ap, int rc)
4021 struct vop_link_args *a = ap;
4024 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK);
4025 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE);
4030 vop_mkdir_post(void *ap, int rc)
4032 struct vop_mkdir_args *a = ap;
4035 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4039 vop_mknod_post(void *ap, int rc)
4041 struct vop_mknod_args *a = ap;
4044 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4048 vop_remove_post(void *ap, int rc)
4050 struct vop_remove_args *a = ap;
4053 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4054 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4059 vop_rename_post(void *ap, int rc)
4061 struct vop_rename_args *a = ap;
4064 VFS_KNOTE_UNLOCKED(a->a_fdvp, NOTE_WRITE);
4065 VFS_KNOTE_UNLOCKED(a->a_tdvp, NOTE_WRITE);
4066 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME);
4068 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE);
4070 if (a->a_tdvp != a->a_fdvp)
4072 if (a->a_tvp != a->a_fvp)
4080 vop_rmdir_post(void *ap, int rc)
4082 struct vop_rmdir_args *a = ap;
4085 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4086 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4091 vop_setattr_post(void *ap, int rc)
4093 struct vop_setattr_args *a = ap;
4096 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4100 vop_setextattr_post(void *ap, int rc)
4102 struct vop_setextattr_args *a = ap;
4105 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4109 vop_symlink_post(void *ap, int rc)
4111 struct vop_symlink_args *a = ap;
4114 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4117 static struct knlist fs_knlist;
4120 vfs_event_init(void *arg)
4122 knlist_init_mtx(&fs_knlist, NULL);
4124 /* XXX - correct order? */
4125 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL);
4128 vfs_event_signal(fsid_t *fsid, u_int32_t event, intptr_t data __unused)
4131 KNOTE_UNLOCKED(&fs_knlist, event);
4134 static int filt_fsattach(struct knote *kn);
4135 static void filt_fsdetach(struct knote *kn);
4136 static int filt_fsevent(struct knote *kn, long hint);
4138 struct filterops fs_filtops =
4139 { 0, filt_fsattach, filt_fsdetach, filt_fsevent };
4142 filt_fsattach(struct knote *kn)
4145 kn->kn_flags |= EV_CLEAR;
4146 knlist_add(&fs_knlist, kn, 0);
4151 filt_fsdetach(struct knote *kn)
4154 knlist_remove(&fs_knlist, kn, 0);
4158 filt_fsevent(struct knote *kn, long hint)
4161 kn->kn_fflags |= hint;
4162 return (kn->kn_fflags != 0);
4166 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS)
4172 error = SYSCTL_IN(req, &vc, sizeof(vc));
4175 if (vc.vc_vers != VFS_CTL_VERS1)
4177 mp = vfs_getvfs(&vc.vc_fsid);
4180 /* ensure that a specific sysctl goes to the right filesystem. */
4181 if (strcmp(vc.vc_fstypename, "*") != 0 &&
4182 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) {
4186 VCTLTOREQ(&vc, req);
4187 error = VFS_SYSCTL(mp, vc.vc_op, req);
4192 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLFLAG_WR, NULL, 0, sysctl_vfs_ctl, "",
4196 * Function to initialize a va_filerev field sensibly.
4197 * XXX: Wouldn't a random number make a lot more sense ??
4200 init_va_filerev(void)
4205 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL));
4208 static int filt_vfsread(struct knote *kn, long hint);
4209 static int filt_vfswrite(struct knote *kn, long hint);
4210 static int filt_vfsvnode(struct knote *kn, long hint);
4211 static void filt_vfsdetach(struct knote *kn);
4212 static struct filterops vfsread_filtops =
4213 { 1, NULL, filt_vfsdetach, filt_vfsread };
4214 static struct filterops vfswrite_filtops =
4215 { 1, NULL, filt_vfsdetach, filt_vfswrite };
4216 static struct filterops vfsvnode_filtops =
4217 { 1, NULL, filt_vfsdetach, filt_vfsvnode };
4220 vfs_knllock(void *arg)
4222 struct vnode *vp = arg;
4224 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4228 vfs_knlunlock(void *arg)
4230 struct vnode *vp = arg;
4236 vfs_knl_assert_locked(void *arg)
4238 #ifdef DEBUG_VFS_LOCKS
4239 struct vnode *vp = arg;
4241 ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked");
4246 vfs_knl_assert_unlocked(void *arg)
4248 #ifdef DEBUG_VFS_LOCKS
4249 struct vnode *vp = arg;
4251 ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked");
4256 vfs_kqfilter(struct vop_kqfilter_args *ap)
4258 struct vnode *vp = ap->a_vp;
4259 struct knote *kn = ap->a_kn;
4262 switch (kn->kn_filter) {
4264 kn->kn_fop = &vfsread_filtops;
4267 kn->kn_fop = &vfswrite_filtops;
4270 kn->kn_fop = &vfsvnode_filtops;
4276 kn->kn_hook = (caddr_t)vp;
4279 if (vp->v_pollinfo == NULL)
4281 knl = &vp->v_pollinfo->vpi_selinfo.si_note;
4282 knlist_add(knl, kn, 0);
4288 * Detach knote from vnode
4291 filt_vfsdetach(struct knote *kn)
4293 struct vnode *vp = (struct vnode *)kn->kn_hook;
4295 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo"));
4296 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
4301 filt_vfsread(struct knote *kn, long hint)
4303 struct vnode *vp = (struct vnode *)kn->kn_hook;
4308 * filesystem is gone, so set the EOF flag and schedule
4309 * the knote for deletion.
4311 if (hint == NOTE_REVOKE) {
4313 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4318 if (VOP_GETATTR(vp, &va, curthread->td_ucred))
4322 kn->kn_data = va.va_size - kn->kn_fp->f_offset;
4323 res = (kn->kn_data != 0);
4330 filt_vfswrite(struct knote *kn, long hint)
4332 struct vnode *vp = (struct vnode *)kn->kn_hook;
4337 * filesystem is gone, so set the EOF flag and schedule
4338 * the knote for deletion.
4340 if (hint == NOTE_REVOKE)
4341 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4349 filt_vfsvnode(struct knote *kn, long hint)
4351 struct vnode *vp = (struct vnode *)kn->kn_hook;
4355 if (kn->kn_sfflags & hint)
4356 kn->kn_fflags |= hint;
4357 if (hint == NOTE_REVOKE) {
4358 kn->kn_flags |= EV_EOF;
4362 res = (kn->kn_fflags != 0);
4368 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off)
4372 if (dp->d_reclen > ap->a_uio->uio_resid)
4373 return (ENAMETOOLONG);
4374 error = uiomove(dp, dp->d_reclen, ap->a_uio);
4376 if (ap->a_ncookies != NULL) {
4377 if (ap->a_cookies != NULL)
4378 free(ap->a_cookies, M_TEMP);
4379 ap->a_cookies = NULL;
4380 *ap->a_ncookies = 0;
4384 if (ap->a_ncookies == NULL)
4387 KASSERT(ap->a_cookies,
4388 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!"));
4390 *ap->a_cookies = realloc(*ap->a_cookies,
4391 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO);
4392 (*ap->a_cookies)[*ap->a_ncookies] = off;
4397 * Mark for update the access time of the file if the filesystem
4398 * supports VOP_MARKATIME. This functionality is used by execve and
4399 * mmap, so we want to avoid the I/O implied by directly setting
4400 * va_atime for the sake of efficiency.
4403 vfs_mark_atime(struct vnode *vp, struct ucred *cred)
4408 VFS_ASSERT_GIANT(mp);
4409 ASSERT_VOP_LOCKED(vp, "vfs_mark_atime");
4410 if (mp != NULL && (mp->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
4411 (void)VOP_MARKATIME(vp);
4415 * The purpose of this routine is to remove granularity from accmode_t,
4416 * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE,
4417 * VADMIN and VAPPEND.
4419 * If it returns 0, the caller is supposed to continue with the usual
4420 * access checks using 'accmode' as modified by this routine. If it
4421 * returns nonzero value, the caller is supposed to return that value
4424 * Note that after this routine runs, accmode may be zero.
4427 vfs_unixify_accmode(accmode_t *accmode)
4430 * There is no way to specify explicit "deny" rule using
4431 * file mode or POSIX.1e ACLs.
4433 if (*accmode & VEXPLICIT_DENY) {
4439 * None of these can be translated into usual access bits.
4440 * Also, the common case for NFSv4 ACLs is to not contain
4441 * either of these bits. Caller should check for VWRITE
4442 * on the containing directory instead.
4444 if (*accmode & (VDELETE_CHILD | VDELETE))
4447 if (*accmode & VADMIN_PERMS) {
4448 *accmode &= ~VADMIN_PERMS;
4453 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL
4454 * or VSYNCHRONIZE using file mode or POSIX.1e ACL.
4456 *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE);