xref: /netbsd-src/sys/nfs/nfs_kq.c (revision 8ac07aec990b9d2e483062509d0a9fa5b4f57cf2)
1 /*	$NetBSD: nfs_kq.c,v 1.21 2008/03/21 21:55:01 ad Exp $	*/
2 
3 /*-
4  * Copyright (c) 2002, 2008 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.21 2008/03/21 21:55:01 ad Exp $");
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/condvar.h>
45 #include <sys/kernel.h>
46 #include <sys/proc.h>
47 #include <sys/kmem.h>
48 #include <sys/mount.h>
49 #include <sys/mutex.h>
50 #include <sys/vnode.h>
51 #include <sys/unistd.h>
52 #include <sys/file.h>
53 #include <sys/kthread.h>
54 
55 #include <uvm/uvm_extern.h>
56 #include <uvm/uvm.h>
57 
58 #include <nfs/rpcv2.h>
59 #include <nfs/nfsproto.h>
60 #include <nfs/nfs.h>
61 #include <nfs/nfsnode.h>
62 #include <nfs/nfs_var.h>
63 
64 struct kevq {
65 	SLIST_ENTRY(kevq)	kev_link;
66 	struct vnode		*vp;
67 	u_int			usecount;
68 	u_int			flags;
69 #define KEVQ_BUSY	0x01	/* currently being processed */
70 	struct timespec		omtime;	/* old modification time */
71 	struct timespec		octime;	/* old change time */
72 	nlink_t			onlink;	/* old number of references to file */
73 	kcondvar_t		cv;
74 };
75 SLIST_HEAD(kevqlist, kevq);
76 
77 static kmutex_t nfskevq_lock;
78 static struct lwp *nfskq_thread;
79 static kcondvar_t nfskq_cv;
80 static struct kevqlist kevlist = SLIST_HEAD_INITIALIZER(kevlist);
81 
82 void
83 nfs_kqinit(void)
84 {
85 
86 	mutex_init(&nfskevq_lock, MUTEX_DEFAULT, IPL_NONE);
87 	cv_init(&nfskq_cv, "nfskqpw");
88 }
89 
90 /*
91  * This quite simplistic routine periodically checks for server changes
92  * of any of the watched files every NFS_MINATTRTIMO/2 seconds.
93  * Only changes in size, modification time, change time and nlinks
94  * are being checked, everything else is ignored.
95  * The routine only calls VOP_GETATTR() when it's likely it would get
96  * some new data, i.e. when the vnode expires from attrcache. This
97  * should give same result as periodically running stat(2) from userland,
98  * while keeping CPU/network usage low, and still provide proper kevent
99  * semantics.
100  * The poller thread is created when first vnode is added to watch list,
101  * and exits when the watch list is empty. The overhead of thread creation
102  * isn't really important, neither speed of attach and detach of knote.
103  */
104 /* ARGSUSED */
105 static void
106 nfs_kqpoll(void *arg)
107 {
108 	struct kevq *ke;
109 	struct vattr attr;
110 	struct lwp *l = curlwp;
111 	u_quad_t osize;
112 
113 	mutex_enter(&nfskevq_lock);
114 	for (;;) {
115 		SLIST_FOREACH(ke, &kevlist, kev_link) {
116 			/* skip if still in attrcache */
117 			if (nfs_getattrcache(ke->vp, &attr) != ENOENT)
118 				continue;
119 
120 			/*
121 			 * Mark entry busy, release lock and check
122 			 * for changes.
123 			 */
124 			ke->flags |= KEVQ_BUSY;
125 			mutex_exit(&nfskevq_lock);
126 
127 			/* save v_size, nfs_getattr() updates it */
128 			osize = ke->vp->v_size;
129 
130 			(void) VOP_GETATTR(ke->vp, &attr, l->l_cred);
131 
132 			/* following is a bit fragile, but about best
133 			 * we can get */
134 			if (attr.va_size != osize) {
135 				int extended = (attr.va_size > osize);
136 				VN_KNOTE(ke->vp, NOTE_WRITE
137 					| (extended ? NOTE_EXTEND : 0));
138 				ke->omtime = attr.va_mtime;
139 			} else if (attr.va_mtime.tv_sec != ke->omtime.tv_sec
140 			    || attr.va_mtime.tv_nsec != ke->omtime.tv_nsec) {
141 				VN_KNOTE(ke->vp, NOTE_WRITE);
142 				ke->omtime = attr.va_mtime;
143 			}
144 
145 			if (attr.va_ctime.tv_sec != ke->octime.tv_sec
146 			    || attr.va_ctime.tv_nsec != ke->octime.tv_nsec) {
147 				VN_KNOTE(ke->vp, NOTE_ATTRIB);
148 				ke->octime = attr.va_ctime;
149 			}
150 
151 			if (attr.va_nlink != ke->onlink) {
152 				VN_KNOTE(ke->vp, NOTE_LINK);
153 				ke->onlink = attr.va_nlink;
154 			}
155 
156 			mutex_enter(&nfskevq_lock);
157 			ke->flags &= ~KEVQ_BUSY;
158 			cv_signal(&ke->cv);
159 		}
160 
161 		if (SLIST_EMPTY(&kevlist)) {
162 			/* Nothing more to watch, exit */
163 			nfskq_thread = NULL;
164 			mutex_exit(&nfskevq_lock);
165 			kthread_exit(0);
166 		}
167 
168 		/* wait a while before checking for changes again */
169 		cv_timedwait(&nfskq_cv, &nfskevq_lock,
170 		    NFS_MINATTRTIMO * hz / 2);
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 	mutex_enter(&vp->v_interlock);
181 	SLIST_REMOVE(&vp->v_klist, kn, knote, kn_selnext);
182 	mutex_exit(&vp->v_interlock);
183 
184 	/* Remove the vnode from watch list */
185 	mutex_enter(&nfskevq_lock);
186 	SLIST_FOREACH(ke, &kevlist, kev_link) {
187 		if (ke->vp == vp) {
188 			while (ke->flags & KEVQ_BUSY) {
189 				cv_wait(&ke->cv, &nfskevq_lock);
190 			}
191 
192 			if (ke->usecount > 1) {
193 				/* keep, other kevents need this */
194 				ke->usecount--;
195 			} else {
196 				/* last user, g/c */
197 				cv_destroy(&ke->cv);
198 				SLIST_REMOVE(&kevlist, ke, kevq, kev_link);
199 				kmem_free(ke, sizeof(*ke));
200 			}
201 			break;
202 		}
203 	}
204 	mutex_exit(&nfskevq_lock);
205 }
206 
207 static int
208 filt_nfsread(struct knote *kn, long hint)
209 {
210 	struct vnode *vp = (struct vnode *)kn->kn_hook;
211 	int rv;
212 
213 	/*
214 	 * filesystem is gone, so set the EOF flag and schedule
215 	 * the knote for deletion.
216 	 */
217 	switch (hint) {
218 	case NOTE_REVOKE:
219 		KASSERT(mutex_owned(&vp->v_interlock));
220 		kn->kn_flags |= (EV_EOF | EV_ONESHOT);
221 		return (1);
222 	case 0:
223 		mutex_enter(&vp->v_interlock);
224 		kn->kn_data = vp->v_size - ((file_t *)kn->kn_obj)->f_offset;
225 		rv = (kn->kn_data != 0);
226 		mutex_exit(&vp->v_interlock);
227 		return rv;
228 	default:
229 		KASSERT(mutex_owned(&vp->v_interlock));
230 		kn->kn_data = vp->v_size - ((file_t *)kn->kn_obj)->f_offset;
231 		return (kn->kn_data != 0);
232 	}
233 }
234 
235 static int
236 filt_nfsvnode(struct knote *kn, long hint)
237 {
238 	struct vnode *vp = (struct vnode *)kn->kn_hook;
239 	int fflags;
240 
241 	switch (hint) {
242 	case NOTE_REVOKE:
243 		KASSERT(mutex_owned(&vp->v_interlock));
244 		kn->kn_flags |= EV_EOF;
245 		if ((kn->kn_sfflags & hint) != 0)
246 			kn->kn_fflags |= hint;
247 		return (1);
248 	case 0:
249 		mutex_enter(&vp->v_interlock);
250 		fflags = kn->kn_fflags;
251 		mutex_exit(&vp->v_interlock);
252 		break;
253 	default:
254 		KASSERT(mutex_owned(&vp->v_interlock));
255 		if ((kn->kn_sfflags & hint) != 0)
256 			kn->kn_fflags |= hint;
257 		fflags = kn->kn_fflags;
258 		break;
259 	}
260 
261 	return (fflags != 0);
262 }
263 
264 
265 static const struct filterops nfsread_filtops =
266 	{ 1, NULL, filt_nfsdetach, filt_nfsread };
267 static const struct filterops nfsvnode_filtops =
268 	{ 1, NULL, filt_nfsdetach, filt_nfsvnode };
269 
270 int
271 nfs_kqfilter(void *v)
272 {
273 	struct vop_kqfilter_args /* {
274 		struct vnode	*a_vp;
275 		struct knote	*a_kn;
276 	} */ *ap = v;
277 	struct vnode *vp;
278 	struct knote *kn;
279 	struct kevq *ke;
280 	int error = 0;
281 	struct vattr attr;
282 	struct lwp *l = curlwp;
283 
284 	vp = ap->a_vp;
285 	kn = ap->a_kn;
286 	switch (kn->kn_filter) {
287 	case EVFILT_READ:
288 		kn->kn_fop = &nfsread_filtops;
289 		break;
290 	case EVFILT_VNODE:
291 		kn->kn_fop = &nfsvnode_filtops;
292 		break;
293 	default:
294 		return (EINVAL);
295 	}
296 
297 	/*
298 	 * Put the vnode to watched list.
299 	 */
300 
301 	/*
302 	 * Fetch current attributes. It's only needed when the vnode
303 	 * is not watched yet, but we need to do this without lock
304 	 * held. This is likely cheap due to attrcache, so do it now.
305 	 */
306 	memset(&attr, 0, sizeof(attr));
307 	(void) VOP_GETATTR(vp, &attr, l->l_cred);
308 
309 	mutex_enter(&nfskevq_lock);
310 
311 	/* ensure the poller is running */
312 	if (!nfskq_thread) {
313 		error = kthread_create(PRI_NONE, 0, NULL, nfs_kqpoll,
314 		    NULL, &nfskq_thread, "nfskqpoll");
315 		if (error) {
316 			mutex_exit(&nfskevq_lock);
317 			return error;
318 		}
319 	}
320 
321 	SLIST_FOREACH(ke, &kevlist, kev_link) {
322 		if (ke->vp == vp)
323 			break;
324 	}
325 
326 	if (ke) {
327 		/* already watched, so just bump usecount */
328 		ke->usecount++;
329 	} else {
330 		/* need a new one */
331 		ke = kmem_alloc(sizeof(*ke), KM_SLEEP);
332 		ke->vp = vp;
333 		ke->usecount = 1;
334 		ke->flags = 0;
335 		ke->omtime = attr.va_mtime;
336 		ke->octime = attr.va_ctime;
337 		ke->onlink = attr.va_nlink;
338 		cv_init(&ke->cv, "nfskqdet");
339 		SLIST_INSERT_HEAD(&kevlist, ke, kev_link);
340 	}
341 
342 	mutex_enter(&vp->v_interlock);
343 	SLIST_INSERT_HEAD(&vp->v_klist, kn, kn_selnext);
344 	kn->kn_hook = vp;
345 	mutex_exit(&vp->v_interlock);
346 
347 	/* kick the poller */
348 	cv_signal(&nfskq_cv);
349 	mutex_exit(&nfskevq_lock);
350 
351 
352 	return (error);
353 }
354