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