xref: /openbsd/sys/nfs/nfs_kq.c (revision 564dba15)
1 /*	$OpenBSD: nfs_kq.c,v 1.37 2024/05/01 13:15:59 jsg 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  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/proc.h>
36 #include <sys/mount.h>
37 #include <sys/malloc.h>
38 #include <sys/vnode.h>
39 #include <sys/file.h>
40 #include <sys/kthread.h>
41 #include <sys/rwlock.h>
42 #include <sys/queue.h>
43 
44 #include <nfs/nfsproto.h>
45 #include <nfs/nfs.h>
46 #include <nfs/nfsnode.h>
47 #include <nfs/nfs_var.h>
48 
49 void	nfs_kqpoll(void *);
50 int	nfs_kqwatch(struct vnode *);
51 void	nfs_kqunwatch(struct vnode *);
52 
53 void	filt_nfsdetach(struct knote *);
54 int	filt_nfsread(struct knote *, long);
55 int	filt_nfswrite(struct knote *, long);
56 int	filt_nfsvnode(struct knote *, long);
57 
58 struct kevq {
59 	SLIST_ENTRY(kevq)	kev_link;
60 	struct vnode		*vp;
61 	u_int			usecount;
62 	u_int			flags;
63 #define KEVQ_BUSY	0x01	/* currently being processed */
64 #define KEVQ_WANT	0x02	/* want to change this entry */
65 	struct timespec		omtime;	/* old modification time */
66 	struct timespec		octime;	/* old change time */
67 	nlink_t			onlink;	/* old number of references to file */
68 };
69 SLIST_HEAD(kevqlist, kevq);
70 
71 struct rwlock nfskevq_lock = RWLOCK_INITIALIZER("nfskqlk");
72 struct proc *pnfskq;
73 struct kevqlist kevlist = SLIST_HEAD_INITIALIZER(kevlist);
74 
75 /*
76  * This quite simplistic routine periodically checks for server changes
77  * of any of the watched files every NFS_MINATTRTIMO/2 seconds.
78  * Only changes in size, modification time, change time and nlinks
79  * are being checked, everything else is ignored.
80  * The routine only calls VOP_GETATTR() when it's likely it would get
81  * some new data, i.e. when the vnode expires from attrcache. This
82  * should give same result as periodically running stat(2) from userland,
83  * while keeping CPU/network usage low, and still provide proper kevent
84  * semantics.
85  * The poller thread is created when first vnode is added to watch list,
86  * and exits when the watch list is empty. The overhead of thread creation
87  * isn't really important, neither speed of attach and detach of knote.
88  */
89 void
nfs_kqpoll(void * arg)90 nfs_kqpoll(void *arg)
91 {
92 	struct kevq *ke;
93 	struct vattr attr;
94 	struct proc *p = pnfskq;
95 	u_quad_t osize;
96 	int error;
97 
98 	for(;;) {
99 		rw_enter_write(&nfskevq_lock);
100 		SLIST_FOREACH(ke, &kevlist, kev_link) {
101 			struct nfsnode *np = VTONFS(ke->vp);
102 
103 #ifdef DEBUG
104 			printf("nfs_kqpoll on: ");
105 			VOP_PRINT(ke->vp);
106 #endif
107 			/* skip if still in attrcache */
108 			if (nfs_getattrcache(ke->vp, &attr) != ENOENT)
109 				continue;
110 
111 			/*
112 			 * Mark entry busy, release lock and check
113 			 * for changes.
114 			 */
115 			ke->flags |= KEVQ_BUSY;
116 			rw_exit_write(&nfskevq_lock);
117 
118 			/* save v_size, nfs_getattr() updates it */
119 			osize = np->n_size;
120 
121 			error = VOP_GETATTR(ke->vp, &attr, p->p_ucred, p);
122 			if (error == ESTALE) {
123 				NFS_INVALIDATE_ATTRCACHE(np);
124 				VN_KNOTE(ke->vp, NOTE_DELETE);
125 				goto next;
126 			}
127 
128 			/* following is a bit fragile, but about best
129 			 * we can get */
130 			if (attr.va_size != osize) {
131 				int flags = NOTE_WRITE;
132 
133 				if (attr.va_size > osize)
134 					flags |= NOTE_EXTEND;
135 				else
136 					flags |= NOTE_TRUNCATE;
137 
138 				VN_KNOTE(ke->vp, flags);
139 				ke->omtime = attr.va_mtime;
140 			} else if (attr.va_mtime.tv_sec != ke->omtime.tv_sec
141 			    || attr.va_mtime.tv_nsec != ke->omtime.tv_nsec) {
142 				VN_KNOTE(ke->vp, NOTE_WRITE);
143 				ke->omtime = attr.va_mtime;
144 			}
145 
146 			if (attr.va_ctime.tv_sec != ke->octime.tv_sec
147 			    || attr.va_ctime.tv_nsec != ke->octime.tv_nsec) {
148 				VN_KNOTE(ke->vp, NOTE_ATTRIB);
149 				ke->octime = attr.va_ctime;
150 			}
151 
152 			if (attr.va_nlink != ke->onlink) {
153 				VN_KNOTE(ke->vp, NOTE_LINK);
154 				ke->onlink = attr.va_nlink;
155 			}
156 
157 next:
158 			rw_enter_write(&nfskevq_lock);
159 			ke->flags &= ~KEVQ_BUSY;
160 			if (ke->flags & KEVQ_WANT) {
161 				ke->flags &= ~KEVQ_WANT;
162 				wakeup(ke);
163 			}
164 		}
165 
166 		if (SLIST_EMPTY(&kevlist)) {
167 			/* Nothing more to watch, exit */
168 			pnfskq = NULL;
169 			rw_exit_write(&nfskevq_lock);
170 			kthread_exit(0);
171 		}
172 		rw_exit_write(&nfskevq_lock);
173 
174 		/* wait a while before checking for changes again */
175 		tsleep_nsec(pnfskq, PSOCK, "nfskqpw",
176 		    SEC_TO_NSEC(NFS_MINATTRTIMO) / 2);
177 	}
178 }
179 
180 void
filt_nfsdetach(struct knote * kn)181 filt_nfsdetach(struct knote *kn)
182 {
183 	struct vnode *vp = (struct vnode *)kn->kn_hook;
184 
185 	klist_remove_locked(&vp->v_klist, kn);
186 
187 	/* Remove the vnode from watch list */
188 	if ((kn->kn_flags & (__EV_POLL | __EV_SELECT)) == 0)
189 		nfs_kqunwatch(vp);
190 }
191 
192 void
nfs_kqunwatch(struct vnode * vp)193 nfs_kqunwatch(struct vnode *vp)
194 {
195 	struct kevq *ke;
196 
197 	rw_enter_write(&nfskevq_lock);
198 	SLIST_FOREACH(ke, &kevlist, kev_link) {
199 		if (ke->vp == vp) {
200 			while (ke->flags & KEVQ_BUSY) {
201 				ke->flags |= KEVQ_WANT;
202 				rw_exit_write(&nfskevq_lock);
203 				tsleep_nsec(ke, PSOCK, "nfskqdet", INFSLP);
204 				rw_enter_write(&nfskevq_lock);
205 			}
206 
207 			if (ke->usecount > 1) {
208 				/* keep, other kevents need this */
209 				ke->usecount--;
210 			} else {
211 				/* last user, g/c */
212 				SLIST_REMOVE(&kevlist, ke, kevq, kev_link);
213 				free(ke, M_KEVENT, sizeof(*ke));
214 			}
215 			break;
216 		}
217 	}
218 	rw_exit_write(&nfskevq_lock);
219 }
220 
221 int
filt_nfsread(struct knote * kn,long hint)222 filt_nfsread(struct knote *kn, long hint)
223 {
224 	struct vnode *vp = (struct vnode *)kn->kn_hook;
225 	struct nfsnode *np = VTONFS(vp);
226 
227 	/*
228 	 * filesystem is gone, so set the EOF flag and schedule
229 	 * the knote for deletion.
230 	 */
231 	if (hint == NOTE_REVOKE) {
232 		kn->kn_flags |= (EV_EOF | EV_ONESHOT);
233 		return (1);
234 	}
235 
236 	kn->kn_data = np->n_size - foffset(kn->kn_fp);
237 #ifdef DEBUG
238 	printf("nfsread event. %lld\n", kn->kn_data);
239 #endif
240 	if (kn->kn_data == 0 && kn->kn_sfflags & NOTE_EOF) {
241 		kn->kn_fflags |= NOTE_EOF;
242 		return (1);
243 	}
244 
245 	if (kn->kn_flags & (__EV_POLL | __EV_SELECT))
246 		return (1);
247 
248         return (kn->kn_data != 0);
249 }
250 
251 int
filt_nfswrite(struct knote * kn,long hint)252 filt_nfswrite(struct knote *kn, long hint)
253 {
254 	/*
255 	 * filesystem is gone, so set the EOF flag and schedule
256 	 * the knote for deletion.
257 	 */
258 	if (hint == NOTE_REVOKE) {
259 		kn->kn_flags |= (EV_EOF | EV_ONESHOT);
260 		return (1);
261 	}
262 
263 	kn->kn_data = 0;
264 	return (1);
265 }
266 
267 int
filt_nfsvnode(struct knote * kn,long hint)268 filt_nfsvnode(struct knote *kn, long hint)
269 {
270 	if (kn->kn_sfflags & hint)
271 		kn->kn_fflags |= hint;
272 	if (hint == NOTE_REVOKE) {
273 		kn->kn_flags |= EV_EOF;
274 		return (1);
275 	}
276 	return (kn->kn_fflags != 0);
277 }
278 
279 static const struct filterops nfsread_filtops = {
280 	.f_flags	= FILTEROP_ISFD,
281 	.f_attach	= NULL,
282 	.f_detach	= filt_nfsdetach,
283 	.f_event	= filt_nfsread,
284 };
285 
286 static const struct filterops nfswrite_filtops = {
287 	.f_flags	= FILTEROP_ISFD,
288 	.f_attach	= NULL,
289 	.f_detach	= filt_nfsdetach,
290 	.f_event	= filt_nfswrite,
291 };
292 
293 static const struct filterops nfsvnode_filtops = {
294 	.f_flags	= FILTEROP_ISFD,
295 	.f_attach	= NULL,
296 	.f_detach	= filt_nfsdetach,
297 	.f_event	= filt_nfsvnode,
298 };
299 
300 int
nfs_kqfilter(void * v)301 nfs_kqfilter(void *v)
302 {
303 	struct vop_kqfilter_args *ap = v;
304 	struct vnode *vp;
305 	struct knote *kn;
306 
307 	vp = ap->a_vp;
308 	kn = ap->a_kn;
309 
310 #ifdef DEBUG
311 	printf("nfs_kqfilter(%d) on: ", kn->kn_filter);
312 	VOP_PRINT(vp);
313 #endif
314 
315 	switch (kn->kn_filter) {
316 	case EVFILT_READ:
317 		kn->kn_fop = &nfsread_filtops;
318 		break;
319 	case EVFILT_WRITE:
320 		kn->kn_fop = &nfswrite_filtops;
321 		break;
322 	case EVFILT_VNODE:
323 		kn->kn_fop = &nfsvnode_filtops;
324 		break;
325 	default:
326 		return (EINVAL);
327 	}
328 
329 	kn->kn_hook = vp;
330 
331 	/*
332 	 * Put the vnode to watched list.
333 	 */
334 	if ((kn->kn_flags & (__EV_POLL | __EV_SELECT)) == 0) {
335 		int error;
336 
337 		error = nfs_kqwatch(vp);
338 		if (error)
339 			return (error);
340 	}
341 
342 	klist_insert_locked(&vp->v_klist, kn);
343 
344 	return (0);
345 }
346 
347 int
nfs_kqwatch(struct vnode * vp)348 nfs_kqwatch(struct vnode *vp)
349 {
350 	struct proc *p = curproc;	/* XXX */
351 	struct vattr attr;
352 	struct kevq *ke;
353 	int error = 0;
354 
355 	/*
356 	 * Fetch current attributes. It's only needed when the vnode
357 	 * is not watched yet, but we need to do this without lock
358 	 * held. This is likely cheap due to attrcache, so do it now.
359 	 */
360 	memset(&attr, 0, sizeof(attr));
361 	(void) VOP_GETATTR(vp, &attr, p->p_ucred, p);
362 
363 	rw_enter_write(&nfskevq_lock);
364 
365 	/* ensure the poller is running */
366 	if (!pnfskq) {
367 		error = kthread_create(nfs_kqpoll, NULL, &pnfskq,
368 				"nfskqpoll");
369 		if (error)
370 			goto out;
371 	}
372 
373 	SLIST_FOREACH(ke, &kevlist, kev_link)
374 		if (ke->vp == vp)
375 			break;
376 
377 	if (ke) {
378 		/* already watched, so just bump usecount */
379 		ke->usecount++;
380 	} else {
381 		/* need a new one */
382 		ke = malloc(sizeof(*ke), M_KEVENT, M_WAITOK);
383 		ke->vp = vp;
384 		ke->usecount = 1;
385 		ke->flags = 0;
386 		ke->omtime = attr.va_mtime;
387 		ke->octime = attr.va_ctime;
388 		ke->onlink = attr.va_nlink;
389 		SLIST_INSERT_HEAD(&kevlist, ke, kev_link);
390 	}
391 
392 	/* kick the poller */
393 	wakeup(pnfskq);
394 
395 out:
396 	rw_exit_write(&nfskevq_lock);
397 	return (error);
398 }
399