The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/nfs/nfs_kq.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: nfs_kq.c,v 1.7 2003/10/30 01:43:10 simonb Exp $        */
    2 
    3 /*-
    4  * Copyright (c) 2002 The NetBSD Foundation, Inc.
    5  * All rights reserved.
    6  *
    7  * This code is derived from software contributed to The NetBSD Foundation
    8  * by Jaromir Dolecek.
    9  *
   10  * Redistribution and use in source and binary forms, with or without
   11  * modification, are permitted provided that the following conditions
   12  * are met:
   13  * 1. Redistributions of source code must retain the above copyright
   14  *    notice, this list of conditions and the following disclaimer.
   15  * 2. Redistributions in binary form must reproduce the above copyright
   16  *    notice, this list of conditions and the following disclaimer in the
   17  *    documentation and/or other materials provided with the distribution.
   18  * 3. All advertising materials mentioning features or use of this software
   19  *    must display the following acknowledgement:
   20  *        This product includes software developed by the NetBSD
   21  *        Foundation, Inc. and its contributors.
   22  * 4. Neither the name of The NetBSD Foundation nor the names of its
   23  *    contributors may be used to endorse or promote products derived
   24  *    from this software without specific prior written permission.
   25  *
   26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   36  * POSSIBILITY OF SUCH DAMAGE.
   37  */
   38 
   39 #include <sys/cdefs.h>
   40 __KERNEL_RCSID(0, "$NetBSD: nfs_kq.c,v 1.7 2003/10/30 01:43:10 simonb Exp $");
   41 
   42 #include <sys/param.h>
   43 #include <sys/systm.h>
   44 #include <sys/kernel.h>
   45 #include <sys/proc.h>
   46 #include <sys/mount.h>
   47 #include <sys/malloc.h>
   48 #include <sys/vnode.h>
   49 #include <sys/unistd.h>
   50 #include <sys/file.h>
   51 #include <sys/kthread.h>
   52 
   53 #include <uvm/uvm_extern.h>
   54 #include <uvm/uvm.h>
   55 
   56 #include <nfs/rpcv2.h>
   57 #include <nfs/nfsproto.h>
   58 #include <nfs/nfs.h>
   59 #include <nfs/nfsnode.h>
   60 #include <nfs/nfs_var.h>
   61 
   62 struct kevq {
   63         SLIST_ENTRY(kevq)       kev_link;
   64         struct vnode            *vp;
   65         u_int                   usecount;
   66         u_int                   flags;
   67 #define KEVQ_BUSY       0x01    /* currently being processed */
   68 #define KEVQ_WANT       0x02    /* want to change this entry */
   69         struct timespec         omtime; /* old modification time */
   70         struct timespec         octime; /* old change time */
   71         nlink_t                 onlink; /* old number of references to file */
   72 };
   73 SLIST_HEAD(kevqlist, kevq);
   74 
   75 static struct lock nfskevq_lock;
   76 static struct proc *pnfskq;
   77 static struct kevqlist kevlist = SLIST_HEAD_INITIALIZER(kevlist);
   78 
   79 void
   80 nfs_kqinit(void)
   81 {
   82         lockinit(&nfskevq_lock, PSOCK, "nfskqlck", 0, 0);
   83 }
   84 
   85 /*
   86  * This quite simplistic routine periodically checks for server changes
   87  * of any of the watched files every NFS_MINATTRTIMO/2 seconds.
   88  * Only changes in size, modification time, change time and nlinks
   89  * are being checked, everything else is ignored.
   90  * The routine only calls VOP_GETATTR() when it's likely it would get
   91  * some new data, i.e. when the vnode expires from attrcache. This
   92  * should give same result as periodically running stat(2) from userland,
   93  * while keeping CPU/network usage low, and still provide proper kevent
   94  * semantics.
   95  * The poller thread is created when first vnode is added to watch list,
   96  * and exits when the watch list is empty. The overhead of thread creation
   97  * isn't really important, neither speed of attach and detach of knote.
   98  */
   99 /* ARGSUSED */
  100 static void
  101 nfs_kqpoll(void *arg)
  102 {
  103         struct kevq *ke;
  104         struct vattr attr;
  105         struct proc *p = pnfskq;
  106         u_quad_t osize;
  107 
  108         for(;;) {
  109                 lockmgr(&nfskevq_lock, LK_EXCLUSIVE, NULL);
  110                 SLIST_FOREACH(ke, &kevlist, kev_link) {
  111                         /* skip if still in attrcache */
  112                         if (nfs_getattrcache(ke->vp, &attr) != ENOENT)
  113                                 continue;
  114 
  115                         /*
  116                          * Mark entry busy, release lock and check
  117                          * for changes.
  118                          */
  119                         ke->flags |= KEVQ_BUSY;
  120                         lockmgr(&nfskevq_lock, LK_RELEASE, NULL);
  121 
  122                         /* save v_size, nfs_getattr() updates it */
  123                         osize = ke->vp->v_size;
  124 
  125                         (void) VOP_GETATTR(ke->vp, &attr, p->p_ucred, p);
  126 
  127                         /* following is a bit fragile, but about best
  128                          * we can get */
  129                         if (attr.va_size != osize) {
  130                                 int extended = (attr.va_size > osize);
  131                                 VN_KNOTE(ke->vp, NOTE_WRITE
  132                                         | (extended ? NOTE_EXTEND : 0));
  133                                 ke->omtime = attr.va_mtime;
  134                         } else if (attr.va_mtime.tv_sec != ke->omtime.tv_sec
  135                             || attr.va_mtime.tv_nsec != ke->omtime.tv_nsec) {
  136                                 VN_KNOTE(ke->vp, NOTE_WRITE);
  137                                 ke->omtime = attr.va_mtime;
  138                         }
  139 
  140                         if (attr.va_ctime.tv_sec != ke->octime.tv_sec
  141                             || attr.va_ctime.tv_nsec != ke->octime.tv_nsec) {
  142                                 VN_KNOTE(ke->vp, NOTE_ATTRIB);
  143                                 ke->octime = attr.va_ctime;
  144                         }
  145 
  146                         if (attr.va_nlink != ke->onlink) {
  147                                 VN_KNOTE(ke->vp, NOTE_LINK);
  148                                 ke->onlink = attr.va_nlink;
  149                         }
  150 
  151                         lockmgr(&nfskevq_lock, LK_EXCLUSIVE, NULL);
  152                         ke->flags &= ~KEVQ_BUSY;
  153                         if (ke->flags & KEVQ_WANT) {
  154                                 ke->flags &= ~KEVQ_WANT;
  155                                 wakeup(ke);
  156                         }
  157                 }
  158 
  159                 if (SLIST_EMPTY(&kevlist)) {
  160                         /* Nothing more to watch, exit */
  161                         pnfskq = NULL;
  162                         lockmgr(&nfskevq_lock, LK_RELEASE, NULL);
  163                         kthread_exit(0);
  164                 }
  165                 lockmgr(&nfskevq_lock, LK_RELEASE, NULL);
  166 
  167                 /* wait a while before checking for changes again */
  168                 tsleep(pnfskq, PSOCK, "nfskqpw",
  169                         NFS_MINATTRTIMO * hz / 2);
  170 
  171         }
  172 }
  173 
  174 static void
  175 filt_nfsdetach(struct knote *kn)
  176 {
  177         struct vnode *vp = (struct vnode *)kn->kn_hook;
  178         struct kevq *ke;
  179 
  180         /* XXXLUKEM lock the struct? */
  181         SLIST_REMOVE(&vp->v_klist, kn, knote, kn_selnext);
  182 
  183         /* Remove the vnode from watch list */
  184         lockmgr(&nfskevq_lock, LK_EXCLUSIVE, NULL);
  185         SLIST_FOREACH(ke, &kevlist, kev_link) {
  186                 if (ke->vp == vp) {
  187                         while (ke->flags & KEVQ_BUSY) {
  188                                 ke->flags |= KEVQ_WANT;
  189                                 lockmgr(&nfskevq_lock, LK_RELEASE, NULL);
  190                                 (void) tsleep(ke, PSOCK, "nfskqdet", 0);
  191                                 lockmgr(&nfskevq_lock, LK_EXCLUSIVE, NULL);
  192                         }
  193 
  194                         if (ke->usecount > 1) {
  195                                 /* keep, other kevents need this */
  196                                 ke->usecount--;
  197                         } else {
  198                                 /* last user, g/c */
  199                                 SLIST_REMOVE(&kevlist, ke, kevq, kev_link);
  200                                 FREE(ke, M_KEVENT);
  201                         }
  202                         break;
  203                 }
  204         }
  205         lockmgr(&nfskevq_lock, LK_RELEASE, NULL);
  206 }
  207 
  208 static int
  209 filt_nfsread(struct knote *kn, long hint)
  210 {
  211         struct vnode *vp = (struct vnode *)kn->kn_hook;
  212 
  213         /*
  214          * filesystem is gone, so set the EOF flag and schedule
  215          * the knote for deletion.
  216          */
  217         if (hint == NOTE_REVOKE) {
  218                 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
  219                 return (1);
  220         }
  221 
  222         /* XXXLUKEM lock the struct? */
  223         kn->kn_data = vp->v_size - kn->kn_fp->f_offset;
  224         return (kn->kn_data != 0);
  225 }
  226 
  227 static int
  228 filt_nfsvnode(struct knote *kn, long hint)
  229 {
  230 
  231         if (kn->kn_sfflags & hint)
  232                 kn->kn_fflags |= hint;
  233         if (hint == NOTE_REVOKE) {
  234                 kn->kn_flags |= EV_EOF;
  235                 return (1);
  236         }
  237         return (kn->kn_fflags != 0);
  238 }
  239 
  240 static const struct filterops nfsread_filtops = 
  241         { 1, NULL, filt_nfsdetach, filt_nfsread };
  242 static const struct filterops nfsvnode_filtops = 
  243         { 1, NULL, filt_nfsdetach, filt_nfsvnode };
  244 
  245 int
  246 nfs_kqfilter(void *v)
  247 {
  248         struct vop_kqfilter_args /* {
  249                 struct vnode    *a_vp;
  250                 struct knote    *a_kn;
  251         } */ *ap = v;
  252         struct vnode *vp;
  253         struct knote *kn;
  254         struct kevq *ke;
  255         int error = 0;
  256         struct vattr attr;
  257         struct proc *p = curproc;       /* XXX */
  258 
  259         vp = ap->a_vp;
  260         kn = ap->a_kn;
  261         switch (kn->kn_filter) {
  262         case EVFILT_READ:
  263                 kn->kn_fop = &nfsread_filtops;
  264                 break;
  265         case EVFILT_VNODE:
  266                 kn->kn_fop = &nfsvnode_filtops;
  267                 break;
  268         default:
  269                 return (1);
  270         }
  271 
  272         kn->kn_hook = vp;
  273 
  274         /*
  275          * Put the vnode to watched list.
  276          */
  277         
  278         /*
  279          * Fetch current attributes. It's only needed when the vnode
  280          * is not watched yet, but we need to do this without lock
  281          * held. This is likely cheap due to attrcache, so do it now.
  282          */ 
  283         memset(&attr, 0, sizeof(attr));
  284         (void) VOP_GETATTR(vp, &attr, p->p_ucred, p);
  285 
  286         lockmgr(&nfskevq_lock, LK_EXCLUSIVE, NULL);
  287 
  288         /* ensure the poller is running */
  289         if (!pnfskq) {
  290                 error = kthread_create1(nfs_kqpoll, NULL, &pnfskq,
  291                                 "nfskqpoll");
  292                 if (error)
  293                         goto out;
  294         }
  295 
  296         SLIST_FOREACH(ke, &kevlist, kev_link) {
  297                 if (ke->vp == vp)
  298                         break;
  299         }
  300 
  301         if (ke) {
  302                 /* already watched, so just bump usecount */
  303                 ke->usecount++;
  304         } else {
  305                 /* need a new one */
  306                 MALLOC(ke, struct kevq *, sizeof(struct kevq), M_KEVENT,
  307                         M_WAITOK);
  308                 ke->vp = vp;
  309                 ke->usecount = 1;
  310                 ke->flags = 0;
  311                 ke->omtime = attr.va_mtime;
  312                 ke->octime = attr.va_ctime;
  313                 ke->onlink = attr.va_nlink;
  314                 SLIST_INSERT_HEAD(&kevlist, ke, kev_link);
  315         }
  316 
  317         /* kick the poller */
  318         wakeup(pnfskq);
  319 
  320         /* XXXLUKEM lock the struct? */
  321         SLIST_INSERT_HEAD(&vp->v_klist, kn, kn_selnext);
  322 
  323     out:
  324         lockmgr(&nfskevq_lock, LK_RELEASE, NULL);
  325 
  326         return (error);
  327 }

Cache object: 1f6a5e5ee44208f728efea3b5ca51749


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.