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