1 /* $NetBSD: nfs_kq.c,v 1.22 2008/04/28 20:24:10 martin Exp $ */
4 * Copyright (c) 2002, 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: nfs_kq.c,v 1.22 2008/04/28 20:24:10 martin Exp $");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/condvar.h>
38 #include <sys/kernel.h>
41 #include <sys/mount.h>
42 #include <sys/mutex.h>
43 #include <sys/vnode.h>
44 #include <sys/unistd.h>
46 #include <sys/kthread.h>
48 #include <uvm/uvm_extern.h>
51 #include <nfs/rpcv2.h>
52 #include <nfs/nfsproto.h>
54 #include <nfs/nfsnode.h>
55 #include <nfs/nfs_var.h>
58 SLIST_ENTRY(kevq
) kev_link
;
62 #define KEVQ_BUSY 0x01 /* currently being processed */
63 struct timespec omtime
; /* old modification time */
64 struct timespec octime
; /* old change time */
65 nlink_t onlink
; /* old number of references to file */
68 SLIST_HEAD(kevqlist
, kevq
);
70 static kmutex_t nfskq_lock
;
71 static struct lwp
*nfskq_thread
;
72 static kcondvar_t nfskq_cv
;
73 static struct kevqlist kevlist
= SLIST_HEAD_INITIALIZER(kevlist
);
74 static bool nfskq_thread_exit
;
80 mutex_init(&nfskq_lock
, MUTEX_DEFAULT
, IPL_NONE
);
81 cv_init(&nfskq_cv
, "nfskqpw");
88 if (nfskq_thread
!= NULL
) {
89 mutex_enter(&nfskq_lock
);
90 nfskq_thread_exit
= true;
91 cv_broadcast(&nfskq_cv
);
93 cv_wait(&nfskq_cv
, &nfskq_lock
);
94 } while (nfskq_thread
!= NULL
);
95 mutex_exit(&nfskq_lock
);
97 mutex_destroy(&nfskq_lock
);
98 cv_destroy(&nfskq_cv
);
102 * This quite simplistic routine periodically checks for server changes
103 * of any of the watched files every NFS_MINATTRTIMO/2 seconds.
104 * Only changes in size, modification time, change time and nlinks
105 * are being checked, everything else is ignored.
106 * The routine only calls VOP_GETATTR() when it's likely it would get
107 * some new data, i.e. when the vnode expires from attrcache. This
108 * should give same result as periodically running stat(2) from userland,
109 * while keeping CPU/network usage low, and still provide proper kevent
111 * The poller thread is created when first vnode is added to watch list,
112 * and exits when the watch list is empty. The overhead of thread creation
113 * isn't really important, neither speed of attach and detach of knote.
117 nfs_kqpoll(void *arg
)
121 struct lwp
*l
= curlwp
;
124 mutex_enter(&nfskq_lock
);
125 while (!nfskq_thread_exit
) {
126 SLIST_FOREACH(ke
, &kevlist
, kev_link
) {
127 /* skip if still in attrcache */
128 if (nfs_getattrcache(ke
->vp
, &attr
) != ENOENT
)
132 * Mark entry busy, release lock and check
135 ke
->flags
|= KEVQ_BUSY
;
136 mutex_exit(&nfskq_lock
);
138 /* save v_size, nfs_getattr() updates it */
139 osize
= ke
->vp
->v_size
;
141 (void) VOP_GETATTR(ke
->vp
, &attr
, l
->l_cred
);
143 /* following is a bit fragile, but about best
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
;
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
;
162 if (attr
.va_nlink
!= ke
->onlink
) {
163 VN_KNOTE(ke
->vp
, NOTE_LINK
);
164 ke
->onlink
= attr
.va_nlink
;
167 mutex_enter(&nfskq_lock
);
168 ke
->flags
&= ~KEVQ_BUSY
;
172 if (SLIST_EMPTY(&kevlist
)) {
173 /* Nothing more to watch, exit */
175 mutex_exit(&nfskq_lock
);
179 /* wait a while before checking for changes again */
180 cv_timedwait(&nfskq_cv
, &nfskq_lock
,
181 NFS_MINATTRTIMO
* hz
/ 2);
184 cv_broadcast(&nfskq_cv
);
185 mutex_exit(&nfskq_lock
);
189 filt_nfsdetach(struct knote
*kn
)
191 struct vnode
*vp
= (struct vnode
*)kn
->kn_hook
;
194 mutex_enter(&vp
->v_interlock
);
195 SLIST_REMOVE(&vp
->v_klist
, kn
, knote
, kn_selnext
);
196 mutex_exit(&vp
->v_interlock
);
198 /* Remove the vnode from watch list */
199 mutex_enter(&nfskq_lock
);
200 SLIST_FOREACH(ke
, &kevlist
, kev_link
) {
202 while (ke
->flags
& KEVQ_BUSY
) {
203 cv_wait(&ke
->cv
, &nfskq_lock
);
206 if (ke
->usecount
> 1) {
207 /* keep, other kevents need this */
212 SLIST_REMOVE(&kevlist
, ke
, kevq
, kev_link
);
213 kmem_free(ke
, sizeof(*ke
));
218 mutex_exit(&nfskq_lock
);
222 filt_nfsread(struct knote
*kn
, long hint
)
224 struct vnode
*vp
= (struct vnode
*)kn
->kn_hook
;
228 * filesystem is gone, so set the EOF flag and schedule
229 * the knote for deletion.
233 KASSERT(mutex_owned(&vp
->v_interlock
));
234 kn
->kn_flags
|= (EV_EOF
| EV_ONESHOT
);
237 mutex_enter(&vp
->v_interlock
);
238 kn
->kn_data
= vp
->v_size
- ((file_t
*)kn
->kn_obj
)->f_offset
;
239 rv
= (kn
->kn_data
!= 0);
240 mutex_exit(&vp
->v_interlock
);
243 KASSERT(mutex_owned(&vp
->v_interlock
));
244 kn
->kn_data
= vp
->v_size
- ((file_t
*)kn
->kn_obj
)->f_offset
;
245 return (kn
->kn_data
!= 0);
250 filt_nfsvnode(struct knote
*kn
, long hint
)
252 struct vnode
*vp
= (struct vnode
*)kn
->kn_hook
;
257 KASSERT(mutex_owned(&vp
->v_interlock
));
258 kn
->kn_flags
|= EV_EOF
;
259 if ((kn
->kn_sfflags
& hint
) != 0)
260 kn
->kn_fflags
|= hint
;
263 mutex_enter(&vp
->v_interlock
);
264 fflags
= kn
->kn_fflags
;
265 mutex_exit(&vp
->v_interlock
);
268 KASSERT(mutex_owned(&vp
->v_interlock
));
269 if ((kn
->kn_sfflags
& hint
) != 0)
270 kn
->kn_fflags
|= hint
;
271 fflags
= kn
->kn_fflags
;
275 return (fflags
!= 0);
279 static const struct filterops nfsread_filtops
=
280 { 1, NULL
, filt_nfsdetach
, filt_nfsread
};
281 static const struct filterops nfsvnode_filtops
=
282 { 1, NULL
, filt_nfsdetach
, filt_nfsvnode
};
285 nfs_kqfilter(void *v
)
287 struct vop_kqfilter_args
/* {
296 struct lwp
*l
= curlwp
;
300 switch (kn
->kn_filter
) {
302 kn
->kn_fop
= &nfsread_filtops
;
305 kn
->kn_fop
= &nfsvnode_filtops
;
312 * Put the vnode to watched list.
316 * Fetch current attributes. It's only needed when the vnode
317 * is not watched yet, but we need to do this without lock
318 * held. This is likely cheap due to attrcache, so do it now.
320 memset(&attr
, 0, sizeof(attr
));
321 (void) VOP_GETATTR(vp
, &attr
, l
->l_cred
);
323 mutex_enter(&nfskq_lock
);
325 /* ensure the poller is running */
327 error
= kthread_create(PRI_NONE
, 0, NULL
, nfs_kqpoll
,
328 NULL
, &nfskq_thread
, "nfskqpoll");
330 mutex_exit(&nfskq_lock
);
335 SLIST_FOREACH(ke
, &kevlist
, kev_link
) {
341 /* already watched, so just bump usecount */
345 ke
= kmem_alloc(sizeof(*ke
), KM_SLEEP
);
349 ke
->omtime
= attr
.va_mtime
;
350 ke
->octime
= attr
.va_ctime
;
351 ke
->onlink
= attr
.va_nlink
;
352 cv_init(&ke
->cv
, "nfskqdet");
353 SLIST_INSERT_HEAD(&kevlist
, ke
, kev_link
);
356 mutex_enter(&vp
->v_interlock
);
357 SLIST_INSERT_HEAD(&vp
->v_klist
, kn
, kn_selnext
);
359 mutex_exit(&vp
->v_interlock
);
361 /* kick the poller */
362 cv_signal(&nfskq_cv
);
363 mutex_exit(&nfskq_lock
);