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