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$");
44 #include "opt_compat.h"
46 #include "opt_watchdog.h"
48 #include <sys/param.h>
49 #include <sys/systm.h>
52 #include <sys/condvar.h>
54 #include <sys/dirent.h>
55 #include <sys/event.h>
56 #include <sys/eventhandler.h>
57 #include <sys/extattr.h>
59 #include <sys/fcntl.h>
62 #include <sys/kernel.h>
63 #include <sys/kthread.h>
64 #include <sys/lockf.h>
65 #include <sys/malloc.h>
66 #include <sys/mount.h>
67 #include <sys/namei.h>
69 #include <sys/reboot.h>
70 #include <sys/sched.h>
71 #include <sys/sleepqueue.h>
73 #include <sys/sysctl.h>
74 #include <sys/syslog.h>
75 #include <sys/vmmeter.h>
76 #include <sys/vnode.h>
77 #include <sys/watchdog.h>
79 #include <machine/stdarg.h>
81 #include <security/mac/mac_framework.h>
84 #include <vm/vm_object.h>
85 #include <vm/vm_extern.h>
87 #include <vm/vm_map.h>
88 #include <vm/vm_page.h>
89 #include <vm/vm_kern.h>
99 static void delmntque(struct vnode *vp);
100 static int flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo,
101 int slpflag, int slptimeo);
102 static void syncer_shutdown(void *arg, int howto);
103 static int vtryrecycle(struct vnode *vp);
104 static void vbusy(struct vnode *vp);
105 static void v_incr_usecount(struct vnode *);
106 static void v_decr_usecount(struct vnode *);
107 static void v_decr_useonly(struct vnode *);
108 static void v_upgrade_usecount(struct vnode *);
109 static void vfree(struct vnode *);
110 static void vnlru_free(int);
111 static void vgonel(struct vnode *);
112 static void vfs_knllock(void *arg);
113 static void vfs_knlunlock(void *arg);
114 static void vfs_knl_assert_locked(void *arg);
115 static void vfs_knl_assert_unlocked(void *arg);
116 static void destroy_vpollinfo(struct vpollinfo *vi);
119 * Number of vnodes in existence. Increased whenever getnewvnode()
120 * allocates a new vnode, decreased on vdestroy() called on VI_DOOMed
123 static unsigned long numvnodes;
125 SYSCTL_LONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0,
126 "Number of vnodes in existence");
129 * Conversion tables for conversion from vnode types to inode formats
132 enum vtype iftovt_tab[16] = {
133 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
134 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
136 int vttoif_tab[10] = {
137 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
138 S_IFSOCK, S_IFIFO, S_IFMT, S_IFMT
142 * List of vnodes that are ready for recycling.
144 static TAILQ_HEAD(freelst, vnode) vnode_free_list;
147 * Free vnode target. Free vnodes may simply be files which have been stat'd
148 * but not read. This is somewhat common, and a small cache of such files
149 * should be kept to avoid recreation costs.
151 static u_long wantfreevnodes;
152 SYSCTL_LONG(_vfs, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, "");
153 /* Number of vnodes in the free list. */
154 static u_long freevnodes;
155 SYSCTL_LONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0,
156 "Number of vnodes in the free list");
158 static int vlru_allow_cache_src;
159 SYSCTL_INT(_vfs, OID_AUTO, vlru_allow_cache_src, CTLFLAG_RW,
160 &vlru_allow_cache_src, 0, "Allow vlru to reclaim source vnode");
163 * Various variables used for debugging the new implementation of
165 * XXX these are probably of (very) limited utility now.
167 static int reassignbufcalls;
168 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0,
169 "Number of calls to reassignbuf");
172 * Cache for the mount type id assigned to NFS. This is used for
173 * special checks in nfs/nfs_nqlease.c and vm/vnode_pager.c.
175 int nfs_mount_type = -1;
177 /* To keep more than one thread at a time from running vfs_getnewfsid */
178 static struct mtx mntid_mtx;
181 * Lock for any access to the following:
186 static struct mtx vnode_free_list_mtx;
188 /* Publicly exported FS */
189 struct nfs_public nfs_pub;
191 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */
192 static uma_zone_t vnode_zone;
193 static uma_zone_t vnodepoll_zone;
195 /* Set to 1 to print out reclaim of active vnodes */
199 * The workitem queue.
201 * It is useful to delay writes of file data and filesystem metadata
202 * for tens of seconds so that quickly created and deleted files need
203 * not waste disk bandwidth being created and removed. To realize this,
204 * we append vnodes to a "workitem" queue. When running with a soft
205 * updates implementation, most pending metadata dependencies should
206 * not wait for more than a few seconds. Thus, mounted on block devices
207 * are delayed only about a half the time that file data is delayed.
208 * Similarly, directory updates are more critical, so are only delayed
209 * about a third the time that file data is delayed. Thus, there are
210 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
211 * one each second (driven off the filesystem syncer process). The
212 * syncer_delayno variable indicates the next queue that is to be processed.
213 * Items that need to be processed soon are placed in this queue:
215 * syncer_workitem_pending[syncer_delayno]
217 * A delay of fifteen seconds is done by placing the request fifteen
218 * entries later in the queue:
220 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
223 static int syncer_delayno;
224 static long syncer_mask;
225 LIST_HEAD(synclist, bufobj);
226 static struct synclist *syncer_workitem_pending[2];
228 * The sync_mtx protects:
233 * syncer_workitem_pending
234 * syncer_worklist_len
237 static struct mtx sync_mtx;
238 static struct cv sync_wakeup;
240 #define SYNCER_MAXDELAY 32
241 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
242 static int syncdelay = 30; /* max time to delay syncing data */
243 static int filedelay = 30; /* time to delay syncing files */
244 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0,
245 "Time to delay syncing files (in seconds)");
246 static int dirdelay = 29; /* time to delay syncing directories */
247 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0,
248 "Time to delay syncing directories (in seconds)");
249 static int metadelay = 28; /* time to delay syncing metadata */
250 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0,
251 "Time to delay syncing metadata (in seconds)");
252 static int rushjob; /* number of slots to run ASAP */
253 static int stat_rush_requests; /* number of times I/O speeded up */
254 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0,
255 "Number of times I/O speeded up (rush requests)");
258 * When shutting down the syncer, run it at four times normal speed.
260 #define SYNCER_SHUTDOWN_SPEEDUP 4
261 static int sync_vnode_count;
262 static int syncer_worklist_len;
263 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY }
267 * Number of vnodes we want to exist at any one time. This is mostly used
268 * to size hash tables in vnode-related code. It is normally not used in
269 * getnewvnode(), as wantfreevnodes is normally nonzero.)
271 * XXX desiredvnodes is historical cruft and should not exist.
274 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
275 &desiredvnodes, 0, "Maximum number of vnodes");
276 SYSCTL_INT(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
277 &wantfreevnodes, 0, "Minimum number of vnodes (legacy)");
278 static int vnlru_nowhere;
279 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW,
280 &vnlru_nowhere, 0, "Number of times the vnlru process ran without success");
283 * Macros to control when a vnode is freed and recycled. All require
284 * the vnode interlock.
286 #define VCANRECYCLE(vp) (((vp)->v_iflag & VI_FREE) && !(vp)->v_holdcnt)
287 #define VSHOULDFREE(vp) (!((vp)->v_iflag & VI_FREE) && !(vp)->v_holdcnt)
288 #define VSHOULDBUSY(vp) (((vp)->v_iflag & VI_FREE) && (vp)->v_holdcnt)
292 * Initialize the vnode management data structures.
294 * Reevaluate the following cap on the number of vnodes after the physical
295 * memory size exceeds 512GB. In the limit, as the physical memory size
296 * grows, the ratio of physical pages to vnodes approaches sixteen to one.
298 #ifndef MAXVNODES_MAX
299 #define MAXVNODES_MAX (512 * (1024 * 1024 * 1024 / (int)PAGE_SIZE / 16))
302 vntblinit(void *dummy __unused)
304 int physvnodes, virtvnodes;
307 * Desiredvnodes is a function of the physical memory size and the
308 * kernel's heap size. Generally speaking, it scales with the
309 * physical memory size. The ratio of desiredvnodes to physical pages
310 * is one to four until desiredvnodes exceeds 98,304. Thereafter, the
311 * marginal ratio of desiredvnodes to physical pages is one to
312 * sixteen. However, desiredvnodes is limited by the kernel's heap
313 * size. The memory required by desiredvnodes vnodes and vm objects
314 * may not exceed one seventh of the kernel's heap size.
316 physvnodes = maxproc + cnt.v_page_count / 16 + 3 * min(98304 * 4,
317 cnt.v_page_count) / 16;
318 virtvnodes = vm_kmem_size / (7 * (sizeof(struct vm_object) +
319 sizeof(struct vnode)));
320 desiredvnodes = min(physvnodes, virtvnodes);
321 if (desiredvnodes > MAXVNODES_MAX) {
323 printf("Reducing kern.maxvnodes %d -> %d\n",
324 desiredvnodes, MAXVNODES_MAX);
325 desiredvnodes = MAXVNODES_MAX;
327 wantfreevnodes = desiredvnodes / 4;
328 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF);
329 TAILQ_INIT(&vnode_free_list);
330 mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF);
331 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL,
332 NULL, NULL, UMA_ALIGN_PTR, 0);
333 vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo),
334 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
336 * Initialize the filesystem syncer.
338 syncer_workitem_pending[WI_MPSAFEQ] = hashinit(syncer_maxdelay, M_VNODE,
340 syncer_workitem_pending[WI_GIANTQ] = hashinit(syncer_maxdelay, M_VNODE,
342 syncer_maxdelay = syncer_mask + 1;
343 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF);
344 cv_init(&sync_wakeup, "syncer");
346 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL);
350 * Mark a mount point as busy. Used to synchronize access and to delay
351 * unmounting. Eventually, mountlist_mtx is not released on failure.
353 * vfs_busy() is a custom lock, it can block the caller.
354 * vfs_busy() only sleeps if the unmount is active on the mount point.
355 * For a mountpoint mp, vfs_busy-enforced lock is before lock of any
356 * vnode belonging to mp.
358 * Lookup uses vfs_busy() to traverse mount points.
360 * / vnode lock A / vnode lock (/var) D
361 * /var vnode lock B /log vnode lock(/var/log) E
362 * vfs_busy lock C vfs_busy lock F
364 * Within each file system, the lock order is C->A->B and F->D->E.
366 * When traversing across mounts, the system follows that lock order:
372 * The lookup() process for namei("/var") illustrates the process:
373 * VOP_LOOKUP() obtains B while A is held
374 * vfs_busy() obtains a shared lock on F while A and B are held
375 * vput() releases lock on B
376 * vput() releases lock on A
377 * VFS_ROOT() obtains lock on D while shared lock on F is held
378 * vfs_unbusy() releases shared lock on F
379 * vn_lock() obtains lock on deadfs vnode vp_crossmp instead of A.
380 * Attempt to lock A (instead of vp_crossmp) while D is held would
381 * violate the global order, causing deadlocks.
383 * dounmount() locks B while F is drained.
386 vfs_busy(struct mount *mp, int flags)
389 MPASS((flags & ~MBF_MASK) == 0);
390 CTR3(KTR_VFS, "%s: mp %p with flags %d", __func__, mp, flags);
395 * If mount point is currenly being unmounted, sleep until the
396 * mount point fate is decided. If thread doing the unmounting fails,
397 * it will clear MNTK_UNMOUNT flag before waking us up, indicating
398 * that this mount point has survived the unmount attempt and vfs_busy
399 * should retry. Otherwise the unmounter thread will set MNTK_REFEXPIRE
400 * flag in addition to MNTK_UNMOUNT, indicating that mount point is
401 * about to be really destroyed. vfs_busy needs to release its
402 * reference on the mount point in this case and return with ENOENT,
403 * telling the caller that mount mount it tried to busy is no longer
406 while (mp->mnt_kern_flag & MNTK_UNMOUNT) {
407 if (flags & MBF_NOWAIT || mp->mnt_kern_flag & MNTK_REFEXPIRE) {
410 CTR1(KTR_VFS, "%s: failed busying before sleeping",
414 if (flags & MBF_MNTLSTLOCK)
415 mtx_unlock(&mountlist_mtx);
416 mp->mnt_kern_flag |= MNTK_MWAIT;
417 msleep(mp, MNT_MTX(mp), PVFS | PDROP, "vfs_busy", 0);
418 if (flags & MBF_MNTLSTLOCK)
419 mtx_lock(&mountlist_mtx);
422 if (flags & MBF_MNTLSTLOCK)
423 mtx_unlock(&mountlist_mtx);
430 * Free a busy filesystem.
433 vfs_unbusy(struct mount *mp)
436 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
439 KASSERT(mp->mnt_lockref > 0, ("negative mnt_lockref"));
441 if (mp->mnt_lockref == 0 && (mp->mnt_kern_flag & MNTK_DRAINING) != 0) {
442 MPASS(mp->mnt_kern_flag & MNTK_UNMOUNT);
443 CTR1(KTR_VFS, "%s: waking up waiters", __func__);
444 mp->mnt_kern_flag &= ~MNTK_DRAINING;
445 wakeup(&mp->mnt_lockref);
451 * Lookup a mount point by filesystem identifier.
454 vfs_getvfs(fsid_t *fsid)
458 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
459 mtx_lock(&mountlist_mtx);
460 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
461 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
462 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
464 mtx_unlock(&mountlist_mtx);
468 mtx_unlock(&mountlist_mtx);
469 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
470 return ((struct mount *) 0);
474 * Lookup a mount point by filesystem identifier, busying it before
478 vfs_busyfs(fsid_t *fsid)
483 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
484 mtx_lock(&mountlist_mtx);
485 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
486 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
487 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
488 error = vfs_busy(mp, MBF_MNTLSTLOCK);
490 mtx_unlock(&mountlist_mtx);
496 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
497 mtx_unlock(&mountlist_mtx);
498 return ((struct mount *) 0);
502 * Check if a user can access privileged mount options.
505 vfs_suser(struct mount *mp, struct thread *td)
510 * If the thread is jailed, but this is not a jail-friendly file
511 * system, deny immediately.
513 if (!(mp->mnt_vfc->vfc_flags & VFCF_JAIL) && jailed(td->td_ucred))
517 * If the file system was mounted outside the jail of the calling
518 * thread, deny immediately.
520 if (prison_check(td->td_ucred, mp->mnt_cred) != 0)
524 * If file system supports delegated administration, we don't check
525 * for the PRIV_VFS_MOUNT_OWNER privilege - it will be better verified
526 * by the file system itself.
527 * If this is not the user that did original mount, we check for
528 * the PRIV_VFS_MOUNT_OWNER privilege.
530 if (!(mp->mnt_vfc->vfc_flags & VFCF_DELEGADMIN) &&
531 mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
532 if ((error = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 0)
539 * Get a new unique fsid. Try to make its val[0] unique, since this value
540 * will be used to create fake device numbers for stat(). Also try (but
541 * not so hard) make its val[0] unique mod 2^16, since some emulators only
542 * support 16-bit device numbers. We end up with unique val[0]'s for the
543 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
545 * Keep in mind that several mounts may be running in parallel. Starting
546 * the search one past where the previous search terminated is both a
547 * micro-optimization and a defense against returning the same fsid to
551 vfs_getnewfsid(struct mount *mp)
553 static u_int16_t mntid_base;
558 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
559 mtx_lock(&mntid_mtx);
560 mtype = mp->mnt_vfc->vfc_typenum;
561 tfsid.val[1] = mtype;
562 mtype = (mtype & 0xFF) << 24;
564 tfsid.val[0] = makedev(255,
565 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
567 if ((nmp = vfs_getvfs(&tfsid)) == NULL)
571 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
572 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
573 mtx_unlock(&mntid_mtx);
577 * Knob to control the precision of file timestamps:
579 * 0 = seconds only; nanoseconds zeroed.
580 * 1 = seconds and nanoseconds, accurate within 1/HZ.
581 * 2 = seconds and nanoseconds, truncated to microseconds.
582 * >=3 = seconds and nanoseconds, maximum precision.
584 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
586 static int timestamp_precision = TSP_SEC;
587 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
588 ×tamp_precision, 0, "File timestamp precision (0: seconds, "
589 "1: sec + ns accurate to 1/HZ, 2: sec + ns truncated to ms, "
590 "3+: sec + ns (max. precision))");
593 * Get a current timestamp.
596 vfs_timestamp(struct timespec *tsp)
600 switch (timestamp_precision) {
602 tsp->tv_sec = time_second;
610 TIMEVAL_TO_TIMESPEC(&tv, tsp);
620 * Set vnode attributes to VNOVAL
623 vattr_null(struct vattr *vap)
627 vap->va_size = VNOVAL;
628 vap->va_bytes = VNOVAL;
629 vap->va_mode = VNOVAL;
630 vap->va_nlink = VNOVAL;
631 vap->va_uid = VNOVAL;
632 vap->va_gid = VNOVAL;
633 vap->va_fsid = VNOVAL;
634 vap->va_fileid = VNOVAL;
635 vap->va_blocksize = VNOVAL;
636 vap->va_rdev = VNOVAL;
637 vap->va_atime.tv_sec = VNOVAL;
638 vap->va_atime.tv_nsec = VNOVAL;
639 vap->va_mtime.tv_sec = VNOVAL;
640 vap->va_mtime.tv_nsec = VNOVAL;
641 vap->va_ctime.tv_sec = VNOVAL;
642 vap->va_ctime.tv_nsec = VNOVAL;
643 vap->va_birthtime.tv_sec = VNOVAL;
644 vap->va_birthtime.tv_nsec = VNOVAL;
645 vap->va_flags = VNOVAL;
646 vap->va_gen = VNOVAL;
651 * This routine is called when we have too many vnodes. It attempts
652 * to free <count> vnodes and will potentially free vnodes that still
653 * have VM backing store (VM backing store is typically the cause
654 * of a vnode blowout so we want to do this). Therefore, this operation
655 * is not considered cheap.
657 * A number of conditions may prevent a vnode from being reclaimed.
658 * the buffer cache may have references on the vnode, a directory
659 * vnode may still have references due to the namei cache representing
660 * underlying files, or the vnode may be in active use. It is not
661 * desireable to reuse such vnodes. These conditions may cause the
662 * number of vnodes to reach some minimum value regardless of what
663 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
666 vlrureclaim(struct mount *mp)
675 * Calculate the trigger point, don't allow user
676 * screwups to blow us up. This prevents us from
677 * recycling vnodes with lots of resident pages. We
678 * aren't trying to free memory, we are trying to
681 usevnodes = desiredvnodes;
684 trigger = cnt.v_page_count * 2 / usevnodes;
686 vn_start_write(NULL, &mp, V_WAIT);
688 count = mp->mnt_nvnodelistsize / 10 + 1;
690 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
691 while (vp != NULL && vp->v_type == VMARKER)
692 vp = TAILQ_NEXT(vp, v_nmntvnodes);
695 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
696 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
701 * If it's been deconstructed already, it's still
702 * referenced, or it exceeds the trigger, skip it.
704 if (vp->v_usecount ||
705 (!vlru_allow_cache_src &&
706 !LIST_EMPTY(&(vp)->v_cache_src)) ||
707 (vp->v_iflag & VI_DOOMED) != 0 || (vp->v_object != NULL &&
708 vp->v_object->resident_page_count > trigger)) {
714 if (VOP_LOCK(vp, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT)) {
716 goto next_iter_mntunlocked;
720 * v_usecount may have been bumped after VOP_LOCK() dropped
721 * the vnode interlock and before it was locked again.
723 * It is not necessary to recheck VI_DOOMED because it can
724 * only be set by another thread that holds both the vnode
725 * lock and vnode interlock. If another thread has the
726 * vnode lock before we get to VOP_LOCK() and obtains the
727 * vnode interlock after VOP_LOCK() drops the vnode
728 * interlock, the other thread will be unable to drop the
729 * vnode lock before our VOP_LOCK() call fails.
731 if (vp->v_usecount ||
732 (!vlru_allow_cache_src &&
733 !LIST_EMPTY(&(vp)->v_cache_src)) ||
734 (vp->v_object != NULL &&
735 vp->v_object->resident_page_count > trigger)) {
736 VOP_UNLOCK(vp, LK_INTERLOCK);
737 goto next_iter_mntunlocked;
739 KASSERT((vp->v_iflag & VI_DOOMED) == 0,
740 ("VI_DOOMED unexpectedly detected in vlrureclaim()"));
745 next_iter_mntunlocked:
746 if ((count % 256) != 0)
750 if ((count % 256) != 0)
759 vn_finished_write(mp);
764 * Attempt to keep the free list at wantfreevnodes length.
767 vnlru_free(int count)
772 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
773 for (; count > 0; count--) {
774 vp = TAILQ_FIRST(&vnode_free_list);
776 * The list can be modified while the free_list_mtx
777 * has been dropped and vp could be NULL here.
781 VNASSERT(vp->v_op != NULL, vp,
782 ("vnlru_free: vnode already reclaimed."));
783 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
785 * Don't recycle if we can't get the interlock.
787 if (!VI_TRYLOCK(vp)) {
788 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
791 VNASSERT(VCANRECYCLE(vp), vp,
792 ("vp inconsistent on freelist"));
794 vp->v_iflag &= ~VI_FREE;
796 mtx_unlock(&vnode_free_list_mtx);
798 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
800 VFS_UNLOCK_GIANT(vfslocked);
802 * If the recycled succeeded this vdrop will actually free
803 * the vnode. If not it will simply place it back on
807 mtx_lock(&vnode_free_list_mtx);
811 * Attempt to recycle vnodes in a context that is always safe to block.
812 * Calling vlrurecycle() from the bowels of filesystem code has some
813 * interesting deadlock problems.
815 static struct proc *vnlruproc;
816 static int vnlruproc_sig;
821 struct mount *mp, *nmp;
823 struct proc *p = vnlruproc;
825 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, p,
829 kproc_suspend_check(p);
830 mtx_lock(&vnode_free_list_mtx);
831 if (freevnodes > wantfreevnodes)
832 vnlru_free(freevnodes - wantfreevnodes);
833 if (numvnodes <= desiredvnodes * 9 / 10) {
835 wakeup(&vnlruproc_sig);
836 msleep(vnlruproc, &vnode_free_list_mtx,
837 PVFS|PDROP, "vlruwt", hz);
840 mtx_unlock(&vnode_free_list_mtx);
842 mtx_lock(&mountlist_mtx);
843 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
844 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
845 nmp = TAILQ_NEXT(mp, mnt_list);
848 vfslocked = VFS_LOCK_GIANT(mp);
849 done += vlrureclaim(mp);
850 VFS_UNLOCK_GIANT(vfslocked);
851 mtx_lock(&mountlist_mtx);
852 nmp = TAILQ_NEXT(mp, mnt_list);
855 mtx_unlock(&mountlist_mtx);
858 /* These messages are temporary debugging aids */
859 if (vnlru_nowhere < 5)
860 printf("vnlru process getting nowhere..\n");
861 else if (vnlru_nowhere == 5)
862 printf("vnlru process messages stopped.\n");
865 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
871 static struct kproc_desc vnlru_kp = {
876 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start,
880 * Routines having to do with the management of the vnode table.
884 vdestroy(struct vnode *vp)
888 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
889 mtx_lock(&vnode_free_list_mtx);
891 mtx_unlock(&vnode_free_list_mtx);
893 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
894 ("cleaned vnode still on the free list."));
895 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't"));
896 VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count"));
897 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count"));
898 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count"));
899 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's"));
900 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0"));
901 VNASSERT(bo->bo_clean.bv_root == NULL, vp, ("cleanblkroot not NULL"));
902 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0"));
903 VNASSERT(bo->bo_dirty.bv_root == NULL, vp, ("dirtyblkroot not NULL"));
904 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst"));
905 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src"));
906 VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for .."));
909 mac_vnode_destroy(vp);
911 if (vp->v_pollinfo != NULL)
912 destroy_vpollinfo(vp->v_pollinfo);
914 /* XXX Elsewhere we can detect an already freed vnode via NULL v_op. */
917 lockdestroy(vp->v_vnlock);
918 mtx_destroy(&vp->v_interlock);
919 mtx_destroy(BO_MTX(bo));
920 uma_zfree(vnode_zone, vp);
924 * Try to recycle a freed vnode. We abort if anyone picks up a reference
925 * before we actually vgone(). This function must be called with the vnode
926 * held to prevent the vnode from being returned to the free list midway
930 vtryrecycle(struct vnode *vp)
934 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
935 VNASSERT(vp->v_holdcnt, vp,
936 ("vtryrecycle: Recycling vp %p without a reference.", vp));
938 * This vnode may found and locked via some other list, if so we
939 * can't recycle it yet.
941 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
943 "%s: impossible to recycle, vp %p lock is already held",
945 return (EWOULDBLOCK);
948 * Don't recycle if its filesystem is being suspended.
950 if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) {
953 "%s: impossible to recycle, cannot start the write for %p",
958 * If we got this far, we need to acquire the interlock and see if
959 * anyone picked up this vnode from another list. If not, we will
960 * mark it with DOOMED via vgonel() so that anyone who does find it
964 if (vp->v_usecount) {
965 VOP_UNLOCK(vp, LK_INTERLOCK);
966 vn_finished_write(vnmp);
968 "%s: impossible to recycle, %p is already referenced",
972 if ((vp->v_iflag & VI_DOOMED) == 0)
974 VOP_UNLOCK(vp, LK_INTERLOCK);
975 vn_finished_write(vnmp);
980 * Return the next vnode from the free list.
983 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops,
986 struct vnode *vp = NULL;
989 CTR3(KTR_VFS, "%s: mp %p with tag %s", __func__, mp, tag);
990 mtx_lock(&vnode_free_list_mtx);
992 * Lend our context to reclaim vnodes if they've exceeded the max.
994 if (freevnodes > wantfreevnodes)
997 * Wait for available vnodes.
999 if (numvnodes > desiredvnodes) {
1000 if (mp != NULL && (mp->mnt_kern_flag & MNTK_SUSPEND)) {
1002 * File system is beeing suspended, we cannot risk a
1003 * deadlock here, so allocate new vnode anyway.
1005 if (freevnodes > wantfreevnodes)
1006 vnlru_free(freevnodes - wantfreevnodes);
1009 if (vnlruproc_sig == 0) {
1010 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
1013 msleep(&vnlruproc_sig, &vnode_free_list_mtx, PVFS,
1015 #if 0 /* XXX Not all VFS_VGET/ffs_vget callers check returns. */
1016 if (numvnodes > desiredvnodes) {
1017 mtx_unlock(&vnode_free_list_mtx);
1024 mtx_unlock(&vnode_free_list_mtx);
1025 vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK|M_ZERO);
1029 vp->v_vnlock = &vp->v_lock;
1030 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF);
1032 * By default, don't allow shared locks unless filesystems
1035 lockinit(vp->v_vnlock, PVFS, tag, VLKTIMEOUT, LK_NOSHARE);
1037 * Initialize bufobj.
1040 bo->__bo_vnode = vp;
1041 mtx_init(BO_MTX(bo), "bufobj interlock", NULL, MTX_DEF);
1042 bo->bo_ops = &buf_ops_bio;
1043 bo->bo_private = vp;
1044 TAILQ_INIT(&bo->bo_clean.bv_hd);
1045 TAILQ_INIT(&bo->bo_dirty.bv_hd);
1047 * Initialize namecache.
1049 LIST_INIT(&vp->v_cache_src);
1050 TAILQ_INIT(&vp->v_cache_dst);
1052 * Finalize various vnode identity bits.
1057 v_incr_usecount(vp);
1061 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0)
1062 mac_vnode_associate_singlelabel(mp, vp);
1063 else if (mp == NULL && vops != &dead_vnodeops)
1064 printf("NULL mp in getnewvnode()\n");
1067 bo->bo_bsize = mp->mnt_stat.f_iosize;
1068 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0)
1069 vp->v_vflag |= VV_NOKNOTE;
1077 * Delete from old mount point vnode list, if on one.
1080 delmntque(struct vnode *vp)
1089 VNASSERT(mp->mnt_nvnodelistsize > 0, vp,
1090 ("bad mount point vnode list size"));
1091 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1092 mp->mnt_nvnodelistsize--;
1098 insmntque_stddtr(struct vnode *vp, void *dtr_arg)
1102 vp->v_op = &dead_vnodeops;
1103 /* XXX non mp-safe fs may still call insmntque with vnode
1105 if (!VOP_ISLOCKED(vp))
1106 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1112 * Insert into list of vnodes for the new mount point, if available.
1115 insmntque1(struct vnode *vp, struct mount *mp,
1116 void (*dtr)(struct vnode *, void *), void *dtr_arg)
1120 KASSERT(vp->v_mount == NULL,
1121 ("insmntque: vnode already on per mount vnode list"));
1122 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)"));
1123 #ifdef DEBUG_VFS_LOCKS
1124 if (!VFS_NEEDSGIANT(mp))
1125 ASSERT_VOP_ELOCKED(vp,
1126 "insmntque: mp-safe fs and non-locked vp");
1129 if ((mp->mnt_kern_flag & MNTK_NOINSMNTQ) != 0 &&
1130 ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 ||
1131 mp->mnt_nvnodelistsize == 0)) {
1132 locked = VOP_ISLOCKED(vp);
1133 if (!locked || (locked == LK_EXCLUSIVE &&
1134 (vp->v_vflag & VV_FORCEINSMQ) == 0)) {
1143 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1144 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp,
1145 ("neg mount point vnode list size"));
1146 mp->mnt_nvnodelistsize++;
1152 insmntque(struct vnode *vp, struct mount *mp)
1155 return (insmntque1(vp, mp, insmntque_stddtr, NULL));
1159 * Flush out and invalidate all buffers associated with a bufobj
1160 * Called with the underlying object locked.
1163 bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo)
1168 if (flags & V_SAVE) {
1169 error = bufobj_wwait(bo, slpflag, slptimeo);
1174 if (bo->bo_dirty.bv_cnt > 0) {
1176 if ((error = BO_SYNC(bo, MNT_WAIT)) != 0)
1179 * XXX We could save a lock/unlock if this was only
1180 * enabled under INVARIANTS
1183 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)
1184 panic("vinvalbuf: dirty bufs");
1188 * If you alter this loop please notice that interlock is dropped and
1189 * reacquired in flushbuflist. Special care is needed to ensure that
1190 * no race conditions occur from this.
1193 error = flushbuflist(&bo->bo_clean,
1194 flags, bo, slpflag, slptimeo);
1195 if (error == 0 && !(flags & V_CLEANONLY))
1196 error = flushbuflist(&bo->bo_dirty,
1197 flags, bo, slpflag, slptimeo);
1198 if (error != 0 && error != EAGAIN) {
1202 } while (error != 0);
1205 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
1206 * have write I/O in-progress but if there is a VM object then the
1207 * VM object can also have read-I/O in-progress.
1210 bufobj_wwait(bo, 0, 0);
1212 if (bo->bo_object != NULL) {
1213 VM_OBJECT_LOCK(bo->bo_object);
1214 vm_object_pip_wait(bo->bo_object, "bovlbx");
1215 VM_OBJECT_UNLOCK(bo->bo_object);
1218 } while (bo->bo_numoutput > 0);
1222 * Destroy the copy in the VM cache, too.
1224 if (bo->bo_object != NULL &&
1225 (flags & (V_ALT | V_NORMAL | V_CLEANONLY)) == 0) {
1226 VM_OBJECT_LOCK(bo->bo_object);
1227 vm_object_page_remove(bo->bo_object, 0, 0,
1228 (flags & V_SAVE) ? TRUE : FALSE);
1229 VM_OBJECT_UNLOCK(bo->bo_object);
1234 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY)) == 0 &&
1235 (bo->bo_dirty.bv_cnt > 0 || bo->bo_clean.bv_cnt > 0))
1236 panic("vinvalbuf: flush failed");
1243 * Flush out and invalidate all buffers associated with a vnode.
1244 * Called with the underlying object locked.
1247 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
1250 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
1251 ASSERT_VOP_LOCKED(vp, "vinvalbuf");
1252 return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo));
1256 * Flush out buffers on the specified list.
1260 flushbuflist( struct bufv *bufv, int flags, struct bufobj *bo, int slpflag,
1263 struct buf *bp, *nbp;
1268 ASSERT_BO_LOCKED(bo);
1271 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) {
1272 if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) ||
1273 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) {
1279 lblkno = nbp->b_lblkno;
1280 xflags = nbp->b_xflags &
1281 (BX_BKGRDMARKER | BX_VNDIRTY | BX_VNCLEAN);
1284 error = BUF_TIMELOCK(bp,
1285 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_MTX(bo),
1286 "flushbuf", slpflag, slptimeo);
1289 return (error != ENOLCK ? error : EAGAIN);
1291 KASSERT(bp->b_bufobj == bo,
1292 ("bp %p wrong b_bufobj %p should be %p",
1293 bp, bp->b_bufobj, bo));
1294 if (bp->b_bufobj != bo) { /* XXX: necessary ? */
1300 * XXX Since there are no node locks for NFS, I
1301 * believe there is a slight chance that a delayed
1302 * write will occur while sleeping just above, so
1305 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
1310 bp->b_flags |= B_ASYNC;
1313 return (EAGAIN); /* XXX: why not loop ? */
1318 bp->b_flags |= (B_INVAL | B_RELBUF);
1319 bp->b_flags &= ~B_ASYNC;
1323 (nbp->b_bufobj != bo ||
1324 nbp->b_lblkno != lblkno ||
1326 (BX_BKGRDMARKER | BX_VNDIRTY | BX_VNCLEAN)) != xflags))
1327 break; /* nbp invalid */
1333 * Truncate a file's buffer and pages to a specified length. This
1334 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
1338 vtruncbuf(struct vnode *vp, struct ucred *cred, struct thread *td,
1339 off_t length, int blksize)
1341 struct buf *bp, *nbp;
1346 CTR5(KTR_VFS, "%s: vp %p with cred %p and block %d:%ju", __func__,
1347 vp, cred, blksize, (uintmax_t)length);
1350 * Round up to the *next* lbn.
1352 trunclbn = (length + blksize - 1) / blksize;
1354 ASSERT_VOP_LOCKED(vp, "vtruncbuf");
1361 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) {
1362 if (bp->b_lblkno < trunclbn)
1365 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1366 BO_MTX(bo)) == ENOLCK)
1372 bp->b_flags |= (B_INVAL | B_RELBUF);
1373 bp->b_flags &= ~B_ASYNC;
1379 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
1380 (nbp->b_vp != vp) ||
1381 (nbp->b_flags & B_DELWRI))) {
1387 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1388 if (bp->b_lblkno < trunclbn)
1391 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1392 BO_MTX(bo)) == ENOLCK)
1397 bp->b_flags |= (B_INVAL | B_RELBUF);
1398 bp->b_flags &= ~B_ASYNC;
1404 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
1405 (nbp->b_vp != vp) ||
1406 (nbp->b_flags & B_DELWRI) == 0)) {
1415 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1416 if (bp->b_lblkno > 0)
1419 * Since we hold the vnode lock this should only
1420 * fail if we're racing with the buf daemon.
1423 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1424 BO_MTX(bo)) == ENOLCK) {
1427 VNASSERT((bp->b_flags & B_DELWRI), vp,
1428 ("buf(%p) on dirty queue without DELWRI", bp));
1439 bufobj_wwait(bo, 0, 0);
1441 vnode_pager_setsize(vp, length);
1447 * buf_splay() - splay tree core for the clean/dirty list of buffers in
1450 * NOTE: We have to deal with the special case of a background bitmap
1451 * buffer, a situation where two buffers will have the same logical
1452 * block offset. We want (1) only the foreground buffer to be accessed
1453 * in a lookup and (2) must differentiate between the foreground and
1454 * background buffer in the splay tree algorithm because the splay
1455 * tree cannot normally handle multiple entities with the same 'index'.
1456 * We accomplish this by adding differentiating flags to the splay tree's
1461 buf_splay(daddr_t lblkno, b_xflags_t xflags, struct buf *root)
1464 struct buf *lefttreemax, *righttreemin, *y;
1468 lefttreemax = righttreemin = &dummy;
1470 if (lblkno < root->b_lblkno ||
1471 (lblkno == root->b_lblkno &&
1472 (xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
1473 if ((y = root->b_left) == NULL)
1475 if (lblkno < y->b_lblkno) {
1477 root->b_left = y->b_right;
1480 if ((y = root->b_left) == NULL)
1483 /* Link into the new root's right tree. */
1484 righttreemin->b_left = root;
1485 righttreemin = root;
1486 } else if (lblkno > root->b_lblkno ||
1487 (lblkno == root->b_lblkno &&
1488 (xflags & BX_BKGRDMARKER) > (root->b_xflags & BX_BKGRDMARKER))) {
1489 if ((y = root->b_right) == NULL)
1491 if (lblkno > y->b_lblkno) {
1493 root->b_right = y->b_left;
1496 if ((y = root->b_right) == NULL)
1499 /* Link into the new root's left tree. */
1500 lefttreemax->b_right = root;
1507 /* Assemble the new root. */
1508 lefttreemax->b_right = root->b_left;
1509 righttreemin->b_left = root->b_right;
1510 root->b_left = dummy.b_right;
1511 root->b_right = dummy.b_left;
1516 buf_vlist_remove(struct buf *bp)
1521 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1522 ASSERT_BO_LOCKED(bp->b_bufobj);
1523 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) !=
1524 (BX_VNDIRTY|BX_VNCLEAN),
1525 ("buf_vlist_remove: Buf %p is on two lists", bp));
1526 if (bp->b_xflags & BX_VNDIRTY)
1527 bv = &bp->b_bufobj->bo_dirty;
1529 bv = &bp->b_bufobj->bo_clean;
1530 if (bp != bv->bv_root) {
1531 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root);
1532 KASSERT(root == bp, ("splay lookup failed in remove"));
1534 if (bp->b_left == NULL) {
1537 root = buf_splay(bp->b_lblkno, bp->b_xflags, bp->b_left);
1538 root->b_right = bp->b_right;
1541 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs);
1543 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1547 * Add the buffer to the sorted clean or dirty block list using a
1548 * splay tree algorithm.
1550 * NOTE: xflags is passed as a constant, optimizing this inline function!
1553 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags)
1558 ASSERT_BO_LOCKED(bo);
1559 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0,
1560 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags));
1561 bp->b_xflags |= xflags;
1562 if (xflags & BX_VNDIRTY)
1567 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root);
1571 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs);
1572 } else if (bp->b_lblkno < root->b_lblkno ||
1573 (bp->b_lblkno == root->b_lblkno &&
1574 (bp->b_xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
1575 bp->b_left = root->b_left;
1577 root->b_left = NULL;
1578 TAILQ_INSERT_BEFORE(root, bp, b_bobufs);
1580 bp->b_right = root->b_right;
1582 root->b_right = NULL;
1583 TAILQ_INSERT_AFTER(&bv->bv_hd, root, bp, b_bobufs);
1590 * Lookup a buffer using the splay tree. Note that we specifically avoid
1591 * shadow buffers used in background bitmap writes.
1593 * This code isn't quite efficient as it could be because we are maintaining
1594 * two sorted lists and do not know which list the block resides in.
1596 * During a "make buildworld" the desired buffer is found at one of
1597 * the roots more than 60% of the time. Thus, checking both roots
1598 * before performing either splay eliminates unnecessary splays on the
1599 * first tree splayed.
1602 gbincore(struct bufobj *bo, daddr_t lblkno)
1606 ASSERT_BO_LOCKED(bo);
1607 if ((bp = bo->bo_clean.bv_root) != NULL &&
1608 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1610 if ((bp = bo->bo_dirty.bv_root) != NULL &&
1611 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1613 if ((bp = bo->bo_clean.bv_root) != NULL) {
1614 bo->bo_clean.bv_root = bp = buf_splay(lblkno, 0, bp);
1615 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1618 if ((bp = bo->bo_dirty.bv_root) != NULL) {
1619 bo->bo_dirty.bv_root = bp = buf_splay(lblkno, 0, bp);
1620 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1627 * Associate a buffer with a vnode.
1630 bgetvp(struct vnode *vp, struct buf *bp)
1635 ASSERT_BO_LOCKED(bo);
1636 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free"));
1638 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags);
1639 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp,
1640 ("bgetvp: bp already attached! %p", bp));
1643 if (VFS_NEEDSGIANT(vp->v_mount) || bo->bo_flag & BO_NEEDSGIANT)
1644 bp->b_flags |= B_NEEDSGIANT;
1648 * Insert onto list for new vnode.
1650 buf_vlist_add(bp, bo, BX_VNCLEAN);
1654 * Disassociate a buffer from a vnode.
1657 brelvp(struct buf *bp)
1662 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1663 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1666 * Delete from old vnode list, if on one.
1668 vp = bp->b_vp; /* XXX */
1671 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1672 buf_vlist_remove(bp);
1674 panic("brelvp: Buffer %p not on queue.", bp);
1675 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
1676 bo->bo_flag &= ~BO_ONWORKLST;
1677 mtx_lock(&sync_mtx);
1678 LIST_REMOVE(bo, bo_synclist);
1679 syncer_worklist_len--;
1680 mtx_unlock(&sync_mtx);
1682 bp->b_flags &= ~B_NEEDSGIANT;
1684 bp->b_bufobj = NULL;
1690 * Add an item to the syncer work queue.
1693 vn_syncer_add_to_worklist(struct bufobj *bo, int delay)
1697 ASSERT_BO_LOCKED(bo);
1699 mtx_lock(&sync_mtx);
1700 if (bo->bo_flag & BO_ONWORKLST)
1701 LIST_REMOVE(bo, bo_synclist);
1703 bo->bo_flag |= BO_ONWORKLST;
1704 syncer_worklist_len++;
1707 if (delay > syncer_maxdelay - 2)
1708 delay = syncer_maxdelay - 2;
1709 slot = (syncer_delayno + delay) & syncer_mask;
1711 queue = VFS_NEEDSGIANT(bo->__bo_vnode->v_mount) ? WI_GIANTQ :
1713 LIST_INSERT_HEAD(&syncer_workitem_pending[queue][slot], bo,
1715 mtx_unlock(&sync_mtx);
1719 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS)
1723 mtx_lock(&sync_mtx);
1724 len = syncer_worklist_len - sync_vnode_count;
1725 mtx_unlock(&sync_mtx);
1726 error = SYSCTL_OUT(req, &len, sizeof(len));
1730 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, CTLTYPE_INT | CTLFLAG_RD, NULL, 0,
1731 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length");
1733 static struct proc *updateproc;
1734 static void sched_sync(void);
1735 static struct kproc_desc up_kp = {
1740 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp);
1743 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td)
1748 *bo = LIST_FIRST(slp);
1751 vp = (*bo)->__bo_vnode; /* XXX */
1752 if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0)
1755 * We use vhold in case the vnode does not
1756 * successfully sync. vhold prevents the vnode from
1757 * going away when we unlock the sync_mtx so that
1758 * we can acquire the vnode interlock.
1761 mtx_unlock(&sync_mtx);
1763 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1765 mtx_lock(&sync_mtx);
1766 return (*bo == LIST_FIRST(slp));
1768 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1769 (void) VOP_FSYNC(vp, MNT_LAZY, td);
1771 vn_finished_write(mp);
1773 if (((*bo)->bo_flag & BO_ONWORKLST) != 0) {
1775 * Put us back on the worklist. The worklist
1776 * routine will remove us from our current
1777 * position and then add us back in at a later
1780 vn_syncer_add_to_worklist(*bo, syncdelay);
1784 mtx_lock(&sync_mtx);
1789 * System filesystem synchronizer daemon.
1794 struct synclist *gnext, *next;
1795 struct synclist *gslp, *slp;
1798 struct thread *td = curthread;
1800 int net_worklist_len;
1801 int syncer_final_iter;
1806 syncer_final_iter = 0;
1808 syncer_state = SYNCER_RUNNING;
1809 starttime = time_uptime;
1810 td->td_pflags |= TDP_NORUNNINGBUF;
1812 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc,
1815 mtx_lock(&sync_mtx);
1817 if (syncer_state == SYNCER_FINAL_DELAY &&
1818 syncer_final_iter == 0) {
1819 mtx_unlock(&sync_mtx);
1820 kproc_suspend_check(td->td_proc);
1821 mtx_lock(&sync_mtx);
1823 net_worklist_len = syncer_worklist_len - sync_vnode_count;
1824 if (syncer_state != SYNCER_RUNNING &&
1825 starttime != time_uptime) {
1827 printf("\nSyncing disks, vnodes remaining...");
1830 printf("%d ", net_worklist_len);
1832 starttime = time_uptime;
1835 * Push files whose dirty time has expired. Be careful
1836 * of interrupt race on slp queue.
1838 * Skip over empty worklist slots when shutting down.
1841 slp = &syncer_workitem_pending[WI_MPSAFEQ][syncer_delayno];
1842 gslp = &syncer_workitem_pending[WI_GIANTQ][syncer_delayno];
1843 syncer_delayno += 1;
1844 if (syncer_delayno == syncer_maxdelay)
1846 next = &syncer_workitem_pending[WI_MPSAFEQ][syncer_delayno];
1847 gnext = &syncer_workitem_pending[WI_GIANTQ][syncer_delayno];
1849 * If the worklist has wrapped since the
1850 * it was emptied of all but syncer vnodes,
1851 * switch to the FINAL_DELAY state and run
1852 * for one more second.
1854 if (syncer_state == SYNCER_SHUTTING_DOWN &&
1855 net_worklist_len == 0 &&
1856 last_work_seen == syncer_delayno) {
1857 syncer_state = SYNCER_FINAL_DELAY;
1858 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP;
1860 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) &&
1861 LIST_EMPTY(gslp) && syncer_worklist_len > 0);
1864 * Keep track of the last time there was anything
1865 * on the worklist other than syncer vnodes.
1866 * Return to the SHUTTING_DOWN state if any
1869 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING)
1870 last_work_seen = syncer_delayno;
1871 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY)
1872 syncer_state = SYNCER_SHUTTING_DOWN;
1873 while (!LIST_EMPTY(slp)) {
1874 error = sync_vnode(slp, &bo, td);
1876 LIST_REMOVE(bo, bo_synclist);
1877 LIST_INSERT_HEAD(next, bo, bo_synclist);
1881 if (first_printf == 0)
1882 wdog_kern_pat(WD_LASTVAL);
1885 if (!LIST_EMPTY(gslp)) {
1886 mtx_unlock(&sync_mtx);
1888 mtx_lock(&sync_mtx);
1889 while (!LIST_EMPTY(gslp)) {
1890 error = sync_vnode(gslp, &bo, td);
1892 LIST_REMOVE(bo, bo_synclist);
1893 LIST_INSERT_HEAD(gnext, bo,
1900 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0)
1901 syncer_final_iter--;
1903 * The variable rushjob allows the kernel to speed up the
1904 * processing of the filesystem syncer process. A rushjob
1905 * value of N tells the filesystem syncer to process the next
1906 * N seconds worth of work on its queue ASAP. Currently rushjob
1907 * is used by the soft update code to speed up the filesystem
1908 * syncer process when the incore state is getting so far
1909 * ahead of the disk that the kernel memory pool is being
1910 * threatened with exhaustion.
1917 * Just sleep for a short period of time between
1918 * iterations when shutting down to allow some I/O
1921 * If it has taken us less than a second to process the
1922 * current work, then wait. Otherwise start right over
1923 * again. We can still lose time if any single round
1924 * takes more than two seconds, but it does not really
1925 * matter as we are just trying to generally pace the
1926 * filesystem activity.
1928 if (syncer_state != SYNCER_RUNNING ||
1929 time_uptime == starttime) {
1931 sched_prio(td, PPAUSE);
1934 if (syncer_state != SYNCER_RUNNING)
1935 cv_timedwait(&sync_wakeup, &sync_mtx,
1936 hz / SYNCER_SHUTDOWN_SPEEDUP);
1937 else if (time_uptime == starttime)
1938 cv_timedwait(&sync_wakeup, &sync_mtx, hz);
1943 * Request the syncer daemon to speed up its work.
1944 * We never push it to speed up more than half of its
1945 * normal turn time, otherwise it could take over the cpu.
1948 speedup_syncer(void)
1952 mtx_lock(&sync_mtx);
1953 if (rushjob < syncdelay / 2) {
1955 stat_rush_requests += 1;
1958 mtx_unlock(&sync_mtx);
1959 cv_broadcast(&sync_wakeup);
1964 * Tell the syncer to speed up its work and run though its work
1965 * list several times, then tell it to shut down.
1968 syncer_shutdown(void *arg, int howto)
1971 if (howto & RB_NOSYNC)
1973 mtx_lock(&sync_mtx);
1974 syncer_state = SYNCER_SHUTTING_DOWN;
1976 mtx_unlock(&sync_mtx);
1977 cv_broadcast(&sync_wakeup);
1978 kproc_shutdown(arg, howto);
1982 * Reassign a buffer from one vnode to another.
1983 * Used to assign file specific control information
1984 * (indirect blocks) to the vnode to which they belong.
1987 reassignbuf(struct buf *bp)
2000 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X",
2001 bp, bp->b_vp, bp->b_flags);
2003 * B_PAGING flagged buffers cannot be reassigned because their vp
2004 * is not fully linked in.
2006 if (bp->b_flags & B_PAGING)
2007 panic("cannot reassign paging buffer");
2010 * Delete from old vnode list, if on one.
2013 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
2014 buf_vlist_remove(bp);
2016 panic("reassignbuf: Buffer %p not on queue.", bp);
2018 * If dirty, put on list of dirty buffers; otherwise insert onto list
2021 if (bp->b_flags & B_DELWRI) {
2022 if ((bo->bo_flag & BO_ONWORKLST) == 0) {
2023 switch (vp->v_type) {
2033 vn_syncer_add_to_worklist(bo, delay);
2035 buf_vlist_add(bp, bo, BX_VNDIRTY);
2037 buf_vlist_add(bp, bo, BX_VNCLEAN);
2039 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
2040 mtx_lock(&sync_mtx);
2041 LIST_REMOVE(bo, bo_synclist);
2042 syncer_worklist_len--;
2043 mtx_unlock(&sync_mtx);
2044 bo->bo_flag &= ~BO_ONWORKLST;
2049 bp = TAILQ_FIRST(&bv->bv_hd);
2050 KASSERT(bp == NULL || bp->b_bufobj == bo,
2051 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2052 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2053 KASSERT(bp == NULL || bp->b_bufobj == bo,
2054 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2056 bp = TAILQ_FIRST(&bv->bv_hd);
2057 KASSERT(bp == NULL || bp->b_bufobj == bo,
2058 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2059 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2060 KASSERT(bp == NULL || bp->b_bufobj == bo,
2061 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2067 * Increment the use and hold counts on the vnode, taking care to reference
2068 * the driver's usecount if this is a chardev. The vholdl() will remove
2069 * the vnode from the free list if it is presently free. Requires the
2070 * vnode interlock and returns with it held.
2073 v_incr_usecount(struct vnode *vp)
2076 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2078 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2080 vp->v_rdev->si_usecount++;
2087 * Turn a holdcnt into a use+holdcnt such that only one call to
2088 * v_decr_usecount is needed.
2091 v_upgrade_usecount(struct vnode *vp)
2094 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2096 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2098 vp->v_rdev->si_usecount++;
2104 * Decrement the vnode use and hold count along with the driver's usecount
2105 * if this is a chardev. The vdropl() below releases the vnode interlock
2106 * as it may free the vnode.
2109 v_decr_usecount(struct vnode *vp)
2112 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2113 VNASSERT(vp->v_usecount > 0, vp,
2114 ("v_decr_usecount: negative usecount"));
2115 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2117 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2119 vp->v_rdev->si_usecount--;
2126 * Decrement only the use count and driver use count. This is intended to
2127 * be paired with a follow on vdropl() to release the remaining hold count.
2128 * In this way we may vgone() a vnode with a 0 usecount without risk of
2129 * having it end up on a free list because the hold count is kept above 0.
2132 v_decr_useonly(struct vnode *vp)
2135 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2136 VNASSERT(vp->v_usecount > 0, vp,
2137 ("v_decr_useonly: negative usecount"));
2138 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2140 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2142 vp->v_rdev->si_usecount--;
2148 * Grab a particular vnode from the free list, increment its
2149 * reference count and lock it. VI_DOOMED is set if the vnode
2150 * is being destroyed. Only callers who specify LK_RETRY will
2151 * see doomed vnodes. If inactive processing was delayed in
2152 * vput try to do it here.
2155 vget(struct vnode *vp, int flags, struct thread *td)
2160 VFS_ASSERT_GIANT(vp->v_mount);
2161 VNASSERT((flags & LK_TYPE_MASK) != 0, vp,
2162 ("vget: invalid lock operation"));
2163 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
2165 if ((flags & LK_INTERLOCK) == 0)
2168 if ((error = vn_lock(vp, flags | LK_INTERLOCK)) != 0) {
2170 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__,
2174 if (vp->v_iflag & VI_DOOMED && (flags & LK_RETRY) == 0)
2175 panic("vget: vn_lock failed to return ENOENT\n");
2177 /* Upgrade our holdcnt to a usecount. */
2178 v_upgrade_usecount(vp);
2180 * We don't guarantee that any particular close will
2181 * trigger inactive processing so just make a best effort
2182 * here at preventing a reference to a removed file. If
2183 * we don't succeed no harm is done.
2185 if (vp->v_iflag & VI_OWEINACT) {
2186 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE &&
2187 (flags & LK_NOWAIT) == 0)
2189 vp->v_iflag &= ~VI_OWEINACT;
2196 * Increase the reference count of a vnode.
2199 vref(struct vnode *vp)
2202 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2204 v_incr_usecount(vp);
2209 * Return reference count of a vnode.
2211 * The results of this call are only guaranteed when some mechanism other
2212 * than the VI lock is used to stop other processes from gaining references
2213 * to the vnode. This may be the case if the caller holds the only reference.
2214 * This is also useful when stale data is acceptable as race conditions may
2215 * be accounted for by some other means.
2218 vrefcnt(struct vnode *vp)
2223 usecnt = vp->v_usecount;
2229 #define VPUTX_VRELE 1
2230 #define VPUTX_VPUT 2
2231 #define VPUTX_VUNREF 3
2234 vputx(struct vnode *vp, int func)
2238 KASSERT(vp != NULL, ("vputx: null vp"));
2239 if (func == VPUTX_VUNREF)
2240 ASSERT_VOP_LOCKED(vp, "vunref");
2241 else if (func == VPUTX_VPUT)
2242 ASSERT_VOP_LOCKED(vp, "vput");
2244 KASSERT(func == VPUTX_VRELE, ("vputx: wrong func"));
2245 VFS_ASSERT_GIANT(vp->v_mount);
2246 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2249 /* Skip this v_writecount check if we're going to panic below. */
2250 VNASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, vp,
2251 ("vputx: missed vn_close"));
2254 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) &&
2255 vp->v_usecount == 1)) {
2256 if (func == VPUTX_VPUT)
2258 v_decr_usecount(vp);
2262 if (vp->v_usecount != 1) {
2263 vprint("vputx: negative ref count", vp);
2264 panic("vputx: negative ref cnt");
2266 CTR2(KTR_VFS, "%s: return vnode %p to the freelist", __func__, vp);
2268 * We want to hold the vnode until the inactive finishes to
2269 * prevent vgone() races. We drop the use count here and the
2270 * hold count below when we're done.
2274 * We must call VOP_INACTIVE with the node locked. Mark
2275 * as VI_DOINGINACT to avoid recursion.
2277 vp->v_iflag |= VI_OWEINACT;
2280 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
2284 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2285 error = VOP_LOCK(vp, LK_UPGRADE | LK_INTERLOCK |
2291 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
2295 if (vp->v_usecount > 0)
2296 vp->v_iflag &= ~VI_OWEINACT;
2298 if (vp->v_iflag & VI_OWEINACT)
2299 vinactive(vp, curthread);
2300 if (func != VPUTX_VUNREF)
2307 * Vnode put/release.
2308 * If count drops to zero, call inactive routine and return to freelist.
2311 vrele(struct vnode *vp)
2314 vputx(vp, VPUTX_VRELE);
2318 * Release an already locked vnode. This give the same effects as
2319 * unlock+vrele(), but takes less time and avoids releasing and
2320 * re-aquiring the lock (as vrele() acquires the lock internally.)
2323 vput(struct vnode *vp)
2326 vputx(vp, VPUTX_VPUT);
2330 * Release an exclusively locked vnode. Do not unlock the vnode lock.
2333 vunref(struct vnode *vp)
2336 vputx(vp, VPUTX_VUNREF);
2340 * Somebody doesn't want the vnode recycled.
2343 vhold(struct vnode *vp)
2352 vholdl(struct vnode *vp)
2355 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2357 if (VSHOULDBUSY(vp))
2362 * Note that there is one less who cares about this vnode. vdrop() is the
2363 * opposite of vhold().
2366 vdrop(struct vnode *vp)
2374 * Drop the hold count of the vnode. If this is the last reference to
2375 * the vnode we will free it if it has been vgone'd otherwise it is
2376 * placed on the free list.
2379 vdropl(struct vnode *vp)
2382 ASSERT_VI_LOCKED(vp, "vdropl");
2383 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2384 if (vp->v_holdcnt <= 0)
2385 panic("vdrop: holdcnt %d", vp->v_holdcnt);
2387 if (vp->v_holdcnt == 0) {
2388 if (vp->v_iflag & VI_DOOMED) {
2389 CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__,
2400 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT
2401 * flags. DOINGINACT prevents us from recursing in calls to vinactive.
2402 * OWEINACT tracks whether a vnode missed a call to inactive due to a
2403 * failed lock upgrade.
2406 vinactive(struct vnode *vp, struct thread *td)
2409 ASSERT_VOP_ELOCKED(vp, "vinactive");
2410 ASSERT_VI_LOCKED(vp, "vinactive");
2411 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
2412 ("vinactive: recursed on VI_DOINGINACT"));
2413 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2414 vp->v_iflag |= VI_DOINGINACT;
2415 vp->v_iflag &= ~VI_OWEINACT;
2417 VOP_INACTIVE(vp, td);
2419 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
2420 ("vinactive: lost VI_DOINGINACT"));
2421 vp->v_iflag &= ~VI_DOINGINACT;
2425 * Remove any vnodes in the vnode table belonging to mount point mp.
2427 * If FORCECLOSE is not specified, there should not be any active ones,
2428 * return error if any are found (nb: this is a user error, not a
2429 * system error). If FORCECLOSE is specified, detach any active vnodes
2432 * If WRITECLOSE is set, only flush out regular file vnodes open for
2435 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
2437 * `rootrefs' specifies the base reference count for the root vnode
2438 * of this filesystem. The root vnode is considered busy if its
2439 * v_usecount exceeds this value. On a successful return, vflush(, td)
2440 * will call vrele() on the root vnode exactly rootrefs times.
2441 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
2445 static int busyprt = 0; /* print out busy vnodes */
2446 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "Print out busy vnodes");
2450 vflush( struct mount *mp, int rootrefs, int flags, struct thread *td)
2452 struct vnode *vp, *mvp, *rootvp = NULL;
2454 int busy = 0, error;
2456 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp,
2459 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
2460 ("vflush: bad args"));
2462 * Get the filesystem root vnode. We can vput() it
2463 * immediately, since with rootrefs > 0, it won't go away.
2465 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) {
2466 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d",
2475 MNT_VNODE_FOREACH(vp, mp, mvp) {
2480 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE);
2484 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
2488 * Skip over a vnodes marked VV_SYSTEM.
2490 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
2497 * If WRITECLOSE is set, flush out unlinked but still open
2498 * files (even if open only for reading) and regular file
2499 * vnodes open for writing.
2501 if (flags & WRITECLOSE) {
2502 if (vp->v_object != NULL) {
2503 VM_OBJECT_LOCK(vp->v_object);
2504 vm_object_page_clean(vp->v_object, 0, 0, 0);
2505 VM_OBJECT_UNLOCK(vp->v_object);
2507 error = VOP_FSYNC(vp, MNT_WAIT, td);
2511 MNT_VNODE_FOREACH_ABORT(mp, mvp);
2514 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
2517 if ((vp->v_type == VNON ||
2518 (error == 0 && vattr.va_nlink > 0)) &&
2519 (vp->v_writecount == 0 || vp->v_type != VREG)) {
2528 * With v_usecount == 0, all we need to do is clear out the
2529 * vnode data structures and we are done.
2531 * If FORCECLOSE is set, forcibly close the vnode.
2533 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) {
2534 VNASSERT(vp->v_usecount == 0 ||
2535 (vp->v_type != VCHR && vp->v_type != VBLK), vp,
2536 ("device VNODE %p is FORCECLOSED", vp));
2542 vprint("vflush: busy vnode", vp);
2550 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
2552 * If just the root vnode is busy, and if its refcount
2553 * is equal to `rootrefs', then go ahead and kill it.
2556 KASSERT(busy > 0, ("vflush: not busy"));
2557 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp,
2558 ("vflush: usecount %d < rootrefs %d",
2559 rootvp->v_usecount, rootrefs));
2560 if (busy == 1 && rootvp->v_usecount == rootrefs) {
2561 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK);
2563 VOP_UNLOCK(rootvp, 0);
2569 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__,
2573 for (; rootrefs > 0; rootrefs--)
2579 * Recycle an unused vnode to the front of the free list.
2582 vrecycle(struct vnode *vp, struct thread *td)
2586 ASSERT_VOP_ELOCKED(vp, "vrecycle");
2587 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2590 if (vp->v_usecount == 0) {
2599 * Eliminate all activity associated with a vnode
2600 * in preparation for reuse.
2603 vgone(struct vnode *vp)
2611 * vgone, with the vp interlock held.
2614 vgonel(struct vnode *vp)
2621 ASSERT_VOP_ELOCKED(vp, "vgonel");
2622 ASSERT_VI_LOCKED(vp, "vgonel");
2623 VNASSERT(vp->v_holdcnt, vp,
2624 ("vgonel: vp %p has no reference.", vp));
2625 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2629 * Don't vgonel if we're already doomed.
2631 if (vp->v_iflag & VI_DOOMED)
2633 vp->v_iflag |= VI_DOOMED;
2635 * Check to see if the vnode is in use. If so, we have to call
2636 * VOP_CLOSE() and VOP_INACTIVE().
2638 active = vp->v_usecount;
2639 oweinact = (vp->v_iflag & VI_OWEINACT);
2642 * Clean out any buffers associated with the vnode.
2643 * If the flush fails, just toss the buffers.
2646 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd))
2647 (void) vn_start_secondary_write(vp, &mp, V_WAIT);
2648 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0)
2649 vinvalbuf(vp, 0, 0, 0);
2652 * If purging an active vnode, it must be closed and
2653 * deactivated before being reclaimed.
2656 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
2657 if (oweinact || active) {
2659 if ((vp->v_iflag & VI_DOINGINACT) == 0)
2664 * Reclaim the vnode.
2666 if (VOP_RECLAIM(vp, td))
2667 panic("vgone: cannot reclaim");
2669 vn_finished_secondary_write(mp);
2670 VNASSERT(vp->v_object == NULL, vp,
2671 ("vop_reclaim left v_object vp=%p, tag=%s", vp, vp->v_tag));
2673 * Clear the advisory locks and wake up waiting threads.
2675 lf_purgelocks(vp, &(vp->v_lockf));
2677 * Delete from old mount point vnode list.
2682 * Done with purge, reset to the standard lock and invalidate
2686 vp->v_vnlock = &vp->v_lock;
2687 vp->v_op = &dead_vnodeops;
2693 * Calculate the total number of references to a special device.
2696 vcount(struct vnode *vp)
2701 count = vp->v_rdev->si_usecount;
2707 * Same as above, but using the struct cdev *as argument
2710 count_dev(struct cdev *dev)
2715 count = dev->si_usecount;
2721 * Print out a description of a vnode.
2723 static char *typename[] =
2724 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD",
2728 vn_printf(struct vnode *vp, const char *fmt, ...)
2731 char buf[256], buf2[16];
2737 printf("%p: ", (void *)vp);
2738 printf("tag %s, type %s\n", vp->v_tag, typename[vp->v_type]);
2739 printf(" usecount %d, writecount %d, refcount %d mountedhere %p\n",
2740 vp->v_usecount, vp->v_writecount, vp->v_holdcnt, vp->v_mountedhere);
2743 if (vp->v_vflag & VV_ROOT)
2744 strlcat(buf, "|VV_ROOT", sizeof(buf));
2745 if (vp->v_vflag & VV_ISTTY)
2746 strlcat(buf, "|VV_ISTTY", sizeof(buf));
2747 if (vp->v_vflag & VV_NOSYNC)
2748 strlcat(buf, "|VV_NOSYNC", sizeof(buf));
2749 if (vp->v_vflag & VV_CACHEDLABEL)
2750 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf));
2751 if (vp->v_vflag & VV_TEXT)
2752 strlcat(buf, "|VV_TEXT", sizeof(buf));
2753 if (vp->v_vflag & VV_COPYONWRITE)
2754 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf));
2755 if (vp->v_vflag & VV_SYSTEM)
2756 strlcat(buf, "|VV_SYSTEM", sizeof(buf));
2757 if (vp->v_vflag & VV_PROCDEP)
2758 strlcat(buf, "|VV_PROCDEP", sizeof(buf));
2759 if (vp->v_vflag & VV_NOKNOTE)
2760 strlcat(buf, "|VV_NOKNOTE", sizeof(buf));
2761 if (vp->v_vflag & VV_DELETED)
2762 strlcat(buf, "|VV_DELETED", sizeof(buf));
2763 if (vp->v_vflag & VV_MD)
2764 strlcat(buf, "|VV_MD", sizeof(buf));
2765 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC |
2766 VV_CACHEDLABEL | VV_TEXT | VV_COPYONWRITE | VV_SYSTEM | VV_PROCDEP |
2767 VV_NOKNOTE | VV_DELETED | VV_MD);
2769 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags);
2770 strlcat(buf, buf2, sizeof(buf));
2772 if (vp->v_iflag & VI_MOUNT)
2773 strlcat(buf, "|VI_MOUNT", sizeof(buf));
2774 if (vp->v_iflag & VI_AGE)
2775 strlcat(buf, "|VI_AGE", sizeof(buf));
2776 if (vp->v_iflag & VI_DOOMED)
2777 strlcat(buf, "|VI_DOOMED", sizeof(buf));
2778 if (vp->v_iflag & VI_FREE)
2779 strlcat(buf, "|VI_FREE", sizeof(buf));
2780 if (vp->v_iflag & VI_DOINGINACT)
2781 strlcat(buf, "|VI_DOINGINACT", sizeof(buf));
2782 if (vp->v_iflag & VI_OWEINACT)
2783 strlcat(buf, "|VI_OWEINACT", sizeof(buf));
2784 flags = vp->v_iflag & ~(VI_MOUNT | VI_AGE | VI_DOOMED | VI_FREE |
2785 VI_DOINGINACT | VI_OWEINACT);
2787 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags);
2788 strlcat(buf, buf2, sizeof(buf));
2790 printf(" flags (%s)\n", buf + 1);
2791 if (mtx_owned(VI_MTX(vp)))
2792 printf(" VI_LOCKed");
2793 if (vp->v_object != NULL)
2794 printf(" v_object %p ref %d pages %d\n",
2795 vp->v_object, vp->v_object->ref_count,
2796 vp->v_object->resident_page_count);
2798 lockmgr_printinfo(vp->v_vnlock);
2799 if (vp->v_data != NULL)
2805 * List all of the locked vnodes in the system.
2806 * Called when debugging the kernel.
2808 DB_SHOW_COMMAND(lockedvnods, lockedvnodes)
2810 struct mount *mp, *nmp;
2814 * Note: because this is DDB, we can't obey the locking semantics
2815 * for these structures, which means we could catch an inconsistent
2816 * state and dereference a nasty pointer. Not much to be done
2819 db_printf("Locked vnodes\n");
2820 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2821 nmp = TAILQ_NEXT(mp, mnt_list);
2822 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
2823 if (vp->v_type != VMARKER &&
2827 nmp = TAILQ_NEXT(mp, mnt_list);
2832 * Show details about the given vnode.
2834 DB_SHOW_COMMAND(vnode, db_show_vnode)
2840 vp = (struct vnode *)addr;
2841 vn_printf(vp, "vnode ");
2845 * Show details about the given mount point.
2847 DB_SHOW_COMMAND(mount, db_show_mount)
2857 /* No address given, print short info about all mount points. */
2858 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2859 db_printf("%p %s on %s (%s)\n", mp,
2860 mp->mnt_stat.f_mntfromname,
2861 mp->mnt_stat.f_mntonname,
2862 mp->mnt_stat.f_fstypename);
2866 db_printf("\nMore info: show mount <addr>\n");
2870 mp = (struct mount *)addr;
2871 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname,
2872 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename);
2875 flags = mp->mnt_flag;
2876 #define MNT_FLAG(flag) do { \
2877 if (flags & (flag)) { \
2878 if (buf[0] != '\0') \
2879 strlcat(buf, ", ", sizeof(buf)); \
2880 strlcat(buf, (#flag) + 4, sizeof(buf)); \
2884 MNT_FLAG(MNT_RDONLY);
2885 MNT_FLAG(MNT_SYNCHRONOUS);
2886 MNT_FLAG(MNT_NOEXEC);
2887 MNT_FLAG(MNT_NOSUID);
2888 MNT_FLAG(MNT_UNION);
2889 MNT_FLAG(MNT_ASYNC);
2890 MNT_FLAG(MNT_SUIDDIR);
2891 MNT_FLAG(MNT_SOFTDEP);
2892 MNT_FLAG(MNT_NOSYMFOLLOW);
2893 MNT_FLAG(MNT_GJOURNAL);
2894 MNT_FLAG(MNT_MULTILABEL);
2896 MNT_FLAG(MNT_NOATIME);
2897 MNT_FLAG(MNT_NOCLUSTERR);
2898 MNT_FLAG(MNT_NOCLUSTERW);
2899 MNT_FLAG(MNT_NFS4ACLS);
2900 MNT_FLAG(MNT_EXRDONLY);
2901 MNT_FLAG(MNT_EXPORTED);
2902 MNT_FLAG(MNT_DEFEXPORTED);
2903 MNT_FLAG(MNT_EXPORTANON);
2904 MNT_FLAG(MNT_EXKERB);
2905 MNT_FLAG(MNT_EXPUBLIC);
2906 MNT_FLAG(MNT_LOCAL);
2907 MNT_FLAG(MNT_QUOTA);
2908 MNT_FLAG(MNT_ROOTFS);
2910 MNT_FLAG(MNT_IGNORE);
2911 MNT_FLAG(MNT_UPDATE);
2912 MNT_FLAG(MNT_DELEXPORT);
2913 MNT_FLAG(MNT_RELOAD);
2914 MNT_FLAG(MNT_FORCE);
2915 MNT_FLAG(MNT_SNAPSHOT);
2916 MNT_FLAG(MNT_BYFSID);
2920 strlcat(buf, ", ", sizeof(buf));
2921 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
2924 db_printf(" mnt_flag = %s\n", buf);
2927 flags = mp->mnt_kern_flag;
2928 #define MNT_KERN_FLAG(flag) do { \
2929 if (flags & (flag)) { \
2930 if (buf[0] != '\0') \
2931 strlcat(buf, ", ", sizeof(buf)); \
2932 strlcat(buf, (#flag) + 5, sizeof(buf)); \
2936 MNT_KERN_FLAG(MNTK_UNMOUNTF);
2937 MNT_KERN_FLAG(MNTK_ASYNC);
2938 MNT_KERN_FLAG(MNTK_SOFTDEP);
2939 MNT_KERN_FLAG(MNTK_NOINSMNTQ);
2940 MNT_KERN_FLAG(MNTK_UNMOUNT);
2941 MNT_KERN_FLAG(MNTK_MWAIT);
2942 MNT_KERN_FLAG(MNTK_SUSPEND);
2943 MNT_KERN_FLAG(MNTK_SUSPEND2);
2944 MNT_KERN_FLAG(MNTK_SUSPENDED);
2945 MNT_KERN_FLAG(MNTK_MPSAFE);
2946 MNT_KERN_FLAG(MNTK_NOKNOTE);
2947 MNT_KERN_FLAG(MNTK_LOOKUP_SHARED);
2948 #undef MNT_KERN_FLAG
2951 strlcat(buf, ", ", sizeof(buf));
2952 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
2955 db_printf(" mnt_kern_flag = %s\n", buf);
2957 db_printf(" mnt_opt = ");
2958 opt = TAILQ_FIRST(mp->mnt_opt);
2960 db_printf("%s", opt->name);
2961 opt = TAILQ_NEXT(opt, link);
2962 while (opt != NULL) {
2963 db_printf(", %s", opt->name);
2964 opt = TAILQ_NEXT(opt, link);
2970 db_printf(" mnt_stat = { version=%u type=%u flags=0x%016jx "
2971 "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju "
2972 "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju "
2973 "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n",
2974 (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags,
2975 (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize,
2976 (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree,
2977 (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files,
2978 (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites,
2979 (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads,
2980 (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax,
2981 (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]);
2983 db_printf(" mnt_cred = { uid=%u ruid=%u",
2984 (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid);
2985 if (jailed(mp->mnt_cred))
2986 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id);
2988 db_printf(" mnt_ref = %d\n", mp->mnt_ref);
2989 db_printf(" mnt_gen = %d\n", mp->mnt_gen);
2990 db_printf(" mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize);
2991 db_printf(" mnt_writeopcount = %d\n", mp->mnt_writeopcount);
2992 db_printf(" mnt_noasync = %u\n", mp->mnt_noasync);
2993 db_printf(" mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen);
2994 db_printf(" mnt_iosize_max = %d\n", mp->mnt_iosize_max);
2995 db_printf(" mnt_hashseed = %u\n", mp->mnt_hashseed);
2996 db_printf(" mnt_secondary_writes = %d\n", mp->mnt_secondary_writes);
2997 db_printf(" mnt_secondary_accwrites = %d\n",
2998 mp->mnt_secondary_accwrites);
2999 db_printf(" mnt_gjprovider = %s\n",
3000 mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL");
3003 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3004 if (vp->v_type != VMARKER) {
3005 vn_printf(vp, "vnode ");
3014 * Fill in a struct xvfsconf based on a struct vfsconf.
3017 vfsconf2x(struct sysctl_req *req, struct vfsconf *vfsp)
3019 struct xvfsconf xvfsp;
3021 bzero(&xvfsp, sizeof(xvfsp));
3022 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
3023 xvfsp.vfc_typenum = vfsp->vfc_typenum;
3024 xvfsp.vfc_refcount = vfsp->vfc_refcount;
3025 xvfsp.vfc_flags = vfsp->vfc_flags;
3027 * These are unused in userland, we keep them
3028 * to not break binary compatibility.
3030 xvfsp.vfc_vfsops = NULL;
3031 xvfsp.vfc_next = NULL;
3032 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3035 #ifdef COMPAT_FREEBSD32
3037 uint32_t vfc_vfsops;
3038 char vfc_name[MFSNAMELEN];
3039 int32_t vfc_typenum;
3040 int32_t vfc_refcount;
3046 vfsconf2x32(struct sysctl_req *req, struct vfsconf *vfsp)
3048 struct xvfsconf32 xvfsp;
3050 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
3051 xvfsp.vfc_typenum = vfsp->vfc_typenum;
3052 xvfsp.vfc_refcount = vfsp->vfc_refcount;
3053 xvfsp.vfc_flags = vfsp->vfc_flags;
3054 xvfsp.vfc_vfsops = 0;
3056 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3061 * Top level filesystem related information gathering.
3064 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS)
3066 struct vfsconf *vfsp;
3070 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3071 #ifdef COMPAT_FREEBSD32
3072 if (req->flags & SCTL_MASK32)
3073 error = vfsconf2x32(req, vfsp);
3076 error = vfsconf2x(req, vfsp);
3083 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLFLAG_RD, NULL, 0, sysctl_vfs_conflist,
3084 "S,xvfsconf", "List of all configured filesystems");
3086 #ifndef BURN_BRIDGES
3087 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS);
3090 vfs_sysctl(SYSCTL_HANDLER_ARGS)
3092 int *name = (int *)arg1 - 1; /* XXX */
3093 u_int namelen = arg2 + 1; /* XXX */
3094 struct vfsconf *vfsp;
3096 printf("WARNING: userland calling deprecated sysctl, "
3097 "please rebuild world\n");
3099 #if 1 || defined(COMPAT_PRELITE2)
3100 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
3102 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
3106 case VFS_MAXTYPENUM:
3109 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
3112 return (ENOTDIR); /* overloaded */
3113 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list)
3114 if (vfsp->vfc_typenum == name[2])
3117 return (EOPNOTSUPP);
3118 #ifdef COMPAT_FREEBSD32
3119 if (req->flags & SCTL_MASK32)
3120 return (vfsconf2x32(req, vfsp));
3123 return (vfsconf2x(req, vfsp));
3125 return (EOPNOTSUPP);
3128 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP,
3129 vfs_sysctl, "Generic filesystem");
3131 #if 1 || defined(COMPAT_PRELITE2)
3134 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
3137 struct vfsconf *vfsp;
3138 struct ovfsconf ovfs;
3140 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3141 bzero(&ovfs, sizeof(ovfs));
3142 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
3143 strcpy(ovfs.vfc_name, vfsp->vfc_name);
3144 ovfs.vfc_index = vfsp->vfc_typenum;
3145 ovfs.vfc_refcount = vfsp->vfc_refcount;
3146 ovfs.vfc_flags = vfsp->vfc_flags;
3147 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
3154 #endif /* 1 || COMPAT_PRELITE2 */
3155 #endif /* !BURN_BRIDGES */
3157 #define KINFO_VNODESLOP 10
3160 * Dump vnode list (via sysctl).
3164 sysctl_vnode(SYSCTL_HANDLER_ARGS)
3172 * Stale numvnodes access is not fatal here.
3175 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn;
3177 /* Make an estimate */
3178 return (SYSCTL_OUT(req, 0, len));
3180 error = sysctl_wire_old_buffer(req, 0);
3183 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK);
3185 mtx_lock(&mountlist_mtx);
3186 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3187 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK))
3190 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3194 xvn[n].xv_size = sizeof *xvn;
3195 xvn[n].xv_vnode = vp;
3196 xvn[n].xv_id = 0; /* XXX compat */
3197 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field
3199 XV_COPY(writecount);
3205 xvn[n].xv_flag = vp->v_vflag;
3207 switch (vp->v_type) {
3214 if (vp->v_rdev == NULL) {
3218 xvn[n].xv_dev = dev2udev(vp->v_rdev);
3221 xvn[n].xv_socket = vp->v_socket;
3224 xvn[n].xv_fifo = vp->v_fifoinfo;
3229 /* shouldn't happen? */
3237 mtx_lock(&mountlist_mtx);
3242 mtx_unlock(&mountlist_mtx);
3244 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn);
3249 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD,
3250 0, 0, sysctl_vnode, "S,xvnode", "");
3254 * Unmount all filesystems. The list is traversed in reverse order
3255 * of mounting to avoid dependencies.
3258 vfs_unmountall(void)
3264 KASSERT(curthread != NULL, ("vfs_unmountall: NULL curthread"));
3265 CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__);
3269 * Since this only runs when rebooting, it is not interlocked.
3271 while(!TAILQ_EMPTY(&mountlist)) {
3272 mp = TAILQ_LAST(&mountlist, mntlist);
3273 error = dounmount(mp, MNT_FORCE, td);
3275 TAILQ_REMOVE(&mountlist, mp, mnt_list);
3277 * XXX: Due to the way in which we mount the root
3278 * file system off of devfs, devfs will generate a
3279 * "busy" warning when we try to unmount it before
3280 * the root. Don't print a warning as a result in
3281 * order to avoid false positive errors that may
3282 * cause needless upset.
3284 if (strcmp(mp->mnt_vfc->vfc_name, "devfs") != 0) {
3285 printf("unmount of %s failed (",
3286 mp->mnt_stat.f_mntonname);
3290 printf("%d)\n", error);
3293 /* The unmount has removed mp from the mountlist */
3299 * perform msync on all vnodes under a mount point
3300 * the mount point must be locked.
3303 vfs_msync(struct mount *mp, int flags)
3305 struct vnode *vp, *mvp;
3306 struct vm_object *obj;
3308 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
3310 MNT_VNODE_FOREACH(vp, mp, mvp) {
3313 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0 &&
3314 (flags == MNT_WAIT || VOP_ISLOCKED(vp) == 0)) {
3317 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK,
3319 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */
3327 VM_OBJECT_LOCK(obj);
3328 vm_object_page_clean(obj, 0, 0,
3330 OBJPC_SYNC : OBJPC_NOSYNC);
3331 VM_OBJECT_UNLOCK(obj);
3343 * Mark a vnode as free, putting it up for recycling.
3346 vfree(struct vnode *vp)
3349 ASSERT_VI_LOCKED(vp, "vfree");
3350 mtx_lock(&vnode_free_list_mtx);
3351 VNASSERT(vp->v_op != NULL, vp, ("vfree: vnode already reclaimed."));
3352 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp, ("vnode already free"));
3353 VNASSERT(VSHOULDFREE(vp), vp, ("vfree: freeing when we shouldn't"));
3354 VNASSERT((vp->v_iflag & VI_DOOMED) == 0, vp,
3355 ("vfree: Freeing doomed vnode"));
3356 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3357 if (vp->v_iflag & VI_AGE) {
3358 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
3360 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
3363 vp->v_iflag &= ~VI_AGE;
3364 vp->v_iflag |= VI_FREE;
3365 mtx_unlock(&vnode_free_list_mtx);
3369 * Opposite of vfree() - mark a vnode as in use.
3372 vbusy(struct vnode *vp)
3374 ASSERT_VI_LOCKED(vp, "vbusy");
3375 VNASSERT((vp->v_iflag & VI_FREE) != 0, vp, ("vnode not free"));
3376 VNASSERT(vp->v_op != NULL, vp, ("vbusy: vnode already reclaimed."));
3377 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3379 mtx_lock(&vnode_free_list_mtx);
3380 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
3382 vp->v_iflag &= ~(VI_FREE|VI_AGE);
3383 mtx_unlock(&vnode_free_list_mtx);
3387 destroy_vpollinfo(struct vpollinfo *vi)
3389 seldrain(&vi->vpi_selinfo);
3390 knlist_destroy(&vi->vpi_selinfo.si_note);
3391 mtx_destroy(&vi->vpi_lock);
3392 uma_zfree(vnodepoll_zone, vi);
3396 * Initalize per-vnode helper structure to hold poll-related state.
3399 v_addpollinfo(struct vnode *vp)
3401 struct vpollinfo *vi;
3403 if (vp->v_pollinfo != NULL)
3405 vi = uma_zalloc(vnodepoll_zone, M_WAITOK);
3406 mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF);
3407 knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock,
3408 vfs_knlunlock, vfs_knl_assert_locked, vfs_knl_assert_unlocked);
3410 if (vp->v_pollinfo != NULL) {
3412 destroy_vpollinfo(vi);
3415 vp->v_pollinfo = vi;
3420 * Record a process's interest in events which might happen to
3421 * a vnode. Because poll uses the historic select-style interface
3422 * internally, this routine serves as both the ``check for any
3423 * pending events'' and the ``record my interest in future events''
3424 * functions. (These are done together, while the lock is held,
3425 * to avoid race conditions.)
3428 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
3432 mtx_lock(&vp->v_pollinfo->vpi_lock);
3433 if (vp->v_pollinfo->vpi_revents & events) {
3435 * This leaves events we are not interested
3436 * in available for the other process which
3437 * which presumably had requested them
3438 * (otherwise they would never have been
3441 events &= vp->v_pollinfo->vpi_revents;
3442 vp->v_pollinfo->vpi_revents &= ~events;
3444 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3447 vp->v_pollinfo->vpi_events |= events;
3448 selrecord(td, &vp->v_pollinfo->vpi_selinfo);
3449 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3454 * Routine to create and manage a filesystem syncer vnode.
3456 #define sync_close ((int (*)(struct vop_close_args *))nullop)
3457 static int sync_fsync(struct vop_fsync_args *);
3458 static int sync_inactive(struct vop_inactive_args *);
3459 static int sync_reclaim(struct vop_reclaim_args *);
3461 static struct vop_vector sync_vnodeops = {
3462 .vop_bypass = VOP_EOPNOTSUPP,
3463 .vop_close = sync_close, /* close */
3464 .vop_fsync = sync_fsync, /* fsync */
3465 .vop_inactive = sync_inactive, /* inactive */
3466 .vop_reclaim = sync_reclaim, /* reclaim */
3467 .vop_lock1 = vop_stdlock, /* lock */
3468 .vop_unlock = vop_stdunlock, /* unlock */
3469 .vop_islocked = vop_stdislocked, /* islocked */
3473 * Create a new filesystem syncer vnode for the specified mount point.
3476 vfs_allocate_syncvnode(struct mount *mp)
3480 static long start, incr, next;
3483 /* Allocate a new vnode */
3484 if ((error = getnewvnode("syncer", mp, &sync_vnodeops, &vp)) != 0) {
3485 mp->mnt_syncer = NULL;
3489 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3490 vp->v_vflag |= VV_FORCEINSMQ;
3491 error = insmntque(vp, mp);
3493 panic("vfs_allocate_syncvnode: insmntque failed");
3494 vp->v_vflag &= ~VV_FORCEINSMQ;
3497 * Place the vnode onto the syncer worklist. We attempt to
3498 * scatter them about on the list so that they will go off
3499 * at evenly distributed times even if all the filesystems
3500 * are mounted at once.
3503 if (next == 0 || next > syncer_maxdelay) {
3507 start = syncer_maxdelay / 2;
3508 incr = syncer_maxdelay;
3514 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0);
3515 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */
3516 mtx_lock(&sync_mtx);
3518 mtx_unlock(&sync_mtx);
3520 mp->mnt_syncer = vp;
3525 * Do a lazy sync of the filesystem.
3528 sync_fsync(struct vop_fsync_args *ap)
3530 struct vnode *syncvp = ap->a_vp;
3531 struct mount *mp = syncvp->v_mount;
3536 * We only need to do something if this is a lazy evaluation.
3538 if (ap->a_waitfor != MNT_LAZY)
3542 * Move ourselves to the back of the sync list.
3544 bo = &syncvp->v_bufobj;
3546 vn_syncer_add_to_worklist(bo, syncdelay);
3550 * Walk the list of vnodes pushing all that are dirty and
3551 * not already on the sync list.
3553 mtx_lock(&mountlist_mtx);
3554 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
3555 mtx_unlock(&mountlist_mtx);
3558 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) {
3562 save = curthread_pflags_set(TDP_SYNCIO);
3563 vfs_msync(mp, MNT_NOWAIT);
3564 error = VFS_SYNC(mp, MNT_LAZY);
3565 curthread_pflags_restore(save);
3566 vn_finished_write(mp);
3572 * The syncer vnode is no referenced.
3575 sync_inactive(struct vop_inactive_args *ap)
3583 * The syncer vnode is no longer needed and is being decommissioned.
3585 * Modifications to the worklist must be protected by sync_mtx.
3588 sync_reclaim(struct vop_reclaim_args *ap)
3590 struct vnode *vp = ap->a_vp;
3595 vp->v_mount->mnt_syncer = NULL;
3596 if (bo->bo_flag & BO_ONWORKLST) {
3597 mtx_lock(&sync_mtx);
3598 LIST_REMOVE(bo, bo_synclist);
3599 syncer_worklist_len--;
3601 mtx_unlock(&sync_mtx);
3602 bo->bo_flag &= ~BO_ONWORKLST;
3610 * Check if vnode represents a disk device
3613 vn_isdisk(struct vnode *vp, int *errp)
3619 if (vp->v_type != VCHR)
3621 else if (vp->v_rdev == NULL)
3623 else if (vp->v_rdev->si_devsw == NULL)
3625 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK))
3630 return (error == 0);
3634 * Common filesystem object access control check routine. Accepts a
3635 * vnode's type, "mode", uid and gid, requested access mode, credentials,
3636 * and optional call-by-reference privused argument allowing vaccess()
3637 * to indicate to the caller whether privilege was used to satisfy the
3638 * request (obsoleted). Returns 0 on success, or an errno on failure.
3641 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid,
3642 accmode_t accmode, struct ucred *cred, int *privused)
3644 accmode_t dac_granted;
3645 accmode_t priv_granted;
3647 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0,
3648 ("invalid bit in accmode"));
3651 * Look for a normal, non-privileged way to access the file/directory
3652 * as requested. If it exists, go with that.
3655 if (privused != NULL)
3660 /* Check the owner. */
3661 if (cred->cr_uid == file_uid) {
3662 dac_granted |= VADMIN;
3663 if (file_mode & S_IXUSR)
3664 dac_granted |= VEXEC;
3665 if (file_mode & S_IRUSR)
3666 dac_granted |= VREAD;
3667 if (file_mode & S_IWUSR)
3668 dac_granted |= (VWRITE | VAPPEND);
3670 if ((accmode & dac_granted) == accmode)
3676 /* Otherwise, check the groups (first match) */
3677 if (groupmember(file_gid, cred)) {
3678 if (file_mode & S_IXGRP)
3679 dac_granted |= VEXEC;
3680 if (file_mode & S_IRGRP)
3681 dac_granted |= VREAD;
3682 if (file_mode & S_IWGRP)
3683 dac_granted |= (VWRITE | VAPPEND);
3685 if ((accmode & dac_granted) == accmode)
3691 /* Otherwise, check everyone else. */
3692 if (file_mode & S_IXOTH)
3693 dac_granted |= VEXEC;
3694 if (file_mode & S_IROTH)
3695 dac_granted |= VREAD;
3696 if (file_mode & S_IWOTH)
3697 dac_granted |= (VWRITE | VAPPEND);
3698 if ((accmode & dac_granted) == accmode)
3703 * Build a privilege mask to determine if the set of privileges
3704 * satisfies the requirements when combined with the granted mask
3705 * from above. For each privilege, if the privilege is required,
3706 * bitwise or the request type onto the priv_granted mask.
3712 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC
3713 * requests, instead of PRIV_VFS_EXEC.
3715 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3716 !priv_check_cred(cred, PRIV_VFS_LOOKUP, 0))
3717 priv_granted |= VEXEC;
3719 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3720 !priv_check_cred(cred, PRIV_VFS_EXEC, 0))
3721 priv_granted |= VEXEC;
3724 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) &&
3725 !priv_check_cred(cred, PRIV_VFS_READ, 0))
3726 priv_granted |= VREAD;
3728 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) &&
3729 !priv_check_cred(cred, PRIV_VFS_WRITE, 0))
3730 priv_granted |= (VWRITE | VAPPEND);
3732 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) &&
3733 !priv_check_cred(cred, PRIV_VFS_ADMIN, 0))
3734 priv_granted |= VADMIN;
3736 if ((accmode & (priv_granted | dac_granted)) == accmode) {
3737 /* XXX audit: privilege used */
3738 if (privused != NULL)
3743 return ((accmode & VADMIN) ? EPERM : EACCES);
3747 * Credential check based on process requesting service, and per-attribute
3751 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred,
3752 struct thread *td, accmode_t accmode)
3756 * Kernel-invoked always succeeds.
3762 * Do not allow privileged processes in jail to directly manipulate
3763 * system attributes.
3765 switch (attrnamespace) {
3766 case EXTATTR_NAMESPACE_SYSTEM:
3767 /* Potentially should be: return (EPERM); */
3768 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM, 0));
3769 case EXTATTR_NAMESPACE_USER:
3770 return (VOP_ACCESS(vp, accmode, cred, td));
3776 #ifdef DEBUG_VFS_LOCKS
3778 * This only exists to supress warnings from unlocked specfs accesses. It is
3779 * no longer ok to have an unlocked VFS.
3781 #define IGNORE_LOCK(vp) (panicstr != NULL || (vp) == NULL || \
3782 (vp)->v_type == VCHR || (vp)->v_type == VBAD)
3784 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */
3785 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0,
3786 "Drop into debugger on lock violation");
3788 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */
3789 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex,
3790 0, "Check for interlock across VOPs");
3792 int vfs_badlock_print = 1; /* Print lock violations. */
3793 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print,
3794 0, "Print lock violations");
3797 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */
3798 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW,
3799 &vfs_badlock_backtrace, 0, "Print backtrace at lock violations");
3803 vfs_badlock(const char *msg, const char *str, struct vnode *vp)
3807 if (vfs_badlock_backtrace)
3810 if (vfs_badlock_print)
3811 printf("%s: %p %s\n", str, (void *)vp, msg);
3812 if (vfs_badlock_ddb)
3813 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
3817 assert_vi_locked(struct vnode *vp, const char *str)
3820 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp)))
3821 vfs_badlock("interlock is not locked but should be", str, vp);
3825 assert_vi_unlocked(struct vnode *vp, const char *str)
3828 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp)))
3829 vfs_badlock("interlock is locked but should not be", str, vp);
3833 assert_vop_locked(struct vnode *vp, const char *str)
3836 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == 0)
3837 vfs_badlock("is not locked but should be", str, vp);
3841 assert_vop_unlocked(struct vnode *vp, const char *str)
3844 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
3845 vfs_badlock("is locked but should not be", str, vp);
3849 assert_vop_elocked(struct vnode *vp, const char *str)
3852 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
3853 vfs_badlock("is not exclusive locked but should be", str, vp);
3858 assert_vop_elocked_other(struct vnode *vp, const char *str)
3861 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLOTHER)
3862 vfs_badlock("is not exclusive locked by another thread",
3867 assert_vop_slocked(struct vnode *vp, const char *str)
3870 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_SHARED)
3871 vfs_badlock("is not locked shared but should be", str, vp);
3874 #endif /* DEBUG_VFS_LOCKS */
3877 vop_rename_fail(struct vop_rename_args *ap)
3880 if (ap->a_tvp != NULL)
3882 if (ap->a_tdvp == ap->a_tvp)
3891 vop_rename_pre(void *ap)
3893 struct vop_rename_args *a = ap;
3895 #ifdef DEBUG_VFS_LOCKS
3897 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME");
3898 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME");
3899 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME");
3900 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME");
3902 /* Check the source (from). */
3903 if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock &&
3904 (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock))
3905 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked");
3906 if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock)
3907 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked");
3909 /* Check the target. */
3911 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked");
3912 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked");
3914 if (a->a_tdvp != a->a_fdvp)
3916 if (a->a_tvp != a->a_fvp)
3924 vop_strategy_pre(void *ap)
3926 #ifdef DEBUG_VFS_LOCKS
3927 struct vop_strategy_args *a;
3934 * Cluster ops lock their component buffers but not the IO container.
3936 if ((bp->b_flags & B_CLUSTER) != 0)
3939 if (panicstr == NULL && !BUF_ISLOCKED(bp)) {
3940 if (vfs_badlock_print)
3942 "VOP_STRATEGY: bp is not locked but should be\n");
3943 if (vfs_badlock_ddb)
3944 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
3950 vop_lookup_pre(void *ap)
3952 #ifdef DEBUG_VFS_LOCKS
3953 struct vop_lookup_args *a;
3958 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP");
3959 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP");
3964 vop_lookup_post(void *ap, int rc)
3966 #ifdef DEBUG_VFS_LOCKS
3967 struct vop_lookup_args *a;
3975 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP");
3976 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP");
3979 ASSERT_VOP_LOCKED(vp, "VOP_LOOKUP (child)");
3984 vop_lock_pre(void *ap)
3986 #ifdef DEBUG_VFS_LOCKS
3987 struct vop_lock1_args *a = ap;
3989 if ((a->a_flags & LK_INTERLOCK) == 0)
3990 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
3992 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK");
3997 vop_lock_post(void *ap, int rc)
3999 #ifdef DEBUG_VFS_LOCKS
4000 struct vop_lock1_args *a = ap;
4002 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
4004 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK");
4009 vop_unlock_pre(void *ap)
4011 #ifdef DEBUG_VFS_LOCKS
4012 struct vop_unlock_args *a = ap;
4014 if (a->a_flags & LK_INTERLOCK)
4015 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK");
4016 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK");
4021 vop_unlock_post(void *ap, int rc)
4023 #ifdef DEBUG_VFS_LOCKS
4024 struct vop_unlock_args *a = ap;
4026 if (a->a_flags & LK_INTERLOCK)
4027 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK");
4032 vop_create_post(void *ap, int rc)
4034 struct vop_create_args *a = ap;
4037 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4041 vop_deleteextattr_post(void *ap, int rc)
4043 struct vop_deleteextattr_args *a = ap;
4046 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4050 vop_link_post(void *ap, int rc)
4052 struct vop_link_args *a = ap;
4055 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK);
4056 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE);
4061 vop_mkdir_post(void *ap, int rc)
4063 struct vop_mkdir_args *a = ap;
4066 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4070 vop_mknod_post(void *ap, int rc)
4072 struct vop_mknod_args *a = ap;
4075 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4079 vop_remove_post(void *ap, int rc)
4081 struct vop_remove_args *a = ap;
4084 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4085 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4090 vop_rename_post(void *ap, int rc)
4092 struct vop_rename_args *a = ap;
4095 VFS_KNOTE_UNLOCKED(a->a_fdvp, NOTE_WRITE);
4096 VFS_KNOTE_UNLOCKED(a->a_tdvp, NOTE_WRITE);
4097 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME);
4099 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE);
4101 if (a->a_tdvp != a->a_fdvp)
4103 if (a->a_tvp != a->a_fvp)
4111 vop_rmdir_post(void *ap, int rc)
4113 struct vop_rmdir_args *a = ap;
4116 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4117 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4122 vop_setattr_post(void *ap, int rc)
4124 struct vop_setattr_args *a = ap;
4127 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4131 vop_setextattr_post(void *ap, int rc)
4133 struct vop_setextattr_args *a = ap;
4136 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4140 vop_symlink_post(void *ap, int rc)
4142 struct vop_symlink_args *a = ap;
4145 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4148 static struct knlist fs_knlist;
4151 vfs_event_init(void *arg)
4153 knlist_init_mtx(&fs_knlist, NULL);
4155 /* XXX - correct order? */
4156 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL);
4159 vfs_event_signal(fsid_t *fsid, u_int32_t event, intptr_t data __unused)
4162 KNOTE_UNLOCKED(&fs_knlist, event);
4165 static int filt_fsattach(struct knote *kn);
4166 static void filt_fsdetach(struct knote *kn);
4167 static int filt_fsevent(struct knote *kn, long hint);
4169 struct filterops fs_filtops =
4170 { 0, filt_fsattach, filt_fsdetach, filt_fsevent };
4173 filt_fsattach(struct knote *kn)
4176 kn->kn_flags |= EV_CLEAR;
4177 knlist_add(&fs_knlist, kn, 0);
4182 filt_fsdetach(struct knote *kn)
4185 knlist_remove(&fs_knlist, kn, 0);
4189 filt_fsevent(struct knote *kn, long hint)
4192 kn->kn_fflags |= hint;
4193 return (kn->kn_fflags != 0);
4197 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS)
4203 error = SYSCTL_IN(req, &vc, sizeof(vc));
4206 if (vc.vc_vers != VFS_CTL_VERS1)
4208 mp = vfs_getvfs(&vc.vc_fsid);
4211 /* ensure that a specific sysctl goes to the right filesystem. */
4212 if (strcmp(vc.vc_fstypename, "*") != 0 &&
4213 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) {
4217 VCTLTOREQ(&vc, req);
4218 error = VFS_SYSCTL(mp, vc.vc_op, req);
4223 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLFLAG_WR, NULL, 0, sysctl_vfs_ctl, "",
4227 * Function to initialize a va_filerev field sensibly.
4228 * XXX: Wouldn't a random number make a lot more sense ??
4231 init_va_filerev(void)
4236 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL));
4239 static int filt_vfsread(struct knote *kn, long hint);
4240 static int filt_vfswrite(struct knote *kn, long hint);
4241 static int filt_vfsvnode(struct knote *kn, long hint);
4242 static void filt_vfsdetach(struct knote *kn);
4243 static struct filterops vfsread_filtops =
4244 { 1, NULL, filt_vfsdetach, filt_vfsread };
4245 static struct filterops vfswrite_filtops =
4246 { 1, NULL, filt_vfsdetach, filt_vfswrite };
4247 static struct filterops vfsvnode_filtops =
4248 { 1, NULL, filt_vfsdetach, filt_vfsvnode };
4251 vfs_knllock(void *arg)
4253 struct vnode *vp = arg;
4255 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4259 vfs_knlunlock(void *arg)
4261 struct vnode *vp = arg;
4267 vfs_knl_assert_locked(void *arg)
4269 #ifdef DEBUG_VFS_LOCKS
4270 struct vnode *vp = arg;
4272 ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked");
4277 vfs_knl_assert_unlocked(void *arg)
4279 #ifdef DEBUG_VFS_LOCKS
4280 struct vnode *vp = arg;
4282 ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked");
4287 vfs_kqfilter(struct vop_kqfilter_args *ap)
4289 struct vnode *vp = ap->a_vp;
4290 struct knote *kn = ap->a_kn;
4293 switch (kn->kn_filter) {
4295 kn->kn_fop = &vfsread_filtops;
4298 kn->kn_fop = &vfswrite_filtops;
4301 kn->kn_fop = &vfsvnode_filtops;
4307 kn->kn_hook = (caddr_t)vp;
4310 if (vp->v_pollinfo == NULL)
4312 knl = &vp->v_pollinfo->vpi_selinfo.si_note;
4313 knlist_add(knl, kn, 0);
4319 * Detach knote from vnode
4322 filt_vfsdetach(struct knote *kn)
4324 struct vnode *vp = (struct vnode *)kn->kn_hook;
4326 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo"));
4327 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
4332 filt_vfsread(struct knote *kn, long hint)
4334 struct vnode *vp = (struct vnode *)kn->kn_hook;
4339 * filesystem is gone, so set the EOF flag and schedule
4340 * the knote for deletion.
4342 if (hint == NOTE_REVOKE) {
4344 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4349 if (VOP_GETATTR(vp, &va, curthread->td_ucred))
4353 kn->kn_data = va.va_size - kn->kn_fp->f_offset;
4354 res = (kn->kn_data != 0);
4361 filt_vfswrite(struct knote *kn, long hint)
4363 struct vnode *vp = (struct vnode *)kn->kn_hook;
4368 * filesystem is gone, so set the EOF flag and schedule
4369 * the knote for deletion.
4371 if (hint == NOTE_REVOKE)
4372 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4380 filt_vfsvnode(struct knote *kn, long hint)
4382 struct vnode *vp = (struct vnode *)kn->kn_hook;
4386 if (kn->kn_sfflags & hint)
4387 kn->kn_fflags |= hint;
4388 if (hint == NOTE_REVOKE) {
4389 kn->kn_flags |= EV_EOF;
4393 res = (kn->kn_fflags != 0);
4399 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off)
4403 if (dp->d_reclen > ap->a_uio->uio_resid)
4404 return (ENAMETOOLONG);
4405 error = uiomove(dp, dp->d_reclen, ap->a_uio);
4407 if (ap->a_ncookies != NULL) {
4408 if (ap->a_cookies != NULL)
4409 free(ap->a_cookies, M_TEMP);
4410 ap->a_cookies = NULL;
4411 *ap->a_ncookies = 0;
4415 if (ap->a_ncookies == NULL)
4418 KASSERT(ap->a_cookies,
4419 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!"));
4421 *ap->a_cookies = realloc(*ap->a_cookies,
4422 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO);
4423 (*ap->a_cookies)[*ap->a_ncookies] = off;
4428 * Mark for update the access time of the file if the filesystem
4429 * supports VOP_MARKATIME. This functionality is used by execve and
4430 * mmap, so we want to avoid the I/O implied by directly setting
4431 * va_atime for the sake of efficiency.
4434 vfs_mark_atime(struct vnode *vp, struct ucred *cred)
4439 VFS_ASSERT_GIANT(mp);
4440 ASSERT_VOP_LOCKED(vp, "vfs_mark_atime");
4441 if (mp != NULL && (mp->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
4442 (void)VOP_MARKATIME(vp);
4446 * The purpose of this routine is to remove granularity from accmode_t,
4447 * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE,
4448 * VADMIN and VAPPEND.
4450 * If it returns 0, the caller is supposed to continue with the usual
4451 * access checks using 'accmode' as modified by this routine. If it
4452 * returns nonzero value, the caller is supposed to return that value
4455 * Note that after this routine runs, accmode may be zero.
4458 vfs_unixify_accmode(accmode_t *accmode)
4461 * There is no way to specify explicit "deny" rule using
4462 * file mode or POSIX.1e ACLs.
4464 if (*accmode & VEXPLICIT_DENY) {
4470 * None of these can be translated into usual access bits.
4471 * Also, the common case for NFSv4 ACLs is to not contain
4472 * either of these bits. Caller should check for VWRITE
4473 * on the containing directory instead.
4475 if (*accmode & (VDELETE_CHILD | VDELETE))
4478 if (*accmode & VADMIN_PERMS) {
4479 *accmode &= ~VADMIN_PERMS;
4484 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL
4485 * or VSYNCHRONIZE using file mode or POSIX.1e ACL.
4487 *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE);