1 /* $NetBSD: nfs_kq.c,v 1.23 2008/11/19 18:36:09 ad 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.23 2008/11/19 18:36:09 ad 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 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; 75 76 void 77 nfs_kqinit(void) 78 { 79 80 mutex_init(&nfskq_lock, MUTEX_DEFAULT, IPL_NONE); 81 cv_init(&nfskq_cv, "nfskqpw"); 82 } 83 84 void 85 nfs_kqfini(void) 86 { 87 88 if (nfskq_thread != NULL) { 89 mutex_enter(&nfskq_lock); 90 nfskq_thread_exit = true; 91 cv_broadcast(&nfskq_cv); 92 do { 93 cv_wait(&nfskq_cv, &nfskq_lock); 94 } while (nfskq_thread != NULL); 95 mutex_exit(&nfskq_lock); 96 } 97 mutex_destroy(&nfskq_lock); 98 cv_destroy(&nfskq_cv); 99 } 100 101 /* 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 110 * semantics. 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. 114 */ 115 /* ARGSUSED */ 116 static void 117 nfs_kqpoll(void *arg) 118 { 119 struct kevq *ke; 120 struct vattr attr; 121 struct lwp *l = curlwp; 122 u_quad_t osize; 123 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) 129 continue; 130 131 /* 132 * Mark entry busy, release lock and check 133 * for changes. 134 */ 135 ke->flags |= KEVQ_BUSY; 136 mutex_exit(&nfskq_lock); 137 138 /* save v_size, nfs_getattr() updates it */ 139 osize = ke->vp->v_size; 140 141 (void) VOP_GETATTR(ke->vp, &attr, l->l_cred); 142 143 /* following is a bit fragile, but about best 144 * we can get */ 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; 154 } 155 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; 160 } 161 162 if (attr.va_nlink != ke->onlink) { 163 VN_KNOTE(ke->vp, NOTE_LINK); 164 ke->onlink = attr.va_nlink; 165 } 166 167 mutex_enter(&nfskq_lock); 168 ke->flags &= ~KEVQ_BUSY; 169 cv_signal(&ke->cv); 170 } 171 172 if (SLIST_EMPTY(&kevlist)) { 173 /* Nothing more to watch, exit */ 174 nfskq_thread = NULL; 175 mutex_exit(&nfskq_lock); 176 kthread_exit(0); 177 } 178 179 /* wait a while before checking for changes again */ 180 cv_timedwait(&nfskq_cv, &nfskq_lock, 181 NFS_MINATTRTIMO * hz / 2); 182 } 183 nfskq_thread = NULL; 184 cv_broadcast(&nfskq_cv); 185 mutex_exit(&nfskq_lock); 186 } 187 188 static void 189 filt_nfsdetach(struct knote *kn) 190 { 191 struct vnode *vp = (struct vnode *)kn->kn_hook; 192 struct kevq *ke; 193 194 mutex_enter(&vp->v_interlock); 195 SLIST_REMOVE(&vp->v_klist, kn, knote, kn_selnext); 196 mutex_exit(&vp->v_interlock); 197 198 /* Remove the vnode from watch list */ 199 mutex_enter(&nfskq_lock); 200 SLIST_FOREACH(ke, &kevlist, kev_link) { 201 if (ke->vp == vp) { 202 while (ke->flags & KEVQ_BUSY) { 203 cv_wait(&ke->cv, &nfskq_lock); 204 } 205 206 if (ke->usecount > 1) { 207 /* keep, other kevents need this */ 208 ke->usecount--; 209 } else { 210 /* last user, g/c */ 211 cv_destroy(&ke->cv); 212 SLIST_REMOVE(&kevlist, ke, kevq, kev_link); 213 kmem_free(ke, sizeof(*ke)); 214 } 215 break; 216 } 217 } 218 mutex_exit(&nfskq_lock); 219 } 220 221 static int 222 filt_nfsread(struct knote *kn, long hint) 223 { 224 struct vnode *vp = (struct vnode *)kn->kn_hook; 225 int rv; 226 227 /* 228 * filesystem is gone, so set the EOF flag and schedule 229 * the knote for deletion. 230 */ 231 switch (hint) { 232 case NOTE_REVOKE: 233 KASSERT(mutex_owned(&vp->v_interlock)); 234 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 235 return (1); 236 case 0: 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); 241 return rv; 242 default: 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); 246 } 247 } 248 249 static int 250 filt_nfsvnode(struct knote *kn, long hint) 251 { 252 struct vnode *vp = (struct vnode *)kn->kn_hook; 253 int fflags; 254 255 switch (hint) { 256 case NOTE_REVOKE: 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; 261 return (1); 262 case 0: 263 mutex_enter(&vp->v_interlock); 264 fflags = kn->kn_fflags; 265 mutex_exit(&vp->v_interlock); 266 break; 267 default: 268 KASSERT(mutex_owned(&vp->v_interlock)); 269 if ((kn->kn_sfflags & hint) != 0) 270 kn->kn_fflags |= hint; 271 fflags = kn->kn_fflags; 272 break; 273 } 274 275 return (fflags != 0); 276 } 277 278 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 }; 283 284 int 285 nfs_kqfilter(void *v) 286 { 287 struct vop_kqfilter_args /* { 288 struct vnode *a_vp; 289 struct knote *a_kn; 290 } */ *ap = v; 291 struct vnode *vp; 292 struct knote *kn; 293 struct kevq *ke; 294 int error = 0; 295 struct vattr attr; 296 struct lwp *l = curlwp; 297 298 vp = ap->a_vp; 299 kn = ap->a_kn; 300 switch (kn->kn_filter) { 301 case EVFILT_READ: 302 kn->kn_fop = &nfsread_filtops; 303 break; 304 case EVFILT_VNODE: 305 kn->kn_fop = &nfsvnode_filtops; 306 break; 307 default: 308 return (EINVAL); 309 } 310 311 /* 312 * Put the vnode to watched list. 313 */ 314 315 /* 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. 319 */ 320 memset(&attr, 0, sizeof(attr)); 321 (void) VOP_GETATTR(vp, &attr, l->l_cred); 322 323 mutex_enter(&nfskq_lock); 324 325 /* ensure the poller is running */ 326 if (!nfskq_thread) { 327 error = kthread_create(PRI_NONE, 0, NULL, nfs_kqpoll, 328 NULL, &nfskq_thread, "nfskqpoll"); 329 if (error) { 330 mutex_exit(&nfskq_lock); 331 return error; 332 } 333 } 334 335 SLIST_FOREACH(ke, &kevlist, kev_link) { 336 if (ke->vp == vp) 337 break; 338 } 339 340 if (ke) { 341 /* already watched, so just bump usecount */ 342 ke->usecount++; 343 } else { 344 /* need a new one */ 345 ke = kmem_alloc(sizeof(*ke), KM_SLEEP); 346 ke->vp = vp; 347 ke->usecount = 1; 348 ke->flags = 0; 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); 354 } 355 356 mutex_enter(&vp->v_interlock); 357 SLIST_INSERT_HEAD(&vp->v_klist, kn, kn_selnext); 358 kn->kn_hook = vp; 359 mutex_exit(&vp->v_interlock); 360 361 /* kick the poller */ 362 cv_signal(&nfskq_cv); 363 mutex_exit(&nfskq_lock); 364 365 return (error); 366 } 367