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