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 v_incr_usecount(struct vnode *);
105 static void v_decr_usecount(struct vnode *);
106 static void v_decr_useonly(struct vnode *);
107 static void v_upgrade_usecount(struct vnode *);
108 static void vnlru_free(int);
109 static void vgonel(struct vnode *);
110 static void vfs_knllock(void *arg);
111 static void vfs_knlunlock(void *arg);
112 static void vfs_knl_assert_locked(void *arg);
113 static void vfs_knl_assert_unlocked(void *arg);
114 static void destroy_vpollinfo(struct vpollinfo *vi);
117 * Number of vnodes in existence. Increased whenever getnewvnode()
118 * allocates a new vnode, decreased in vdropl() for VI_DOOMED vnode.
120 static unsigned long numvnodes;
122 SYSCTL_ULONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0,
123 "Number of vnodes in existence");
126 * Conversion tables for conversion from vnode types to inode formats
129 enum vtype iftovt_tab[16] = {
130 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
131 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
133 int vttoif_tab[10] = {
134 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
135 S_IFSOCK, S_IFIFO, S_IFMT, S_IFMT
139 * List of vnodes that are ready for recycling.
141 static TAILQ_HEAD(freelst, vnode) vnode_free_list;
144 * Free vnode target. Free vnodes may simply be files which have been stat'd
145 * but not read. This is somewhat common, and a small cache of such files
146 * should be kept to avoid recreation costs.
148 static u_long wantfreevnodes;
149 SYSCTL_ULONG(_vfs, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, "");
150 /* Number of vnodes in the free list. */
151 static u_long freevnodes;
152 SYSCTL_ULONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0,
153 "Number of vnodes in the free list");
155 static int vlru_allow_cache_src;
156 SYSCTL_INT(_vfs, OID_AUTO, vlru_allow_cache_src, CTLFLAG_RW,
157 &vlru_allow_cache_src, 0, "Allow vlru to reclaim source vnode");
160 * Various variables used for debugging the new implementation of
162 * XXX these are probably of (very) limited utility now.
164 static int reassignbufcalls;
165 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0,
166 "Number of calls to reassignbuf");
169 * Cache for the mount type id assigned to NFS. This is used for
170 * special checks in nfs/nfs_nqlease.c and vm/vnode_pager.c.
172 int nfs_mount_type = -1;
174 /* To keep more than one thread at a time from running vfs_getnewfsid */
175 static struct mtx mntid_mtx;
178 * Lock for any access to the following:
183 static struct mtx vnode_free_list_mtx;
185 /* Publicly exported FS */
186 struct nfs_public nfs_pub;
188 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */
189 static uma_zone_t vnode_zone;
190 static uma_zone_t vnodepoll_zone;
193 * The workitem queue.
195 * It is useful to delay writes of file data and filesystem metadata
196 * for tens of seconds so that quickly created and deleted files need
197 * not waste disk bandwidth being created and removed. To realize this,
198 * we append vnodes to a "workitem" queue. When running with a soft
199 * updates implementation, most pending metadata dependencies should
200 * not wait for more than a few seconds. Thus, mounted on block devices
201 * are delayed only about a half the time that file data is delayed.
202 * Similarly, directory updates are more critical, so are only delayed
203 * about a third the time that file data is delayed. Thus, there are
204 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
205 * one each second (driven off the filesystem syncer process). The
206 * syncer_delayno variable indicates the next queue that is to be processed.
207 * Items that need to be processed soon are placed in this queue:
209 * syncer_workitem_pending[syncer_delayno]
211 * A delay of fifteen seconds is done by placing the request fifteen
212 * entries later in the queue:
214 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
217 static int syncer_delayno;
218 static long syncer_mask;
219 LIST_HEAD(synclist, bufobj);
220 static struct synclist *syncer_workitem_pending[2];
222 * The sync_mtx protects:
227 * syncer_workitem_pending
228 * syncer_worklist_len
231 static struct mtx sync_mtx;
232 static struct cv sync_wakeup;
234 #define SYNCER_MAXDELAY 32
235 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
236 static int syncdelay = 30; /* max time to delay syncing data */
237 static int filedelay = 30; /* time to delay syncing files */
238 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0,
239 "Time to delay syncing files (in seconds)");
240 static int dirdelay = 29; /* time to delay syncing directories */
241 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0,
242 "Time to delay syncing directories (in seconds)");
243 static int metadelay = 28; /* time to delay syncing metadata */
244 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0,
245 "Time to delay syncing metadata (in seconds)");
246 static int rushjob; /* number of slots to run ASAP */
247 static int stat_rush_requests; /* number of times I/O speeded up */
248 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0,
249 "Number of times I/O speeded up (rush requests)");
252 * When shutting down the syncer, run it at four times normal speed.
254 #define SYNCER_SHUTDOWN_SPEEDUP 4
255 static int sync_vnode_count;
256 static int syncer_worklist_len;
257 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY }
261 * Number of vnodes we want to exist at any one time. This is mostly used
262 * to size hash tables in vnode-related code. It is normally not used in
263 * getnewvnode(), as wantfreevnodes is normally nonzero.)
265 * XXX desiredvnodes is historical cruft and should not exist.
268 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
269 &desiredvnodes, 0, "Maximum number of vnodes");
270 SYSCTL_ULONG(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
271 &wantfreevnodes, 0, "Minimum number of vnodes (legacy)");
272 static int vnlru_nowhere;
273 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW,
274 &vnlru_nowhere, 0, "Number of times the vnlru process ran without success");
277 * Macros to control when a vnode is freed and recycled. All require
278 * the vnode interlock.
280 #define VCANRECYCLE(vp) (((vp)->v_iflag & VI_FREE) && !(vp)->v_holdcnt)
281 #define VSHOULDFREE(vp) (!((vp)->v_iflag & VI_FREE) && !(vp)->v_holdcnt)
282 #define VSHOULDBUSY(vp) (((vp)->v_iflag & VI_FREE) && (vp)->v_holdcnt)
286 * Initialize the vnode management data structures.
288 * Reevaluate the following cap on the number of vnodes after the physical
289 * memory size exceeds 512GB. In the limit, as the physical memory size
290 * grows, the ratio of physical pages to vnodes approaches sixteen to one.
292 #ifndef MAXVNODES_MAX
293 #define MAXVNODES_MAX (512 * (1024 * 1024 * 1024 / (int)PAGE_SIZE / 16))
296 vntblinit(void *dummy __unused)
298 int physvnodes, virtvnodes;
301 * Desiredvnodes is a function of the physical memory size and the
302 * kernel's heap size. Generally speaking, it scales with the
303 * physical memory size. The ratio of desiredvnodes to physical pages
304 * is one to four until desiredvnodes exceeds 98,304. Thereafter, the
305 * marginal ratio of desiredvnodes to physical pages is one to
306 * sixteen. However, desiredvnodes is limited by the kernel's heap
307 * size. The memory required by desiredvnodes vnodes and vm objects
308 * may not exceed one seventh of the kernel's heap size.
310 physvnodes = maxproc + cnt.v_page_count / 16 + 3 * min(98304 * 4,
311 cnt.v_page_count) / 16;
312 virtvnodes = vm_kmem_size / (7 * (sizeof(struct vm_object) +
313 sizeof(struct vnode)));
314 desiredvnodes = min(physvnodes, virtvnodes);
315 if (desiredvnodes > MAXVNODES_MAX) {
317 printf("Reducing kern.maxvnodes %d -> %d\n",
318 desiredvnodes, MAXVNODES_MAX);
319 desiredvnodes = MAXVNODES_MAX;
321 wantfreevnodes = desiredvnodes / 4;
322 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF);
323 TAILQ_INIT(&vnode_free_list);
324 mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF);
325 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL,
326 NULL, NULL, UMA_ALIGN_PTR, 0);
327 vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo),
328 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
330 * Initialize the filesystem syncer.
332 syncer_workitem_pending[WI_MPSAFEQ] = hashinit(syncer_maxdelay, M_VNODE,
334 syncer_workitem_pending[WI_GIANTQ] = hashinit(syncer_maxdelay, M_VNODE,
336 syncer_maxdelay = syncer_mask + 1;
337 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF);
338 cv_init(&sync_wakeup, "syncer");
340 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL);
344 * Mark a mount point as busy. Used to synchronize access and to delay
345 * unmounting. Eventually, mountlist_mtx is not released on failure.
347 * vfs_busy() is a custom lock, it can block the caller.
348 * vfs_busy() only sleeps if the unmount is active on the mount point.
349 * For a mountpoint mp, vfs_busy-enforced lock is before lock of any
350 * vnode belonging to mp.
352 * Lookup uses vfs_busy() to traverse mount points.
354 * / vnode lock A / vnode lock (/var) D
355 * /var vnode lock B /log vnode lock(/var/log) E
356 * vfs_busy lock C vfs_busy lock F
358 * Within each file system, the lock order is C->A->B and F->D->E.
360 * When traversing across mounts, the system follows that lock order:
366 * The lookup() process for namei("/var") illustrates the process:
367 * VOP_LOOKUP() obtains B while A is held
368 * vfs_busy() obtains a shared lock on F while A and B are held
369 * vput() releases lock on B
370 * vput() releases lock on A
371 * VFS_ROOT() obtains lock on D while shared lock on F is held
372 * vfs_unbusy() releases shared lock on F
373 * vn_lock() obtains lock on deadfs vnode vp_crossmp instead of A.
374 * Attempt to lock A (instead of vp_crossmp) while D is held would
375 * violate the global order, causing deadlocks.
377 * dounmount() locks B while F is drained.
380 vfs_busy(struct mount *mp, int flags)
383 MPASS((flags & ~MBF_MASK) == 0);
384 CTR3(KTR_VFS, "%s: mp %p with flags %d", __func__, mp, flags);
389 * If mount point is currenly being unmounted, sleep until the
390 * mount point fate is decided. If thread doing the unmounting fails,
391 * it will clear MNTK_UNMOUNT flag before waking us up, indicating
392 * that this mount point has survived the unmount attempt and vfs_busy
393 * should retry. Otherwise the unmounter thread will set MNTK_REFEXPIRE
394 * flag in addition to MNTK_UNMOUNT, indicating that mount point is
395 * about to be really destroyed. vfs_busy needs to release its
396 * reference on the mount point in this case and return with ENOENT,
397 * telling the caller that mount mount it tried to busy is no longer
400 while (mp->mnt_kern_flag & MNTK_UNMOUNT) {
401 if (flags & MBF_NOWAIT || mp->mnt_kern_flag & MNTK_REFEXPIRE) {
404 CTR1(KTR_VFS, "%s: failed busying before sleeping",
408 if (flags & MBF_MNTLSTLOCK)
409 mtx_unlock(&mountlist_mtx);
410 mp->mnt_kern_flag |= MNTK_MWAIT;
411 msleep(mp, MNT_MTX(mp), PVFS | PDROP, "vfs_busy", 0);
412 if (flags & MBF_MNTLSTLOCK)
413 mtx_lock(&mountlist_mtx);
416 if (flags & MBF_MNTLSTLOCK)
417 mtx_unlock(&mountlist_mtx);
424 * Free a busy filesystem.
427 vfs_unbusy(struct mount *mp)
430 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
433 KASSERT(mp->mnt_lockref > 0, ("negative mnt_lockref"));
435 if (mp->mnt_lockref == 0 && (mp->mnt_kern_flag & MNTK_DRAINING) != 0) {
436 MPASS(mp->mnt_kern_flag & MNTK_UNMOUNT);
437 CTR1(KTR_VFS, "%s: waking up waiters", __func__);
438 mp->mnt_kern_flag &= ~MNTK_DRAINING;
439 wakeup(&mp->mnt_lockref);
445 * Lookup a mount point by filesystem identifier.
448 vfs_getvfs(fsid_t *fsid)
452 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
453 mtx_lock(&mountlist_mtx);
454 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
455 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
456 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
458 mtx_unlock(&mountlist_mtx);
462 mtx_unlock(&mountlist_mtx);
463 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
464 return ((struct mount *) 0);
468 * Lookup a mount point by filesystem identifier, busying it before
472 vfs_busyfs(fsid_t *fsid)
477 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
478 mtx_lock(&mountlist_mtx);
479 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
480 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
481 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
482 error = vfs_busy(mp, MBF_MNTLSTLOCK);
484 mtx_unlock(&mountlist_mtx);
490 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
491 mtx_unlock(&mountlist_mtx);
492 return ((struct mount *) 0);
496 * Check if a user can access privileged mount options.
499 vfs_suser(struct mount *mp, struct thread *td)
504 * If the thread is jailed, but this is not a jail-friendly file
505 * system, deny immediately.
507 if (!(mp->mnt_vfc->vfc_flags & VFCF_JAIL) && jailed(td->td_ucred))
511 * If the file system was mounted outside the jail of the calling
512 * thread, deny immediately.
514 if (prison_check(td->td_ucred, mp->mnt_cred) != 0)
518 * If file system supports delegated administration, we don't check
519 * for the PRIV_VFS_MOUNT_OWNER privilege - it will be better verified
520 * by the file system itself.
521 * If this is not the user that did original mount, we check for
522 * the PRIV_VFS_MOUNT_OWNER privilege.
524 if (!(mp->mnt_vfc->vfc_flags & VFCF_DELEGADMIN) &&
525 mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
526 if ((error = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 0)
533 * Get a new unique fsid. Try to make its val[0] unique, since this value
534 * will be used to create fake device numbers for stat(). Also try (but
535 * not so hard) make its val[0] unique mod 2^16, since some emulators only
536 * support 16-bit device numbers. We end up with unique val[0]'s for the
537 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
539 * Keep in mind that several mounts may be running in parallel. Starting
540 * the search one past where the previous search terminated is both a
541 * micro-optimization and a defense against returning the same fsid to
545 vfs_getnewfsid(struct mount *mp)
547 static uint16_t mntid_base;
552 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
553 mtx_lock(&mntid_mtx);
554 mtype = mp->mnt_vfc->vfc_typenum;
555 tfsid.val[1] = mtype;
556 mtype = (mtype & 0xFF) << 24;
558 tfsid.val[0] = makedev(255,
559 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
561 if ((nmp = vfs_getvfs(&tfsid)) == NULL)
565 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
566 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
567 mtx_unlock(&mntid_mtx);
571 * Knob to control the precision of file timestamps:
573 * 0 = seconds only; nanoseconds zeroed.
574 * 1 = seconds and nanoseconds, accurate within 1/HZ.
575 * 2 = seconds and nanoseconds, truncated to microseconds.
576 * >=3 = seconds and nanoseconds, maximum precision.
578 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
580 static int timestamp_precision = TSP_SEC;
581 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
582 ×tamp_precision, 0, "File timestamp precision (0: seconds, "
583 "1: sec + ns accurate to 1/HZ, 2: sec + ns truncated to ms, "
584 "3+: sec + ns (max. precision))");
587 * Get a current timestamp.
590 vfs_timestamp(struct timespec *tsp)
594 switch (timestamp_precision) {
596 tsp->tv_sec = time_second;
604 TIMEVAL_TO_TIMESPEC(&tv, tsp);
614 * Set vnode attributes to VNOVAL
617 vattr_null(struct vattr *vap)
621 vap->va_size = VNOVAL;
622 vap->va_bytes = VNOVAL;
623 vap->va_mode = VNOVAL;
624 vap->va_nlink = VNOVAL;
625 vap->va_uid = VNOVAL;
626 vap->va_gid = VNOVAL;
627 vap->va_fsid = VNOVAL;
628 vap->va_fileid = VNOVAL;
629 vap->va_blocksize = VNOVAL;
630 vap->va_rdev = VNOVAL;
631 vap->va_atime.tv_sec = VNOVAL;
632 vap->va_atime.tv_nsec = VNOVAL;
633 vap->va_mtime.tv_sec = VNOVAL;
634 vap->va_mtime.tv_nsec = VNOVAL;
635 vap->va_ctime.tv_sec = VNOVAL;
636 vap->va_ctime.tv_nsec = VNOVAL;
637 vap->va_birthtime.tv_sec = VNOVAL;
638 vap->va_birthtime.tv_nsec = VNOVAL;
639 vap->va_flags = VNOVAL;
640 vap->va_gen = VNOVAL;
645 * This routine is called when we have too many vnodes. It attempts
646 * to free <count> vnodes and will potentially free vnodes that still
647 * have VM backing store (VM backing store is typically the cause
648 * of a vnode blowout so we want to do this). Therefore, this operation
649 * is not considered cheap.
651 * A number of conditions may prevent a vnode from being reclaimed.
652 * the buffer cache may have references on the vnode, a directory
653 * vnode may still have references due to the namei cache representing
654 * underlying files, or the vnode may be in active use. It is not
655 * desireable to reuse such vnodes. These conditions may cause the
656 * number of vnodes to reach some minimum value regardless of what
657 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
660 vlrureclaim(struct mount *mp)
669 * Calculate the trigger point, don't allow user
670 * screwups to blow us up. This prevents us from
671 * recycling vnodes with lots of resident pages. We
672 * aren't trying to free memory, we are trying to
675 usevnodes = desiredvnodes;
678 trigger = cnt.v_page_count * 2 / usevnodes;
680 vn_start_write(NULL, &mp, V_WAIT);
682 count = mp->mnt_nvnodelistsize / 10 + 1;
684 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
685 while (vp != NULL && vp->v_type == VMARKER)
686 vp = TAILQ_NEXT(vp, v_nmntvnodes);
689 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
690 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
695 * If it's been deconstructed already, it's still
696 * referenced, or it exceeds the trigger, skip it.
698 if (vp->v_usecount ||
699 (!vlru_allow_cache_src &&
700 !LIST_EMPTY(&(vp)->v_cache_src)) ||
701 (vp->v_iflag & VI_DOOMED) != 0 || (vp->v_object != NULL &&
702 vp->v_object->resident_page_count > trigger)) {
708 if (VOP_LOCK(vp, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT)) {
710 goto next_iter_mntunlocked;
714 * v_usecount may have been bumped after VOP_LOCK() dropped
715 * the vnode interlock and before it was locked again.
717 * It is not necessary to recheck VI_DOOMED because it can
718 * only be set by another thread that holds both the vnode
719 * lock and vnode interlock. If another thread has the
720 * vnode lock before we get to VOP_LOCK() and obtains the
721 * vnode interlock after VOP_LOCK() drops the vnode
722 * interlock, the other thread will be unable to drop the
723 * vnode lock before our VOP_LOCK() call fails.
725 if (vp->v_usecount ||
726 (!vlru_allow_cache_src &&
727 !LIST_EMPTY(&(vp)->v_cache_src)) ||
728 (vp->v_object != NULL &&
729 vp->v_object->resident_page_count > trigger)) {
730 VOP_UNLOCK(vp, LK_INTERLOCK);
731 goto next_iter_mntunlocked;
733 KASSERT((vp->v_iflag & VI_DOOMED) == 0,
734 ("VI_DOOMED unexpectedly detected in vlrureclaim()"));
739 next_iter_mntunlocked:
748 kern_yield(PRI_UNCHANGED);
753 vn_finished_write(mp);
758 * Attempt to keep the free list at wantfreevnodes length.
761 vnlru_free(int count)
766 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
767 for (; count > 0; count--) {
768 vp = TAILQ_FIRST(&vnode_free_list);
770 * The list can be modified while the free_list_mtx
771 * has been dropped and vp could be NULL here.
775 VNASSERT(vp->v_op != NULL, vp,
776 ("vnlru_free: vnode already reclaimed."));
777 KASSERT((vp->v_iflag & VI_FREE) != 0,
778 ("Removing vnode not on freelist"));
779 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
780 ("Mangling active vnode"));
781 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
783 * Don't recycle if we can't get the interlock.
785 if (!VI_TRYLOCK(vp)) {
786 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_actfreelist);
789 VNASSERT(VCANRECYCLE(vp), vp,
790 ("vp inconsistent on freelist"));
792 vp->v_iflag &= ~VI_FREE;
794 mtx_unlock(&vnode_free_list_mtx);
796 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
798 VFS_UNLOCK_GIANT(vfslocked);
800 * If the recycled succeeded this vdrop will actually free
801 * the vnode. If not it will simply place it back on
805 mtx_lock(&vnode_free_list_mtx);
809 * Attempt to recycle vnodes in a context that is always safe to block.
810 * Calling vlrurecycle() from the bowels of filesystem code has some
811 * interesting deadlock problems.
813 static struct proc *vnlruproc;
814 static int vnlruproc_sig;
819 struct mount *mp, *nmp;
821 struct proc *p = vnlruproc;
823 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, p,
827 kproc_suspend_check(p);
828 mtx_lock(&vnode_free_list_mtx);
829 if (freevnodes > wantfreevnodes)
830 vnlru_free(freevnodes - wantfreevnodes);
831 if (numvnodes <= desiredvnodes * 9 / 10) {
833 wakeup(&vnlruproc_sig);
834 msleep(vnlruproc, &vnode_free_list_mtx,
835 PVFS|PDROP, "vlruwt", hz);
838 mtx_unlock(&vnode_free_list_mtx);
840 mtx_lock(&mountlist_mtx);
841 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
842 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
843 nmp = TAILQ_NEXT(mp, mnt_list);
846 vfslocked = VFS_LOCK_GIANT(mp);
847 done += vlrureclaim(mp);
848 VFS_UNLOCK_GIANT(vfslocked);
849 mtx_lock(&mountlist_mtx);
850 nmp = TAILQ_NEXT(mp, mnt_list);
853 mtx_unlock(&mountlist_mtx);
856 /* These messages are temporary debugging aids */
857 if (vnlru_nowhere < 5)
858 printf("vnlru process getting nowhere..\n");
859 else if (vnlru_nowhere == 5)
860 printf("vnlru process messages stopped.\n");
863 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
865 kern_yield(PRI_UNCHANGED);
869 static struct kproc_desc vnlru_kp = {
874 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start,
878 * Routines having to do with the management of the vnode table.
882 * Try to recycle a freed vnode. We abort if anyone picks up a reference
883 * before we actually vgone(). This function must be called with the vnode
884 * held to prevent the vnode from being returned to the free list midway
888 vtryrecycle(struct vnode *vp)
892 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
893 VNASSERT(vp->v_holdcnt, vp,
894 ("vtryrecycle: Recycling vp %p without a reference.", vp));
896 * This vnode may found and locked via some other list, if so we
897 * can't recycle it yet.
899 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
901 "%s: impossible to recycle, vp %p lock is already held",
903 return (EWOULDBLOCK);
906 * Don't recycle if its filesystem is being suspended.
908 if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) {
911 "%s: impossible to recycle, cannot start the write for %p",
916 * If we got this far, we need to acquire the interlock and see if
917 * anyone picked up this vnode from another list. If not, we will
918 * mark it with DOOMED via vgonel() so that anyone who does find it
922 if (vp->v_usecount) {
923 VOP_UNLOCK(vp, LK_INTERLOCK);
924 vn_finished_write(vnmp);
926 "%s: impossible to recycle, %p is already referenced",
930 if ((vp->v_iflag & VI_DOOMED) == 0)
932 VOP_UNLOCK(vp, LK_INTERLOCK);
933 vn_finished_write(vnmp);
938 * Wait for available vnodes.
941 getnewvnode_wait(int suspended)
944 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
945 if (numvnodes > desiredvnodes) {
948 * File system is beeing suspended, we cannot risk a
949 * deadlock here, so allocate new vnode anyway.
951 if (freevnodes > wantfreevnodes)
952 vnlru_free(freevnodes - wantfreevnodes);
955 if (vnlruproc_sig == 0) {
956 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
959 msleep(&vnlruproc_sig, &vnode_free_list_mtx, PVFS,
962 return (numvnodes > desiredvnodes ? ENFILE : 0);
966 getnewvnode_reserve(u_int count)
971 mtx_lock(&vnode_free_list_mtx);
973 if (getnewvnode_wait(0) == 0) {
979 mtx_unlock(&vnode_free_list_mtx);
983 getnewvnode_drop_reserve(void)
988 mtx_lock(&vnode_free_list_mtx);
989 KASSERT(numvnodes >= td->td_vp_reserv, ("reserve too large"));
990 numvnodes -= td->td_vp_reserv;
991 mtx_unlock(&vnode_free_list_mtx);
992 td->td_vp_reserv = 0;
996 * Return the next vnode from the free list.
999 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops,
1007 CTR3(KTR_VFS, "%s: mp %p with tag %s", __func__, mp, tag);
1010 if (td->td_vp_reserv > 0) {
1011 td->td_vp_reserv -= 1;
1014 mtx_lock(&vnode_free_list_mtx);
1016 * Lend our context to reclaim vnodes if they've exceeded the max.
1018 if (freevnodes > wantfreevnodes)
1020 error = getnewvnode_wait(mp != NULL && (mp->mnt_kern_flag &
1022 #if 0 /* XXX Not all VFS_VGET/ffs_vget callers check returns. */
1024 mtx_unlock(&vnode_free_list_mtx);
1029 mtx_unlock(&vnode_free_list_mtx);
1031 vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK|M_ZERO);
1035 vp->v_vnlock = &vp->v_lock;
1036 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF);
1038 * By default, don't allow shared locks unless filesystems
1041 lockinit(vp->v_vnlock, PVFS, tag, VLKTIMEOUT, LK_NOSHARE);
1043 * Initialize bufobj.
1046 bo->__bo_vnode = vp;
1047 mtx_init(BO_MTX(bo), "bufobj interlock", NULL, MTX_DEF);
1048 bo->bo_ops = &buf_ops_bio;
1049 bo->bo_private = vp;
1050 TAILQ_INIT(&bo->bo_clean.bv_hd);
1051 TAILQ_INIT(&bo->bo_dirty.bv_hd);
1053 * Initialize namecache.
1055 LIST_INIT(&vp->v_cache_src);
1056 TAILQ_INIT(&vp->v_cache_dst);
1058 * Finalize various vnode identity bits.
1063 v_incr_usecount(vp);
1067 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0)
1068 mac_vnode_associate_singlelabel(mp, vp);
1069 else if (mp == NULL && vops != &dead_vnodeops)
1070 printf("NULL mp in getnewvnode()\n");
1073 bo->bo_bsize = mp->mnt_stat.f_iosize;
1074 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0)
1075 vp->v_vflag |= VV_NOKNOTE;
1077 rangelock_init(&vp->v_rl);
1084 * Delete from old mount point vnode list, if on one.
1087 delmntque(struct vnode *vp)
1097 KASSERT(mp->mnt_activevnodelistsize <= mp->mnt_nvnodelistsize,
1098 ("Active vnode list size %d > Vnode list size %d",
1099 mp->mnt_activevnodelistsize, mp->mnt_nvnodelistsize));
1100 active = vp->v_iflag & VI_ACTIVE;
1101 vp->v_iflag &= ~VI_ACTIVE;
1103 mtx_lock(&vnode_free_list_mtx);
1104 TAILQ_REMOVE(&mp->mnt_activevnodelist, vp, v_actfreelist);
1105 mp->mnt_activevnodelistsize--;
1106 mtx_unlock(&vnode_free_list_mtx);
1110 VNASSERT(mp->mnt_nvnodelistsize > 0, vp,
1111 ("bad mount point vnode list size"));
1112 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1113 mp->mnt_nvnodelistsize--;
1119 insmntque_stddtr(struct vnode *vp, void *dtr_arg)
1123 vp->v_op = &dead_vnodeops;
1124 /* XXX non mp-safe fs may still call insmntque with vnode
1126 if (!VOP_ISLOCKED(vp))
1127 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1133 * Insert into list of vnodes for the new mount point, if available.
1136 insmntque1(struct vnode *vp, struct mount *mp,
1137 void (*dtr)(struct vnode *, void *), void *dtr_arg)
1141 KASSERT(vp->v_mount == NULL,
1142 ("insmntque: vnode already on per mount vnode list"));
1143 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)"));
1144 #ifdef DEBUG_VFS_LOCKS
1145 if (!VFS_NEEDSGIANT(mp))
1146 ASSERT_VOP_ELOCKED(vp,
1147 "insmntque: mp-safe fs and non-locked vp");
1150 * We acquire the vnode interlock early to ensure that the
1151 * vnode cannot be recycled by another process releasing a
1152 * holdcnt on it before we get it on both the vnode list
1153 * and the active vnode list. The mount mutex protects only
1154 * manipulation of the vnode list and the vnode freelist
1155 * mutex protects only manipulation of the active vnode list.
1156 * Hence the need to hold the vnode interlock throughout.
1160 if ((mp->mnt_kern_flag & MNTK_NOINSMNTQ) != 0 &&
1161 ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 ||
1162 mp->mnt_nvnodelistsize == 0)) {
1163 locked = VOP_ISLOCKED(vp);
1164 if (!locked || (locked == LK_EXCLUSIVE &&
1165 (vp->v_vflag & VV_FORCEINSMQ) == 0)) {
1175 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1176 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp,
1177 ("neg mount point vnode list size"));
1178 mp->mnt_nvnodelistsize++;
1179 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
1180 ("Activating already active vnode"));
1181 vp->v_iflag |= VI_ACTIVE;
1182 mtx_lock(&vnode_free_list_mtx);
1183 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
1184 mp->mnt_activevnodelistsize++;
1185 mtx_unlock(&vnode_free_list_mtx);
1192 insmntque(struct vnode *vp, struct mount *mp)
1195 return (insmntque1(vp, mp, insmntque_stddtr, NULL));
1199 * Flush out and invalidate all buffers associated with a bufobj
1200 * Called with the underlying object locked.
1203 bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo)
1208 if (flags & V_SAVE) {
1209 error = bufobj_wwait(bo, slpflag, slptimeo);
1214 if (bo->bo_dirty.bv_cnt > 0) {
1216 if ((error = BO_SYNC(bo, MNT_WAIT)) != 0)
1219 * XXX We could save a lock/unlock if this was only
1220 * enabled under INVARIANTS
1223 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)
1224 panic("vinvalbuf: dirty bufs");
1228 * If you alter this loop please notice that interlock is dropped and
1229 * reacquired in flushbuflist. Special care is needed to ensure that
1230 * no race conditions occur from this.
1233 error = flushbuflist(&bo->bo_clean,
1234 flags, bo, slpflag, slptimeo);
1235 if (error == 0 && !(flags & V_CLEANONLY))
1236 error = flushbuflist(&bo->bo_dirty,
1237 flags, bo, slpflag, slptimeo);
1238 if (error != 0 && error != EAGAIN) {
1242 } while (error != 0);
1245 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
1246 * have write I/O in-progress but if there is a VM object then the
1247 * VM object can also have read-I/O in-progress.
1250 bufobj_wwait(bo, 0, 0);
1252 if (bo->bo_object != NULL) {
1253 VM_OBJECT_LOCK(bo->bo_object);
1254 vm_object_pip_wait(bo->bo_object, "bovlbx");
1255 VM_OBJECT_UNLOCK(bo->bo_object);
1258 } while (bo->bo_numoutput > 0);
1262 * Destroy the copy in the VM cache, too.
1264 if (bo->bo_object != NULL &&
1265 (flags & (V_ALT | V_NORMAL | V_CLEANONLY)) == 0) {
1266 VM_OBJECT_LOCK(bo->bo_object);
1267 vm_object_page_remove(bo->bo_object, 0, 0, (flags & V_SAVE) ?
1268 OBJPR_CLEANONLY : 0);
1269 VM_OBJECT_UNLOCK(bo->bo_object);
1274 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY)) == 0 &&
1275 (bo->bo_dirty.bv_cnt > 0 || bo->bo_clean.bv_cnt > 0))
1276 panic("vinvalbuf: flush failed");
1283 * Flush out and invalidate all buffers associated with a vnode.
1284 * Called with the underlying object locked.
1287 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
1290 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
1291 ASSERT_VOP_LOCKED(vp, "vinvalbuf");
1292 return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo));
1296 * Flush out buffers on the specified list.
1300 flushbuflist( struct bufv *bufv, int flags, struct bufobj *bo, int slpflag,
1303 struct buf *bp, *nbp;
1308 ASSERT_BO_LOCKED(bo);
1311 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) {
1312 if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) ||
1313 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) {
1319 lblkno = nbp->b_lblkno;
1320 xflags = nbp->b_xflags &
1321 (BX_BKGRDMARKER | BX_VNDIRTY | BX_VNCLEAN);
1324 error = BUF_TIMELOCK(bp,
1325 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_MTX(bo),
1326 "flushbuf", slpflag, slptimeo);
1329 return (error != ENOLCK ? error : EAGAIN);
1331 KASSERT(bp->b_bufobj == bo,
1332 ("bp %p wrong b_bufobj %p should be %p",
1333 bp, bp->b_bufobj, bo));
1334 if (bp->b_bufobj != bo) { /* XXX: necessary ? */
1340 * XXX Since there are no node locks for NFS, I
1341 * believe there is a slight chance that a delayed
1342 * write will occur while sleeping just above, so
1345 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
1350 bp->b_flags |= B_ASYNC;
1353 return (EAGAIN); /* XXX: why not loop ? */
1358 bp->b_flags |= (B_INVAL | B_RELBUF);
1359 bp->b_flags &= ~B_ASYNC;
1363 (nbp->b_bufobj != bo ||
1364 nbp->b_lblkno != lblkno ||
1366 (BX_BKGRDMARKER | BX_VNDIRTY | BX_VNCLEAN)) != xflags))
1367 break; /* nbp invalid */
1373 * Truncate a file's buffer and pages to a specified length. This
1374 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
1378 vtruncbuf(struct vnode *vp, struct ucred *cred, struct thread *td,
1379 off_t length, int blksize)
1381 struct buf *bp, *nbp;
1386 CTR5(KTR_VFS, "%s: vp %p with cred %p and block %d:%ju", __func__,
1387 vp, cred, blksize, (uintmax_t)length);
1390 * Round up to the *next* lbn.
1392 trunclbn = (length + blksize - 1) / blksize;
1394 ASSERT_VOP_LOCKED(vp, "vtruncbuf");
1401 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) {
1402 if (bp->b_lblkno < trunclbn)
1405 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1406 BO_MTX(bo)) == ENOLCK)
1412 bp->b_flags |= (B_INVAL | B_RELBUF);
1413 bp->b_flags &= ~B_ASYNC;
1419 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
1420 (nbp->b_vp != vp) ||
1421 (nbp->b_flags & B_DELWRI))) {
1427 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1428 if (bp->b_lblkno < trunclbn)
1431 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1432 BO_MTX(bo)) == ENOLCK)
1437 bp->b_flags |= (B_INVAL | B_RELBUF);
1438 bp->b_flags &= ~B_ASYNC;
1444 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
1445 (nbp->b_vp != vp) ||
1446 (nbp->b_flags & B_DELWRI) == 0)) {
1455 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1456 if (bp->b_lblkno > 0)
1459 * Since we hold the vnode lock this should only
1460 * fail if we're racing with the buf daemon.
1463 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1464 BO_MTX(bo)) == ENOLCK) {
1467 VNASSERT((bp->b_flags & B_DELWRI), vp,
1468 ("buf(%p) on dirty queue without DELWRI", bp));
1479 bufobj_wwait(bo, 0, 0);
1481 vnode_pager_setsize(vp, length);
1487 * buf_splay() - splay tree core for the clean/dirty list of buffers in
1490 * NOTE: We have to deal with the special case of a background bitmap
1491 * buffer, a situation where two buffers will have the same logical
1492 * block offset. We want (1) only the foreground buffer to be accessed
1493 * in a lookup and (2) must differentiate between the foreground and
1494 * background buffer in the splay tree algorithm because the splay
1495 * tree cannot normally handle multiple entities with the same 'index'.
1496 * We accomplish this by adding differentiating flags to the splay tree's
1501 buf_splay(daddr_t lblkno, b_xflags_t xflags, struct buf *root)
1504 struct buf *lefttreemax, *righttreemin, *y;
1508 lefttreemax = righttreemin = &dummy;
1510 if (lblkno < root->b_lblkno ||
1511 (lblkno == root->b_lblkno &&
1512 (xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
1513 if ((y = root->b_left) == NULL)
1515 if (lblkno < y->b_lblkno) {
1517 root->b_left = y->b_right;
1520 if ((y = root->b_left) == NULL)
1523 /* Link into the new root's right tree. */
1524 righttreemin->b_left = root;
1525 righttreemin = root;
1526 } else if (lblkno > root->b_lblkno ||
1527 (lblkno == root->b_lblkno &&
1528 (xflags & BX_BKGRDMARKER) > (root->b_xflags & BX_BKGRDMARKER))) {
1529 if ((y = root->b_right) == NULL)
1531 if (lblkno > y->b_lblkno) {
1533 root->b_right = y->b_left;
1536 if ((y = root->b_right) == NULL)
1539 /* Link into the new root's left tree. */
1540 lefttreemax->b_right = root;
1547 /* Assemble the new root. */
1548 lefttreemax->b_right = root->b_left;
1549 righttreemin->b_left = root->b_right;
1550 root->b_left = dummy.b_right;
1551 root->b_right = dummy.b_left;
1556 buf_vlist_remove(struct buf *bp)
1561 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1562 ASSERT_BO_LOCKED(bp->b_bufobj);
1563 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) !=
1564 (BX_VNDIRTY|BX_VNCLEAN),
1565 ("buf_vlist_remove: Buf %p is on two lists", bp));
1566 if (bp->b_xflags & BX_VNDIRTY)
1567 bv = &bp->b_bufobj->bo_dirty;
1569 bv = &bp->b_bufobj->bo_clean;
1570 if (bp != bv->bv_root) {
1571 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root);
1572 KASSERT(root == bp, ("splay lookup failed in remove"));
1574 if (bp->b_left == NULL) {
1577 root = buf_splay(bp->b_lblkno, bp->b_xflags, bp->b_left);
1578 root->b_right = bp->b_right;
1581 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs);
1583 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1587 * Add the buffer to the sorted clean or dirty block list using a
1588 * splay tree algorithm.
1590 * NOTE: xflags is passed as a constant, optimizing this inline function!
1593 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags)
1598 ASSERT_BO_LOCKED(bo);
1599 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0,
1600 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags));
1601 bp->b_xflags |= xflags;
1602 if (xflags & BX_VNDIRTY)
1607 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root);
1611 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs);
1612 } else if (bp->b_lblkno < root->b_lblkno ||
1613 (bp->b_lblkno == root->b_lblkno &&
1614 (bp->b_xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
1615 bp->b_left = root->b_left;
1617 root->b_left = NULL;
1618 TAILQ_INSERT_BEFORE(root, bp, b_bobufs);
1620 bp->b_right = root->b_right;
1622 root->b_right = NULL;
1623 TAILQ_INSERT_AFTER(&bv->bv_hd, root, bp, b_bobufs);
1630 * Lookup a buffer using the splay tree. Note that we specifically avoid
1631 * shadow buffers used in background bitmap writes.
1633 * This code isn't quite efficient as it could be because we are maintaining
1634 * two sorted lists and do not know which list the block resides in.
1636 * During a "make buildworld" the desired buffer is found at one of
1637 * the roots more than 60% of the time. Thus, checking both roots
1638 * before performing either splay eliminates unnecessary splays on the
1639 * first tree splayed.
1642 gbincore(struct bufobj *bo, daddr_t lblkno)
1646 ASSERT_BO_LOCKED(bo);
1647 if ((bp = bo->bo_clean.bv_root) != NULL &&
1648 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1650 if ((bp = bo->bo_dirty.bv_root) != NULL &&
1651 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1653 if ((bp = bo->bo_clean.bv_root) != NULL) {
1654 bo->bo_clean.bv_root = bp = buf_splay(lblkno, 0, bp);
1655 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1658 if ((bp = bo->bo_dirty.bv_root) != NULL) {
1659 bo->bo_dirty.bv_root = bp = buf_splay(lblkno, 0, bp);
1660 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1667 * Associate a buffer with a vnode.
1670 bgetvp(struct vnode *vp, struct buf *bp)
1675 ASSERT_BO_LOCKED(bo);
1676 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free"));
1678 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags);
1679 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp,
1680 ("bgetvp: bp already attached! %p", bp));
1683 if (VFS_NEEDSGIANT(vp->v_mount) || bo->bo_flag & BO_NEEDSGIANT)
1684 bp->b_flags |= B_NEEDSGIANT;
1688 * Insert onto list for new vnode.
1690 buf_vlist_add(bp, bo, BX_VNCLEAN);
1694 * Disassociate a buffer from a vnode.
1697 brelvp(struct buf *bp)
1702 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1703 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1706 * Delete from old vnode list, if on one.
1708 vp = bp->b_vp; /* XXX */
1711 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1712 buf_vlist_remove(bp);
1714 panic("brelvp: Buffer %p not on queue.", bp);
1715 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
1716 bo->bo_flag &= ~BO_ONWORKLST;
1717 mtx_lock(&sync_mtx);
1718 LIST_REMOVE(bo, bo_synclist);
1719 syncer_worklist_len--;
1720 mtx_unlock(&sync_mtx);
1722 bp->b_flags &= ~B_NEEDSGIANT;
1724 bp->b_bufobj = NULL;
1730 * Add an item to the syncer work queue.
1733 vn_syncer_add_to_worklist(struct bufobj *bo, int delay)
1737 ASSERT_BO_LOCKED(bo);
1739 mtx_lock(&sync_mtx);
1740 if (bo->bo_flag & BO_ONWORKLST)
1741 LIST_REMOVE(bo, bo_synclist);
1743 bo->bo_flag |= BO_ONWORKLST;
1744 syncer_worklist_len++;
1747 if (delay > syncer_maxdelay - 2)
1748 delay = syncer_maxdelay - 2;
1749 slot = (syncer_delayno + delay) & syncer_mask;
1751 queue = VFS_NEEDSGIANT(bo->__bo_vnode->v_mount) ? WI_GIANTQ :
1753 LIST_INSERT_HEAD(&syncer_workitem_pending[queue][slot], bo,
1755 mtx_unlock(&sync_mtx);
1759 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS)
1763 mtx_lock(&sync_mtx);
1764 len = syncer_worklist_len - sync_vnode_count;
1765 mtx_unlock(&sync_mtx);
1766 error = SYSCTL_OUT(req, &len, sizeof(len));
1770 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, CTLTYPE_INT | CTLFLAG_RD, NULL, 0,
1771 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length");
1773 static struct proc *updateproc;
1774 static void sched_sync(void);
1775 static struct kproc_desc up_kp = {
1780 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp);
1783 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td)
1788 *bo = LIST_FIRST(slp);
1791 vp = (*bo)->__bo_vnode; /* XXX */
1792 if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0)
1795 * We use vhold in case the vnode does not
1796 * successfully sync. vhold prevents the vnode from
1797 * going away when we unlock the sync_mtx so that
1798 * we can acquire the vnode interlock.
1801 mtx_unlock(&sync_mtx);
1803 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1805 mtx_lock(&sync_mtx);
1806 return (*bo == LIST_FIRST(slp));
1808 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1809 (void) VOP_FSYNC(vp, MNT_LAZY, td);
1811 vn_finished_write(mp);
1813 if (((*bo)->bo_flag & BO_ONWORKLST) != 0) {
1815 * Put us back on the worklist. The worklist
1816 * routine will remove us from our current
1817 * position and then add us back in at a later
1820 vn_syncer_add_to_worklist(*bo, syncdelay);
1824 mtx_lock(&sync_mtx);
1829 * System filesystem synchronizer daemon.
1834 struct synclist *gnext, *next;
1835 struct synclist *gslp, *slp;
1838 struct thread *td = curthread;
1840 int net_worklist_len;
1841 int syncer_final_iter;
1846 syncer_final_iter = 0;
1848 syncer_state = SYNCER_RUNNING;
1849 starttime = time_uptime;
1850 td->td_pflags |= TDP_NORUNNINGBUF;
1852 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc,
1855 mtx_lock(&sync_mtx);
1857 if (syncer_state == SYNCER_FINAL_DELAY &&
1858 syncer_final_iter == 0) {
1859 mtx_unlock(&sync_mtx);
1860 kproc_suspend_check(td->td_proc);
1861 mtx_lock(&sync_mtx);
1863 net_worklist_len = syncer_worklist_len - sync_vnode_count;
1864 if (syncer_state != SYNCER_RUNNING &&
1865 starttime != time_uptime) {
1867 printf("\nSyncing disks, vnodes remaining...");
1870 printf("%d ", net_worklist_len);
1872 starttime = time_uptime;
1875 * Push files whose dirty time has expired. Be careful
1876 * of interrupt race on slp queue.
1878 * Skip over empty worklist slots when shutting down.
1881 slp = &syncer_workitem_pending[WI_MPSAFEQ][syncer_delayno];
1882 gslp = &syncer_workitem_pending[WI_GIANTQ][syncer_delayno];
1883 syncer_delayno += 1;
1884 if (syncer_delayno == syncer_maxdelay)
1886 next = &syncer_workitem_pending[WI_MPSAFEQ][syncer_delayno];
1887 gnext = &syncer_workitem_pending[WI_GIANTQ][syncer_delayno];
1889 * If the worklist has wrapped since the
1890 * it was emptied of all but syncer vnodes,
1891 * switch to the FINAL_DELAY state and run
1892 * for one more second.
1894 if (syncer_state == SYNCER_SHUTTING_DOWN &&
1895 net_worklist_len == 0 &&
1896 last_work_seen == syncer_delayno) {
1897 syncer_state = SYNCER_FINAL_DELAY;
1898 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP;
1900 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) &&
1901 LIST_EMPTY(gslp) && syncer_worklist_len > 0);
1904 * Keep track of the last time there was anything
1905 * on the worklist other than syncer vnodes.
1906 * Return to the SHUTTING_DOWN state if any
1909 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING)
1910 last_work_seen = syncer_delayno;
1911 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY)
1912 syncer_state = SYNCER_SHUTTING_DOWN;
1913 while (!LIST_EMPTY(slp)) {
1914 error = sync_vnode(slp, &bo, td);
1916 LIST_REMOVE(bo, bo_synclist);
1917 LIST_INSERT_HEAD(next, bo, bo_synclist);
1921 if (first_printf == 0)
1922 wdog_kern_pat(WD_LASTVAL);
1925 if (!LIST_EMPTY(gslp)) {
1926 mtx_unlock(&sync_mtx);
1928 mtx_lock(&sync_mtx);
1929 while (!LIST_EMPTY(gslp)) {
1930 error = sync_vnode(gslp, &bo, td);
1932 LIST_REMOVE(bo, bo_synclist);
1933 LIST_INSERT_HEAD(gnext, bo,
1940 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0)
1941 syncer_final_iter--;
1943 * The variable rushjob allows the kernel to speed up the
1944 * processing of the filesystem syncer process. A rushjob
1945 * value of N tells the filesystem syncer to process the next
1946 * N seconds worth of work on its queue ASAP. Currently rushjob
1947 * is used by the soft update code to speed up the filesystem
1948 * syncer process when the incore state is getting so far
1949 * ahead of the disk that the kernel memory pool is being
1950 * threatened with exhaustion.
1957 * Just sleep for a short period of time between
1958 * iterations when shutting down to allow some I/O
1961 * If it has taken us less than a second to process the
1962 * current work, then wait. Otherwise start right over
1963 * again. We can still lose time if any single round
1964 * takes more than two seconds, but it does not really
1965 * matter as we are just trying to generally pace the
1966 * filesystem activity.
1968 if (syncer_state != SYNCER_RUNNING ||
1969 time_uptime == starttime) {
1971 sched_prio(td, PPAUSE);
1974 if (syncer_state != SYNCER_RUNNING)
1975 cv_timedwait(&sync_wakeup, &sync_mtx,
1976 hz / SYNCER_SHUTDOWN_SPEEDUP);
1977 else if (time_uptime == starttime)
1978 cv_timedwait(&sync_wakeup, &sync_mtx, hz);
1983 * Request the syncer daemon to speed up its work.
1984 * We never push it to speed up more than half of its
1985 * normal turn time, otherwise it could take over the cpu.
1988 speedup_syncer(void)
1992 mtx_lock(&sync_mtx);
1993 if (rushjob < syncdelay / 2) {
1995 stat_rush_requests += 1;
1998 mtx_unlock(&sync_mtx);
1999 cv_broadcast(&sync_wakeup);
2004 * Tell the syncer to speed up its work and run though its work
2005 * list several times, then tell it to shut down.
2008 syncer_shutdown(void *arg, int howto)
2011 if (howto & RB_NOSYNC)
2013 mtx_lock(&sync_mtx);
2014 syncer_state = SYNCER_SHUTTING_DOWN;
2016 mtx_unlock(&sync_mtx);
2017 cv_broadcast(&sync_wakeup);
2018 kproc_shutdown(arg, howto);
2022 * Reassign a buffer from one vnode to another.
2023 * Used to assign file specific control information
2024 * (indirect blocks) to the vnode to which they belong.
2027 reassignbuf(struct buf *bp)
2040 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X",
2041 bp, bp->b_vp, bp->b_flags);
2043 * B_PAGING flagged buffers cannot be reassigned because their vp
2044 * is not fully linked in.
2046 if (bp->b_flags & B_PAGING)
2047 panic("cannot reassign paging buffer");
2050 * Delete from old vnode list, if on one.
2053 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
2054 buf_vlist_remove(bp);
2056 panic("reassignbuf: Buffer %p not on queue.", bp);
2058 * If dirty, put on list of dirty buffers; otherwise insert onto list
2061 if (bp->b_flags & B_DELWRI) {
2062 if ((bo->bo_flag & BO_ONWORKLST) == 0) {
2063 switch (vp->v_type) {
2073 vn_syncer_add_to_worklist(bo, delay);
2075 buf_vlist_add(bp, bo, BX_VNDIRTY);
2077 buf_vlist_add(bp, bo, BX_VNCLEAN);
2079 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
2080 mtx_lock(&sync_mtx);
2081 LIST_REMOVE(bo, bo_synclist);
2082 syncer_worklist_len--;
2083 mtx_unlock(&sync_mtx);
2084 bo->bo_flag &= ~BO_ONWORKLST;
2089 bp = TAILQ_FIRST(&bv->bv_hd);
2090 KASSERT(bp == NULL || bp->b_bufobj == bo,
2091 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2092 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2093 KASSERT(bp == NULL || bp->b_bufobj == bo,
2094 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2096 bp = TAILQ_FIRST(&bv->bv_hd);
2097 KASSERT(bp == NULL || bp->b_bufobj == bo,
2098 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2099 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2100 KASSERT(bp == NULL || bp->b_bufobj == bo,
2101 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2107 * Increment the use and hold counts on the vnode, taking care to reference
2108 * the driver's usecount if this is a chardev. The vholdl() will remove
2109 * the vnode from the free list if it is presently free. Requires the
2110 * vnode interlock and returns with it held.
2113 v_incr_usecount(struct vnode *vp)
2116 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2118 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2120 vp->v_rdev->si_usecount++;
2127 * Turn a holdcnt into a use+holdcnt such that only one call to
2128 * v_decr_usecount is needed.
2131 v_upgrade_usecount(struct vnode *vp)
2134 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2136 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2138 vp->v_rdev->si_usecount++;
2144 * Decrement the vnode use and hold count along with the driver's usecount
2145 * if this is a chardev. The vdropl() below releases the vnode interlock
2146 * as it may free the vnode.
2149 v_decr_usecount(struct vnode *vp)
2152 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2153 VNASSERT(vp->v_usecount > 0, vp,
2154 ("v_decr_usecount: negative usecount"));
2155 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2157 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2159 vp->v_rdev->si_usecount--;
2166 * Decrement only the use count and driver use count. This is intended to
2167 * be paired with a follow on vdropl() to release the remaining hold count.
2168 * In this way we may vgone() a vnode with a 0 usecount without risk of
2169 * having it end up on a free list because the hold count is kept above 0.
2172 v_decr_useonly(struct vnode *vp)
2175 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2176 VNASSERT(vp->v_usecount > 0, vp,
2177 ("v_decr_useonly: negative usecount"));
2178 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2180 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2182 vp->v_rdev->si_usecount--;
2188 * Grab a particular vnode from the free list, increment its
2189 * reference count and lock it. VI_DOOMED is set if the vnode
2190 * is being destroyed. Only callers who specify LK_RETRY will
2191 * see doomed vnodes. If inactive processing was delayed in
2192 * vput try to do it here.
2195 vget(struct vnode *vp, int flags, struct thread *td)
2200 VFS_ASSERT_GIANT(vp->v_mount);
2201 VNASSERT((flags & LK_TYPE_MASK) != 0, vp,
2202 ("vget: invalid lock operation"));
2203 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
2205 if ((flags & LK_INTERLOCK) == 0)
2208 if ((error = vn_lock(vp, flags | LK_INTERLOCK)) != 0) {
2210 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__,
2214 if (vp->v_iflag & VI_DOOMED && (flags & LK_RETRY) == 0)
2215 panic("vget: vn_lock failed to return ENOENT\n");
2217 /* Upgrade our holdcnt to a usecount. */
2218 v_upgrade_usecount(vp);
2220 * We don't guarantee that any particular close will
2221 * trigger inactive processing so just make a best effort
2222 * here at preventing a reference to a removed file. If
2223 * we don't succeed no harm is done.
2225 if (vp->v_iflag & VI_OWEINACT) {
2226 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE &&
2227 (flags & LK_NOWAIT) == 0)
2229 vp->v_iflag &= ~VI_OWEINACT;
2236 * Increase the reference count of a vnode.
2239 vref(struct vnode *vp)
2242 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2244 v_incr_usecount(vp);
2249 * Return reference count of a vnode.
2251 * The results of this call are only guaranteed when some mechanism other
2252 * than the VI lock is used to stop other processes from gaining references
2253 * to the vnode. This may be the case if the caller holds the only reference.
2254 * This is also useful when stale data is acceptable as race conditions may
2255 * be accounted for by some other means.
2258 vrefcnt(struct vnode *vp)
2263 usecnt = vp->v_usecount;
2269 #define VPUTX_VRELE 1
2270 #define VPUTX_VPUT 2
2271 #define VPUTX_VUNREF 3
2274 vputx(struct vnode *vp, int func)
2278 KASSERT(vp != NULL, ("vputx: null vp"));
2279 if (func == VPUTX_VUNREF)
2280 ASSERT_VOP_LOCKED(vp, "vunref");
2281 else if (func == VPUTX_VPUT)
2282 ASSERT_VOP_LOCKED(vp, "vput");
2284 KASSERT(func == VPUTX_VRELE, ("vputx: wrong func"));
2285 VFS_ASSERT_GIANT(vp->v_mount);
2286 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2289 /* Skip this v_writecount check if we're going to panic below. */
2290 VNASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, vp,
2291 ("vputx: missed vn_close"));
2294 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) &&
2295 vp->v_usecount == 1)) {
2296 if (func == VPUTX_VPUT)
2298 v_decr_usecount(vp);
2302 if (vp->v_usecount != 1) {
2303 vprint("vputx: negative ref count", vp);
2304 panic("vputx: negative ref cnt");
2306 CTR2(KTR_VFS, "%s: return vnode %p to the freelist", __func__, vp);
2308 * We want to hold the vnode until the inactive finishes to
2309 * prevent vgone() races. We drop the use count here and the
2310 * hold count below when we're done.
2314 * We must call VOP_INACTIVE with the node locked. Mark
2315 * as VI_DOINGINACT to avoid recursion.
2317 vp->v_iflag |= VI_OWEINACT;
2320 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
2324 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2325 error = VOP_LOCK(vp, LK_UPGRADE | LK_INTERLOCK |
2331 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
2335 if (vp->v_usecount > 0)
2336 vp->v_iflag &= ~VI_OWEINACT;
2338 if (vp->v_iflag & VI_OWEINACT)
2339 vinactive(vp, curthread);
2340 if (func != VPUTX_VUNREF)
2347 * Vnode put/release.
2348 * If count drops to zero, call inactive routine and return to freelist.
2351 vrele(struct vnode *vp)
2354 vputx(vp, VPUTX_VRELE);
2358 * Release an already locked vnode. This give the same effects as
2359 * unlock+vrele(), but takes less time and avoids releasing and
2360 * re-aquiring the lock (as vrele() acquires the lock internally.)
2363 vput(struct vnode *vp)
2366 vputx(vp, VPUTX_VPUT);
2370 * Release an exclusively locked vnode. Do not unlock the vnode lock.
2373 vunref(struct vnode *vp)
2376 vputx(vp, VPUTX_VUNREF);
2380 * Somebody doesn't want the vnode recycled.
2383 vhold(struct vnode *vp)
2392 * Increase the hold count and activate if this is the first reference.
2395 vholdl(struct vnode *vp)
2399 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2401 if (!VSHOULDBUSY(vp))
2403 ASSERT_VI_LOCKED(vp, "vholdl");
2404 VNASSERT((vp->v_iflag & VI_FREE) != 0, vp, ("vnode not free"));
2405 VNASSERT(vp->v_op != NULL, vp, ("vholdl: vnode already reclaimed."));
2407 * Remove a vnode from the free list, mark it as in use,
2408 * and put it on the active list.
2410 mtx_lock(&vnode_free_list_mtx);
2411 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
2413 vp->v_iflag &= ~(VI_FREE|VI_AGE);
2414 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
2415 ("Activating already active vnode"));
2416 vp->v_iflag |= VI_ACTIVE;
2418 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
2419 mp->mnt_activevnodelistsize++;
2420 mtx_unlock(&vnode_free_list_mtx);
2424 * Note that there is one less who cares about this vnode.
2425 * vdrop() is the opposite of vhold().
2428 vdrop(struct vnode *vp)
2436 * Drop the hold count of the vnode. If this is the last reference to
2437 * the vnode we place it on the free list unless it has been vgone'd
2438 * (marked VI_DOOMED) in which case we will free it.
2441 vdropl(struct vnode *vp)
2447 ASSERT_VI_LOCKED(vp, "vdropl");
2448 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2449 if (vp->v_holdcnt <= 0)
2450 panic("vdrop: holdcnt %d", vp->v_holdcnt);
2452 if (vp->v_holdcnt > 0) {
2456 if ((vp->v_iflag & VI_DOOMED) == 0) {
2458 * Mark a vnode as free: remove it from its active list
2459 * and put it up for recycling on the freelist.
2461 VNASSERT(vp->v_op != NULL, vp,
2462 ("vdropl: vnode already reclaimed."));
2463 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
2464 ("vnode already free"));
2465 VNASSERT(VSHOULDFREE(vp), vp,
2466 ("vdropl: freeing when we shouldn't"));
2467 active = vp->v_iflag & VI_ACTIVE;
2468 vp->v_iflag &= ~VI_ACTIVE;
2470 mtx_lock(&vnode_free_list_mtx);
2472 TAILQ_REMOVE(&mp->mnt_activevnodelist, vp,
2474 mp->mnt_activevnodelistsize--;
2476 if (vp->v_iflag & VI_AGE) {
2477 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_actfreelist);
2479 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_actfreelist);
2482 vp->v_iflag &= ~VI_AGE;
2483 vp->v_iflag |= VI_FREE;
2484 mtx_unlock(&vnode_free_list_mtx);
2489 * The vnode has been marked for destruction, so free it.
2491 CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__, vp);
2492 mtx_lock(&vnode_free_list_mtx);
2494 mtx_unlock(&vnode_free_list_mtx);
2496 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
2497 ("cleaned vnode still on the free list."));
2498 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't"));
2499 VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count"));
2500 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count"));
2501 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count"));
2502 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's"));
2503 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0"));
2504 VNASSERT(bo->bo_clean.bv_root == NULL, vp, ("cleanblkroot not NULL"));
2505 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0"));
2506 VNASSERT(bo->bo_dirty.bv_root == NULL, vp, ("dirtyblkroot not NULL"));
2507 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst"));
2508 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src"));
2509 VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for .."));
2512 mac_vnode_destroy(vp);
2514 if (vp->v_pollinfo != NULL)
2515 destroy_vpollinfo(vp->v_pollinfo);
2517 /* XXX Elsewhere we detect an already freed vnode via NULL v_op. */
2520 rangelock_destroy(&vp->v_rl);
2521 lockdestroy(vp->v_vnlock);
2522 mtx_destroy(&vp->v_interlock);
2523 mtx_destroy(BO_MTX(bo));
2524 uma_zfree(vnode_zone, vp);
2528 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT
2529 * flags. DOINGINACT prevents us from recursing in calls to vinactive.
2530 * OWEINACT tracks whether a vnode missed a call to inactive due to a
2531 * failed lock upgrade.
2534 vinactive(struct vnode *vp, struct thread *td)
2536 struct vm_object *obj;
2538 ASSERT_VOP_ELOCKED(vp, "vinactive");
2539 ASSERT_VI_LOCKED(vp, "vinactive");
2540 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
2541 ("vinactive: recursed on VI_DOINGINACT"));
2542 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2543 vp->v_iflag |= VI_DOINGINACT;
2544 vp->v_iflag &= ~VI_OWEINACT;
2547 * Before moving off the active list, we must be sure that any
2548 * modified pages are on the vnode's dirty list since these will
2549 * no longer be checked once the vnode is on the inactive list.
2550 * Because the vnode vm object keeps a hold reference on the vnode
2551 * if there is at least one resident non-cached page, the vnode
2552 * cannot leave the active list without the page cleanup done.
2555 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0) {
2556 VM_OBJECT_LOCK(obj);
2557 vm_object_page_clean(obj, 0, 0, OBJPC_NOSYNC);
2558 VM_OBJECT_UNLOCK(obj);
2560 VOP_INACTIVE(vp, td);
2562 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
2563 ("vinactive: lost VI_DOINGINACT"));
2564 vp->v_iflag &= ~VI_DOINGINACT;
2568 * Remove any vnodes in the vnode table belonging to mount point mp.
2570 * If FORCECLOSE is not specified, there should not be any active ones,
2571 * return error if any are found (nb: this is a user error, not a
2572 * system error). If FORCECLOSE is specified, detach any active vnodes
2575 * If WRITECLOSE is set, only flush out regular file vnodes open for
2578 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
2580 * `rootrefs' specifies the base reference count for the root vnode
2581 * of this filesystem. The root vnode is considered busy if its
2582 * v_usecount exceeds this value. On a successful return, vflush(, td)
2583 * will call vrele() on the root vnode exactly rootrefs times.
2584 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
2588 static int busyprt = 0; /* print out busy vnodes */
2589 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "Print out busy vnodes");
2593 vflush(struct mount *mp, int rootrefs, int flags, struct thread *td)
2595 struct vnode *vp, *mvp, *rootvp = NULL;
2597 int busy = 0, error;
2599 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp,
2602 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
2603 ("vflush: bad args"));
2605 * Get the filesystem root vnode. We can vput() it
2606 * immediately, since with rootrefs > 0, it won't go away.
2608 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) {
2609 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d",
2616 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
2618 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE);
2621 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
2625 * Skip over a vnodes marked VV_SYSTEM.
2627 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
2633 * If WRITECLOSE is set, flush out unlinked but still open
2634 * files (even if open only for reading) and regular file
2635 * vnodes open for writing.
2637 if (flags & WRITECLOSE) {
2638 if (vp->v_object != NULL) {
2639 VM_OBJECT_LOCK(vp->v_object);
2640 vm_object_page_clean(vp->v_object, 0, 0, 0);
2641 VM_OBJECT_UNLOCK(vp->v_object);
2643 error = VOP_FSYNC(vp, MNT_WAIT, td);
2647 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
2650 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
2653 if ((vp->v_type == VNON ||
2654 (error == 0 && vattr.va_nlink > 0)) &&
2655 (vp->v_writecount == 0 || vp->v_type != VREG)) {
2663 * With v_usecount == 0, all we need to do is clear out the
2664 * vnode data structures and we are done.
2666 * If FORCECLOSE is set, forcibly close the vnode.
2668 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) {
2669 VNASSERT(vp->v_usecount == 0 ||
2670 (vp->v_type != VCHR && vp->v_type != VBLK), vp,
2671 ("device VNODE %p is FORCECLOSED", vp));
2677 vprint("vflush: busy vnode", vp);
2683 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
2685 * If just the root vnode is busy, and if its refcount
2686 * is equal to `rootrefs', then go ahead and kill it.
2689 KASSERT(busy > 0, ("vflush: not busy"));
2690 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp,
2691 ("vflush: usecount %d < rootrefs %d",
2692 rootvp->v_usecount, rootrefs));
2693 if (busy == 1 && rootvp->v_usecount == rootrefs) {
2694 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK);
2696 VOP_UNLOCK(rootvp, 0);
2702 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__,
2706 for (; rootrefs > 0; rootrefs--)
2712 * Recycle an unused vnode to the front of the free list.
2715 vrecycle(struct vnode *vp, struct thread *td)
2719 ASSERT_VOP_ELOCKED(vp, "vrecycle");
2720 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2723 if (vp->v_usecount == 0) {
2732 * Eliminate all activity associated with a vnode
2733 * in preparation for reuse.
2736 vgone(struct vnode *vp)
2744 * vgone, with the vp interlock held.
2747 vgonel(struct vnode *vp)
2754 ASSERT_VOP_ELOCKED(vp, "vgonel");
2755 ASSERT_VI_LOCKED(vp, "vgonel");
2756 VNASSERT(vp->v_holdcnt, vp,
2757 ("vgonel: vp %p has no reference.", vp));
2758 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2762 * Don't vgonel if we're already doomed.
2764 if (vp->v_iflag & VI_DOOMED)
2766 vp->v_iflag |= VI_DOOMED;
2768 * Check to see if the vnode is in use. If so, we have to call
2769 * VOP_CLOSE() and VOP_INACTIVE().
2771 active = vp->v_usecount;
2772 oweinact = (vp->v_iflag & VI_OWEINACT);
2775 * Clean out any buffers associated with the vnode.
2776 * If the flush fails, just toss the buffers.
2779 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd))
2780 (void) vn_start_secondary_write(vp, &mp, V_WAIT);
2781 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0)
2782 vinvalbuf(vp, 0, 0, 0);
2785 * If purging an active vnode, it must be closed and
2786 * deactivated before being reclaimed.
2789 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
2790 if (oweinact || active) {
2792 if ((vp->v_iflag & VI_DOINGINACT) == 0)
2796 if (vp->v_type == VSOCK)
2797 vfs_unp_reclaim(vp);
2799 * Reclaim the vnode.
2801 if (VOP_RECLAIM(vp, td))
2802 panic("vgone: cannot reclaim");
2804 vn_finished_secondary_write(mp);
2805 VNASSERT(vp->v_object == NULL, vp,
2806 ("vop_reclaim left v_object vp=%p, tag=%s", vp, vp->v_tag));
2808 * Clear the advisory locks and wake up waiting threads.
2810 (void)VOP_ADVLOCKPURGE(vp);
2812 * Delete from old mount point vnode list.
2817 * Done with purge, reset to the standard lock and invalidate
2821 vp->v_vnlock = &vp->v_lock;
2822 vp->v_op = &dead_vnodeops;
2828 * Calculate the total number of references to a special device.
2831 vcount(struct vnode *vp)
2836 count = vp->v_rdev->si_usecount;
2842 * Same as above, but using the struct cdev *as argument
2845 count_dev(struct cdev *dev)
2850 count = dev->si_usecount;
2856 * Print out a description of a vnode.
2858 static char *typename[] =
2859 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD",
2863 vn_printf(struct vnode *vp, const char *fmt, ...)
2866 char buf[256], buf2[16];
2872 printf("%p: ", (void *)vp);
2873 printf("tag %s, type %s\n", vp->v_tag, typename[vp->v_type]);
2874 printf(" usecount %d, writecount %d, refcount %d mountedhere %p\n",
2875 vp->v_usecount, vp->v_writecount, vp->v_holdcnt, vp->v_mountedhere);
2878 if (vp->v_vflag & VV_ROOT)
2879 strlcat(buf, "|VV_ROOT", sizeof(buf));
2880 if (vp->v_vflag & VV_ISTTY)
2881 strlcat(buf, "|VV_ISTTY", sizeof(buf));
2882 if (vp->v_vflag & VV_NOSYNC)
2883 strlcat(buf, "|VV_NOSYNC", sizeof(buf));
2884 if (vp->v_vflag & VV_ETERNALDEV)
2885 strlcat(buf, "|VV_ETERNALDEV", sizeof(buf));
2886 if (vp->v_vflag & VV_CACHEDLABEL)
2887 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf));
2888 if (vp->v_vflag & VV_TEXT)
2889 strlcat(buf, "|VV_TEXT", sizeof(buf));
2890 if (vp->v_vflag & VV_COPYONWRITE)
2891 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf));
2892 if (vp->v_vflag & VV_SYSTEM)
2893 strlcat(buf, "|VV_SYSTEM", sizeof(buf));
2894 if (vp->v_vflag & VV_PROCDEP)
2895 strlcat(buf, "|VV_PROCDEP", sizeof(buf));
2896 if (vp->v_vflag & VV_NOKNOTE)
2897 strlcat(buf, "|VV_NOKNOTE", sizeof(buf));
2898 if (vp->v_vflag & VV_DELETED)
2899 strlcat(buf, "|VV_DELETED", sizeof(buf));
2900 if (vp->v_vflag & VV_MD)
2901 strlcat(buf, "|VV_MD", sizeof(buf));
2902 if (vp->v_vflag & VV_FORCEINSMQ)
2903 strlcat(buf, "|VV_FORCEINSMQ", sizeof(buf));
2904 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC | VV_ETERNALDEV |
2905 VV_CACHEDLABEL | VV_TEXT | VV_COPYONWRITE | VV_SYSTEM | VV_PROCDEP |
2906 VV_NOKNOTE | VV_DELETED | VV_MD | VV_FORCEINSMQ);
2908 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags);
2909 strlcat(buf, buf2, sizeof(buf));
2911 if (vp->v_iflag & VI_MOUNT)
2912 strlcat(buf, "|VI_MOUNT", sizeof(buf));
2913 if (vp->v_iflag & VI_AGE)
2914 strlcat(buf, "|VI_AGE", sizeof(buf));
2915 if (vp->v_iflag & VI_DOOMED)
2916 strlcat(buf, "|VI_DOOMED", sizeof(buf));
2917 if (vp->v_iflag & VI_FREE)
2918 strlcat(buf, "|VI_FREE", sizeof(buf));
2919 if (vp->v_iflag & VI_ACTIVE)
2920 strlcat(buf, "|VI_ACTIVE", sizeof(buf));
2921 if (vp->v_iflag & VI_DOINGINACT)
2922 strlcat(buf, "|VI_DOINGINACT", sizeof(buf));
2923 if (vp->v_iflag & VI_OWEINACT)
2924 strlcat(buf, "|VI_OWEINACT", sizeof(buf));
2925 flags = vp->v_iflag & ~(VI_MOUNT | VI_AGE | VI_DOOMED | VI_FREE |
2926 VI_ACTIVE | VI_DOINGINACT | VI_OWEINACT);
2928 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags);
2929 strlcat(buf, buf2, sizeof(buf));
2931 printf(" flags (%s)\n", buf + 1);
2932 if (mtx_owned(VI_MTX(vp)))
2933 printf(" VI_LOCKed");
2934 if (vp->v_object != NULL)
2935 printf(" v_object %p ref %d pages %d\n",
2936 vp->v_object, vp->v_object->ref_count,
2937 vp->v_object->resident_page_count);
2939 lockmgr_printinfo(vp->v_vnlock);
2940 if (vp->v_data != NULL)
2946 * List all of the locked vnodes in the system.
2947 * Called when debugging the kernel.
2949 DB_SHOW_COMMAND(lockedvnods, lockedvnodes)
2951 struct mount *mp, *nmp;
2955 * Note: because this is DDB, we can't obey the locking semantics
2956 * for these structures, which means we could catch an inconsistent
2957 * state and dereference a nasty pointer. Not much to be done
2960 db_printf("Locked vnodes\n");
2961 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2962 nmp = TAILQ_NEXT(mp, mnt_list);
2963 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
2964 if (vp->v_type != VMARKER &&
2968 nmp = TAILQ_NEXT(mp, mnt_list);
2973 * Show details about the given vnode.
2975 DB_SHOW_COMMAND(vnode, db_show_vnode)
2981 vp = (struct vnode *)addr;
2982 vn_printf(vp, "vnode ");
2986 * Show details about the given mount point.
2988 DB_SHOW_COMMAND(mount, db_show_mount)
2999 /* No address given, print short info about all mount points. */
3000 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3001 db_printf("%p %s on %s (%s)\n", mp,
3002 mp->mnt_stat.f_mntfromname,
3003 mp->mnt_stat.f_mntonname,
3004 mp->mnt_stat.f_fstypename);
3008 db_printf("\nMore info: show mount <addr>\n");
3012 mp = (struct mount *)addr;
3013 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname,
3014 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename);
3017 mflags = mp->mnt_flag;
3018 #define MNT_FLAG(flag) do { \
3019 if (mflags & (flag)) { \
3020 if (buf[0] != '\0') \
3021 strlcat(buf, ", ", sizeof(buf)); \
3022 strlcat(buf, (#flag) + 4, sizeof(buf)); \
3023 mflags &= ~(flag); \
3026 MNT_FLAG(MNT_RDONLY);
3027 MNT_FLAG(MNT_SYNCHRONOUS);
3028 MNT_FLAG(MNT_NOEXEC);
3029 MNT_FLAG(MNT_NOSUID);
3030 MNT_FLAG(MNT_NFS4ACLS);
3031 MNT_FLAG(MNT_UNION);
3032 MNT_FLAG(MNT_ASYNC);
3033 MNT_FLAG(MNT_SUIDDIR);
3034 MNT_FLAG(MNT_SOFTDEP);
3035 MNT_FLAG(MNT_NOSYMFOLLOW);
3036 MNT_FLAG(MNT_GJOURNAL);
3037 MNT_FLAG(MNT_MULTILABEL);
3039 MNT_FLAG(MNT_NOATIME);
3040 MNT_FLAG(MNT_NOCLUSTERR);
3041 MNT_FLAG(MNT_NOCLUSTERW);
3043 MNT_FLAG(MNT_EXRDONLY);
3044 MNT_FLAG(MNT_EXPORTED);
3045 MNT_FLAG(MNT_DEFEXPORTED);
3046 MNT_FLAG(MNT_EXPORTANON);
3047 MNT_FLAG(MNT_EXKERB);
3048 MNT_FLAG(MNT_EXPUBLIC);
3049 MNT_FLAG(MNT_LOCAL);
3050 MNT_FLAG(MNT_QUOTA);
3051 MNT_FLAG(MNT_ROOTFS);
3053 MNT_FLAG(MNT_IGNORE);
3054 MNT_FLAG(MNT_UPDATE);
3055 MNT_FLAG(MNT_DELEXPORT);
3056 MNT_FLAG(MNT_RELOAD);
3057 MNT_FLAG(MNT_FORCE);
3058 MNT_FLAG(MNT_SNAPSHOT);
3059 MNT_FLAG(MNT_BYFSID);
3063 strlcat(buf, ", ", sizeof(buf));
3064 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
3065 "0x%016jx", mflags);
3067 db_printf(" mnt_flag = %s\n", buf);
3070 flags = mp->mnt_kern_flag;
3071 #define MNT_KERN_FLAG(flag) do { \
3072 if (flags & (flag)) { \
3073 if (buf[0] != '\0') \
3074 strlcat(buf, ", ", sizeof(buf)); \
3075 strlcat(buf, (#flag) + 5, sizeof(buf)); \
3079 MNT_KERN_FLAG(MNTK_UNMOUNTF);
3080 MNT_KERN_FLAG(MNTK_ASYNC);
3081 MNT_KERN_FLAG(MNTK_SOFTDEP);
3082 MNT_KERN_FLAG(MNTK_NOINSMNTQ);
3083 MNT_KERN_FLAG(MNTK_DRAINING);
3084 MNT_KERN_FLAG(MNTK_REFEXPIRE);
3085 MNT_KERN_FLAG(MNTK_EXTENDED_SHARED);
3086 MNT_KERN_FLAG(MNTK_SHARED_WRITES);
3087 MNT_KERN_FLAG(MNTK_NOASYNC);
3088 MNT_KERN_FLAG(MNTK_UNMOUNT);
3089 MNT_KERN_FLAG(MNTK_MWAIT);
3090 MNT_KERN_FLAG(MNTK_SUSPEND);
3091 MNT_KERN_FLAG(MNTK_SUSPEND2);
3092 MNT_KERN_FLAG(MNTK_SUSPENDED);
3093 MNT_KERN_FLAG(MNTK_MPSAFE);
3094 MNT_KERN_FLAG(MNTK_LOOKUP_SHARED);
3095 MNT_KERN_FLAG(MNTK_NOKNOTE);
3096 #undef MNT_KERN_FLAG
3099 strlcat(buf, ", ", sizeof(buf));
3100 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
3103 db_printf(" mnt_kern_flag = %s\n", buf);
3105 db_printf(" mnt_opt = ");
3106 opt = TAILQ_FIRST(mp->mnt_opt);
3108 db_printf("%s", opt->name);
3109 opt = TAILQ_NEXT(opt, link);
3110 while (opt != NULL) {
3111 db_printf(", %s", opt->name);
3112 opt = TAILQ_NEXT(opt, link);
3118 db_printf(" mnt_stat = { version=%u type=%u flags=0x%016jx "
3119 "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju "
3120 "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju "
3121 "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n",
3122 (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags,
3123 (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize,
3124 (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree,
3125 (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files,
3126 (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites,
3127 (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads,
3128 (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax,
3129 (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]);
3131 db_printf(" mnt_cred = { uid=%u ruid=%u",
3132 (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid);
3133 if (jailed(mp->mnt_cred))
3134 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id);
3136 db_printf(" mnt_ref = %d\n", mp->mnt_ref);
3137 db_printf(" mnt_gen = %d\n", mp->mnt_gen);
3138 db_printf(" mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize);
3139 db_printf(" mnt_activevnodelistsize = %d\n",
3140 mp->mnt_activevnodelistsize);
3141 db_printf(" mnt_writeopcount = %d\n", mp->mnt_writeopcount);
3142 db_printf(" mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen);
3143 db_printf(" mnt_iosize_max = %d\n", mp->mnt_iosize_max);
3144 db_printf(" mnt_hashseed = %u\n", mp->mnt_hashseed);
3145 db_printf(" mnt_secondary_writes = %d\n", mp->mnt_secondary_writes);
3146 db_printf(" mnt_secondary_accwrites = %d\n",
3147 mp->mnt_secondary_accwrites);
3148 db_printf(" mnt_gjprovider = %s\n",
3149 mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL");
3151 db_printf("\n\nList of active vnodes\n");
3152 TAILQ_FOREACH(vp, &mp->mnt_activevnodelist, v_actfreelist) {
3153 if (vp->v_type != VMARKER) {
3154 vn_printf(vp, "vnode ");
3159 db_printf("\n\nList of inactive vnodes\n");
3160 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3161 if (vp->v_type != VMARKER && (vp->v_iflag & VI_ACTIVE) == 0) {
3162 vn_printf(vp, "vnode ");
3171 * Fill in a struct xvfsconf based on a struct vfsconf.
3174 vfsconf2x(struct sysctl_req *req, struct vfsconf *vfsp)
3176 struct xvfsconf xvfsp;
3178 bzero(&xvfsp, sizeof(xvfsp));
3179 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
3180 xvfsp.vfc_typenum = vfsp->vfc_typenum;
3181 xvfsp.vfc_refcount = vfsp->vfc_refcount;
3182 xvfsp.vfc_flags = vfsp->vfc_flags;
3184 * These are unused in userland, we keep them
3185 * to not break binary compatibility.
3187 xvfsp.vfc_vfsops = NULL;
3188 xvfsp.vfc_next = NULL;
3189 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3192 #ifdef COMPAT_FREEBSD32
3194 uint32_t vfc_vfsops;
3195 char vfc_name[MFSNAMELEN];
3196 int32_t vfc_typenum;
3197 int32_t vfc_refcount;
3203 vfsconf2x32(struct sysctl_req *req, struct vfsconf *vfsp)
3205 struct xvfsconf32 xvfsp;
3207 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
3208 xvfsp.vfc_typenum = vfsp->vfc_typenum;
3209 xvfsp.vfc_refcount = vfsp->vfc_refcount;
3210 xvfsp.vfc_flags = vfsp->vfc_flags;
3211 xvfsp.vfc_vfsops = 0;
3213 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3218 * Top level filesystem related information gathering.
3221 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS)
3223 struct vfsconf *vfsp;
3227 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3228 #ifdef COMPAT_FREEBSD32
3229 if (req->flags & SCTL_MASK32)
3230 error = vfsconf2x32(req, vfsp);
3233 error = vfsconf2x(req, vfsp);
3240 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLTYPE_OPAQUE | CTLFLAG_RD,
3241 NULL, 0, sysctl_vfs_conflist,
3242 "S,xvfsconf", "List of all configured filesystems");
3244 #ifndef BURN_BRIDGES
3245 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS);
3248 vfs_sysctl(SYSCTL_HANDLER_ARGS)
3250 int *name = (int *)arg1 - 1; /* XXX */
3251 u_int namelen = arg2 + 1; /* XXX */
3252 struct vfsconf *vfsp;
3254 printf("WARNING: userland calling deprecated sysctl, "
3255 "please rebuild world\n");
3257 #if 1 || defined(COMPAT_PRELITE2)
3258 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
3260 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
3264 case VFS_MAXTYPENUM:
3267 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
3270 return (ENOTDIR); /* overloaded */
3271 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list)
3272 if (vfsp->vfc_typenum == name[2])
3275 return (EOPNOTSUPP);
3276 #ifdef COMPAT_FREEBSD32
3277 if (req->flags & SCTL_MASK32)
3278 return (vfsconf2x32(req, vfsp));
3281 return (vfsconf2x(req, vfsp));
3283 return (EOPNOTSUPP);
3286 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP,
3287 vfs_sysctl, "Generic filesystem");
3289 #if 1 || defined(COMPAT_PRELITE2)
3292 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
3295 struct vfsconf *vfsp;
3296 struct ovfsconf ovfs;
3298 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3299 bzero(&ovfs, sizeof(ovfs));
3300 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
3301 strcpy(ovfs.vfc_name, vfsp->vfc_name);
3302 ovfs.vfc_index = vfsp->vfc_typenum;
3303 ovfs.vfc_refcount = vfsp->vfc_refcount;
3304 ovfs.vfc_flags = vfsp->vfc_flags;
3305 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
3312 #endif /* 1 || COMPAT_PRELITE2 */
3313 #endif /* !BURN_BRIDGES */
3315 #define KINFO_VNODESLOP 10
3318 * Dump vnode list (via sysctl).
3322 sysctl_vnode(SYSCTL_HANDLER_ARGS)
3330 * Stale numvnodes access is not fatal here.
3333 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn;
3335 /* Make an estimate */
3336 return (SYSCTL_OUT(req, 0, len));
3338 error = sysctl_wire_old_buffer(req, 0);
3341 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK);
3343 mtx_lock(&mountlist_mtx);
3344 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3345 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK))
3348 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3352 xvn[n].xv_size = sizeof *xvn;
3353 xvn[n].xv_vnode = vp;
3354 xvn[n].xv_id = 0; /* XXX compat */
3355 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field
3357 XV_COPY(writecount);
3363 xvn[n].xv_flag = vp->v_vflag;
3365 switch (vp->v_type) {
3372 if (vp->v_rdev == NULL) {
3376 xvn[n].xv_dev = dev2udev(vp->v_rdev);
3379 xvn[n].xv_socket = vp->v_socket;
3382 xvn[n].xv_fifo = vp->v_fifoinfo;
3387 /* shouldn't happen? */
3395 mtx_lock(&mountlist_mtx);
3400 mtx_unlock(&mountlist_mtx);
3402 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn);
3407 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD,
3408 0, 0, sysctl_vnode, "S,xvnode", "");
3412 * Unmount all filesystems. The list is traversed in reverse order
3413 * of mounting to avoid dependencies.
3416 vfs_unmountall(void)
3422 KASSERT(curthread != NULL, ("vfs_unmountall: NULL curthread"));
3423 CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__);
3427 * Since this only runs when rebooting, it is not interlocked.
3429 while(!TAILQ_EMPTY(&mountlist)) {
3430 mp = TAILQ_LAST(&mountlist, mntlist);
3431 error = dounmount(mp, MNT_FORCE, td);
3433 TAILQ_REMOVE(&mountlist, mp, mnt_list);
3435 * XXX: Due to the way in which we mount the root
3436 * file system off of devfs, devfs will generate a
3437 * "busy" warning when we try to unmount it before
3438 * the root. Don't print a warning as a result in
3439 * order to avoid false positive errors that may
3440 * cause needless upset.
3442 if (strcmp(mp->mnt_vfc->vfc_name, "devfs") != 0) {
3443 printf("unmount of %s failed (",
3444 mp->mnt_stat.f_mntonname);
3448 printf("%d)\n", error);
3451 /* The unmount has removed mp from the mountlist */
3457 * perform msync on all vnodes under a mount point
3458 * the mount point must be locked.
3461 vfs_msync(struct mount *mp, int flags)
3463 struct vnode *vp, *mvp;
3464 struct vm_object *obj;
3466 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
3467 MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) {
3469 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0 &&
3470 (flags == MNT_WAIT || VOP_ISLOCKED(vp) == 0)) {
3472 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK,
3474 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */
3481 VM_OBJECT_LOCK(obj);
3482 vm_object_page_clean(obj, 0, 0,
3484 OBJPC_SYNC : OBJPC_NOSYNC);
3485 VM_OBJECT_UNLOCK(obj);
3495 destroy_vpollinfo(struct vpollinfo *vi)
3497 seldrain(&vi->vpi_selinfo);
3498 knlist_destroy(&vi->vpi_selinfo.si_note);
3499 mtx_destroy(&vi->vpi_lock);
3500 uma_zfree(vnodepoll_zone, vi);
3504 * Initalize per-vnode helper structure to hold poll-related state.
3507 v_addpollinfo(struct vnode *vp)
3509 struct vpollinfo *vi;
3511 if (vp->v_pollinfo != NULL)
3513 vi = uma_zalloc(vnodepoll_zone, M_WAITOK);
3514 mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF);
3515 knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock,
3516 vfs_knlunlock, vfs_knl_assert_locked, vfs_knl_assert_unlocked);
3518 if (vp->v_pollinfo != NULL) {
3520 destroy_vpollinfo(vi);
3523 vp->v_pollinfo = vi;
3528 * Record a process's interest in events which might happen to
3529 * a vnode. Because poll uses the historic select-style interface
3530 * internally, this routine serves as both the ``check for any
3531 * pending events'' and the ``record my interest in future events''
3532 * functions. (These are done together, while the lock is held,
3533 * to avoid race conditions.)
3536 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
3540 mtx_lock(&vp->v_pollinfo->vpi_lock);
3541 if (vp->v_pollinfo->vpi_revents & events) {
3543 * This leaves events we are not interested
3544 * in available for the other process which
3545 * which presumably had requested them
3546 * (otherwise they would never have been
3549 events &= vp->v_pollinfo->vpi_revents;
3550 vp->v_pollinfo->vpi_revents &= ~events;
3552 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3555 vp->v_pollinfo->vpi_events |= events;
3556 selrecord(td, &vp->v_pollinfo->vpi_selinfo);
3557 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3562 * Routine to create and manage a filesystem syncer vnode.
3564 #define sync_close ((int (*)(struct vop_close_args *))nullop)
3565 static int sync_fsync(struct vop_fsync_args *);
3566 static int sync_inactive(struct vop_inactive_args *);
3567 static int sync_reclaim(struct vop_reclaim_args *);
3569 static struct vop_vector sync_vnodeops = {
3570 .vop_bypass = VOP_EOPNOTSUPP,
3571 .vop_close = sync_close, /* close */
3572 .vop_fsync = sync_fsync, /* fsync */
3573 .vop_inactive = sync_inactive, /* inactive */
3574 .vop_reclaim = sync_reclaim, /* reclaim */
3575 .vop_lock1 = vop_stdlock, /* lock */
3576 .vop_unlock = vop_stdunlock, /* unlock */
3577 .vop_islocked = vop_stdislocked, /* islocked */
3581 * Create a new filesystem syncer vnode for the specified mount point.
3584 vfs_allocate_syncvnode(struct mount *mp)
3588 static long start, incr, next;
3591 /* Allocate a new vnode */
3592 error = getnewvnode("syncer", mp, &sync_vnodeops, &vp);
3594 panic("vfs_allocate_syncvnode: getnewvnode() failed");
3596 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3597 vp->v_vflag |= VV_FORCEINSMQ;
3598 error = insmntque(vp, mp);
3600 panic("vfs_allocate_syncvnode: insmntque() failed");
3601 vp->v_vflag &= ~VV_FORCEINSMQ;
3604 * Place the vnode onto the syncer worklist. We attempt to
3605 * scatter them about on the list so that they will go off
3606 * at evenly distributed times even if all the filesystems
3607 * are mounted at once.
3610 if (next == 0 || next > syncer_maxdelay) {
3614 start = syncer_maxdelay / 2;
3615 incr = syncer_maxdelay;
3621 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0);
3622 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */
3623 mtx_lock(&sync_mtx);
3625 if (mp->mnt_syncer == NULL) {
3626 mp->mnt_syncer = vp;
3629 mtx_unlock(&sync_mtx);
3632 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3639 vfs_deallocate_syncvnode(struct mount *mp)
3643 mtx_lock(&sync_mtx);
3644 vp = mp->mnt_syncer;
3646 mp->mnt_syncer = NULL;
3647 mtx_unlock(&sync_mtx);
3653 * Do a lazy sync of the filesystem.
3656 sync_fsync(struct vop_fsync_args *ap)
3658 struct vnode *syncvp = ap->a_vp;
3659 struct mount *mp = syncvp->v_mount;
3664 * We only need to do something if this is a lazy evaluation.
3666 if (ap->a_waitfor != MNT_LAZY)
3670 * Move ourselves to the back of the sync list.
3672 bo = &syncvp->v_bufobj;
3674 vn_syncer_add_to_worklist(bo, syncdelay);
3678 * Walk the list of vnodes pushing all that are dirty and
3679 * not already on the sync list.
3681 mtx_lock(&mountlist_mtx);
3682 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
3683 mtx_unlock(&mountlist_mtx);
3686 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) {
3690 save = curthread_pflags_set(TDP_SYNCIO);
3691 vfs_msync(mp, MNT_NOWAIT);
3692 error = VFS_SYNC(mp, MNT_LAZY);
3693 curthread_pflags_restore(save);
3694 vn_finished_write(mp);
3700 * The syncer vnode is no referenced.
3703 sync_inactive(struct vop_inactive_args *ap)
3711 * The syncer vnode is no longer needed and is being decommissioned.
3713 * Modifications to the worklist must be protected by sync_mtx.
3716 sync_reclaim(struct vop_reclaim_args *ap)
3718 struct vnode *vp = ap->a_vp;
3723 mtx_lock(&sync_mtx);
3724 if (vp->v_mount->mnt_syncer == vp)
3725 vp->v_mount->mnt_syncer = NULL;
3726 if (bo->bo_flag & BO_ONWORKLST) {
3727 LIST_REMOVE(bo, bo_synclist);
3728 syncer_worklist_len--;
3730 bo->bo_flag &= ~BO_ONWORKLST;
3732 mtx_unlock(&sync_mtx);
3739 * Check if vnode represents a disk device
3742 vn_isdisk(struct vnode *vp, int *errp)
3748 if (vp->v_type != VCHR)
3750 else if (vp->v_rdev == NULL)
3752 else if (vp->v_rdev->si_devsw == NULL)
3754 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK))
3759 return (error == 0);
3763 * Common filesystem object access control check routine. Accepts a
3764 * vnode's type, "mode", uid and gid, requested access mode, credentials,
3765 * and optional call-by-reference privused argument allowing vaccess()
3766 * to indicate to the caller whether privilege was used to satisfy the
3767 * request (obsoleted). Returns 0 on success, or an errno on failure.
3770 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid,
3771 accmode_t accmode, struct ucred *cred, int *privused)
3773 accmode_t dac_granted;
3774 accmode_t priv_granted;
3776 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0,
3777 ("invalid bit in accmode"));
3778 KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE),
3779 ("VAPPEND without VWRITE"));
3782 * Look for a normal, non-privileged way to access the file/directory
3783 * as requested. If it exists, go with that.
3786 if (privused != NULL)
3791 /* Check the owner. */
3792 if (cred->cr_uid == file_uid) {
3793 dac_granted |= VADMIN;
3794 if (file_mode & S_IXUSR)
3795 dac_granted |= VEXEC;
3796 if (file_mode & S_IRUSR)
3797 dac_granted |= VREAD;
3798 if (file_mode & S_IWUSR)
3799 dac_granted |= (VWRITE | VAPPEND);
3801 if ((accmode & dac_granted) == accmode)
3807 /* Otherwise, check the groups (first match) */
3808 if (groupmember(file_gid, cred)) {
3809 if (file_mode & S_IXGRP)
3810 dac_granted |= VEXEC;
3811 if (file_mode & S_IRGRP)
3812 dac_granted |= VREAD;
3813 if (file_mode & S_IWGRP)
3814 dac_granted |= (VWRITE | VAPPEND);
3816 if ((accmode & dac_granted) == accmode)
3822 /* Otherwise, check everyone else. */
3823 if (file_mode & S_IXOTH)
3824 dac_granted |= VEXEC;
3825 if (file_mode & S_IROTH)
3826 dac_granted |= VREAD;
3827 if (file_mode & S_IWOTH)
3828 dac_granted |= (VWRITE | VAPPEND);
3829 if ((accmode & dac_granted) == accmode)
3834 * Build a privilege mask to determine if the set of privileges
3835 * satisfies the requirements when combined with the granted mask
3836 * from above. For each privilege, if the privilege is required,
3837 * bitwise or the request type onto the priv_granted mask.
3843 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC
3844 * requests, instead of PRIV_VFS_EXEC.
3846 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3847 !priv_check_cred(cred, PRIV_VFS_LOOKUP, 0))
3848 priv_granted |= VEXEC;
3851 * Ensure that at least one execute bit is on. Otherwise,
3852 * a privileged user will always succeed, and we don't want
3853 * this to happen unless the file really is executable.
3855 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3856 (file_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 &&
3857 !priv_check_cred(cred, PRIV_VFS_EXEC, 0))
3858 priv_granted |= VEXEC;
3861 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) &&
3862 !priv_check_cred(cred, PRIV_VFS_READ, 0))
3863 priv_granted |= VREAD;
3865 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) &&
3866 !priv_check_cred(cred, PRIV_VFS_WRITE, 0))
3867 priv_granted |= (VWRITE | VAPPEND);
3869 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) &&
3870 !priv_check_cred(cred, PRIV_VFS_ADMIN, 0))
3871 priv_granted |= VADMIN;
3873 if ((accmode & (priv_granted | dac_granted)) == accmode) {
3874 /* XXX audit: privilege used */
3875 if (privused != NULL)
3880 return ((accmode & VADMIN) ? EPERM : EACCES);
3884 * Credential check based on process requesting service, and per-attribute
3888 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred,
3889 struct thread *td, accmode_t accmode)
3893 * Kernel-invoked always succeeds.
3899 * Do not allow privileged processes in jail to directly manipulate
3900 * system attributes.
3902 switch (attrnamespace) {
3903 case EXTATTR_NAMESPACE_SYSTEM:
3904 /* Potentially should be: return (EPERM); */
3905 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM, 0));
3906 case EXTATTR_NAMESPACE_USER:
3907 return (VOP_ACCESS(vp, accmode, cred, td));
3913 #ifdef DEBUG_VFS_LOCKS
3915 * This only exists to supress warnings from unlocked specfs accesses. It is
3916 * no longer ok to have an unlocked VFS.
3918 #define IGNORE_LOCK(vp) (panicstr != NULL || (vp) == NULL || \
3919 (vp)->v_type == VCHR || (vp)->v_type == VBAD)
3921 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */
3922 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0,
3923 "Drop into debugger on lock violation");
3925 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */
3926 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex,
3927 0, "Check for interlock across VOPs");
3929 int vfs_badlock_print = 1; /* Print lock violations. */
3930 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print,
3931 0, "Print lock violations");
3934 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */
3935 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW,
3936 &vfs_badlock_backtrace, 0, "Print backtrace at lock violations");
3940 vfs_badlock(const char *msg, const char *str, struct vnode *vp)
3944 if (vfs_badlock_backtrace)
3947 if (vfs_badlock_print)
3948 printf("%s: %p %s\n", str, (void *)vp, msg);
3949 if (vfs_badlock_ddb)
3950 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
3954 assert_vi_locked(struct vnode *vp, const char *str)
3957 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp)))
3958 vfs_badlock("interlock is not locked but should be", str, vp);
3962 assert_vi_unlocked(struct vnode *vp, const char *str)
3965 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp)))
3966 vfs_badlock("interlock is locked but should not be", str, vp);
3970 assert_vop_locked(struct vnode *vp, const char *str)
3973 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == 0)
3974 vfs_badlock("is not locked but should be", str, vp);
3978 assert_vop_unlocked(struct vnode *vp, const char *str)
3981 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
3982 vfs_badlock("is locked but should not be", str, vp);
3986 assert_vop_elocked(struct vnode *vp, const char *str)
3989 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
3990 vfs_badlock("is not exclusive locked but should be", str, vp);
3995 assert_vop_elocked_other(struct vnode *vp, const char *str)
3998 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLOTHER)
3999 vfs_badlock("is not exclusive locked by another thread",
4004 assert_vop_slocked(struct vnode *vp, const char *str)
4007 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_SHARED)
4008 vfs_badlock("is not locked shared but should be", str, vp);
4011 #endif /* DEBUG_VFS_LOCKS */
4014 vop_rename_fail(struct vop_rename_args *ap)
4017 if (ap->a_tvp != NULL)
4019 if (ap->a_tdvp == ap->a_tvp)
4028 vop_rename_pre(void *ap)
4030 struct vop_rename_args *a = ap;
4032 #ifdef DEBUG_VFS_LOCKS
4034 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME");
4035 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME");
4036 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME");
4037 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME");
4039 /* Check the source (from). */
4040 if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock &&
4041 (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock))
4042 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked");
4043 if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock)
4044 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked");
4046 /* Check the target. */
4048 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked");
4049 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked");
4051 if (a->a_tdvp != a->a_fdvp)
4053 if (a->a_tvp != a->a_fvp)
4061 vop_strategy_pre(void *ap)
4063 #ifdef DEBUG_VFS_LOCKS
4064 struct vop_strategy_args *a;
4071 * Cluster ops lock their component buffers but not the IO container.
4073 if ((bp->b_flags & B_CLUSTER) != 0)
4076 if (panicstr == NULL && !BUF_ISLOCKED(bp)) {
4077 if (vfs_badlock_print)
4079 "VOP_STRATEGY: bp is not locked but should be\n");
4080 if (vfs_badlock_ddb)
4081 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
4087 vop_lookup_pre(void *ap)
4089 #ifdef DEBUG_VFS_LOCKS
4090 struct vop_lookup_args *a;
4095 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP");
4096 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP");
4101 vop_lookup_post(void *ap, int rc)
4103 #ifdef DEBUG_VFS_LOCKS
4104 struct vop_lookup_args *a;
4112 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP");
4113 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP");
4116 ASSERT_VOP_LOCKED(vp, "VOP_LOOKUP (child)");
4121 vop_lock_pre(void *ap)
4123 #ifdef DEBUG_VFS_LOCKS
4124 struct vop_lock1_args *a = ap;
4126 if ((a->a_flags & LK_INTERLOCK) == 0)
4127 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
4129 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK");
4134 vop_lock_post(void *ap, int rc)
4136 #ifdef DEBUG_VFS_LOCKS
4137 struct vop_lock1_args *a = ap;
4139 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
4141 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK");
4146 vop_unlock_pre(void *ap)
4148 #ifdef DEBUG_VFS_LOCKS
4149 struct vop_unlock_args *a = ap;
4151 if (a->a_flags & LK_INTERLOCK)
4152 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK");
4153 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK");
4158 vop_unlock_post(void *ap, int rc)
4160 #ifdef DEBUG_VFS_LOCKS
4161 struct vop_unlock_args *a = ap;
4163 if (a->a_flags & LK_INTERLOCK)
4164 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK");
4169 vop_create_post(void *ap, int rc)
4171 struct vop_create_args *a = ap;
4174 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4178 vop_deleteextattr_post(void *ap, int rc)
4180 struct vop_deleteextattr_args *a = ap;
4183 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4187 vop_link_post(void *ap, int rc)
4189 struct vop_link_args *a = ap;
4192 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK);
4193 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE);
4198 vop_mkdir_post(void *ap, int rc)
4200 struct vop_mkdir_args *a = ap;
4203 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4207 vop_mknod_post(void *ap, int rc)
4209 struct vop_mknod_args *a = ap;
4212 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4216 vop_remove_post(void *ap, int rc)
4218 struct vop_remove_args *a = ap;
4221 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4222 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4227 vop_rename_post(void *ap, int rc)
4229 struct vop_rename_args *a = ap;
4232 VFS_KNOTE_UNLOCKED(a->a_fdvp, NOTE_WRITE);
4233 VFS_KNOTE_UNLOCKED(a->a_tdvp, NOTE_WRITE);
4234 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME);
4236 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE);
4238 if (a->a_tdvp != a->a_fdvp)
4240 if (a->a_tvp != a->a_fvp)
4248 vop_rmdir_post(void *ap, int rc)
4250 struct vop_rmdir_args *a = ap;
4253 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4254 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4259 vop_setattr_post(void *ap, int rc)
4261 struct vop_setattr_args *a = ap;
4264 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4268 vop_setextattr_post(void *ap, int rc)
4270 struct vop_setextattr_args *a = ap;
4273 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4277 vop_symlink_post(void *ap, int rc)
4279 struct vop_symlink_args *a = ap;
4282 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4285 static struct knlist fs_knlist;
4288 vfs_event_init(void *arg)
4290 knlist_init_mtx(&fs_knlist, NULL);
4292 /* XXX - correct order? */
4293 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL);
4296 vfs_event_signal(fsid_t *fsid, uint32_t event, intptr_t data __unused)
4299 KNOTE_UNLOCKED(&fs_knlist, event);
4302 static int filt_fsattach(struct knote *kn);
4303 static void filt_fsdetach(struct knote *kn);
4304 static int filt_fsevent(struct knote *kn, long hint);
4306 struct filterops fs_filtops = {
4308 .f_attach = filt_fsattach,
4309 .f_detach = filt_fsdetach,
4310 .f_event = filt_fsevent
4314 filt_fsattach(struct knote *kn)
4317 kn->kn_flags |= EV_CLEAR;
4318 knlist_add(&fs_knlist, kn, 0);
4323 filt_fsdetach(struct knote *kn)
4326 knlist_remove(&fs_knlist, kn, 0);
4330 filt_fsevent(struct knote *kn, long hint)
4333 kn->kn_fflags |= hint;
4334 return (kn->kn_fflags != 0);
4338 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS)
4344 error = SYSCTL_IN(req, &vc, sizeof(vc));
4347 if (vc.vc_vers != VFS_CTL_VERS1)
4349 mp = vfs_getvfs(&vc.vc_fsid);
4352 /* ensure that a specific sysctl goes to the right filesystem. */
4353 if (strcmp(vc.vc_fstypename, "*") != 0 &&
4354 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) {
4358 VCTLTOREQ(&vc, req);
4359 error = VFS_SYSCTL(mp, vc.vc_op, req);
4364 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLTYPE_OPAQUE | CTLFLAG_WR,
4365 NULL, 0, sysctl_vfs_ctl, "",
4369 * Function to initialize a va_filerev field sensibly.
4370 * XXX: Wouldn't a random number make a lot more sense ??
4373 init_va_filerev(void)
4378 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL));
4381 static int filt_vfsread(struct knote *kn, long hint);
4382 static int filt_vfswrite(struct knote *kn, long hint);
4383 static int filt_vfsvnode(struct knote *kn, long hint);
4384 static void filt_vfsdetach(struct knote *kn);
4385 static struct filterops vfsread_filtops = {
4387 .f_detach = filt_vfsdetach,
4388 .f_event = filt_vfsread
4390 static struct filterops vfswrite_filtops = {
4392 .f_detach = filt_vfsdetach,
4393 .f_event = filt_vfswrite
4395 static struct filterops vfsvnode_filtops = {
4397 .f_detach = filt_vfsdetach,
4398 .f_event = filt_vfsvnode
4402 vfs_knllock(void *arg)
4404 struct vnode *vp = arg;
4406 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4410 vfs_knlunlock(void *arg)
4412 struct vnode *vp = arg;
4418 vfs_knl_assert_locked(void *arg)
4420 #ifdef DEBUG_VFS_LOCKS
4421 struct vnode *vp = arg;
4423 ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked");
4428 vfs_knl_assert_unlocked(void *arg)
4430 #ifdef DEBUG_VFS_LOCKS
4431 struct vnode *vp = arg;
4433 ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked");
4438 vfs_kqfilter(struct vop_kqfilter_args *ap)
4440 struct vnode *vp = ap->a_vp;
4441 struct knote *kn = ap->a_kn;
4444 switch (kn->kn_filter) {
4446 kn->kn_fop = &vfsread_filtops;
4449 kn->kn_fop = &vfswrite_filtops;
4452 kn->kn_fop = &vfsvnode_filtops;
4458 kn->kn_hook = (caddr_t)vp;
4461 if (vp->v_pollinfo == NULL)
4463 knl = &vp->v_pollinfo->vpi_selinfo.si_note;
4464 knlist_add(knl, kn, 0);
4470 * Detach knote from vnode
4473 filt_vfsdetach(struct knote *kn)
4475 struct vnode *vp = (struct vnode *)kn->kn_hook;
4477 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo"));
4478 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
4483 filt_vfsread(struct knote *kn, long hint)
4485 struct vnode *vp = (struct vnode *)kn->kn_hook;
4490 * filesystem is gone, so set the EOF flag and schedule
4491 * the knote for deletion.
4493 if (hint == NOTE_REVOKE) {
4495 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4500 if (VOP_GETATTR(vp, &va, curthread->td_ucred))
4504 kn->kn_data = va.va_size - kn->kn_fp->f_offset;
4505 res = (kn->kn_data != 0);
4512 filt_vfswrite(struct knote *kn, long hint)
4514 struct vnode *vp = (struct vnode *)kn->kn_hook;
4519 * filesystem is gone, so set the EOF flag and schedule
4520 * the knote for deletion.
4522 if (hint == NOTE_REVOKE)
4523 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4531 filt_vfsvnode(struct knote *kn, long hint)
4533 struct vnode *vp = (struct vnode *)kn->kn_hook;
4537 if (kn->kn_sfflags & hint)
4538 kn->kn_fflags |= hint;
4539 if (hint == NOTE_REVOKE) {
4540 kn->kn_flags |= EV_EOF;
4544 res = (kn->kn_fflags != 0);
4550 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off)
4554 if (dp->d_reclen > ap->a_uio->uio_resid)
4555 return (ENAMETOOLONG);
4556 error = uiomove(dp, dp->d_reclen, ap->a_uio);
4558 if (ap->a_ncookies != NULL) {
4559 if (ap->a_cookies != NULL)
4560 free(ap->a_cookies, M_TEMP);
4561 ap->a_cookies = NULL;
4562 *ap->a_ncookies = 0;
4566 if (ap->a_ncookies == NULL)
4569 KASSERT(ap->a_cookies,
4570 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!"));
4572 *ap->a_cookies = realloc(*ap->a_cookies,
4573 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO);
4574 (*ap->a_cookies)[*ap->a_ncookies] = off;
4579 * Mark for update the access time of the file if the filesystem
4580 * supports VOP_MARKATIME. This functionality is used by execve and
4581 * mmap, so we want to avoid the I/O implied by directly setting
4582 * va_atime for the sake of efficiency.
4585 vfs_mark_atime(struct vnode *vp, struct ucred *cred)
4590 VFS_ASSERT_GIANT(mp);
4591 ASSERT_VOP_LOCKED(vp, "vfs_mark_atime");
4592 if (mp != NULL && (mp->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
4593 (void)VOP_MARKATIME(vp);
4597 * The purpose of this routine is to remove granularity from accmode_t,
4598 * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE,
4599 * VADMIN and VAPPEND.
4601 * If it returns 0, the caller is supposed to continue with the usual
4602 * access checks using 'accmode' as modified by this routine. If it
4603 * returns nonzero value, the caller is supposed to return that value
4606 * Note that after this routine runs, accmode may be zero.
4609 vfs_unixify_accmode(accmode_t *accmode)
4612 * There is no way to specify explicit "deny" rule using
4613 * file mode or POSIX.1e ACLs.
4615 if (*accmode & VEXPLICIT_DENY) {
4621 * None of these can be translated into usual access bits.
4622 * Also, the common case for NFSv4 ACLs is to not contain
4623 * either of these bits. Caller should check for VWRITE
4624 * on the containing directory instead.
4626 if (*accmode & (VDELETE_CHILD | VDELETE))
4629 if (*accmode & VADMIN_PERMS) {
4630 *accmode &= ~VADMIN_PERMS;
4635 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL
4636 * or VSYNCHRONIZE using file mode or POSIX.1e ACL.
4638 *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE);
4644 * These are helper functions for filesystems to traverse all
4645 * their vnodes. See MNT_VNODE_FOREACH_ALL() in sys/mount.h.
4647 * This interface replaces MNT_VNODE_FOREACH.
4650 MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker");
4653 __mnt_vnode_next_all(struct vnode **mvp, struct mount *mp)
4658 kern_yield(PRI_UNCHANGED);
4660 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4661 vp = TAILQ_NEXT(*mvp, v_nmntvnodes);
4662 while (vp != NULL && (vp->v_type == VMARKER ||
4663 (vp->v_iflag & VI_DOOMED) != 0))
4664 vp = TAILQ_NEXT(vp, v_nmntvnodes);
4666 /* Check if we are done */
4668 __mnt_vnode_markerfree_all(mvp, mp);
4669 /* MNT_IUNLOCK(mp); -- done in above function */
4670 mtx_assert(MNT_MTX(mp), MA_NOTOWNED);
4673 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
4674 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
4681 __mnt_vnode_first_all(struct vnode **mvp, struct mount *mp)
4685 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
4688 (*mvp)->v_type = VMARKER;
4690 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
4691 while (vp != NULL && (vp->v_type == VMARKER ||
4692 (vp->v_iflag & VI_DOOMED) != 0))
4693 vp = TAILQ_NEXT(vp, v_nmntvnodes);
4695 /* Check if we are done */
4699 free(*mvp, M_VNODE_MARKER);
4703 (*mvp)->v_mount = mp;
4704 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
4712 __mnt_vnode_markerfree_all(struct vnode **mvp, struct mount *mp)
4720 mtx_assert(MNT_MTX(mp), MA_OWNED);
4722 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4723 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
4726 free(*mvp, M_VNODE_MARKER);
4731 * These are helper functions for filesystems to traverse their
4732 * active vnodes. See MNT_VNODE_FOREACH_ACTIVE() in sys/mount.h
4735 __mnt_vnode_next_active(struct vnode **mvp, struct mount *mp)
4737 struct vnode *vp, *nvp;
4740 kern_yield(PRI_UNCHANGED);
4742 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4743 vp = TAILQ_NEXT(*mvp, v_actfreelist);
4744 while (vp != NULL) {
4746 if (vp->v_mount == mp && vp->v_type != VMARKER &&
4747 (vp->v_iflag & VI_DOOMED) == 0)
4749 nvp = TAILQ_NEXT(vp, v_actfreelist);
4754 /* Check if we are done */
4756 __mnt_vnode_markerfree_active(mvp, mp);
4757 /* MNT_IUNLOCK(mp); -- done in above function */
4758 mtx_assert(MNT_MTX(mp), MA_NOTOWNED);
4761 mtx_lock(&vnode_free_list_mtx);
4762 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
4763 TAILQ_INSERT_AFTER(&mp->mnt_activevnodelist, vp, *mvp, v_actfreelist);
4764 mtx_unlock(&vnode_free_list_mtx);
4770 __mnt_vnode_first_active(struct vnode **mvp, struct mount *mp)
4772 struct vnode *vp, *nvp;
4774 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
4777 (*mvp)->v_type = VMARKER;
4779 vp = TAILQ_FIRST(&mp->mnt_activevnodelist);
4780 while (vp != NULL) {
4782 if (vp->v_mount == mp && vp->v_type != VMARKER &&
4783 (vp->v_iflag & VI_DOOMED) == 0)
4785 nvp = TAILQ_NEXT(vp, v_actfreelist);
4790 /* Check if we are done */
4794 free(*mvp, M_VNODE_MARKER);
4798 (*mvp)->v_mount = mp;
4799 mtx_lock(&vnode_free_list_mtx);
4800 TAILQ_INSERT_AFTER(&mp->mnt_activevnodelist, vp, *mvp, v_actfreelist);
4801 mtx_unlock(&vnode_free_list_mtx);
4807 __mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp)
4815 mtx_assert(MNT_MTX(mp), MA_OWNED);
4817 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4818 mtx_lock(&vnode_free_list_mtx);
4819 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
4820 mtx_unlock(&vnode_free_list_mtx);
4823 free(*mvp, M_VNODE_MARKER);