Home | History | Annotate | Line # | Download | only in nfs
nfs_kq.c revision 1.5
      1 /*	$NetBSD: nfs_kq.c,v 1.5 2003/06/28 14:22:17 darrenr 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.5 2003/06/28 14:22:17 darrenr 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 	int error;
    106 	struct proc *p = pnfskq;
    107 	struct lwp *l = proc_representative_lwp(p);
    108 	u_quad_t osize;
    109 
    110 	for(;;) {
    111 		lockmgr(&nfskevq_lock, LK_EXCLUSIVE, NULL);
    112 		SLIST_FOREACH(ke, &kevlist, kev_link) {
    113 			/* skip if still in attrcache */
    114 			if (nfs_getattrcache(ke->vp, &attr) != ENOENT)
    115 				continue;
    116 
    117 			/*
    118 			 * Mark entry busy, release lock and check
    119 			 * for changes.
    120 			 */
    121 			ke->flags |= KEVQ_BUSY;
    122 			lockmgr(&nfskevq_lock, LK_RELEASE, NULL);
    123 
    124 			/* save v_size, nfs_getattr() updates it */
    125 			osize = ke->vp->v_size;
    126 
    127 			error = VOP_GETATTR(ke->vp, &attr, p->p_ucred, l);
    128 
    129 			/* following is a bit fragile, but about best
    130 			 * we can get */
    131 			if (attr.va_size != osize) {
    132 				int extended = (attr.va_size > osize);
    133 				VN_KNOTE(ke->vp, NOTE_WRITE
    134 					| (extended ? NOTE_EXTEND : 0));
    135 				ke->omtime = attr.va_mtime;
    136 			} else if (attr.va_mtime.tv_sec != ke->omtime.tv_sec
    137 			    || attr.va_mtime.tv_nsec != ke->omtime.tv_nsec) {
    138 				VN_KNOTE(ke->vp, NOTE_WRITE);
    139 				ke->omtime = attr.va_mtime;
    140 			}
    141 
    142 			if (attr.va_ctime.tv_sec != ke->octime.tv_sec
    143 			    || attr.va_ctime.tv_nsec != ke->octime.tv_nsec) {
    144 				VN_KNOTE(ke->vp, NOTE_ATTRIB);
    145 				ke->octime = attr.va_ctime;
    146 			}
    147 
    148 			if (attr.va_nlink != ke->onlink) {
    149 				VN_KNOTE(ke->vp, NOTE_LINK);
    150 				ke->onlink = attr.va_nlink;
    151 			}
    152 
    153 			lockmgr(&nfskevq_lock, LK_EXCLUSIVE, NULL);
    154 			ke->flags &= ~KEVQ_BUSY;
    155 			if (ke->flags & KEVQ_WANT) {
    156 				ke->flags &= ~KEVQ_WANT;
    157 				wakeup(ke);
    158 			}
    159 		}
    160 
    161 		if (SLIST_EMPTY(&kevlist)) {
    162 			/* Nothing more to watch, exit */
    163 			pnfskq = NULL;
    164 			lockmgr(&nfskevq_lock, LK_RELEASE, NULL);
    165 			kthread_exit(0);
    166 		}
    167 		lockmgr(&nfskevq_lock, LK_RELEASE, NULL);
    168 
    169 		/* wait a while before checking for changes again */
    170 		tsleep(pnfskq, PSOCK, "nfskqpw",
    171 			NFS_MINATTRTIMO * hz / 2);
    172 
    173 	}
    174 }
    175 
    176 static void
    177 filt_nfsdetach(struct knote *kn)
    178 {
    179 	struct vnode *vp = (struct vnode *)kn->kn_hook;
    180 	struct kevq *ke;
    181 
    182 	/* XXXLUKEM lock the struct? */
    183 	SLIST_REMOVE(&vp->v_klist, kn, knote, kn_selnext);
    184 
    185 	/* Remove the vnode from watch list */
    186 	lockmgr(&nfskevq_lock, LK_EXCLUSIVE, NULL);
    187 	SLIST_FOREACH(ke, &kevlist, kev_link) {
    188 		if (ke->vp == vp) {
    189 			while (ke->flags & KEVQ_BUSY) {
    190 				ke->flags |= KEVQ_WANT;
    191 				lockmgr(&nfskevq_lock, LK_RELEASE, NULL);
    192 				(void) tsleep(ke, PSOCK, "nfskqdet", 0);
    193 				lockmgr(&nfskevq_lock, LK_EXCLUSIVE, NULL);
    194 			}
    195 
    196 			if (ke->usecount > 1) {
    197 				/* keep, other kevents need this */
    198 				ke->usecount--;
    199 			} else {
    200 				/* last user, g/c */
    201 				SLIST_REMOVE(&kevlist, ke, kevq, kev_link);
    202 				FREE(ke, M_KEVENT);
    203 			}
    204 			break;
    205 		}
    206 	}
    207 	lockmgr(&nfskevq_lock, LK_RELEASE, NULL);
    208 }
    209 
    210 static int
    211 filt_nfsread(struct knote *kn, long hint)
    212 {
    213 	struct vnode *vp = (struct vnode *)kn->kn_hook;
    214 
    215 	/*
    216 	 * filesystem is gone, so set the EOF flag and schedule
    217 	 * the knote for deletion.
    218 	 */
    219 	if (hint == NOTE_REVOKE) {
    220 		kn->kn_flags |= (EV_EOF | EV_ONESHOT);
    221 		return (1);
    222 	}
    223 
    224 	/* XXXLUKEM lock the struct? */
    225 	kn->kn_data = vp->v_size - kn->kn_fp->f_offset;
    226         return (kn->kn_data != 0);
    227 }
    228 
    229 static int
    230 filt_nfsvnode(struct knote *kn, long hint)
    231 {
    232 
    233 	if (kn->kn_sfflags & hint)
    234 		kn->kn_fflags |= hint;
    235 	if (hint == NOTE_REVOKE) {
    236 		kn->kn_flags |= EV_EOF;
    237 		return (1);
    238 	}
    239 	return (kn->kn_fflags != 0);
    240 }
    241 
    242 static const struct filterops nfsread_filtops =
    243 	{ 1, NULL, filt_nfsdetach, filt_nfsread };
    244 static const struct filterops nfsvnode_filtops =
    245 	{ 1, NULL, filt_nfsdetach, filt_nfsvnode };
    246 
    247 int
    248 nfs_kqfilter(void *v)
    249 {
    250 	struct vop_kqfilter_args /* {
    251 		struct vnode	*a_vp;
    252 		struct knote	*a_kn;
    253 	} */ *ap = v;
    254 	struct vnode *vp;
    255 	struct knote *kn;
    256 	struct kevq *ke;
    257 	int error = 0;
    258 	struct vattr attr;
    259 	struct lwp *l = curlwp;		/* XXX */
    260 
    261 	vp = ap->a_vp;
    262 	kn = ap->a_kn;
    263 	switch (kn->kn_filter) {
    264 	case EVFILT_READ:
    265 		kn->kn_fop = &nfsread_filtops;
    266 		break;
    267 	case EVFILT_VNODE:
    268 		kn->kn_fop = &nfsvnode_filtops;
    269 		break;
    270 	default:
    271 		return (1);
    272 	}
    273 
    274 	kn->kn_hook = vp;
    275 
    276 	/*
    277 	 * Put the vnode to watched list.
    278 	 */
    279 
    280 	/*
    281 	 * Fetch current attributes. It's only needed when the vnode
    282 	 * is not watched yet, but we need to do this without lock
    283 	 * held. This is likely cheap due to attrcache, so do it now.
    284 	 */
    285 	memset(&attr, 0, sizeof(attr));
    286 	(void) VOP_GETATTR(vp, &attr, l->l_proc->p_ucred, l);
    287 
    288 	lockmgr(&nfskevq_lock, LK_EXCLUSIVE, NULL);
    289 
    290 	/* ensure the poller is running */
    291 	if (!pnfskq) {
    292 		error = kthread_create1(nfs_kqpoll, NULL, &pnfskq,
    293 				"nfskqpoll");
    294 		if (error)
    295 			goto out;
    296 	}
    297 
    298 	SLIST_FOREACH(ke, &kevlist, kev_link) {
    299 		if (ke->vp == vp)
    300 			break;
    301 	}
    302 
    303 	if (ke) {
    304 		/* already watched, so just bump usecount */
    305 		ke->usecount++;
    306 	} else {
    307 		/* need a new one */
    308 		MALLOC(ke, struct kevq *, sizeof(struct kevq), M_KEVENT,
    309 			M_WAITOK);
    310 		ke->vp = vp;
    311 		ke->usecount = 1;
    312 		ke->flags = 0;
    313 		ke->omtime = attr.va_mtime;
    314 		ke->octime = attr.va_ctime;
    315 		ke->onlink = attr.va_nlink;
    316 		SLIST_INSERT_HEAD(&kevlist, ke, kev_link);
    317 	}
    318 
    319 	/* kick the poller */
    320 	wakeup(pnfskq);
    321 
    322 	/* XXXLUKEM lock the struct? */
    323 	SLIST_INSERT_HEAD(&vp->v_klist, kn, kn_selnext);
    324 
    325     out:
    326 	lockmgr(&nfskevq_lock, LK_RELEASE, NULL);
    327 
    328 	return (error);
    329 }
    330