2014-02-27 20:51:37 +04:00
|
|
|
/* $NetBSD: vfs_vnode.c,v 1.32 2014/02/27 16:51:38 hannken Exp $ */
|
2011-04-02 08:28:56 +04:00
|
|
|
|
|
|
|
/*-
|
2011-04-02 08:45:24 +04:00
|
|
|
* Copyright (c) 1997-2011 The NetBSD Foundation, Inc.
|
2011-04-02 08:28:56 +04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
|
|
|
|
* NASA Ames Research Center, by Charles M. Hannum, and by Andrew Doran.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1989, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
* (c) UNIX System Laboratories, Inc.
|
|
|
|
* All or some portions of this file are derived from material licensed
|
|
|
|
* to the University of California by American Telephone and Telegraph
|
|
|
|
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
|
|
|
|
* the permission of UNIX System Laboratories, Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* @(#)vfs_subr.c 8.13 (Berkeley) 4/18/94
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2011-05-19 07:26:06 +04:00
|
|
|
* The vnode cache subsystem.
|
|
|
|
*
|
|
|
|
* Life-cycle
|
|
|
|
*
|
|
|
|
* Normally, there are two points where new vnodes are created:
|
|
|
|
* VOP_CREATE(9) and VOP_LOOKUP(9). The life-cycle of a vnode
|
|
|
|
* starts in one of the following ways:
|
|
|
|
*
|
|
|
|
* - Allocation, via getnewvnode(9) and/or vnalloc(9).
|
|
|
|
* - Reclamation of inactive vnode, via vget(9).
|
|
|
|
*
|
2012-10-13 01:10:55 +04:00
|
|
|
* Recycle from a free list, via getnewvnode(9) -> getcleanvnode(9)
|
|
|
|
* was another, traditional way. Currently, only the draining thread
|
|
|
|
* recycles the vnodes. This behaviour might be revisited.
|
|
|
|
*
|
2011-05-19 07:26:06 +04:00
|
|
|
* The life-cycle ends when the last reference is dropped, usually
|
|
|
|
* in VOP_REMOVE(9). In such case, VOP_INACTIVE(9) is called to inform
|
|
|
|
* the file system that vnode is inactive. Via this call, file system
|
2012-10-13 01:10:55 +04:00
|
|
|
* indicates whether vnode can be recycled (usually, it checks its own
|
|
|
|
* references, e.g. count of links, whether the file was removed).
|
2011-05-19 07:26:06 +04:00
|
|
|
*
|
|
|
|
* Depending on indication, vnode can be put into a free list (cache),
|
|
|
|
* or cleaned via vclean(9), which calls VOP_RECLAIM(9) to disassociate
|
|
|
|
* underlying file system from the vnode, and finally destroyed.
|
|
|
|
*
|
|
|
|
* Reference counting
|
|
|
|
*
|
|
|
|
* Vnode is considered active, if reference count (vnode_t::v_usecount)
|
|
|
|
* is non-zero. It is maintained using: vref(9) and vrele(9), as well
|
|
|
|
* as vput(9), routines. Common points holding references are e.g.
|
|
|
|
* file openings, current working directory, mount points, etc.
|
|
|
|
*
|
|
|
|
* Note on v_usecount and its locking
|
|
|
|
*
|
|
|
|
* At nearly all points it is known that v_usecount could be zero,
|
|
|
|
* the vnode_t::v_interlock will be held. To change v_usecount away
|
|
|
|
* from zero, the interlock must be held. To change from a non-zero
|
|
|
|
* value to zero, again the interlock must be held.
|
|
|
|
*
|
2013-11-03 12:33:00 +04:00
|
|
|
* Changing the usecount from a non-zero value to a non-zero value can
|
|
|
|
* safely be done using atomic operations, without the interlock held.
|
2011-05-19 07:26:06 +04:00
|
|
|
*
|
|
|
|
* Note: if VI_CLEAN is set, vnode_t::v_interlock will be released while
|
|
|
|
* mntvnode_lock is still held.
|
2013-09-21 23:51:33 +04:00
|
|
|
*
|
|
|
|
* See PR 41374.
|
2011-04-02 08:28:56 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2014-02-27 20:51:37 +04:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: vfs_vnode.c,v 1.32 2014/02/27 16:51:38 hannken Exp $");
|
2013-10-29 13:53:51 +04:00
|
|
|
|
|
|
|
#define _VFS_VNODE_PRIVATE
|
2011-04-02 08:28:56 +04:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
|
|
|
|
#include <sys/atomic.h>
|
|
|
|
#include <sys/buf.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/device.h>
|
|
|
|
#include <sys/kauth.h>
|
|
|
|
#include <sys/kmem.h>
|
|
|
|
#include <sys/kthread.h>
|
|
|
|
#include <sys/module.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/namei.h>
|
|
|
|
#include <sys/syscallargs.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/vnode.h>
|
|
|
|
#include <sys/wapbl.h>
|
2013-11-03 12:33:00 +04:00
|
|
|
#include <sys/fstrans.h>
|
2011-04-02 08:28:56 +04:00
|
|
|
|
|
|
|
#include <uvm/uvm.h>
|
|
|
|
#include <uvm/uvm_readahead.h>
|
|
|
|
|
2013-10-29 13:53:51 +04:00
|
|
|
/* Flags to vrelel. */
|
|
|
|
#define VRELEL_ASYNC_RELE 0x0001 /* Always defer to vrele thread. */
|
2013-12-01 21:29:40 +04:00
|
|
|
#define VRELEL_CHANGING_SET 0x0002 /* VI_CHANGING set by caller. */
|
2013-10-29 13:53:51 +04:00
|
|
|
|
2011-05-14 02:16:43 +04:00
|
|
|
u_int numvnodes __cacheline_aligned;
|
2011-04-02 08:28:56 +04:00
|
|
|
|
2011-05-14 02:16:43 +04:00
|
|
|
static pool_cache_t vnode_cache __read_mostly;
|
2014-02-27 17:00:06 +04:00
|
|
|
static struct mount *dead_mount;
|
2011-04-02 08:28:56 +04:00
|
|
|
|
2012-10-13 01:10:55 +04:00
|
|
|
/*
|
|
|
|
* There are two free lists: one is for vnodes which have no buffer/page
|
|
|
|
* references and one for those which do (i.e. v_holdcnt is non-zero).
|
|
|
|
* Vnode recycling mechanism first attempts to look into the former list.
|
|
|
|
*/
|
|
|
|
static kmutex_t vnode_free_list_lock __cacheline_aligned;
|
2011-05-14 02:16:43 +04:00
|
|
|
static vnodelst_t vnode_free_list __cacheline_aligned;
|
|
|
|
static vnodelst_t vnode_hold_list __cacheline_aligned;
|
2012-10-13 01:10:55 +04:00
|
|
|
static kcondvar_t vdrain_cv __cacheline_aligned;
|
2011-04-02 08:28:56 +04:00
|
|
|
|
2012-10-13 01:10:55 +04:00
|
|
|
static vnodelst_t vrele_list __cacheline_aligned;
|
2011-05-14 02:16:43 +04:00
|
|
|
static kmutex_t vrele_lock __cacheline_aligned;
|
|
|
|
static kcondvar_t vrele_cv __cacheline_aligned;
|
|
|
|
static lwp_t * vrele_lwp __cacheline_aligned;
|
|
|
|
static int vrele_pending __cacheline_aligned;
|
|
|
|
static int vrele_gen __cacheline_aligned;
|
2011-04-02 08:28:56 +04:00
|
|
|
|
2011-10-02 17:00:06 +04:00
|
|
|
static int cleanvnode(void);
|
2013-11-07 13:48:34 +04:00
|
|
|
static void vclean(vnode_t *);
|
2013-10-29 13:53:51 +04:00
|
|
|
static void vrelel(vnode_t *, int);
|
2011-10-02 17:00:06 +04:00
|
|
|
static void vdrain_thread(void *);
|
2011-04-02 08:28:56 +04:00
|
|
|
static void vrele_thread(void *);
|
2011-09-30 00:51:38 +04:00
|
|
|
static void vnpanic(vnode_t *, const char *, ...)
|
2013-02-09 04:31:21 +04:00
|
|
|
__printflike(2, 3);
|
2011-04-02 08:28:56 +04:00
|
|
|
|
|
|
|
/* Routines having to do with the management of the vnode table. */
|
|
|
|
extern int (**dead_vnodeop_p)(void *);
|
2014-02-27 17:00:06 +04:00
|
|
|
extern struct vfsops dead_vfsops;
|
2011-04-02 08:28:56 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
vfs_vnode_sysinit(void)
|
|
|
|
{
|
2013-10-26 00:37:17 +04:00
|
|
|
int error __diagused;
|
2011-04-02 08:28:56 +04:00
|
|
|
|
|
|
|
vnode_cache = pool_cache_init(sizeof(vnode_t), 0, 0, 0, "vnodepl",
|
|
|
|
NULL, IPL_NONE, NULL, NULL, NULL);
|
|
|
|
KASSERT(vnode_cache != NULL);
|
|
|
|
|
2014-02-27 17:00:06 +04:00
|
|
|
dead_mount = vfs_mountalloc(&dead_vfsops, NULL);
|
|
|
|
KASSERT(dead_mount != NULL);
|
|
|
|
dead_mount->mnt_iflag = IMNT_MPSAFE;
|
|
|
|
|
2011-04-02 08:28:56 +04:00
|
|
|
mutex_init(&vnode_free_list_lock, MUTEX_DEFAULT, IPL_NONE);
|
|
|
|
TAILQ_INIT(&vnode_free_list);
|
|
|
|
TAILQ_INIT(&vnode_hold_list);
|
|
|
|
TAILQ_INIT(&vrele_list);
|
|
|
|
|
|
|
|
mutex_init(&vrele_lock, MUTEX_DEFAULT, IPL_NONE);
|
2011-10-02 17:00:06 +04:00
|
|
|
cv_init(&vdrain_cv, "vdrain");
|
2011-04-02 08:28:56 +04:00
|
|
|
cv_init(&vrele_cv, "vrele");
|
2011-10-02 17:00:06 +04:00
|
|
|
error = kthread_create(PRI_VM, KTHREAD_MPSAFE, NULL, vdrain_thread,
|
|
|
|
NULL, NULL, "vdrain");
|
|
|
|
KASSERT(error == 0);
|
2011-04-02 08:28:56 +04:00
|
|
|
error = kthread_create(PRI_VM, KTHREAD_MPSAFE, NULL, vrele_thread,
|
|
|
|
NULL, &vrele_lwp, "vrele");
|
|
|
|
KASSERT(error == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate a new, uninitialized vnode. If 'mp' is non-NULL, this is a
|
2011-10-03 14:30:13 +04:00
|
|
|
* marker vnode.
|
2011-04-02 08:28:56 +04:00
|
|
|
*/
|
|
|
|
vnode_t *
|
|
|
|
vnalloc(struct mount *mp)
|
|
|
|
{
|
|
|
|
vnode_t *vp;
|
|
|
|
|
2011-10-03 14:30:13 +04:00
|
|
|
vp = pool_cache_get(vnode_cache, PR_WAITOK);
|
|
|
|
KASSERT(vp != NULL);
|
2011-04-02 08:28:56 +04:00
|
|
|
|
|
|
|
memset(vp, 0, sizeof(*vp));
|
2011-06-12 07:35:36 +04:00
|
|
|
uvm_obj_init(&vp->v_uobj, &uvm_vnodeops, true, 0);
|
2011-04-02 08:28:56 +04:00
|
|
|
cv_init(&vp->v_cv, "vnode");
|
|
|
|
/*
|
|
|
|
* Done by memset() above.
|
|
|
|
* LIST_INIT(&vp->v_nclist);
|
|
|
|
* LIST_INIT(&vp->v_dnclist);
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (mp != NULL) {
|
|
|
|
vp->v_mount = mp;
|
|
|
|
vp->v_type = VBAD;
|
|
|
|
vp->v_iflag = VI_MARKER;
|
|
|
|
} else {
|
|
|
|
rw_init(&vp->v_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
return vp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free an unused, unreferenced vnode.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
vnfree(vnode_t *vp)
|
|
|
|
{
|
|
|
|
|
|
|
|
KASSERT(vp->v_usecount == 0);
|
|
|
|
|
|
|
|
if ((vp->v_iflag & VI_MARKER) == 0) {
|
|
|
|
rw_destroy(&vp->v_lock);
|
|
|
|
mutex_enter(&vnode_free_list_lock);
|
|
|
|
numvnodes--;
|
|
|
|
mutex_exit(&vnode_free_list_lock);
|
|
|
|
}
|
|
|
|
|
2011-06-12 07:35:36 +04:00
|
|
|
/*
|
|
|
|
* Note: the vnode interlock will either be freed, of reference
|
|
|
|
* dropped (if VI_LOCKSHARE was in use).
|
|
|
|
*/
|
|
|
|
uvm_obj_destroy(&vp->v_uobj, true);
|
2011-04-02 08:28:56 +04:00
|
|
|
cv_destroy(&vp->v_cv);
|
|
|
|
pool_cache_put(vnode_cache, vp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-10-02 17:00:06 +04:00
|
|
|
* cleanvnode: grab a vnode from freelist, clean and free it.
|
2011-04-04 06:46:57 +04:00
|
|
|
*
|
|
|
|
* => Releases vnode_free_list_lock.
|
2011-04-02 08:28:56 +04:00
|
|
|
*/
|
2011-10-02 17:00:06 +04:00
|
|
|
static int
|
|
|
|
cleanvnode(void)
|
2011-04-02 08:28:56 +04:00
|
|
|
{
|
|
|
|
vnode_t *vp;
|
|
|
|
vnodelst_t *listhd;
|
2013-11-03 12:33:00 +04:00
|
|
|
struct mount *mp;
|
2011-04-02 08:28:56 +04:00
|
|
|
|
|
|
|
KASSERT(mutex_owned(&vnode_free_list_lock));
|
2013-11-03 12:33:00 +04:00
|
|
|
|
2011-04-02 08:28:56 +04:00
|
|
|
listhd = &vnode_free_list;
|
|
|
|
try_nextlist:
|
|
|
|
TAILQ_FOREACH(vp, listhd, v_freelist) {
|
|
|
|
/*
|
|
|
|
* It's safe to test v_usecount and v_iflag
|
|
|
|
* without holding the interlock here, since
|
|
|
|
* these vnodes should never appear on the
|
|
|
|
* lists.
|
|
|
|
*/
|
2011-04-04 06:46:57 +04:00
|
|
|
KASSERT(vp->v_usecount == 0);
|
|
|
|
KASSERT((vp->v_iflag & VI_CLEAN) == 0);
|
|
|
|
KASSERT(vp->v_freelisthd == listhd);
|
|
|
|
|
2011-06-12 07:35:36 +04:00
|
|
|
if (!mutex_tryenter(vp->v_interlock))
|
2011-04-02 08:28:56 +04:00
|
|
|
continue;
|
2013-11-03 12:33:00 +04:00
|
|
|
if ((vp->v_iflag & VI_XLOCK) != 0) {
|
|
|
|
mutex_exit(vp->v_interlock);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
mp = vp->v_mount;
|
|
|
|
if (fstrans_start_nowait(mp, FSTRANS_SHARED) != 0) {
|
|
|
|
mutex_exit(vp->v_interlock);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
2011-04-02 08:28:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (vp == NULL) {
|
|
|
|
if (listhd == &vnode_free_list) {
|
|
|
|
listhd = &vnode_hold_list;
|
|
|
|
goto try_nextlist;
|
|
|
|
}
|
|
|
|
mutex_exit(&vnode_free_list_lock);
|
2011-10-02 17:00:06 +04:00
|
|
|
return EBUSY;
|
2011-04-02 08:28:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove it from the freelist. */
|
|
|
|
TAILQ_REMOVE(listhd, vp, v_freelist);
|
|
|
|
vp->v_freelisthd = NULL;
|
|
|
|
mutex_exit(&vnode_free_list_lock);
|
|
|
|
|
|
|
|
KASSERT(vp->v_usecount == 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The vnode is still associated with a file system, so we must
|
2011-10-02 17:00:06 +04:00
|
|
|
* clean it out before freeing it. We need to add a reference
|
2013-11-03 12:33:00 +04:00
|
|
|
* before doing this.
|
2011-04-02 08:28:56 +04:00
|
|
|
*/
|
2013-11-03 12:33:00 +04:00
|
|
|
vp->v_usecount = 1;
|
2013-12-01 21:29:40 +04:00
|
|
|
KASSERT((vp->v_iflag & VI_CHANGING) == 0);
|
|
|
|
vp->v_iflag |= VI_CHANGING;
|
2013-11-07 13:48:34 +04:00
|
|
|
vclean(vp);
|
2013-12-01 21:29:40 +04:00
|
|
|
vrelel(vp, VRELEL_CHANGING_SET);
|
2013-11-03 12:33:00 +04:00
|
|
|
fstrans_done(mp);
|
2011-10-02 17:00:06 +04:00
|
|
|
|
|
|
|
return 0;
|
2011-04-02 08:28:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-10-02 17:00:06 +04:00
|
|
|
* getnewvnode: return a fresh vnode.
|
2011-04-04 06:46:57 +04:00
|
|
|
*
|
|
|
|
* => Returns referenced vnode, moved into the mount queue.
|
2011-06-12 07:35:36 +04:00
|
|
|
* => Shares the interlock specified by 'slock', if it is not NULL.
|
2011-04-02 08:28:56 +04:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
getnewvnode(enum vtagtype tag, struct mount *mp, int (**vops)(void *),
|
2011-06-12 07:35:36 +04:00
|
|
|
kmutex_t *slock, vnode_t **vpp)
|
2011-04-02 08:28:56 +04:00
|
|
|
{
|
2013-10-26 00:37:17 +04:00
|
|
|
struct uvm_object *uobj __diagused;
|
2011-04-02 08:28:56 +04:00
|
|
|
vnode_t *vp;
|
2011-10-02 17:00:06 +04:00
|
|
|
int error = 0;
|
2011-04-02 08:28:56 +04:00
|
|
|
|
|
|
|
if (mp != NULL) {
|
|
|
|
/*
|
2011-04-02 11:33:49 +04:00
|
|
|
* Mark filesystem busy while we are creating a vnode.
|
|
|
|
* If unmount is in progress, this will fail.
|
2011-04-02 08:28:56 +04:00
|
|
|
*/
|
|
|
|
error = vfs_busy(mp, NULL);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
vp = NULL;
|
|
|
|
|
2011-10-02 17:00:06 +04:00
|
|
|
/* Allocate a new vnode. */
|
2011-04-02 08:28:56 +04:00
|
|
|
mutex_enter(&vnode_free_list_lock);
|
2011-10-02 17:00:06 +04:00
|
|
|
numvnodes++;
|
|
|
|
if (numvnodes > desiredvnodes + desiredvnodes / 10)
|
|
|
|
cv_signal(&vdrain_cv);
|
|
|
|
mutex_exit(&vnode_free_list_lock);
|
2011-10-07 13:35:04 +04:00
|
|
|
vp = vnalloc(NULL);
|
2011-10-02 17:00:06 +04:00
|
|
|
|
2011-04-02 08:28:56 +04:00
|
|
|
KASSERT(vp->v_freelisthd == NULL);
|
|
|
|
KASSERT(LIST_EMPTY(&vp->v_nclist));
|
|
|
|
KASSERT(LIST_EMPTY(&vp->v_dnclist));
|
|
|
|
|
2011-04-04 06:46:57 +04:00
|
|
|
/* Initialize vnode. */
|
2011-10-07 13:35:04 +04:00
|
|
|
vp->v_usecount = 1;
|
2011-04-02 08:28:56 +04:00
|
|
|
vp->v_type = VNON;
|
|
|
|
vp->v_tag = tag;
|
|
|
|
vp->v_op = vops;
|
|
|
|
vp->v_data = NULL;
|
|
|
|
|
|
|
|
uobj = &vp->v_uobj;
|
|
|
|
KASSERT(uobj->pgops == &uvm_vnodeops);
|
|
|
|
KASSERT(uobj->uo_npages == 0);
|
|
|
|
KASSERT(TAILQ_FIRST(&uobj->memq) == NULL);
|
|
|
|
vp->v_size = vp->v_writesize = VSIZENOTSET;
|
|
|
|
|
2011-06-12 07:35:36 +04:00
|
|
|
/* Share the vnode_t::v_interlock, if requested. */
|
|
|
|
if (slock) {
|
|
|
|
/* Set the interlock and mark that it is shared. */
|
|
|
|
KASSERT(vp->v_mount == NULL);
|
|
|
|
mutex_obj_hold(slock);
|
|
|
|
uvm_obj_setlock(&vp->v_uobj, slock);
|
|
|
|
KASSERT(vp->v_interlock == slock);
|
|
|
|
vp->v_iflag |= VI_LOCKSHARE;
|
|
|
|
}
|
|
|
|
|
2011-04-04 06:46:57 +04:00
|
|
|
/* Finally, move vnode into the mount queue. */
|
|
|
|
vfs_insmntque(vp, mp);
|
|
|
|
|
2011-04-02 08:28:56 +04:00
|
|
|
if (mp != NULL) {
|
|
|
|
if ((mp->mnt_iflag & IMNT_MPSAFE) != 0)
|
|
|
|
vp->v_vflag |= VV_MPSAFE;
|
|
|
|
vfs_unbusy(mp, true, NULL);
|
|
|
|
}
|
|
|
|
|
2011-04-04 06:46:57 +04:00
|
|
|
*vpp = vp;
|
2011-04-02 11:33:49 +04:00
|
|
|
return 0;
|
2011-04-02 08:28:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is really just the reverse of getnewvnode(). Needed for
|
|
|
|
* VFS_VGET functions who may need to push back a vnode in case
|
|
|
|
* of a locking race.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ungetnewvnode(vnode_t *vp)
|
|
|
|
{
|
|
|
|
|
|
|
|
KASSERT(vp->v_usecount == 1);
|
|
|
|
KASSERT(vp->v_data == NULL);
|
|
|
|
KASSERT(vp->v_freelisthd == NULL);
|
|
|
|
|
2011-06-12 07:35:36 +04:00
|
|
|
mutex_enter(vp->v_interlock);
|
2011-04-02 08:28:56 +04:00
|
|
|
vp->v_iflag |= VI_CLEAN;
|
|
|
|
vrelel(vp, 0);
|
|
|
|
}
|
|
|
|
|
2011-10-02 17:00:06 +04:00
|
|
|
/*
|
|
|
|
* Helper thread to keep the number of vnodes below desiredvnodes.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
vdrain_thread(void *cookie)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
mutex_enter(&vnode_free_list_lock);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
cv_timedwait(&vdrain_cv, &vnode_free_list_lock, hz);
|
|
|
|
while (numvnodes > desiredvnodes) {
|
|
|
|
error = cleanvnode();
|
|
|
|
if (error)
|
|
|
|
kpause("vndsbusy", false, hz, NULL);
|
|
|
|
mutex_enter(&vnode_free_list_lock);
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-02 08:28:56 +04:00
|
|
|
/*
|
|
|
|
* Remove a vnode from its freelist.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
vremfree(vnode_t *vp)
|
|
|
|
{
|
|
|
|
|
2011-06-12 07:35:36 +04:00
|
|
|
KASSERT(mutex_owned(vp->v_interlock));
|
2011-04-02 08:28:56 +04:00
|
|
|
KASSERT(vp->v_usecount == 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note that the reference count must not change until
|
|
|
|
* the vnode is removed.
|
|
|
|
*/
|
|
|
|
mutex_enter(&vnode_free_list_lock);
|
|
|
|
if (vp->v_holdcnt > 0) {
|
|
|
|
KASSERT(vp->v_freelisthd == &vnode_hold_list);
|
|
|
|
} else {
|
|
|
|
KASSERT(vp->v_freelisthd == &vnode_free_list);
|
|
|
|
}
|
|
|
|
TAILQ_REMOVE(vp->v_freelisthd, vp, v_freelist);
|
|
|
|
vp->v_freelisthd = NULL;
|
|
|
|
mutex_exit(&vnode_free_list_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-04-02 11:33:49 +04:00
|
|
|
* vget: get a particular vnode from the free list, increment its reference
|
|
|
|
* count and lock it.
|
|
|
|
*
|
|
|
|
* => Should be called with v_interlock held.
|
|
|
|
*
|
2013-12-01 21:29:40 +04:00
|
|
|
* If VI_CHANGING is set, the vnode may be eliminated in vgone()/vclean().
|
2011-04-02 11:33:49 +04:00
|
|
|
* In that case, we cannot grab the vnode, so the process is awakened when
|
|
|
|
* the transition is completed, and an error returned to indicate that the
|
2013-12-01 21:29:40 +04:00
|
|
|
* vnode is no longer usable.
|
2011-04-02 08:28:56 +04:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
vget(vnode_t *vp, int flags)
|
|
|
|
{
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
KASSERT((vp->v_iflag & VI_MARKER) == 0);
|
2011-06-12 07:35:36 +04:00
|
|
|
KASSERT(mutex_owned(vp->v_interlock));
|
2011-04-02 08:28:56 +04:00
|
|
|
KASSERT((flags & ~(LK_SHARED|LK_EXCLUSIVE|LK_NOWAIT)) == 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Before adding a reference, we must remove the vnode
|
|
|
|
* from its freelist.
|
|
|
|
*/
|
|
|
|
if (vp->v_usecount == 0) {
|
|
|
|
vremfree(vp);
|
|
|
|
vp->v_usecount = 1;
|
|
|
|
} else {
|
|
|
|
atomic_inc_uint(&vp->v_usecount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-12-01 21:29:40 +04:00
|
|
|
* If the vnode is in the process of changing state we wait
|
|
|
|
* for the change to complete and take care not to return
|
|
|
|
* a clean vnode.
|
2011-04-02 08:28:56 +04:00
|
|
|
*/
|
2013-12-01 21:29:40 +04:00
|
|
|
if ((vp->v_iflag & VI_CHANGING) != 0) {
|
2011-04-02 08:28:56 +04:00
|
|
|
if ((flags & LK_NOWAIT) != 0) {
|
|
|
|
vrelel(vp, 0);
|
|
|
|
return EBUSY;
|
|
|
|
}
|
2013-12-01 21:29:40 +04:00
|
|
|
vwait(vp, VI_CHANGING);
|
2012-11-12 15:00:07 +04:00
|
|
|
if ((vp->v_iflag & VI_CLEAN) != 0) {
|
|
|
|
vrelel(vp, 0);
|
|
|
|
return ENOENT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-02 08:28:56 +04:00
|
|
|
/*
|
|
|
|
* Ok, we got it in good shape. Just locking left.
|
|
|
|
*/
|
|
|
|
KASSERT((vp->v_iflag & VI_CLEAN) == 0);
|
2011-06-12 07:35:36 +04:00
|
|
|
mutex_exit(vp->v_interlock);
|
2011-04-02 08:28:56 +04:00
|
|
|
if (flags & (LK_EXCLUSIVE | LK_SHARED)) {
|
|
|
|
error = vn_lock(vp, flags);
|
|
|
|
if (error != 0) {
|
|
|
|
vrele(vp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-04-02 11:33:49 +04:00
|
|
|
* vput: unlock and release the reference.
|
2011-04-02 08:28:56 +04:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
vput(vnode_t *vp)
|
|
|
|
{
|
|
|
|
|
|
|
|
KASSERT((vp->v_iflag & VI_MARKER) == 0);
|
|
|
|
|
|
|
|
VOP_UNLOCK(vp);
|
|
|
|
vrele(vp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to drop reference on a vnode. Abort if we are releasing the
|
|
|
|
* last reference. Note: this _must_ succeed if not the last reference.
|
|
|
|
*/
|
|
|
|
static inline bool
|
|
|
|
vtryrele(vnode_t *vp)
|
|
|
|
{
|
|
|
|
u_int use, next;
|
|
|
|
|
|
|
|
for (use = vp->v_usecount;; use = next) {
|
|
|
|
if (use == 1) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-03 12:33:00 +04:00
|
|
|
KASSERT(use > 1);
|
2011-04-02 08:28:56 +04:00
|
|
|
next = atomic_cas_uint(&vp->v_usecount, use, use - 1);
|
|
|
|
if (__predict_true(next == use)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Vnode release. If reference count drops to zero, call inactive
|
|
|
|
* routine and either return to freelist or free to the pool.
|
|
|
|
*/
|
2013-10-29 13:53:51 +04:00
|
|
|
static void
|
2011-04-02 08:28:56 +04:00
|
|
|
vrelel(vnode_t *vp, int flags)
|
|
|
|
{
|
|
|
|
bool recycle, defer;
|
|
|
|
int error;
|
|
|
|
|
2011-06-12 07:35:36 +04:00
|
|
|
KASSERT(mutex_owned(vp->v_interlock));
|
2011-04-02 08:28:56 +04:00
|
|
|
KASSERT((vp->v_iflag & VI_MARKER) == 0);
|
|
|
|
KASSERT(vp->v_freelisthd == NULL);
|
|
|
|
|
|
|
|
if (__predict_false(vp->v_op == dead_vnodeop_p &&
|
|
|
|
(vp->v_iflag & (VI_CLEAN|VI_XLOCK)) == 0)) {
|
2011-09-30 00:51:38 +04:00
|
|
|
vnpanic(vp, "dead but not clean");
|
2011-04-02 08:28:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If not the last reference, just drop the reference count
|
|
|
|
* and unlock.
|
|
|
|
*/
|
|
|
|
if (vtryrele(vp)) {
|
2013-12-01 21:29:40 +04:00
|
|
|
if ((flags & VRELEL_CHANGING_SET) != 0) {
|
|
|
|
KASSERT((vp->v_iflag & VI_CHANGING) != 0);
|
|
|
|
vp->v_iflag &= ~VI_CHANGING;
|
|
|
|
cv_broadcast(&vp->v_cv);
|
|
|
|
}
|
2011-06-12 07:35:36 +04:00
|
|
|
mutex_exit(vp->v_interlock);
|
2011-04-02 08:28:56 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (vp->v_usecount <= 0 || vp->v_writecount != 0) {
|
2011-09-30 00:51:38 +04:00
|
|
|
vnpanic(vp, "%s: bad ref count", __func__);
|
2011-04-02 08:28:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
KASSERT((vp->v_iflag & VI_XLOCK) == 0);
|
|
|
|
|
2011-12-20 20:49:37 +04:00
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
if ((vp->v_type == VBLK || vp->v_type == VCHR) &&
|
|
|
|
vp->v_specnode != NULL && vp->v_specnode->sn_opencnt != 0) {
|
|
|
|
vprint("vrelel: missing VOP_CLOSE()", vp);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-04-02 08:28:56 +04:00
|
|
|
/*
|
|
|
|
* If not clean, deactivate the vnode, but preserve
|
|
|
|
* our reference across the call to VOP_INACTIVE().
|
|
|
|
*/
|
|
|
|
if ((vp->v_iflag & VI_CLEAN) == 0) {
|
|
|
|
recycle = false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX This ugly block can be largely eliminated if
|
|
|
|
* locking is pushed down into the file systems.
|
|
|
|
*
|
|
|
|
* Defer vnode release to vrele_thread if caller
|
2013-12-07 14:03:28 +04:00
|
|
|
* requests it explicitly or is the pagedaemon.
|
2011-04-02 08:28:56 +04:00
|
|
|
*/
|
|
|
|
if ((curlwp == uvm.pagedaemon_lwp) ||
|
|
|
|
(flags & VRELEL_ASYNC_RELE) != 0) {
|
|
|
|
defer = true;
|
|
|
|
} else if (curlwp == vrele_lwp) {
|
2012-11-12 15:00:07 +04:00
|
|
|
/*
|
2013-12-01 21:29:40 +04:00
|
|
|
* We have to try harder.
|
2012-11-12 15:00:07 +04:00
|
|
|
*/
|
2011-06-12 07:35:36 +04:00
|
|
|
mutex_exit(vp->v_interlock);
|
2014-02-27 20:51:37 +04:00
|
|
|
error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
|
2013-12-07 14:03:28 +04:00
|
|
|
KASSERT(error == 0);
|
2012-11-12 15:00:07 +04:00
|
|
|
mutex_enter(vp->v_interlock);
|
2011-04-02 08:28:56 +04:00
|
|
|
defer = false;
|
2011-04-02 11:33:49 +04:00
|
|
|
} else {
|
2011-04-02 08:28:56 +04:00
|
|
|
/* If we can't acquire the lock, then defer. */
|
2014-02-27 20:51:37 +04:00
|
|
|
mutex_exit(vp->v_interlock);
|
|
|
|
error = vn_lock(vp,
|
|
|
|
LK_EXCLUSIVE | LK_RETRY | LK_NOWAIT);
|
2013-12-07 14:03:28 +04:00
|
|
|
defer = (error != 0);
|
2014-02-27 20:51:37 +04:00
|
|
|
mutex_enter(vp->v_interlock);
|
2011-04-02 08:28:56 +04:00
|
|
|
}
|
|
|
|
|
2013-12-07 14:03:28 +04:00
|
|
|
KASSERT(mutex_owned(vp->v_interlock));
|
|
|
|
KASSERT(! (curlwp == vrele_lwp && defer));
|
|
|
|
|
2011-04-02 08:28:56 +04:00
|
|
|
if (defer) {
|
|
|
|
/*
|
|
|
|
* Defer reclaim to the kthread; it's not safe to
|
|
|
|
* clean it here. We donate it our last reference.
|
|
|
|
*/
|
2013-12-01 21:29:40 +04:00
|
|
|
if ((flags & VRELEL_CHANGING_SET) != 0) {
|
|
|
|
KASSERT((vp->v_iflag & VI_CHANGING) != 0);
|
|
|
|
vp->v_iflag &= ~VI_CHANGING;
|
|
|
|
cv_broadcast(&vp->v_cv);
|
|
|
|
}
|
2011-04-02 08:28:56 +04:00
|
|
|
mutex_enter(&vrele_lock);
|
|
|
|
TAILQ_INSERT_TAIL(&vrele_list, vp, v_freelist);
|
|
|
|
if (++vrele_pending > (desiredvnodes >> 8))
|
|
|
|
cv_signal(&vrele_cv);
|
|
|
|
mutex_exit(&vrele_lock);
|
2011-06-12 07:35:36 +04:00
|
|
|
mutex_exit(vp->v_interlock);
|
2011-04-02 08:28:56 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-27 20:51:37 +04:00
|
|
|
/*
|
|
|
|
* If the node got another reference while we
|
|
|
|
* released the interlock, don't try to inactivate it yet.
|
|
|
|
*/
|
|
|
|
if (__predict_false(vtryrele(vp))) {
|
|
|
|
VOP_UNLOCK(vp);
|
|
|
|
if ((flags & VRELEL_CHANGING_SET) != 0) {
|
|
|
|
KASSERT((vp->v_iflag & VI_CHANGING) != 0);
|
|
|
|
vp->v_iflag &= ~VI_CHANGING;
|
|
|
|
cv_broadcast(&vp->v_cv);
|
|
|
|
}
|
|
|
|
mutex_exit(vp->v_interlock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-01 21:29:40 +04:00
|
|
|
if ((flags & VRELEL_CHANGING_SET) == 0) {
|
|
|
|
KASSERT((vp->v_iflag & VI_CHANGING) == 0);
|
|
|
|
vp->v_iflag |= VI_CHANGING;
|
|
|
|
}
|
|
|
|
mutex_exit(vp->v_interlock);
|
|
|
|
|
2011-04-02 08:28:56 +04:00
|
|
|
/*
|
|
|
|
* The vnode can gain another reference while being
|
|
|
|
* deactivated. If VOP_INACTIVE() indicates that
|
|
|
|
* the described file has been deleted, then recycle
|
|
|
|
* the vnode irrespective of additional references.
|
|
|
|
* Another thread may be waiting to re-use the on-disk
|
|
|
|
* inode.
|
|
|
|
*
|
|
|
|
* Note that VOP_INACTIVE() will drop the vnode lock.
|
|
|
|
*/
|
|
|
|
VOP_INACTIVE(vp, &recycle);
|
2011-06-12 07:35:36 +04:00
|
|
|
mutex_enter(vp->v_interlock);
|
2011-04-02 08:28:56 +04:00
|
|
|
if (!recycle) {
|
|
|
|
if (vtryrele(vp)) {
|
2013-12-01 21:29:40 +04:00
|
|
|
KASSERT((vp->v_iflag & VI_CHANGING) != 0);
|
|
|
|
vp->v_iflag &= ~VI_CHANGING;
|
|
|
|
cv_broadcast(&vp->v_cv);
|
2011-06-12 07:35:36 +04:00
|
|
|
mutex_exit(vp->v_interlock);
|
2011-04-02 08:28:56 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Take care of space accounting. */
|
|
|
|
if (vp->v_iflag & VI_EXECMAP) {
|
|
|
|
atomic_add_int(&uvmexp.execpages,
|
|
|
|
-vp->v_uobj.uo_npages);
|
|
|
|
atomic_add_int(&uvmexp.filepages,
|
|
|
|
vp->v_uobj.uo_npages);
|
|
|
|
}
|
|
|
|
vp->v_iflag &= ~(VI_TEXT|VI_EXECMAP|VI_WRMAP);
|
|
|
|
vp->v_vflag &= ~VV_MAPPED;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recycle the vnode if the file is now unused (unlinked),
|
|
|
|
* otherwise just free it.
|
|
|
|
*/
|
|
|
|
if (recycle) {
|
2013-11-07 13:48:34 +04:00
|
|
|
vclean(vp);
|
2011-04-02 08:28:56 +04:00
|
|
|
}
|
|
|
|
KASSERT(vp->v_usecount > 0);
|
2013-12-01 21:29:40 +04:00
|
|
|
} else { /* vnode was already clean */
|
|
|
|
if ((flags & VRELEL_CHANGING_SET) == 0) {
|
|
|
|
KASSERT((vp->v_iflag & VI_CHANGING) == 0);
|
|
|
|
vp->v_iflag |= VI_CHANGING;
|
|
|
|
}
|
2011-04-02 08:28:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (atomic_dec_uint_nv(&vp->v_usecount) != 0) {
|
|
|
|
/* Gained another reference while being reclaimed. */
|
2013-12-01 21:29:40 +04:00
|
|
|
KASSERT((vp->v_iflag & VI_CHANGING) != 0);
|
|
|
|
vp->v_iflag &= ~VI_CHANGING;
|
|
|
|
cv_broadcast(&vp->v_cv);
|
2011-06-12 07:35:36 +04:00
|
|
|
mutex_exit(vp->v_interlock);
|
2011-04-02 08:28:56 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((vp->v_iflag & VI_CLEAN) != 0) {
|
|
|
|
/*
|
|
|
|
* It's clean so destroy it. It isn't referenced
|
|
|
|
* anywhere since it has been reclaimed.
|
|
|
|
*/
|
|
|
|
KASSERT(vp->v_holdcnt == 0);
|
|
|
|
KASSERT(vp->v_writecount == 0);
|
2011-06-12 07:35:36 +04:00
|
|
|
mutex_exit(vp->v_interlock);
|
2011-04-02 08:28:56 +04:00
|
|
|
vfs_insmntque(vp, NULL);
|
|
|
|
if (vp->v_type == VBLK || vp->v_type == VCHR) {
|
|
|
|
spec_node_destroy(vp);
|
|
|
|
}
|
|
|
|
vnfree(vp);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Otherwise, put it back onto the freelist. It
|
|
|
|
* can't be destroyed while still associated with
|
|
|
|
* a file system.
|
|
|
|
*/
|
|
|
|
mutex_enter(&vnode_free_list_lock);
|
|
|
|
if (vp->v_holdcnt > 0) {
|
|
|
|
vp->v_freelisthd = &vnode_hold_list;
|
|
|
|
} else {
|
|
|
|
vp->v_freelisthd = &vnode_free_list;
|
|
|
|
}
|
|
|
|
TAILQ_INSERT_TAIL(vp->v_freelisthd, vp, v_freelist);
|
|
|
|
mutex_exit(&vnode_free_list_lock);
|
2013-12-01 21:29:40 +04:00
|
|
|
KASSERT((vp->v_iflag & VI_CHANGING) != 0);
|
|
|
|
vp->v_iflag &= ~VI_CHANGING;
|
|
|
|
cv_broadcast(&vp->v_cv);
|
2011-06-12 07:35:36 +04:00
|
|
|
mutex_exit(vp->v_interlock);
|
2011-04-02 08:28:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
vrele(vnode_t *vp)
|
|
|
|
{
|
|
|
|
|
|
|
|
KASSERT((vp->v_iflag & VI_MARKER) == 0);
|
|
|
|
|
2013-12-01 21:29:40 +04:00
|
|
|
if (vtryrele(vp)) {
|
2011-04-02 08:28:56 +04:00
|
|
|
return;
|
|
|
|
}
|
2011-06-12 07:35:36 +04:00
|
|
|
mutex_enter(vp->v_interlock);
|
2011-04-02 08:28:56 +04:00
|
|
|
vrelel(vp, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Asynchronous vnode release, vnode is released in different context.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
vrele_async(vnode_t *vp)
|
|
|
|
{
|
|
|
|
|
|
|
|
KASSERT((vp->v_iflag & VI_MARKER) == 0);
|
|
|
|
|
2013-12-01 21:29:40 +04:00
|
|
|
if (vtryrele(vp)) {
|
2011-04-02 08:28:56 +04:00
|
|
|
return;
|
|
|
|
}
|
2011-06-12 07:35:36 +04:00
|
|
|
mutex_enter(vp->v_interlock);
|
2011-04-02 08:28:56 +04:00
|
|
|
vrelel(vp, VRELEL_ASYNC_RELE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
vrele_thread(void *cookie)
|
|
|
|
{
|
|
|
|
vnode_t *vp;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
mutex_enter(&vrele_lock);
|
|
|
|
while (TAILQ_EMPTY(&vrele_list)) {
|
|
|
|
vrele_gen++;
|
|
|
|
cv_broadcast(&vrele_cv);
|
|
|
|
cv_timedwait(&vrele_cv, &vrele_lock, hz);
|
|
|
|
}
|
|
|
|
vp = TAILQ_FIRST(&vrele_list);
|
|
|
|
TAILQ_REMOVE(&vrele_list, vp, v_freelist);
|
|
|
|
vrele_pending--;
|
|
|
|
mutex_exit(&vrele_lock);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If not the last reference, then ignore the vnode
|
|
|
|
* and look for more work.
|
|
|
|
*/
|
2011-06-12 07:35:36 +04:00
|
|
|
mutex_enter(vp->v_interlock);
|
2011-04-02 08:28:56 +04:00
|
|
|
vrelel(vp, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-02 08:45:24 +04:00
|
|
|
void
|
|
|
|
vrele_flush(void)
|
|
|
|
{
|
|
|
|
int gen;
|
|
|
|
|
|
|
|
mutex_enter(&vrele_lock);
|
|
|
|
gen = vrele_gen;
|
|
|
|
while (vrele_pending && gen == vrele_gen) {
|
|
|
|
cv_broadcast(&vrele_cv);
|
|
|
|
cv_wait(&vrele_cv, &vrele_lock);
|
|
|
|
}
|
|
|
|
mutex_exit(&vrele_lock);
|
|
|
|
}
|
|
|
|
|
2011-04-02 08:28:56 +04:00
|
|
|
/*
|
|
|
|
* Vnode reference, where a reference is already held by some other
|
|
|
|
* object (for example, a file structure).
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
vref(vnode_t *vp)
|
|
|
|
{
|
|
|
|
|
|
|
|
KASSERT((vp->v_iflag & VI_MARKER) == 0);
|
|
|
|
KASSERT(vp->v_usecount != 0);
|
|
|
|
|
|
|
|
atomic_inc_uint(&vp->v_usecount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Page or buffer structure gets a reference.
|
|
|
|
* Called with v_interlock held.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
vholdl(vnode_t *vp)
|
|
|
|
{
|
|
|
|
|
2011-06-12 07:35:36 +04:00
|
|
|
KASSERT(mutex_owned(vp->v_interlock));
|
2011-04-02 08:28:56 +04:00
|
|
|
KASSERT((vp->v_iflag & VI_MARKER) == 0);
|
|
|
|
|
|
|
|
if (vp->v_holdcnt++ == 0 && vp->v_usecount == 0) {
|
|
|
|
mutex_enter(&vnode_free_list_lock);
|
|
|
|
KASSERT(vp->v_freelisthd == &vnode_free_list);
|
|
|
|
TAILQ_REMOVE(vp->v_freelisthd, vp, v_freelist);
|
|
|
|
vp->v_freelisthd = &vnode_hold_list;
|
|
|
|
TAILQ_INSERT_TAIL(vp->v_freelisthd, vp, v_freelist);
|
|
|
|
mutex_exit(&vnode_free_list_lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Page or buffer structure frees a reference.
|
|
|
|
* Called with v_interlock held.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
holdrelel(vnode_t *vp)
|
|
|
|
{
|
|
|
|
|
2011-06-12 07:35:36 +04:00
|
|
|
KASSERT(mutex_owned(vp->v_interlock));
|
2011-04-02 08:28:56 +04:00
|
|
|
KASSERT((vp->v_iflag & VI_MARKER) == 0);
|
|
|
|
|
|
|
|
if (vp->v_holdcnt <= 0) {
|
2011-09-30 00:51:38 +04:00
|
|
|
vnpanic(vp, "%s: holdcnt vp %p", __func__, vp);
|
2011-04-02 08:28:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
vp->v_holdcnt--;
|
|
|
|
if (vp->v_holdcnt == 0 && vp->v_usecount == 0) {
|
|
|
|
mutex_enter(&vnode_free_list_lock);
|
|
|
|
KASSERT(vp->v_freelisthd == &vnode_hold_list);
|
|
|
|
TAILQ_REMOVE(vp->v_freelisthd, vp, v_freelist);
|
|
|
|
vp->v_freelisthd = &vnode_free_list;
|
|
|
|
TAILQ_INSERT_TAIL(vp->v_freelisthd, vp, v_freelist);
|
|
|
|
mutex_exit(&vnode_free_list_lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Disassociate the underlying file system from a vnode.
|
|
|
|
*
|
|
|
|
* Must be called with the interlock held, and will return with it held.
|
|
|
|
*/
|
2013-11-07 13:48:34 +04:00
|
|
|
static void
|
|
|
|
vclean(vnode_t *vp)
|
2011-04-02 08:28:56 +04:00
|
|
|
{
|
|
|
|
lwp_t *l = curlwp;
|
2013-11-07 13:48:34 +04:00
|
|
|
bool recycle, active, doclose;
|
2011-04-02 08:28:56 +04:00
|
|
|
int error;
|
|
|
|
|
2011-06-12 07:35:36 +04:00
|
|
|
KASSERT(mutex_owned(vp->v_interlock));
|
2011-04-02 08:28:56 +04:00
|
|
|
KASSERT((vp->v_iflag & VI_MARKER) == 0);
|
|
|
|
KASSERT(vp->v_usecount != 0);
|
|
|
|
|
|
|
|
/* If already clean, nothing to do. */
|
|
|
|
if ((vp->v_iflag & VI_CLEAN) != 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-27 20:51:37 +04:00
|
|
|
active = (vp->v_usecount > 1);
|
|
|
|
doclose = ! (active && vp->v_type == VBLK &&
|
|
|
|
spec_node_getmountedfs(vp) != NULL);
|
|
|
|
mutex_exit(vp->v_interlock);
|
|
|
|
|
|
|
|
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
|
|
|
|
|
2011-04-02 08:28:56 +04:00
|
|
|
/*
|
|
|
|
* Prevent the vnode from being recycled or brought into use
|
|
|
|
* while we clean it out.
|
|
|
|
*/
|
2014-02-27 20:51:37 +04:00
|
|
|
mutex_enter(vp->v_interlock);
|
|
|
|
KASSERT((vp->v_iflag & (VI_XLOCK | VI_CLEAN)) == 0);
|
2011-04-02 08:28:56 +04:00
|
|
|
vp->v_iflag |= VI_XLOCK;
|
|
|
|
if (vp->v_iflag & VI_EXECMAP) {
|
|
|
|
atomic_add_int(&uvmexp.execpages, -vp->v_uobj.uo_npages);
|
|
|
|
atomic_add_int(&uvmexp.filepages, vp->v_uobj.uo_npages);
|
|
|
|
}
|
|
|
|
vp->v_iflag &= ~(VI_TEXT|VI_EXECMAP);
|
2011-06-12 07:35:36 +04:00
|
|
|
mutex_exit(vp->v_interlock);
|
2013-10-29 13:53:51 +04:00
|
|
|
|
2011-04-02 08:28:56 +04:00
|
|
|
/*
|
|
|
|
* Clean out any cached data associated with the vnode.
|
|
|
|
* If purging an active vnode, it must be closed and
|
|
|
|
* deactivated before being reclaimed. Note that the
|
|
|
|
* VOP_INACTIVE will unlock the vnode.
|
|
|
|
*/
|
2013-11-07 13:48:34 +04:00
|
|
|
if (doclose) {
|
2011-04-02 08:28:56 +04:00
|
|
|
error = vinvalbuf(vp, V_SAVE, NOCRED, l, 0, 0);
|
|
|
|
if (error != 0) {
|
|
|
|
if (wapbl_vphaswapbl(vp))
|
|
|
|
WAPBL_DISCARD(wapbl_vptomp(vp));
|
|
|
|
error = vinvalbuf(vp, 0, NOCRED, l, 0, 0);
|
|
|
|
}
|
|
|
|
KASSERT(error == 0);
|
|
|
|
KASSERT((vp->v_iflag & VI_ONWORKLST) == 0);
|
|
|
|
if (active && (vp->v_type == VBLK || vp->v_type == VCHR)) {
|
|
|
|
spec_node_revoke(vp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (active) {
|
|
|
|
VOP_INACTIVE(vp, &recycle);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Any other processes trying to obtain this lock must first
|
|
|
|
* wait for VI_XLOCK to clear, then call the new lock operation.
|
|
|
|
*/
|
|
|
|
VOP_UNLOCK(vp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Disassociate the underlying file system from the vnode. */
|
|
|
|
if (VOP_RECLAIM(vp)) {
|
2011-09-30 00:51:38 +04:00
|
|
|
vnpanic(vp, "%s: cannot reclaim", __func__);
|
2011-04-02 08:28:56 +04:00
|
|
|
}
|
|
|
|
|
2011-05-19 07:11:55 +04:00
|
|
|
KASSERT(vp->v_data == NULL);
|
2011-04-02 08:28:56 +04:00
|
|
|
KASSERT(vp->v_uobj.uo_npages == 0);
|
2011-05-19 07:11:55 +04:00
|
|
|
|
2011-04-02 08:28:56 +04:00
|
|
|
if (vp->v_type == VREG && vp->v_ractx != NULL) {
|
|
|
|
uvm_ra_freectx(vp->v_ractx);
|
|
|
|
vp->v_ractx = NULL;
|
|
|
|
}
|
2011-05-19 07:11:55 +04:00
|
|
|
|
|
|
|
/* Purge name cache. */
|
2011-04-02 08:28:56 +04:00
|
|
|
cache_purge(vp);
|
|
|
|
|
2014-02-27 17:00:06 +04:00
|
|
|
/* Move to dead mount. */
|
|
|
|
vp->v_vflag &= ~VV_ROOT;
|
|
|
|
atomic_inc_uint(&dead_mount->mnt_refcnt);
|
|
|
|
vfs_insmntque(vp, dead_mount);
|
2013-10-29 13:53:51 +04:00
|
|
|
|
2011-04-02 08:28:56 +04:00
|
|
|
/* Done with purge, notify sleepers of the grim news. */
|
2011-06-12 07:35:36 +04:00
|
|
|
mutex_enter(vp->v_interlock);
|
2013-11-07 13:48:34 +04:00
|
|
|
if (doclose) {
|
2013-10-29 13:53:51 +04:00
|
|
|
vp->v_op = dead_vnodeop_p;
|
2013-11-07 13:48:34 +04:00
|
|
|
vp->v_vflag |= VV_LOCKSWORK;
|
|
|
|
vp->v_iflag |= VI_CLEAN;
|
|
|
|
} else {
|
|
|
|
vp->v_op = spec_vnodeop_p;
|
|
|
|
vp->v_vflag &= ~VV_LOCKSWORK;
|
2013-10-29 13:53:51 +04:00
|
|
|
}
|
2011-04-02 08:28:56 +04:00
|
|
|
vp->v_tag = VT_NON;
|
|
|
|
KNOTE(&vp->v_klist, NOTE_REVOKE);
|
|
|
|
vp->v_iflag &= ~VI_XLOCK;
|
|
|
|
cv_broadcast(&vp->v_cv);
|
|
|
|
|
|
|
|
KASSERT((vp->v_iflag & VI_ONWORKLST) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recycle an unused vnode to the front of the free list.
|
|
|
|
* Release the passed interlock if the vnode will be recycled.
|
|
|
|
*/
|
|
|
|
int
|
2013-10-29 13:53:51 +04:00
|
|
|
vrecycle(vnode_t *vp, kmutex_t *inter_lkp)
|
2011-04-02 08:28:56 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
KASSERT((vp->v_iflag & VI_MARKER) == 0);
|
|
|
|
|
2011-06-12 07:35:36 +04:00
|
|
|
mutex_enter(vp->v_interlock);
|
2013-11-03 12:33:00 +04:00
|
|
|
if (vp->v_usecount != 0 || (vp->v_iflag & (VI_CLEAN|VI_XLOCK)) != 0) {
|
2011-06-12 07:35:36 +04:00
|
|
|
mutex_exit(vp->v_interlock);
|
2011-04-02 11:33:49 +04:00
|
|
|
return 0;
|
2011-04-02 08:28:56 +04:00
|
|
|
}
|
|
|
|
if (inter_lkp) {
|
|
|
|
mutex_exit(inter_lkp);
|
|
|
|
}
|
|
|
|
vremfree(vp);
|
|
|
|
vp->v_usecount = 1;
|
2013-12-01 21:29:40 +04:00
|
|
|
KASSERT((vp->v_iflag & VI_CHANGING) == 0);
|
|
|
|
vp->v_iflag |= VI_CHANGING;
|
2013-11-07 13:48:34 +04:00
|
|
|
vclean(vp);
|
2013-12-01 21:29:40 +04:00
|
|
|
vrelel(vp, VRELEL_CHANGING_SET);
|
2011-04-02 11:33:49 +04:00
|
|
|
return 1;
|
2011-04-02 08:28:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Eliminate all activity associated with the requested vnode
|
|
|
|
* and with all vnodes aliased to the requested vnode.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
vrevoke(vnode_t *vp)
|
|
|
|
{
|
2013-02-13 18:03:48 +04:00
|
|
|
vnode_t *vq;
|
2011-04-02 08:28:56 +04:00
|
|
|
enum vtype type;
|
|
|
|
dev_t dev;
|
|
|
|
|
|
|
|
KASSERT(vp->v_usecount > 0);
|
|
|
|
|
2011-06-12 07:35:36 +04:00
|
|
|
mutex_enter(vp->v_interlock);
|
2011-04-02 08:28:56 +04:00
|
|
|
if ((vp->v_iflag & VI_CLEAN) != 0) {
|
2011-06-12 07:35:36 +04:00
|
|
|
mutex_exit(vp->v_interlock);
|
2011-04-02 08:28:56 +04:00
|
|
|
return;
|
|
|
|
} else if (vp->v_type != VBLK && vp->v_type != VCHR) {
|
|
|
|
atomic_inc_uint(&vp->v_usecount);
|
2013-12-01 21:29:40 +04:00
|
|
|
mutex_exit(vp->v_interlock);
|
|
|
|
vgone(vp);
|
2011-04-02 08:28:56 +04:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
dev = vp->v_rdev;
|
|
|
|
type = vp->v_type;
|
2011-06-12 07:35:36 +04:00
|
|
|
mutex_exit(vp->v_interlock);
|
2011-04-02 08:28:56 +04:00
|
|
|
}
|
|
|
|
|
2013-02-13 18:03:48 +04:00
|
|
|
while (spec_node_lookup_by_dev(type, dev, &vq) == 0) {
|
2013-12-01 21:29:40 +04:00
|
|
|
vgone(vq);
|
2011-04-02 08:28:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Eliminate all activity associated with a vnode in preparation for
|
|
|
|
* reuse. Drops a reference from the vnode.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
vgone(vnode_t *vp)
|
|
|
|
{
|
|
|
|
|
2011-06-12 07:35:36 +04:00
|
|
|
mutex_enter(vp->v_interlock);
|
2013-12-01 21:29:40 +04:00
|
|
|
if ((vp->v_iflag & VI_CHANGING) != 0)
|
|
|
|
vwait(vp, VI_CHANGING);
|
|
|
|
vp->v_iflag |= VI_CHANGING;
|
2013-11-07 13:48:34 +04:00
|
|
|
vclean(vp);
|
2013-12-01 21:29:40 +04:00
|
|
|
vrelel(vp, VRELEL_CHANGING_SET);
|
2011-04-02 08:28:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update outstanding I/O count and do wakeup if requested.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
vwakeup(struct buf *bp)
|
|
|
|
{
|
|
|
|
vnode_t *vp;
|
|
|
|
|
|
|
|
if ((vp = bp->b_vp) == NULL)
|
|
|
|
return;
|
|
|
|
|
2011-06-12 07:35:36 +04:00
|
|
|
KASSERT(bp->b_objlock == vp->v_interlock);
|
2011-04-02 08:28:56 +04:00
|
|
|
KASSERT(mutex_owned(bp->b_objlock));
|
|
|
|
|
|
|
|
if (--vp->v_numoutput < 0)
|
2011-09-30 00:51:38 +04:00
|
|
|
vnpanic(vp, "%s: neg numoutput, vp %p", __func__, vp);
|
2011-04-02 08:28:56 +04:00
|
|
|
if (vp->v_numoutput == 0)
|
|
|
|
cv_broadcast(&vp->v_cv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wait for a vnode (typically with VI_XLOCK set) to be cleaned or
|
|
|
|
* recycled.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
vwait(vnode_t *vp, int flags)
|
|
|
|
{
|
|
|
|
|
2011-06-12 07:35:36 +04:00
|
|
|
KASSERT(mutex_owned(vp->v_interlock));
|
2011-04-02 08:28:56 +04:00
|
|
|
KASSERT(vp->v_usecount != 0);
|
|
|
|
|
|
|
|
while ((vp->v_iflag & flags) != 0)
|
2011-06-12 07:35:36 +04:00
|
|
|
cv_wait(&vp->v_cv, vp->v_interlock);
|
2011-04-02 08:28:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-04-02 09:07:56 +04:00
|
|
|
vfs_drainvnodes(long target)
|
2011-04-02 08:28:56 +04:00
|
|
|
{
|
2011-10-02 17:00:06 +04:00
|
|
|
int error;
|
2011-04-02 08:28:56 +04:00
|
|
|
|
2011-10-02 17:00:06 +04:00
|
|
|
mutex_enter(&vnode_free_list_lock);
|
2011-04-02 08:28:56 +04:00
|
|
|
|
2011-10-02 17:00:06 +04:00
|
|
|
while (numvnodes > target) {
|
|
|
|
error = cleanvnode();
|
|
|
|
if (error != 0)
|
|
|
|
return error;
|
2011-04-02 08:28:56 +04:00
|
|
|
mutex_enter(&vnode_free_list_lock);
|
|
|
|
}
|
2011-10-02 17:00:06 +04:00
|
|
|
|
|
|
|
mutex_exit(&vnode_free_list_lock);
|
|
|
|
|
2011-04-02 08:28:56 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-09-30 00:51:38 +04:00
|
|
|
vnpanic(vnode_t *vp, const char *fmt, ...)
|
2011-04-02 08:28:56 +04:00
|
|
|
{
|
2011-09-30 00:51:38 +04:00
|
|
|
va_list ap;
|
2011-04-02 08:28:56 +04:00
|
|
|
|
2011-09-30 00:51:38 +04:00
|
|
|
#ifdef DIAGNOSTIC
|
2011-04-02 08:28:56 +04:00
|
|
|
vprint(NULL, vp);
|
|
|
|
#endif
|
2011-09-30 00:51:38 +04:00
|
|
|
va_start(ap, fmt);
|
|
|
|
vpanic(fmt, ap);
|
|
|
|
va_end(ap);
|
2011-04-02 08:28:56 +04:00
|
|
|
}
|