2008-11-19 21:35:57 +03:00
|
|
|
/* $NetBSD: nfs_kq.c,v 1.23 2008/11/19 18:36:09 ad Exp $ */
|
2002-10-23 13:10:23 +04:00
|
|
|
|
|
|
|
/*-
|
2008-02-05 17:19:52 +03:00
|
|
|
* Copyright (c) 2002, 2008 The NetBSD Foundation, Inc.
|
2002-10-23 13:10:23 +04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Jaromir Dolecek.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2008-11-19 21:35:57 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: nfs_kq.c,v 1.23 2008/11/19 18:36:09 ad Exp $");
|
2002-10-23 13:10:23 +04:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
2007-04-29 19:31:53 +04:00
|
|
|
#include <sys/condvar.h>
|
2002-10-23 13:10:23 +04:00
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/proc.h>
|
2008-01-02 22:26:45 +03:00
|
|
|
#include <sys/kmem.h>
|
2002-10-23 13:10:23 +04:00
|
|
|
#include <sys/mount.h>
|
2007-04-29 19:31:53 +04:00
|
|
|
#include <sys/mutex.h>
|
2002-10-23 13:10:23 +04:00
|
|
|
#include <sys/vnode.h>
|
|
|
|
#include <sys/unistd.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/kthread.h>
|
|
|
|
|
|
|
|
#include <uvm/uvm_extern.h>
|
|
|
|
#include <uvm/uvm.h>
|
|
|
|
|
|
|
|
#include <nfs/rpcv2.h>
|
|
|
|
#include <nfs/nfsproto.h>
|
|
|
|
#include <nfs/nfs.h>
|
|
|
|
#include <nfs/nfsnode.h>
|
|
|
|
#include <nfs/nfs_var.h>
|
|
|
|
|
|
|
|
struct kevq {
|
|
|
|
SLIST_ENTRY(kevq) kev_link;
|
|
|
|
struct vnode *vp;
|
|
|
|
u_int usecount;
|
|
|
|
u_int flags;
|
|
|
|
#define KEVQ_BUSY 0x01 /* currently being processed */
|
|
|
|
struct timespec omtime; /* old modification time */
|
|
|
|
struct timespec octime; /* old change time */
|
|
|
|
nlink_t onlink; /* old number of references to file */
|
2007-04-29 19:31:53 +04:00
|
|
|
kcondvar_t cv;
|
2002-10-23 13:10:23 +04:00
|
|
|
};
|
|
|
|
SLIST_HEAD(kevqlist, kevq);
|
|
|
|
|
2008-11-19 21:35:57 +03:00
|
|
|
static kmutex_t nfskq_lock;
|
2007-07-10 00:51:58 +04:00
|
|
|
static struct lwp *nfskq_thread;
|
2007-04-29 19:31:53 +04:00
|
|
|
static kcondvar_t nfskq_cv;
|
2002-10-23 13:10:23 +04:00
|
|
|
static struct kevqlist kevlist = SLIST_HEAD_INITIALIZER(kevlist);
|
2008-11-19 21:35:57 +03:00
|
|
|
static bool nfskq_thread_exit;
|
2002-10-23 13:10:23 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
nfs_kqinit(void)
|
|
|
|
{
|
2007-04-29 19:31:53 +04:00
|
|
|
|
2008-11-19 21:35:57 +03:00
|
|
|
mutex_init(&nfskq_lock, MUTEX_DEFAULT, IPL_NONE);
|
2007-04-29 19:31:53 +04:00
|
|
|
cv_init(&nfskq_cv, "nfskqpw");
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
|
2008-11-19 21:35:57 +03:00
|
|
|
void
|
|
|
|
nfs_kqfini(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (nfskq_thread != NULL) {
|
|
|
|
mutex_enter(&nfskq_lock);
|
|
|
|
nfskq_thread_exit = true;
|
|
|
|
cv_broadcast(&nfskq_cv);
|
|
|
|
do {
|
|
|
|
cv_wait(&nfskq_cv, &nfskq_lock);
|
|
|
|
} while (nfskq_thread != NULL);
|
|
|
|
mutex_exit(&nfskq_lock);
|
|
|
|
}
|
|
|
|
mutex_destroy(&nfskq_lock);
|
|
|
|
cv_destroy(&nfskq_cv);
|
|
|
|
}
|
|
|
|
|
2002-10-23 13:10:23 +04:00
|
|
|
/*
|
|
|
|
* This quite simplistic routine periodically checks for server changes
|
2003-02-27 12:01:09 +03:00
|
|
|
* of any of the watched files every NFS_MINATTRTIMO/2 seconds.
|
2002-10-23 13:10:23 +04:00
|
|
|
* Only changes in size, modification time, change time and nlinks
|
|
|
|
* are being checked, everything else is ignored.
|
|
|
|
* The routine only calls VOP_GETATTR() when it's likely it would get
|
|
|
|
* some new data, i.e. when the vnode expires from attrcache. This
|
|
|
|
* should give same result as periodically running stat(2) from userland,
|
|
|
|
* while keeping CPU/network usage low, and still provide proper kevent
|
|
|
|
* semantics.
|
|
|
|
* The poller thread is created when first vnode is added to watch list,
|
|
|
|
* and exits when the watch list is empty. The overhead of thread creation
|
|
|
|
* isn't really important, neither speed of attach and detach of knote.
|
|
|
|
*/
|
|
|
|
/* ARGSUSED */
|
|
|
|
static void
|
2006-11-09 12:53:57 +03:00
|
|
|
nfs_kqpoll(void *arg)
|
2002-10-23 13:10:23 +04:00
|
|
|
{
|
|
|
|
struct kevq *ke;
|
|
|
|
struct vattr attr;
|
2006-07-24 02:06:03 +04:00
|
|
|
struct lwp *l = curlwp;
|
2002-10-23 13:10:23 +04:00
|
|
|
u_quad_t osize;
|
|
|
|
|
2008-11-19 21:35:57 +03:00
|
|
|
mutex_enter(&nfskq_lock);
|
|
|
|
while (!nfskq_thread_exit) {
|
2002-10-23 13:10:23 +04:00
|
|
|
SLIST_FOREACH(ke, &kevlist, kev_link) {
|
|
|
|
/* skip if still in attrcache */
|
|
|
|
if (nfs_getattrcache(ke->vp, &attr) != ENOENT)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mark entry busy, release lock and check
|
|
|
|
* for changes.
|
|
|
|
*/
|
|
|
|
ke->flags |= KEVQ_BUSY;
|
2008-11-19 21:35:57 +03:00
|
|
|
mutex_exit(&nfskq_lock);
|
2002-10-23 13:10:23 +04:00
|
|
|
|
|
|
|
/* save v_size, nfs_getattr() updates it */
|
|
|
|
osize = ke->vp->v_size;
|
|
|
|
|
2007-11-26 22:01:26 +03:00
|
|
|
(void) VOP_GETATTR(ke->vp, &attr, l->l_cred);
|
2002-10-23 13:10:23 +04:00
|
|
|
|
|
|
|
/* following is a bit fragile, but about best
|
|
|
|
* we can get */
|
|
|
|
if (attr.va_size != osize) {
|
|
|
|
int extended = (attr.va_size > osize);
|
|
|
|
VN_KNOTE(ke->vp, NOTE_WRITE
|
|
|
|
| (extended ? NOTE_EXTEND : 0));
|
|
|
|
ke->omtime = attr.va_mtime;
|
|
|
|
} else if (attr.va_mtime.tv_sec != ke->omtime.tv_sec
|
|
|
|
|| attr.va_mtime.tv_nsec != ke->omtime.tv_nsec) {
|
|
|
|
VN_KNOTE(ke->vp, NOTE_WRITE);
|
|
|
|
ke->omtime = attr.va_mtime;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attr.va_ctime.tv_sec != ke->octime.tv_sec
|
|
|
|
|| attr.va_ctime.tv_nsec != ke->octime.tv_nsec) {
|
|
|
|
VN_KNOTE(ke->vp, NOTE_ATTRIB);
|
|
|
|
ke->octime = attr.va_ctime;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attr.va_nlink != ke->onlink) {
|
|
|
|
VN_KNOTE(ke->vp, NOTE_LINK);
|
|
|
|
ke->onlink = attr.va_nlink;
|
|
|
|
}
|
|
|
|
|
2008-11-19 21:35:57 +03:00
|
|
|
mutex_enter(&nfskq_lock);
|
2002-10-23 13:10:23 +04:00
|
|
|
ke->flags &= ~KEVQ_BUSY;
|
2007-04-29 19:31:53 +04:00
|
|
|
cv_signal(&ke->cv);
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (SLIST_EMPTY(&kevlist)) {
|
|
|
|
/* Nothing more to watch, exit */
|
2007-07-10 00:51:58 +04:00
|
|
|
nfskq_thread = NULL;
|
2008-11-19 21:35:57 +03:00
|
|
|
mutex_exit(&nfskq_lock);
|
2002-10-23 13:10:23 +04:00
|
|
|
kthread_exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* wait a while before checking for changes again */
|
2008-11-19 21:35:57 +03:00
|
|
|
cv_timedwait(&nfskq_cv, &nfskq_lock,
|
2007-04-29 19:31:53 +04:00
|
|
|
NFS_MINATTRTIMO * hz / 2);
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
2008-11-19 21:35:57 +03:00
|
|
|
nfskq_thread = NULL;
|
|
|
|
cv_broadcast(&nfskq_cv);
|
|
|
|
mutex_exit(&nfskq_lock);
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
filt_nfsdetach(struct knote *kn)
|
|
|
|
{
|
|
|
|
struct vnode *vp = (struct vnode *)kn->kn_hook;
|
|
|
|
struct kevq *ke;
|
|
|
|
|
2008-02-05 17:19:52 +03:00
|
|
|
mutex_enter(&vp->v_interlock);
|
2002-10-23 13:10:23 +04:00
|
|
|
SLIST_REMOVE(&vp->v_klist, kn, knote, kn_selnext);
|
2008-02-05 17:19:52 +03:00
|
|
|
mutex_exit(&vp->v_interlock);
|
2002-10-23 13:10:23 +04:00
|
|
|
|
|
|
|
/* Remove the vnode from watch list */
|
2008-11-19 21:35:57 +03:00
|
|
|
mutex_enter(&nfskq_lock);
|
2002-10-23 13:10:23 +04:00
|
|
|
SLIST_FOREACH(ke, &kevlist, kev_link) {
|
|
|
|
if (ke->vp == vp) {
|
|
|
|
while (ke->flags & KEVQ_BUSY) {
|
2008-11-19 21:35:57 +03:00
|
|
|
cv_wait(&ke->cv, &nfskq_lock);
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ke->usecount > 1) {
|
|
|
|
/* keep, other kevents need this */
|
|
|
|
ke->usecount--;
|
|
|
|
} else {
|
|
|
|
/* last user, g/c */
|
2007-04-29 19:31:53 +04:00
|
|
|
cv_destroy(&ke->cv);
|
2002-10-23 13:10:23 +04:00
|
|
|
SLIST_REMOVE(&kevlist, ke, kevq, kev_link);
|
2008-01-02 22:26:45 +03:00
|
|
|
kmem_free(ke, sizeof(*ke));
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-11-19 21:35:57 +03:00
|
|
|
mutex_exit(&nfskq_lock);
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
filt_nfsread(struct knote *kn, long hint)
|
|
|
|
{
|
|
|
|
struct vnode *vp = (struct vnode *)kn->kn_hook;
|
2008-02-05 17:19:52 +03:00
|
|
|
int rv;
|
2002-10-23 13:10:23 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* filesystem is gone, so set the EOF flag and schedule
|
|
|
|
* the knote for deletion.
|
|
|
|
*/
|
2008-02-05 17:19:52 +03:00
|
|
|
switch (hint) {
|
|
|
|
case NOTE_REVOKE:
|
|
|
|
KASSERT(mutex_owned(&vp->v_interlock));
|
2002-10-23 13:10:23 +04:00
|
|
|
kn->kn_flags |= (EV_EOF | EV_ONESHOT);
|
|
|
|
return (1);
|
2008-02-05 17:19:52 +03:00
|
|
|
case 0:
|
|
|
|
mutex_enter(&vp->v_interlock);
|
2008-03-22 00:54:58 +03:00
|
|
|
kn->kn_data = vp->v_size - ((file_t *)kn->kn_obj)->f_offset;
|
2008-02-05 17:19:52 +03:00
|
|
|
rv = (kn->kn_data != 0);
|
|
|
|
mutex_exit(&vp->v_interlock);
|
|
|
|
return rv;
|
|
|
|
default:
|
|
|
|
KASSERT(mutex_owned(&vp->v_interlock));
|
2008-03-22 00:54:58 +03:00
|
|
|
kn->kn_data = vp->v_size - ((file_t *)kn->kn_obj)->f_offset;
|
2008-02-05 17:19:52 +03:00
|
|
|
return (kn->kn_data != 0);
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
filt_nfsvnode(struct knote *kn, long hint)
|
|
|
|
{
|
2008-02-05 17:19:52 +03:00
|
|
|
struct vnode *vp = (struct vnode *)kn->kn_hook;
|
|
|
|
int fflags;
|
2002-10-23 13:10:23 +04:00
|
|
|
|
2008-02-05 17:19:52 +03:00
|
|
|
switch (hint) {
|
|
|
|
case NOTE_REVOKE:
|
|
|
|
KASSERT(mutex_owned(&vp->v_interlock));
|
2002-10-23 13:10:23 +04:00
|
|
|
kn->kn_flags |= EV_EOF;
|
2008-02-05 17:19:52 +03:00
|
|
|
if ((kn->kn_sfflags & hint) != 0)
|
|
|
|
kn->kn_fflags |= hint;
|
2002-10-23 13:10:23 +04:00
|
|
|
return (1);
|
2008-02-05 17:19:52 +03:00
|
|
|
case 0:
|
|
|
|
mutex_enter(&vp->v_interlock);
|
|
|
|
fflags = kn->kn_fflags;
|
|
|
|
mutex_exit(&vp->v_interlock);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
KASSERT(mutex_owned(&vp->v_interlock));
|
|
|
|
if ((kn->kn_sfflags & hint) != 0)
|
|
|
|
kn->kn_fflags |= hint;
|
|
|
|
fflags = kn->kn_fflags;
|
|
|
|
break;
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
2008-02-05 17:19:52 +03:00
|
|
|
|
|
|
|
return (fflags != 0);
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
|
2008-02-05 17:19:52 +03:00
|
|
|
|
2005-02-27 01:31:44 +03:00
|
|
|
static const struct filterops nfsread_filtops =
|
2002-10-23 13:10:23 +04:00
|
|
|
{ 1, NULL, filt_nfsdetach, filt_nfsread };
|
2005-02-27 01:31:44 +03:00
|
|
|
static const struct filterops nfsvnode_filtops =
|
2002-10-23 13:10:23 +04:00
|
|
|
{ 1, NULL, filt_nfsdetach, filt_nfsvnode };
|
|
|
|
|
|
|
|
int
|
|
|
|
nfs_kqfilter(void *v)
|
|
|
|
{
|
|
|
|
struct vop_kqfilter_args /* {
|
|
|
|
struct vnode *a_vp;
|
|
|
|
struct knote *a_kn;
|
|
|
|
} */ *ap = v;
|
|
|
|
struct vnode *vp;
|
|
|
|
struct knote *kn;
|
|
|
|
struct kevq *ke;
|
|
|
|
int error = 0;
|
|
|
|
struct vattr attr;
|
2007-11-26 22:01:26 +03:00
|
|
|
struct lwp *l = curlwp;
|
2002-10-23 13:10:23 +04:00
|
|
|
|
|
|
|
vp = ap->a_vp;
|
|
|
|
kn = ap->a_kn;
|
|
|
|
switch (kn->kn_filter) {
|
|
|
|
case EVFILT_READ:
|
|
|
|
kn->kn_fop = &nfsread_filtops;
|
|
|
|
break;
|
|
|
|
case EVFILT_VNODE:
|
|
|
|
kn->kn_fop = &nfsvnode_filtops;
|
|
|
|
break;
|
|
|
|
default:
|
2007-12-05 20:19:46 +03:00
|
|
|
return (EINVAL);
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Put the vnode to watched list.
|
|
|
|
*/
|
2005-02-27 01:31:44 +03:00
|
|
|
|
2002-10-23 13:10:23 +04:00
|
|
|
/*
|
|
|
|
* Fetch current attributes. It's only needed when the vnode
|
|
|
|
* is not watched yet, but we need to do this without lock
|
|
|
|
* held. This is likely cheap due to attrcache, so do it now.
|
2005-02-27 01:31:44 +03:00
|
|
|
*/
|
2002-10-23 13:10:23 +04:00
|
|
|
memset(&attr, 0, sizeof(attr));
|
2007-11-26 22:01:26 +03:00
|
|
|
(void) VOP_GETATTR(vp, &attr, l->l_cred);
|
2002-10-23 13:10:23 +04:00
|
|
|
|
2008-11-19 21:35:57 +03:00
|
|
|
mutex_enter(&nfskq_lock);
|
2002-10-23 13:10:23 +04:00
|
|
|
|
|
|
|
/* ensure the poller is running */
|
2007-07-10 00:51:58 +04:00
|
|
|
if (!nfskq_thread) {
|
|
|
|
error = kthread_create(PRI_NONE, 0, NULL, nfs_kqpoll,
|
|
|
|
NULL, &nfskq_thread, "nfskqpoll");
|
2008-02-05 17:19:52 +03:00
|
|
|
if (error) {
|
2008-11-19 21:35:57 +03:00
|
|
|
mutex_exit(&nfskq_lock);
|
2008-02-05 17:19:52 +03:00
|
|
|
return error;
|
|
|
|
}
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
SLIST_FOREACH(ke, &kevlist, kev_link) {
|
|
|
|
if (ke->vp == vp)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ke) {
|
|
|
|
/* already watched, so just bump usecount */
|
|
|
|
ke->usecount++;
|
|
|
|
} else {
|
|
|
|
/* need a new one */
|
2008-01-02 22:26:45 +03:00
|
|
|
ke = kmem_alloc(sizeof(*ke), KM_SLEEP);
|
2002-10-23 13:10:23 +04:00
|
|
|
ke->vp = vp;
|
|
|
|
ke->usecount = 1;
|
|
|
|
ke->flags = 0;
|
|
|
|
ke->omtime = attr.va_mtime;
|
|
|
|
ke->octime = attr.va_ctime;
|
|
|
|
ke->onlink = attr.va_nlink;
|
2007-04-29 19:31:53 +04:00
|
|
|
cv_init(&ke->cv, "nfskqdet");
|
2002-10-23 13:10:23 +04:00
|
|
|
SLIST_INSERT_HEAD(&kevlist, ke, kev_link);
|
|
|
|
}
|
|
|
|
|
2008-02-05 17:19:52 +03:00
|
|
|
mutex_enter(&vp->v_interlock);
|
2003-03-27 22:58:26 +03:00
|
|
|
SLIST_INSERT_HEAD(&vp->v_klist, kn, kn_selnext);
|
2008-02-05 17:19:52 +03:00
|
|
|
kn->kn_hook = vp;
|
2008-02-12 11:55:27 +03:00
|
|
|
mutex_exit(&vp->v_interlock);
|
2003-03-27 22:58:26 +03:00
|
|
|
|
2008-02-05 17:19:52 +03:00
|
|
|
/* kick the poller */
|
|
|
|
cv_signal(&nfskq_cv);
|
2008-11-19 21:35:57 +03:00
|
|
|
mutex_exit(&nfskq_lock);
|
2008-02-05 17:19:52 +03:00
|
|
|
|
2002-10-23 13:10:23 +04:00
|
|
|
return (error);
|
|
|
|
}
|