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