diff options
-rw-r--r-- | sys/conf/files | 3 | ||||
-rw-r--r-- | sys/nfs/nfs_bio.c | 15 | ||||
-rw-r--r-- | sys/nfs/nfs_kq.c | 359 | ||||
-rw-r--r-- | sys/nfs/nfs_subs.c | 6 | ||||
-rw-r--r-- | sys/nfs/nfs_var.h | 10 | ||||
-rw-r--r-- | sys/nfs/nfs_vnops.c | 31 | ||||
-rw-r--r-- | sys/sys/malloc.h | 8 |
7 files changed, 421 insertions, 11 deletions
diff --git a/sys/conf/files b/sys/conf/files index caf2d05e24d..2bec01e0d5e 100644 --- a/sys/conf/files +++ b/sys/conf/files @@ -1,4 +1,4 @@ -# $OpenBSD: files,v 1.310 2004/07/20 20:15:13 art Exp $ +# $OpenBSD: files,v 1.311 2004/07/21 17:30:55 marius Exp $ # $NetBSD: files,v 1.87 1996/05/19 17:17:50 jonathan Exp $ # @(#)files.newconf 7.5 (Berkeley) 5/10/93 @@ -811,6 +811,7 @@ file nfs/krpc_subr.c nfsclient file nfs/nfs_bio.c nfsclient file nfs/nfs_boot.c nfsclient file nfs/nfs_node.c nfsclient +file nfs/nfs_kq.c nfsclient file nfs/nfs_serv.c nfsserver file nfs/nfs_socket.c nfsserver | nfsclient file nfs/nfs_srvcache.c nfsserver diff --git a/sys/nfs/nfs_bio.c b/sys/nfs/nfs_bio.c index 8672da68ddd..53caf23abcf 100644 --- a/sys/nfs/nfs_bio.c +++ b/sys/nfs/nfs_bio.c @@ -1,4 +1,4 @@ -/* $OpenBSD: nfs_bio.c,v 1.38 2003/06/02 23:28:19 millert Exp $ */ +/* $OpenBSD: nfs_bio.c,v 1.39 2004/07/21 17:30:55 marius Exp $ */ /* $NetBSD: nfs_bio.c,v 1.25.4.2 1996/07/08 20:47:04 jtc Exp $ */ /* @@ -291,7 +291,7 @@ nfs_write(v) struct vattr vattr; struct nfsmount *nmp = VFSTONFS(vp->v_mount); daddr_t lbn, bn; - int n, on, error = 0; + int n, on, error = 0, extended = 0, wrotedta = 0, truncated = 0; #ifdef DIAGNOSTIC if (uio->uio_rw != UIO_WRITE) @@ -370,7 +370,9 @@ again: if (uio->uio_offset + n > np->n_size) { np->n_size = uio->uio_offset + n; uvm_vnp_setsize(vp, (u_long)np->n_size); - } + extended = 1; + } else if (uio->uio_offset + n < np->n_size) + truncated = 1; /* * If the new write will leave a contiguous dirty @@ -407,6 +409,8 @@ again: bp->b_validend = max(bp->b_validend, bp->b_dirtyend); } + wrotedta = 1; + /* * Since this block is being modified, it must be written * again and not just committed. @@ -429,6 +433,11 @@ again: bdwrite(bp); } } while (uio->uio_resid > 0 && n > 0); + + if (wrotedta) + VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0) | + (truncated ? NOTE_TRUNCATE : 0)); + return (0); } diff --git a/sys/nfs/nfs_kq.c b/sys/nfs/nfs_kq.c new file mode 100644 index 00000000000..d8919486392 --- /dev/null +++ b/sys/nfs/nfs_kq.c @@ -0,0 +1,359 @@ +/* $OpenBSD: nfs_kq.c,v 1.1 2004/07/21 17:30:56 marius Exp $ */ +/* $NetBSD: nfs_kq.c,v 1.7 2003/10/30 01:43:10 simonb Exp $ */ + +/*- + * Copyright (c) 2002 The NetBSD Foundation, Inc. + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation 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 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> +#ifdef __NetBSD__ +__KERNEL_RCSID(0, "$NetBSD: nfs_kq.c,v 1.7 2003/10/30 01:43:10 simonb Exp $"); +#endif /* __NetBSD__ */ + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/kernel.h> +#include <sys/proc.h> +#include <sys/mount.h> +#include <sys/malloc.h> +#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 */ +#define KEVQ_WANT 0x02 /* want to change this entry */ + struct timespec omtime; /* old modification time */ + struct timespec octime; /* old change time */ + nlink_t onlink; /* old number of references to file */ +}; +SLIST_HEAD(kevqlist, kevq); + +static struct lock nfskevq_lock; +static struct proc *pnfskq; +static struct kevqlist kevlist = SLIST_HEAD_INITIALIZER(kevlist); + +void +nfs_kqinit(void) +{ + lockinit(&nfskevq_lock, PSOCK, "nfskqlck", 0, 0); +} + +/* + * This quite simplistic routine periodically checks for server changes + * of any of the watched files every NFS_MINATTRTIMO/2 seconds. + * 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 +nfs_kqpoll(void *arg) +{ + struct kevq *ke; + struct vattr attr; + struct proc *p = pnfskq; + u_quad_t osize; + + for(;;) { + lockmgr(&nfskevq_lock, LK_EXCLUSIVE, NULL, p); + SLIST_FOREACH(ke, &kevlist, kev_link) { + struct nfsnode *np = VTONFS(ke->vp); + +#ifdef DEBUG + printf("nfs_kqpoll on: "); + VOP_PRINT(ke->vp); +#endif + /* 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; + lockmgr(&nfskevq_lock, LK_RELEASE, NULL, p); + + /* save v_size, nfs_getattr() updates it */ + osize = np->n_size; + + (void) VOP_GETATTR(ke->vp, &attr, p->p_ucred, p); + + /* 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; + } + + lockmgr(&nfskevq_lock, LK_EXCLUSIVE, NULL, p); + ke->flags &= ~KEVQ_BUSY; + if (ke->flags & KEVQ_WANT) { + ke->flags &= ~KEVQ_WANT; + wakeup(ke); + } + } + + if (SLIST_EMPTY(&kevlist)) { + /* Nothing more to watch, exit */ + pnfskq = NULL; + lockmgr(&nfskevq_lock, LK_RELEASE, NULL, p); + kthread_exit(0); + } + lockmgr(&nfskevq_lock, LK_RELEASE, NULL, p); + + /* wait a while before checking for changes again */ + tsleep(pnfskq, PSOCK, "nfskqpw", + NFS_MINATTRTIMO * hz / 2); + + } +} + +static void +filt_nfsdetach(struct knote *kn) +{ + struct vnode *vp = (struct vnode *)kn->kn_hook; + struct kevq *ke; + struct proc *p = curproc; + +#ifdef notyet + /* XXXLUKEM lock the struct? */ + SLIST_REMOVE(&vp->v_klist, kn, knote, kn_selnext); +#endif + + simple_lock(&vp->v_selectinfo.vsi_lock); + SLIST_REMOVE(&vp->v_selectinfo.vsi_selinfo.si_note, + kn, knote, kn_selnext); + simple_unlock(&vp->v_selectinfo.vsi_lock); + + /* Remove the vnode from watch list */ + lockmgr(&nfskevq_lock, LK_EXCLUSIVE, NULL, p); + SLIST_FOREACH(ke, &kevlist, kev_link) { + if (ke->vp == vp) { + while (ke->flags & KEVQ_BUSY) { + ke->flags |= KEVQ_WANT; + lockmgr(&nfskevq_lock, LK_RELEASE, NULL, p); + (void) tsleep(ke, PSOCK, "nfskqdet", 0); + lockmgr(&nfskevq_lock, LK_EXCLUSIVE, NULL, p); + } + + if (ke->usecount > 1) { + /* keep, other kevents need this */ + ke->usecount--; + } else { + /* last user, g/c */ + SLIST_REMOVE(&kevlist, ke, kevq, kev_link); + FREE(ke, M_KEVENT); + } + break; + } + } + lockmgr(&nfskevq_lock, LK_RELEASE, NULL, p); +} + +static int +filt_nfsread(struct knote *kn, long hint) +{ + struct vnode *vp = (struct vnode *)kn->kn_hook; + struct nfsnode *np = VTONFS(vp); + + /* + * filesystem is gone, so set the EOF flag and schedule + * the knote for deletion. + */ + if (hint == NOTE_REVOKE) { + kn->kn_flags |= (EV_EOF | EV_ONESHOT); + return (1); + } + + /* XXXLUKEM lock the struct? */ + + kn->kn_data = np->n_size - kn->kn_fp->f_offset; +#ifdef DEBUG + printf("nfsread event. %d\n", kn->kn_data); +#endif + return (kn->kn_data != 0); +} + +static int +filt_nfsvnode(struct knote *kn, long hint) +{ + if (kn->kn_sfflags & hint) + kn->kn_fflags |= hint; + if (hint == NOTE_REVOKE) { + kn->kn_flags |= EV_EOF; + return (1); + } + return (kn->kn_fflags != 0); +} + +static const struct filterops nfsread_filtops = + { 1, NULL, filt_nfsdetach, filt_nfsread }; +static const struct filterops nfsvnode_filtops = + { 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; + struct proc *p = curproc; /* XXX */ + + vp = ap->a_vp; + kn = ap->a_kn; + +#ifdef DEBUG + printf("nfs_kqfilter(%d) on: ", kn->kn_filter); + VOP_PRINT(vp); +#endif + + switch (kn->kn_filter) { + case EVFILT_READ: + kn->kn_fop = &nfsread_filtops; + break; + case EVFILT_VNODE: + kn->kn_fop = &nfsvnode_filtops; + break; + default: + return (1); + } + + kn->kn_hook = vp; + + /* + * Put the vnode to watched list. + */ + + /* + * 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. + */ + memset(&attr, 0, sizeof(attr)); + (void) VOP_GETATTR(vp, &attr, p->p_ucred, p); + + lockmgr(&nfskevq_lock, LK_EXCLUSIVE, NULL, p); + + /* ensure the poller is running */ + if (!pnfskq) { + error = kthread_create(nfs_kqpoll, NULL, &pnfskq, + "nfskqpoll"); + if (error) + goto out; + } + + 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 */ + MALLOC(ke, struct kevq *, + sizeof(struct kevq), M_KEVENT, M_WAITOK); + ke->vp = vp; + ke->usecount = 1; + ke->flags = 0; + ke->omtime = attr.va_mtime; + ke->octime = attr.va_ctime; + ke->onlink = attr.va_nlink; + SLIST_INSERT_HEAD(&kevlist, ke, kev_link); + } + + /* kick the poller */ + wakeup(pnfskq); + + simple_lock(&vp->v_selectinfo.vsi_lock); + SLIST_INSERT_HEAD(&vp->v_selectinfo.vsi_selinfo.si_note, kn, kn_selnext); + simple_unlock(&vp->v_selectinfo.vsi_lock); + +#ifdef notyet + /* XXXLUKEM lock the struct? */ + SLIST_INSERT_HEAD(&vp->v_klist, kn, kn_selnext); +#endif + + out: + lockmgr(&nfskevq_lock, LK_RELEASE, NULL, p); + + return (error); +} diff --git a/sys/nfs/nfs_subs.c b/sys/nfs/nfs_subs.c index 04f487a2a62..03c30ea00da 100644 --- a/sys/nfs/nfs_subs.c +++ b/sys/nfs/nfs_subs.c @@ -1,4 +1,4 @@ -/* $OpenBSD: nfs_subs.c,v 1.47 2004/07/16 15:01:51 henning Exp $ */ +/* $OpenBSD: nfs_subs.c,v 1.48 2004/07/21 17:30:55 marius Exp $ */ /* $NetBSD: nfs_subs.c,v 1.27.4.3 1996/07/08 20:34:24 jtc Exp $ */ /* @@ -1071,6 +1071,10 @@ nfs_init() timeout_set(&nfs_timer_to, nfs_timer, &nfs_timer_to); nfs_timer(&nfs_timer_to); + +#ifdef NFSCLIENT + nfs_kqinit(); +#endif } #ifdef NFSCLIENT diff --git a/sys/nfs/nfs_var.h b/sys/nfs/nfs_var.h index c27b2f08d45..e5a6a45d3b2 100644 --- a/sys/nfs/nfs_var.h +++ b/sys/nfs/nfs_var.h @@ -1,4 +1,4 @@ -/* $OpenBSD: nfs_var.h,v 1.21 2002/07/03 20:57:00 nate Exp $ */ +/* $OpenBSD: nfs_var.h,v 1.22 2004/07/21 17:30:56 marius Exp $ */ /* $NetBSD: nfs_var.h,v 1.3 1996/02/18 11:53:54 fvdl Exp $ */ /* @@ -277,3 +277,11 @@ int nfs_getnickauth(struct nfsmount *, struct ucred *, char **, int *, char *, int); int nfs_savenickauth(struct nfsmount *, struct ucred *, int, NFSKERBKEY_T, struct mbuf **, char **, struct mbuf *); + +/* nfs_kq.c */ + +int nfs_kqfilter(void *); +void nfs_kqinit(void); + +#define VN_KNOTE(vp, b) \ + KNOTE(&vp->v_selectinfo.vsi_selinfo.si_note, (b)) diff --git a/sys/nfs/nfs_vnops.c b/sys/nfs/nfs_vnops.c index 5696925b4fa..f1a28db9e48 100644 --- a/sys/nfs/nfs_vnops.c +++ b/sys/nfs/nfs_vnops.c @@ -1,4 +1,4 @@ -/* $OpenBSD: nfs_vnops.c,v 1.61 2004/06/24 19:35:26 tholo Exp $ */ +/* $OpenBSD: nfs_vnops.c,v 1.62 2004/07/21 17:30:56 marius Exp $ */ /* $NetBSD: nfs_vnops.c,v 1.62.4.1 1996/07/08 20:26:52 jtc Exp $ */ /* @@ -100,7 +100,7 @@ struct vnodeopv_entry_desc nfsv2_vnodeop_entries[] = { { &vop_lease_desc, nfs_lease_check }, /* lease */ { &vop_ioctl_desc, nfs_ioctl }, /* ioctl */ { &vop_poll_desc, nfs_poll }, /* poll */ - { &vop_kqfilter_desc, vop_generic_kqfilter }, /* kqfilter */ + { &vop_kqfilter_desc, nfs_kqfilter }, /* kqfilter */ { &vop_revoke_desc, nfs_revoke }, /* revoke */ { &vop_fsync_desc, nfs_fsync }, /* fsync */ { &vop_remove_desc, nfs_remove }, /* remove */ @@ -559,6 +559,9 @@ nfs_setattr(v) np->n_size = np->n_vattr.va_size = tsize; uvm_vnp_setsize(vp, np->n_size); } + + VN_KNOTE(vp, NOTE_ATTRIB); /* XXX setattrrpc? */ + return (error); } @@ -1235,6 +1238,9 @@ nfs_mknod(v) error = nfs_mknodrpc(ap->a_dvp, &newvp, ap->a_cnp, ap->a_vap); if (!error) vrele(newvp); + + VN_KNOTE(ap->a_dvp, NOTE_WRITE); + return (error); } @@ -1340,6 +1346,7 @@ again: VTONFS(dvp)->n_flag |= NMODIFIED; if (!wccflag) VTONFS(dvp)->n_attrstamp = 0; + VN_KNOTE(ap->a_dvp, NOTE_WRITE); vrele(dvp); return (error); } @@ -1414,6 +1421,10 @@ nfs_remove(v) np->n_attrstamp = 0; vrele(dvp); vrele(vp); + + VN_KNOTE(vp, NOTE_DELETE); + VN_KNOTE(dvp, NOTE_WRITE); + return (error); } @@ -1504,6 +1515,7 @@ nfs_rename(v) */ if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename && tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) { + VN_KNOTE(tvp, NOTE_DELETE); vrele(tvp); tvp = NULL; } @@ -1512,6 +1524,9 @@ nfs_rename(v) tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred, tcnp->cn_proc); + VN_KNOTE(fdvp, NOTE_WRITE); + VN_KNOTE(tdvp, NOTE_WRITE); + if (fvp->v_type == VDIR) { if (tvp != NULL && tvp->v_type == VDIR) cache_purge(tdvp); @@ -1650,6 +1665,9 @@ nfs_link(v) VTONFS(vp)->n_attrstamp = 0; if (!wccflag) VTONFS(dvp)->n_attrstamp = 0; + + VN_KNOTE(vp, NOTE_LINK); + VN_KNOTE(dvp, NOTE_WRITE); vput(dvp); /* * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry. @@ -1717,6 +1735,7 @@ nfs_symlink(v) VTONFS(dvp)->n_flag |= NMODIFIED; if (!wccflag) VTONFS(dvp)->n_attrstamp = 0; + VN_KNOTE(dvp, NOTE_WRITE); vrele(dvp); /* * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry. @@ -1801,8 +1820,10 @@ nfs_mkdir(v) if (error) { if (newvp) vrele(newvp); - } else + } else { + VN_KNOTE(dvp, NOTE_WRITE|NOTE_LINK); *ap->a_vpp = newvp; + } pool_put(&namei_pool, cnp->cn_pnbuf); vrele(dvp); return (error); @@ -1850,6 +1871,10 @@ nfs_rmdir(v) VTONFS(dvp)->n_flag |= NMODIFIED; if (!wccflag) VTONFS(dvp)->n_attrstamp = 0; + + VN_KNOTE(dvp, NOTE_WRITE|NOTE_LINK); + VN_KNOTE(vp, NOTE_DELETE); + cache_purge(dvp); cache_purge(vp); vrele(vp); diff --git a/sys/sys/malloc.h b/sys/sys/malloc.h index ce09678777c..89faaec8189 100644 --- a/sys/sys/malloc.h +++ b/sys/sys/malloc.h @@ -1,4 +1,4 @@ -/* $OpenBSD: malloc.h,v 1.72 2004/06/20 01:04:28 art Exp $ */ +/* $OpenBSD: malloc.h,v 1.73 2004/07/21 17:30:56 marius Exp $ */ /* $NetBSD: malloc.h,v 1.39 1998/07/12 19:52:01 augustss Exp $ */ /* @@ -172,7 +172,10 @@ #define M_NTFSRDATA 134 /* NTFS resident data */ #define M_NTFSDECOMP 135 /* NTFS decompression temporary */ #define M_NTFSRUN 136 /* NTFS vrun storage */ -#define M_LAST 137 /* Must be last type + 1 */ + +#define M_KEVENT 137 /* kqueue related */ + +#define M_LAST 138 /* Must be last type + 1 */ #define INITKMEMNAMES { \ @@ -298,6 +301,7 @@ "NTFS resident data ", /* 134 M_NTFSRDATA */ \ "NTFS decomp", /* 135 M_NTFSDECOMP */ \ "NTFS vrun", /* 136 M_NTFSRUN */ \ + "kqueue", /* 137 M_KEVENT */ \ } struct kmemstats { |