Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/kqueue]: src/sys/nfs add support for kevents to NFS



details:   https://anonhg.NetBSD.org/src/rev/371af3cb2f8e
branches:  kqueue
changeset: 512535:371af3cb2f8e
user:      jdolecek <jdolecek%NetBSD.org@localhost>
date:      Mon Sep 30 20:43:39 2002 +0000

description:
add support for kevents to NFS
to detect file changes on server by other NFS clients, polling kernel thread
is used to periodically check for attribute changes of watched files;
the NFS server is only contacted when the vnode expires from local attrcache
(which takes 5-60 seconds currently), to keep network&CPU overhead low

the routine checking for remote changes is quite simplistic, but hopefully
doing it's job well enough

diffstat:

 sys/nfs/files.nfs   |    3 +-
 sys/nfs/nfs_bio.c   |    9 +-
 sys/nfs/nfs_kq.c    |  329 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 sys/nfs/nfs_subs.c  |    9 +-
 sys/nfs/nfs_var.h   |    5 +-
 sys/nfs/nfs_vnops.c |   19 ++-
 sys/nfs/nfsnode.h   |    3 +-
 7 files changed, 368 insertions(+), 9 deletions(-)

diffs (truncated from 577 to 300 lines):

diff -r 09f342c3d66d -r 371af3cb2f8e sys/nfs/files.nfs
--- a/sys/nfs/files.nfs Sun Sep 29 09:59:14 2002 +0000
+++ b/sys/nfs/files.nfs Mon Sep 30 20:43:39 2002 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: files.nfs,v 1.1.8.2 2002/06/23 17:51:46 jdolecek Exp $
+#      $NetBSD: files.nfs,v 1.1.8.3 2002/09/30 20:43:39 jdolecek Exp $
 
 deffs  fs_nfs.h                NFS
 
@@ -17,6 +17,7 @@
 file   nfs/nfs_boot.c          nfs 
 file   nfs/nfs_bootdhcp.c      nfs & (nfs_boot_bootp | nfs_boot_dhcp)
 file   nfs/nfs_bootparam.c     nfs & nfs_boot_bootparam
+file   nfs/nfs_kq.c            nfs   
 file   nfs/nfs_node.c          nfs   
 file   nfs/nfs_nqlease.c       nfsserver | nfs
 file   nfs/nfs_serv.c          nfsserver
diff -r 09f342c3d66d -r 371af3cb2f8e sys/nfs/nfs_bio.c
--- a/sys/nfs/nfs_bio.c Sun Sep 29 09:59:14 2002 +0000
+++ b/sys/nfs/nfs_bio.c Mon Sep 30 20:43:39 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: nfs_bio.c,v 1.68.2.4 2002/09/06 08:49:47 jdolecek Exp $        */
+/*     $NetBSD: nfs_bio.c,v 1.68.2.5 2002/09/30 20:43:40 jdolecek Exp $        */
 
 /*
  * Copyright (c) 1989, 1993
@@ -39,7 +39,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: nfs_bio.c,v 1.68.2.4 2002/09/06 08:49:47 jdolecek Exp $");
+__KERNEL_RCSID(0, "$NetBSD: nfs_bio.c,v 1.68.2.5 2002/09/30 20:43:40 jdolecek Exp $");
 
 #include "opt_nfs.h"
 #include "opt_ddb.h"
@@ -521,6 +521,7 @@
        voff_t oldoff, origoff;
        vsize_t bytelen;
        int error = 0, iomode, must_commit;
+       int extended = 0, wrotedta = 0;
 
 #ifdef DIAGNOSTIC
        if (uio->uio_rw != UIO_WRITE)
@@ -633,6 +634,7 @@
                if (error) {
                        break;
                }
+               wrotedta = 1;
 
                /*
                 * update UVM's notion of the size now that we've
@@ -641,6 +643,7 @@
 
                if (vp->v_size < uio->uio_offset) {
                        uvm_vnp_setsize(vp, uio->uio_offset);
+                       extended = 1;
                }
 
                if ((oldoff & ~(nmp->nm_wsize - 1)) !=
@@ -652,6 +655,8 @@
                                       ~(nmp->nm_wsize - 1)), PGO_CLEANIT);
                }
        } while (uio->uio_resid > 0);
+       if (wrotedta)
+               VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
        if ((np->n_flag & NQNFSNONCACHE) || (ioflag & IO_SYNC)) {
                simple_lock(&vp->v_interlock);
                error = VOP_PUTPAGES(vp,
diff -r 09f342c3d66d -r 371af3cb2f8e sys/nfs/nfs_kq.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/nfs/nfs_kq.c  Mon Sep 30 20:43:39 2002 +0000
@@ -0,0 +1,329 @@
+/*     $NetBSD: nfs_kq.c,v 1.1.2.1 2002/09/30 20:43:43 jdolecek 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>
+__KERNEL_RCSID(0, "$NetBSD: nfs_kq.c,v 1.1.2.1 2002/09/30 20:43:43 jdolecek Exp $");
+
+#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_MINATTRTIME/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;
+       int error;
+       struct proc *p = pnfskq;
+       u_quad_t osize;
+
+       for(;;) {
+               lockmgr(&nfskevq_lock, LK_EXCLUSIVE, NULL);
+               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;
+                       lockmgr(&nfskevq_lock, LK_RELEASE, NULL);
+
+                       /* save v_size, nfs_getattr() updates it */
+                       osize = ke->vp->v_size;
+
+                       error = 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);
+                       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);
+                       kthread_exit(0);
+               }
+               lockmgr(&nfskevq_lock, LK_RELEASE, NULL);
+
+               /* 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;
+
+       /* XXXLUKEM lock the struct? */
+       SLIST_REMOVE(&vp->v_klist, kn, knote, kn_selnext);
+
+       /* Remove the vnode from watch list */
+       lockmgr(&nfskevq_lock, LK_EXCLUSIVE, NULL);
+       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);
+                               (void) tsleep(ke, PSOCK, "nfskqdet", 0);
+                               lockmgr(&nfskevq_lock, LK_EXCLUSIVE, NULL);
+                       }
+
+                       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);
+}
+
+static int
+filt_nfsread(struct knote *kn, long hint)
+{
+       struct vnode *vp = (struct vnode *)kn->kn_hook;
+
+       /*
+        * 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? */
+       /* XXXLUKEM (jdolecek): clipping to 32bit value */
+       kn->kn_data = vp->v_size - kn->kn_fp->f_offset;
+        return (kn->kn_data != 0);
+}
+



Home | Main Index | Thread Index | Old Index