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