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