1 /* $NetBSD: nfs_kq.c,v 1.17 2007/12/05 17:20:03 pooka Exp $ */
4 * Copyright (c) 2002 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.
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.
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.
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: nfs_kq.c,v 1.17 2007/12/05 17:20:03 pooka Exp $");
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/condvar.h>
45 #include <sys/kernel.h>
48 #include <sys/mount.h>
49 #include <sys/mutex.h>
50 #include <sys/vnode.h>
51 #include <sys/unistd.h>
53 #include <sys/kthread.h>
55 #include <uvm/uvm_extern.h>
58 #include <nfs/rpcv2.h>
59 #include <nfs/nfsproto.h>
61 #include <nfs/nfsnode.h>
62 #include <nfs/nfs_var.h>
65 SLIST_ENTRY(kevq
) kev_link
;
69 #define KEVQ_BUSY 0x01 /* currently being processed */
70 struct timespec omtime
; /* old modification time */
71 struct timespec octime
; /* old change time */
72 nlink_t onlink
; /* old number of references to file */
75 SLIST_HEAD(kevqlist
, kevq
);
77 static kmutex_t nfskevq_lock
;
78 static struct lwp
*nfskq_thread
;
79 static kcondvar_t nfskq_cv
;
80 static struct kevqlist kevlist
= SLIST_HEAD_INITIALIZER(kevlist
);
86 mutex_init(&nfskevq_lock
, MUTEX_DEFAULT
, IPL_NONE
);
87 cv_init(&nfskq_cv
, "nfskqpw");
91 * This quite simplistic routine periodically checks for server changes
92 * of any of the watched files every NFS_MINATTRTIMO/2 seconds.
93 * Only changes in size, modification time, change time and nlinks
94 * are being checked, everything else is ignored.
95 * The routine only calls VOP_GETATTR() when it's likely it would get
96 * some new data, i.e. when the vnode expires from attrcache. This
97 * should give same result as periodically running stat(2) from userland,
98 * while keeping CPU/network usage low, and still provide proper kevent
100 * The poller thread is created when first vnode is added to watch list,
101 * and exits when the watch list is empty. The overhead of thread creation
102 * isn't really important, neither speed of attach and detach of knote.
106 nfs_kqpoll(void *arg
)
110 struct lwp
*l
= curlwp
;
113 mutex_enter(&nfskevq_lock
);
115 SLIST_FOREACH(ke
, &kevlist
, kev_link
) {
116 /* skip if still in attrcache */
117 if (nfs_getattrcache(ke
->vp
, &attr
) != ENOENT
)
121 * Mark entry busy, release lock and check
124 ke
->flags
|= KEVQ_BUSY
;
125 mutex_exit(&nfskevq_lock
);
127 /* save v_size, nfs_getattr() updates it */
128 osize
= ke
->vp
->v_size
;
130 (void) VOP_GETATTR(ke
->vp
, &attr
, l
->l_cred
);
132 /* following is a bit fragile, but about best
134 if (attr
.va_size
!= osize
) {
135 int extended
= (attr
.va_size
> osize
);
136 VN_KNOTE(ke
->vp
, NOTE_WRITE
137 | (extended
? NOTE_EXTEND
: 0));
138 ke
->omtime
= attr
.va_mtime
;
139 } else if (attr
.va_mtime
.tv_sec
!= ke
->omtime
.tv_sec
140 || attr
.va_mtime
.tv_nsec
!= ke
->omtime
.tv_nsec
) {
141 VN_KNOTE(ke
->vp
, NOTE_WRITE
);
142 ke
->omtime
= attr
.va_mtime
;
145 if (attr
.va_ctime
.tv_sec
!= ke
->octime
.tv_sec
146 || attr
.va_ctime
.tv_nsec
!= ke
->octime
.tv_nsec
) {
147 VN_KNOTE(ke
->vp
, NOTE_ATTRIB
);
148 ke
->octime
= attr
.va_ctime
;
151 if (attr
.va_nlink
!= ke
->onlink
) {
152 VN_KNOTE(ke
->vp
, NOTE_LINK
);
153 ke
->onlink
= attr
.va_nlink
;
156 mutex_enter(&nfskevq_lock
);
157 ke
->flags
&= ~KEVQ_BUSY
;
161 if (SLIST_EMPTY(&kevlist
)) {
162 /* Nothing more to watch, exit */
164 mutex_exit(&nfskevq_lock
);
168 /* wait a while before checking for changes again */
169 cv_timedwait(&nfskq_cv
, &nfskevq_lock
,
170 NFS_MINATTRTIMO
* hz
/ 2);
175 filt_nfsdetach(struct knote
*kn
)
177 struct vnode
*vp
= (struct vnode
*)kn
->kn_hook
;
180 /* XXXLUKEM lock the struct? */
181 SLIST_REMOVE(&vp
->v_klist
, kn
, knote
, kn_selnext
);
183 /* Remove the vnode from watch list */
184 mutex_enter(&nfskevq_lock
);
185 SLIST_FOREACH(ke
, &kevlist
, kev_link
) {
187 while (ke
->flags
& KEVQ_BUSY
) {
188 cv_wait(&ke
->cv
, &nfskevq_lock
);
191 if (ke
->usecount
> 1) {
192 /* keep, other kevents need this */
197 SLIST_REMOVE(&kevlist
, ke
, kevq
, kev_link
);
198 kmem_free(ke
, sizeof(*ke
));
203 mutex_exit(&nfskevq_lock
);
207 filt_nfsread(struct knote
*kn
, long hint
)
209 struct vnode
*vp
= (struct vnode
*)kn
->kn_hook
;
212 * filesystem is gone, so set the EOF flag and schedule
213 * the knote for deletion.
215 if (hint
== NOTE_REVOKE
) {
216 kn
->kn_flags
|= (EV_EOF
| EV_ONESHOT
);
220 /* XXXLUKEM lock the struct? */
221 kn
->kn_data
= vp
->v_size
- kn
->kn_fp
->f_offset
;
222 return (kn
->kn_data
!= 0);
226 filt_nfsvnode(struct knote
*kn
, long hint
)
229 if (kn
->kn_sfflags
& hint
)
230 kn
->kn_fflags
|= hint
;
231 if (hint
== NOTE_REVOKE
) {
232 kn
->kn_flags
|= EV_EOF
;
235 return (kn
->kn_fflags
!= 0);
238 static const struct filterops nfsread_filtops
=
239 { 1, NULL
, filt_nfsdetach
, filt_nfsread
};
240 static const struct filterops nfsvnode_filtops
=
241 { 1, NULL
, filt_nfsdetach
, filt_nfsvnode
};
244 nfs_kqfilter(void *v
)
246 struct vop_kqfilter_args
/* {
255 struct lwp
*l
= curlwp
;
259 switch (kn
->kn_filter
) {
261 kn
->kn_fop
= &nfsread_filtops
;
264 kn
->kn_fop
= &nfsvnode_filtops
;
273 * Put the vnode to watched list.
277 * Fetch current attributes. It's only needed when the vnode
278 * is not watched yet, but we need to do this without lock
279 * held. This is likely cheap due to attrcache, so do it now.
281 memset(&attr
, 0, sizeof(attr
));
282 (void) VOP_GETATTR(vp
, &attr
, l
->l_cred
);
284 mutex_enter(&nfskevq_lock
);
286 /* ensure the poller is running */
288 error
= kthread_create(PRI_NONE
, 0, NULL
, nfs_kqpoll
,
289 NULL
, &nfskq_thread
, "nfskqpoll");
294 SLIST_FOREACH(ke
, &kevlist
, kev_link
) {
300 /* already watched, so just bump usecount */
304 ke
= kmem_alloc(sizeof(*ke
), KM_SLEEP
);
308 ke
->omtime
= attr
.va_mtime
;
309 ke
->octime
= attr
.va_ctime
;
310 ke
->onlink
= attr
.va_nlink
;
311 cv_init(&ke
->cv
, "nfskqdet");
312 SLIST_INSERT_HEAD(&kevlist
, ke
, kev_link
);
315 /* kick the poller */
316 cv_signal(&nfskq_cv
);
318 /* XXXLUKEM lock the struct? */
319 SLIST_INSERT_HEAD(&vp
->v_klist
, kn
, kn_selnext
);
322 mutex_exit(&nfskevq_lock
);