2 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 1989, 1993
35 * The Regents of the University of California. All rights reserved.
36 * (c) UNIX System Laboratories, Inc.
37 * All or some portions of this file are derived from material licensed
38 * to the University of California by American Telephone and Telegraph
39 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
40 * the permission of UNIX System Laboratories, Inc.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * $DragonFly: src/sys/kern/vfs_mount.c,v 1.4 2004/12/29 02:40:02 dillon Exp $
74 * External virtual filesystem routines
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/kernel.h>
81 #include <sys/malloc.h>
82 #include <sys/mount.h>
84 #include <sys/vnode.h>
86 #include <sys/eventhandler.h>
87 #include <sys/kthread.h>
88 #include <sys/sysctl.h>
90 #include <machine/limits.h>
93 #include <sys/thread2.h>
96 #include <vm/vm_object.h>
98 static int vnlru_nowhere = 0;
99 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW,
101 "Number of times the vnlru process ran without success");
104 static struct lwkt_token mntid_token;
106 struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist); /* mounted fs */
107 struct lwkt_token mountlist_token;
108 struct lwkt_token mntvnode_token;
112 * Called from vfsinit()
117 lwkt_token_init(&mountlist_token);
118 lwkt_token_init(&mntvnode_token);
119 lwkt_token_init(&mntid_token);
123 * Allocate a new vnode and associate it with a tag, mount point, and
126 * A VX locked and refd vnode is returned. The caller should setup the
127 * remaining fields and vx_put() or, if he wishes to leave a vref,
128 * vx_unlock() the vnode.
131 getnewvnode(enum vtagtype tag, struct mount *mp,
132 struct vnode **vpp, int lktimeout, int lkflags)
136 KKASSERT(mp != NULL);
138 vp = allocvnode(lktimeout, lkflags);
143 * By default the vnode is assigned the mount point's normal
146 vp->v_ops = &mp->mnt_vn_use_ops;
149 * Placing the vnode on the mount point's queue makes it visible.
150 * VNON prevents it from being messed with, however.
153 vfs_object_create(vp, curthread);
156 * A VX locked & refd vnode is returned.
163 * This function creates vnodes with special operations vectors. The
164 * mount point is optional.
166 * This routine is being phased out.
169 getspecialvnode(enum vtagtype tag, struct mount *mp,
170 struct vop_ops **ops_pp,
171 struct vnode **vpp, int lktimeout, int lkflags)
175 vp = allocvnode(lktimeout, lkflags);
181 * Placing the vnode on the mount point's queue makes it visible.
182 * VNON prevents it from being messed with, however.
185 vfs_object_create(vp, curthread);
188 * A VX locked & refd vnode is returned.
195 * Mark a mount point as busy. Used to synchronize access and to delay
196 * unmounting. Interlock is not released on failure.
199 vfs_busy(struct mount *mp, int flags,
200 lwkt_tokref_t interlkp, struct thread *td)
204 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
205 if (flags & LK_NOWAIT)
207 mp->mnt_kern_flag |= MNTK_MWAIT;
209 * Since all busy locks are shared except the exclusive
210 * lock granted when unmounting, the only place that a
211 * wakeup needs to be done is at the release of the
212 * exclusive lock at the end of dounmount.
214 * note: interlkp is a serializer and thus can be safely
215 * held through any sleep
217 tsleep((caddr_t)mp, 0, "vfs_busy", 0);
220 lkflags = LK_SHARED | LK_NOPAUSE;
222 lkflags |= LK_INTERLOCK;
223 if (lockmgr(&mp->mnt_lock, lkflags, interlkp, td))
224 panic("vfs_busy: unexpected lock failure");
229 * Free a busy filesystem.
232 vfs_unbusy(struct mount *mp, struct thread *td)
234 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td);
238 * Lookup a filesystem type, and if found allocate and initialize
239 * a mount structure for it.
241 * Devname is usually updated by mount(8) after booting.
244 vfs_rootmountalloc(char *fstypename, char *devname, struct mount **mpp)
246 struct thread *td = curthread; /* XXX */
247 struct vfsconf *vfsp;
250 if (fstypename == NULL)
252 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
253 if (!strcmp(vfsp->vfc_name, fstypename))
258 mp = malloc(sizeof(struct mount), M_MOUNT, M_WAITOK);
259 bzero((char *)mp, (u_long)sizeof(struct mount));
260 lockinit(&mp->mnt_lock, 0, "vfslock", VLKTIMEOUT, LK_NOPAUSE);
261 vfs_busy(mp, LK_NOWAIT, NULL, td);
262 TAILQ_INIT(&mp->mnt_nvnodelist);
263 TAILQ_INIT(&mp->mnt_reservedvnlist);
264 TAILQ_INIT(&mp->mnt_jlist);
265 mp->mnt_nvnodelistsize = 0;
267 mp->mnt_op = vfsp->vfc_vfsops;
268 mp->mnt_flag = MNT_RDONLY;
269 mp->mnt_vnodecovered = NULLVP;
270 vfsp->vfc_refcount++;
271 mp->mnt_iosize_max = DFLTPHYS;
272 mp->mnt_stat.f_type = vfsp->vfc_typenum;
273 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
274 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
275 mp->mnt_stat.f_mntonname[0] = '/';
276 mp->mnt_stat.f_mntonname[1] = 0;
277 (void) copystr(devname, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 0);
283 * Lookup a mount point by filesystem identifier.
286 vfs_getvfs(fsid_t *fsid)
291 lwkt_gettoken(&ilock, &mountlist_token);
292 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
293 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
294 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
298 lwkt_reltoken(&ilock);
303 * Get a new unique fsid. Try to make its val[0] unique, since this value
304 * will be used to create fake device numbers for stat(). Also try (but
305 * not so hard) make its val[0] unique mod 2^16, since some emulators only
306 * support 16-bit device numbers. We end up with unique val[0]'s for the
307 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
309 * Keep in mind that several mounts may be running in parallel. Starting
310 * the search one past where the previous search terminated is both a
311 * micro-optimization and a defense against returning the same fsid to
315 vfs_getnewfsid(struct mount *mp)
317 static u_int16_t mntid_base;
322 lwkt_gettoken(&ilock, &mntid_token);
323 mtype = mp->mnt_vfc->vfc_typenum;
324 tfsid.val[1] = mtype;
325 mtype = (mtype & 0xFF) << 24;
327 tfsid.val[0] = makeudev(255,
328 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
330 if (vfs_getvfs(&tfsid) == NULL)
333 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
334 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
335 lwkt_reltoken(&ilock);
339 * This routine is called when we have too many vnodes. It attempts
340 * to free <count> vnodes and will potentially free vnodes that still
341 * have VM backing store (VM backing store is typically the cause
342 * of a vnode blowout so we want to do this). Therefore, this operation
343 * is not considered cheap.
345 * A number of conditions may prevent a vnode from being reclaimed.
346 * the buffer cache may have references on the vnode, a directory
347 * vnode may still have references due to the namei cache representing
348 * underlying files, or the vnode may be in active use. It is not
349 * desireable to reuse such vnodes. These conditions may cause the
350 * number of vnodes to reach some minimum value regardless of what
351 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
355 * Return 0 if the vnode is not already on the free list, return 1 if the
356 * vnode, with some additional work could possibly be placed on the free list.
359 vmightfree(struct vnode *vp, int use_count, int page_count)
361 if (vp->v_flag & VFREE)
363 if (vp->v_usecount != use_count || vp->v_holdcnt)
365 if (vp->v_object && vp->v_object->resident_page_count >= page_count)
372 vlrureclaim(struct mount *mp)
382 * Calculate the trigger point, don't allow user
383 * screwups to blow us up. This prevents us from
384 * recycling vnodes with lots of resident pages. We
385 * aren't trying to free memory, we are trying to
388 usevnodes = desiredvnodes;
391 trigger = vmstats.v_page_count * 2 / usevnodes;
394 lwkt_gettoken(&ilock, &mntvnode_token);
395 count = mp->mnt_nvnodelistsize / 10 + 1;
396 while (count && (vp = TAILQ_FIRST(&mp->mnt_nvnodelist)) != NULL) {
400 * The VP will stick around while we hold mntvnode_token,
401 * at least until we block, so we can safely do an initial
402 * check, and then must check again after we lock the vnode.
404 if (vp->v_type == VNON || /* XXX */
405 vp->v_type == VBAD || /* XXX */
406 !vmightfree(vp, 0, trigger) /* critical path opt */
408 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
409 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist,vp, v_nmntvnodes);
415 * VX get the candidate vnode. If the VX get fails the
416 * vnode might still be on the mountlist. Our loop depends
417 * on us at least cycling the vnode to the end of the
420 if (vx_get_nonblock(vp) != 0) {
421 if (vp->v_mount == mp) {
422 TAILQ_REMOVE(&mp->mnt_nvnodelist,
424 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist,
432 * Since we blocked locking the vp, make sure it is still
433 * a candidate for reclamation. That is, it has not already
434 * been reclaimed and only has our VX reference associated
437 if (vp->v_type == VNON || /* XXX */
438 vp->v_type == VBAD || /* XXX */
439 (vp->v_flag & VRECLAIMED) ||
441 !vmightfree(vp, 1, trigger) /* critical path opt */
443 if (vp->v_mount == mp) {
444 TAILQ_REMOVE(&mp->mnt_nvnodelist,
446 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist,
455 * All right, we are good, move the vp to the end of the
456 * mountlist and clean it out. The vget will have returned
457 * an error if the vnode was destroyed (VRECLAIMED set), so we
458 * do not have to check again. The vput() will move the
459 * vnode to the free list if the vgone() was successful.
461 KKASSERT(vp->v_mount == mp);
462 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
463 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist,vp, v_nmntvnodes);
469 lwkt_reltoken(&ilock);
474 * Attempt to recycle vnodes in a context that is always safe to block.
475 * Calling vlrurecycle() from the bowels of file system code has some
476 * interesting deadlock problems.
478 static struct thread *vnlruthread;
479 static int vnlruproc_sig;
482 vnlru_proc_wait(void)
484 if (vnlruproc_sig == 0) {
485 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
488 tsleep(&vnlruproc_sig, 0, "vlruwk", hz);
494 struct mount *mp, *nmp;
498 struct thread *td = curthread;
500 EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc, td,
505 kproc_suspend_loop();
506 if (numvnodes - freevnodes <= desiredvnodes * 9 / 10) {
508 wakeup(&vnlruproc_sig);
509 tsleep(td, 0, "vlruwt", hz);
514 lwkt_gettoken(&ilock, &mountlist_token);
515 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
516 if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
517 nmp = TAILQ_NEXT(mp, mnt_list);
520 done += vlrureclaim(mp);
521 lwkt_gettokref(&ilock);
522 nmp = TAILQ_NEXT(mp, mnt_list);
525 lwkt_reltoken(&ilock);
528 tsleep(td, 0, "vlrup", hz * 3);
529 if (vnlru_nowhere % 10 == 0)
530 printf("vnlru_proc: vnode recycler stopped working!\n");
538 static struct kproc_desc vnlru_kp = {
543 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &vnlru_kp)
546 * Move a vnode from one mount queue to another.
549 insmntque(struct vnode *vp, struct mount *mp)
553 lwkt_gettoken(&ilock, &mntvnode_token);
555 * Delete from old mount point vnode list, if on one.
557 if (vp->v_mount != NULL) {
558 KASSERT(vp->v_mount->mnt_nvnodelistsize > 0,
559 ("bad mount point vnode list size"));
560 TAILQ_REMOVE(&vp->v_mount->mnt_nvnodelist, vp, v_nmntvnodes);
561 vp->v_mount->mnt_nvnodelistsize--;
564 * Insert into list of vnodes for the new mount point, if available.
566 if ((vp->v_mount = mp) == NULL) {
567 lwkt_reltoken(&ilock);
570 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
571 mp->mnt_nvnodelistsize++;
572 lwkt_reltoken(&ilock);
577 * Scan the vnodes under a mount point. The first function is called
578 * with just the mountlist token held (no vnode lock). The second
579 * function is called with the vnode VX locked.
585 int (*fastfunc)(struct mount *mp, struct vnode *vp, void *data),
586 int (*slowfunc)(struct mount *mp, struct vnode *vp, void *data),
595 * Scan the vnodes on the mount's vnode list. Use a placemarker
597 pvp = allocvnode_placemarker();
599 lwkt_gettoken(&ilock, &mntvnode_token);
600 TAILQ_INSERT_HEAD(&mp->mnt_nvnodelist, pvp, v_nmntvnodes);
602 while ((vp = TAILQ_NEXT(pvp, v_nmntvnodes)) != NULL) {
604 * Move the placemarker and skip other placemarkers we
605 * encounter. The nothing can get in our way so the
606 * mount point on the vp must be valid.
608 TAILQ_REMOVE(&mp->mnt_nvnodelist, pvp, v_nmntvnodes);
609 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, pvp, v_nmntvnodes);
610 if (vp->v_flag & VPLACEMARKER) /* another procs placemarker */
612 if (vp->v_type == VNON) /* visible but not ready */
614 KKASSERT(vp->v_mount == mp);
617 * Quick test. A negative return continues the loop without
618 * calling the slow test. 0 continues onto the slow test.
619 * A positive number aborts the loop.
622 if ((r = fastfunc(mp, vp, data)) < 0)
629 * Get a vxlock on the vnode, retry if it has moved or isn't
630 * in the mountlist where we expect it.
637 error = vget(vp, LK_EXCLUSIVE, curthread);
639 case VMSC_GETVP|VMSC_NOWAIT:
640 error = vget(vp, LK_EXCLUSIVE|LK_NOWAIT,
655 if (TAILQ_PREV(pvp, vnodelst, v_nmntvnodes) != vp)
657 if (vp->v_type == VNON)
659 r = slowfunc(mp, vp, data);
663 case VMSC_GETVP|VMSC_NOWAIT:
679 TAILQ_REMOVE(&mp->mnt_nvnodelist, pvp, v_nmntvnodes);
680 freevnode_placemarker(pvp);
681 lwkt_reltoken(&ilock);
686 * Remove any vnodes in the vnode table belonging to mount point mp.
688 * If FORCECLOSE is not specified, there should not be any active ones,
689 * return error if any are found (nb: this is a user error, not a
690 * system error). If FORCECLOSE is specified, detach any active vnodes
693 * If WRITECLOSE is set, only flush out regular file vnodes open for
696 * SKIPSYSTEM causes any vnodes marked VSYSTEM to be skipped.
698 * `rootrefs' specifies the base reference count for the root vnode
699 * of this filesystem. The root vnode is considered busy if its
700 * v_usecount exceeds this value. On a successful return, vflush()
701 * will call vrele() on the root vnode exactly rootrefs times.
702 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
706 static int busyprt = 0; /* print out busy vnodes */
707 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "");
710 static int vflush_scan(struct mount *mp, struct vnode *vp, void *data);
719 vflush(struct mount *mp, int rootrefs, int flags)
721 struct thread *td = curthread; /* XXX */
722 struct vnode *rootvp = NULL;
724 struct vflush_info vflush_info;
727 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
728 ("vflush: bad args"));
730 * Get the filesystem root vnode. We can vput() it
731 * immediately, since with rootrefs > 0, it won't go away.
733 if ((error = VFS_ROOT(mp, &rootvp)) != 0)
738 vflush_info.busy = 0;
739 vflush_info.flags = flags;
741 vmntvnodescan(mp, VMSC_GETVX, NULL, vflush_scan, &vflush_info);
743 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
745 * If just the root vnode is busy, and if its refcount
746 * is equal to `rootrefs', then go ahead and kill it.
748 KASSERT(vflush_info.busy > 0, ("vflush: not busy"));
749 KASSERT(rootvp->v_usecount >= rootrefs, ("vflush: rootrefs"));
750 if (vflush_info.busy == 1 && rootvp->v_usecount == rootrefs) {
751 if (vx_lock(rootvp) == 0) {
754 vflush_info.busy = 0;
758 if (vflush_info.busy)
760 for (; rootrefs > 0; rootrefs--)
766 * The scan callback is made with an VX locked vnode.
769 vflush_scan(struct mount *mp, struct vnode *vp, void *data)
771 struct vflush_info *info = data;
775 * Skip over a vnodes marked VSYSTEM.
777 if ((info->flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM)) {
782 * If WRITECLOSE is set, flush out unlinked but still open
783 * files (even if open only for reading) and regular file
784 * vnodes open for writing.
786 if ((info->flags & WRITECLOSE) &&
787 (vp->v_type == VNON ||
788 (VOP_GETATTR(vp, &vattr, info->td) == 0 &&
789 vattr.va_nlink > 0)) &&
790 (vp->v_writecount == 0 || vp->v_type != VREG)) {
795 * With v_usecount == 0, all we need to do is clear out the
796 * vnode data structures and we are done.
798 if (vp->v_usecount == 1) {
804 * If FORCECLOSE is set, forcibly close the vnode. For block
805 * or character devices, revert to an anonymous device. For
806 * all other files, just kill them.
808 if (info->flags & FORCECLOSE) {
809 if (vp->v_type != VBLK && vp->v_type != VCHR) {
812 vclean(vp, 0, info->td);
813 vp->v_ops = &spec_vnode_vops;
820 vprint("vflush: busy vnode", vp);