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