1 /* 2 * Copyright (c) 1989 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Rick Macklem at The University of Guelph. 7 * 8 * %sccs.include.redist.c% 9 * 10 * @(#)nfs_srvcache.c 7.18 (Berkeley) 09/30/92 11 */ 12 13 /* 14 * Reference: Chet Juszczak, "Improving the Performance and Correctness 15 * of an NFS Server", in Proc. Winter 1989 USENIX Conference, 16 * pages 53-63. San Diego, February 1989. 17 */ 18 #include <sys/param.h> 19 #include <sys/vnode.h> 20 #include <sys/mount.h> 21 #include <sys/kernel.h> 22 #include <sys/systm.h> 23 #include <sys/proc.h> 24 #include <sys/mbuf.h> 25 #include <sys/malloc.h> 26 #include <sys/socket.h> 27 #include <sys/socketvar.h> 28 #include <netinet/in.h> 29 #ifdef ISO 30 #include <netiso/iso.h> 31 #endif 32 #include <nfs/nfsm_subs.h> 33 #include <nfs/rpcv2.h> 34 #include <nfs/nfsv2.h> 35 #include <nfs/nfs.h> 36 #include <nfs/nfsrvcache.h> 37 #include <nfs/nqnfs.h> 38 39 long numnfsrvcache, desirednfsrvcache = NFSRVCACHESIZ; 40 41 #define NFSRCHASH(xid) (((xid) + ((xid) >> 16)) & rheadhash) 42 static struct nfsrvcache *nfsrvlruhead, **nfsrvlrutail = &nfsrvlruhead; 43 static struct nfsrvcache **rheadhtbl; 44 static u_long rheadhash; 45 46 #define TRUE 1 47 #define FALSE 0 48 49 #define NETFAMILY(rp) \ 50 (((rp)->rc_flag & RC_INETADDR) ? AF_INET : AF_ISO) 51 52 /* 53 * Static array that defines which nfs rpc's are nonidempotent 54 */ 55 int nonidempotent[NFS_NPROCS] = { 56 FALSE, 57 FALSE, 58 TRUE, 59 FALSE, 60 FALSE, 61 FALSE, 62 FALSE, 63 FALSE, 64 TRUE, 65 TRUE, 66 TRUE, 67 TRUE, 68 TRUE, 69 TRUE, 70 TRUE, 71 TRUE, 72 FALSE, 73 FALSE, 74 FALSE, 75 FALSE, 76 FALSE, 77 FALSE, 78 FALSE, 79 }; 80 81 /* True iff the rpc reply is an nfs status ONLY! */ 82 static int repliesstatus[NFS_NPROCS] = { 83 FALSE, 84 FALSE, 85 FALSE, 86 FALSE, 87 FALSE, 88 FALSE, 89 FALSE, 90 FALSE, 91 FALSE, 92 FALSE, 93 TRUE, 94 TRUE, 95 TRUE, 96 TRUE, 97 FALSE, 98 TRUE, 99 FALSE, 100 FALSE, 101 FALSE, 102 FALSE, 103 FALSE, 104 FALSE, 105 TRUE, 106 }; 107 108 /* 109 * Initialize the server request cache list 110 */ 111 nfsrv_initcache() 112 { 113 114 rheadhtbl = hashinit(desirednfsrvcache, M_NFSD, &rheadhash); 115 } 116 117 /* 118 * Look for the request in the cache 119 * If found then 120 * return action and optionally reply 121 * else 122 * insert it in the cache 123 * 124 * The rules are as follows: 125 * - if in progress, return DROP request 126 * - if completed within DELAY of the current time, return DROP it 127 * - if completed a longer time ago return REPLY if the reply was cached or 128 * return DOIT 129 * Update/add new request at end of lru list 130 */ 131 nfsrv_getcache(nam, nd, repp) 132 struct mbuf *nam; 133 register struct nfsd *nd; 134 struct mbuf **repp; 135 { 136 register struct nfsrvcache *rp, *rq, **rpp; 137 struct mbuf *mb; 138 struct sockaddr_in *saddr; 139 caddr_t bpos; 140 int ret; 141 142 if (nd->nd_nqlflag != NQL_NOVAL) 143 return (RC_DOIT); 144 rpp = &rheadhtbl[NFSRCHASH(nd->nd_retxid)]; 145 loop: 146 for (rp = *rpp; rp; rp = rp->rc_forw) { 147 if (nd->nd_retxid == rp->rc_xid && nd->nd_procnum == rp->rc_proc && 148 netaddr_match(NETFAMILY(rp), &rp->rc_haddr, nam)) { 149 if ((rp->rc_flag & RC_LOCKED) != 0) { 150 rp->rc_flag |= RC_WANTED; 151 (void) tsleep((caddr_t)rp, PZERO-1, "nfsrc", 0); 152 goto loop; 153 } 154 rp->rc_flag |= RC_LOCKED; 155 /* If not at end of LRU chain, move it there */ 156 if (rp->rc_next) { 157 /* remove from LRU chain */ 158 *rp->rc_prev = rp->rc_next; 159 rp->rc_next->rc_prev = rp->rc_prev; 160 /* and replace at end of it */ 161 rp->rc_next = NULL; 162 rp->rc_prev = nfsrvlrutail; 163 *nfsrvlrutail = rp; 164 nfsrvlrutail = &rp->rc_next; 165 } 166 if (rp->rc_state == RC_UNUSED) 167 panic("nfsrv cache"); 168 if (rp->rc_state == RC_INPROG || 169 (time.tv_sec - rp->rc_timestamp) < RC_DELAY) { 170 nfsstats.srvcache_inproghits++; 171 ret = RC_DROPIT; 172 } else if (rp->rc_flag & RC_REPSTATUS) { 173 nfsstats.srvcache_idemdonehits++; 174 nfs_rephead(0, nd, rp->rc_status, 175 0, (u_quad_t *)0, repp, &mb, &bpos); 176 rp->rc_timestamp = time.tv_sec; 177 ret = RC_REPLY; 178 } else if (rp->rc_flag & RC_REPMBUF) { 179 nfsstats.srvcache_idemdonehits++; 180 *repp = m_copym(rp->rc_reply, 0, M_COPYALL, 181 M_WAIT); 182 rp->rc_timestamp = time.tv_sec; 183 ret = RC_REPLY; 184 } else { 185 nfsstats.srvcache_nonidemdonehits++; 186 rp->rc_state = RC_INPROG; 187 ret = RC_DOIT; 188 } 189 rp->rc_flag &= ~RC_LOCKED; 190 if (rp->rc_flag & RC_WANTED) { 191 rp->rc_flag &= ~RC_WANTED; 192 wakeup((caddr_t)rp); 193 } 194 return (ret); 195 } 196 } 197 nfsstats.srvcache_misses++; 198 if (numnfsrvcache < desirednfsrvcache) { 199 rp = (struct nfsrvcache *)malloc((u_long)sizeof *rp, 200 M_NFSD, M_WAITOK); 201 bzero((char *)rp, sizeof *rp); 202 numnfsrvcache++; 203 rp->rc_flag = RC_LOCKED; 204 } else { 205 rp = nfsrvlruhead; 206 while ((rp->rc_flag & RC_LOCKED) != 0) { 207 rp->rc_flag |= RC_WANTED; 208 (void) tsleep((caddr_t)rp, PZERO-1, "nfsrc", 0); 209 rp = nfsrvlruhead; 210 } 211 rp->rc_flag |= RC_LOCKED; 212 /* remove from hash chain */ 213 if (rq = rp->rc_forw) 214 rq->rc_back = rp->rc_back; 215 *rp->rc_back = rq; 216 /* remove from LRU chain */ 217 *rp->rc_prev = rp->rc_next; 218 rp->rc_next->rc_prev = rp->rc_prev; 219 if (rp->rc_flag & RC_REPMBUF) 220 m_freem(rp->rc_reply); 221 if (rp->rc_flag & RC_NAM) 222 MFREE(rp->rc_nam, mb); 223 rp->rc_flag &= (RC_LOCKED | RC_WANTED); 224 } 225 /* place at end of LRU list */ 226 rp->rc_next = NULL; 227 rp->rc_prev = nfsrvlrutail; 228 *nfsrvlrutail = rp; 229 nfsrvlrutail = &rp->rc_next; 230 rp->rc_state = RC_INPROG; 231 rp->rc_xid = nd->nd_retxid; 232 saddr = mtod(nam, struct sockaddr_in *); 233 switch (saddr->sin_family) { 234 case AF_INET: 235 rp->rc_flag |= RC_INETADDR; 236 rp->rc_inetaddr = saddr->sin_addr.s_addr; 237 break; 238 case AF_ISO: 239 default: 240 rp->rc_flag |= RC_NAM; 241 rp->rc_nam = m_copym(nam, 0, M_COPYALL, M_WAIT); 242 break; 243 }; 244 rp->rc_proc = nd->nd_procnum; 245 /* insert into hash chain */ 246 if (rq = *rpp) 247 rq->rc_back = &rp->rc_forw; 248 rp->rc_forw = rq; 249 rp->rc_back = rpp; 250 *rpp = rp; 251 rp->rc_flag &= ~RC_LOCKED; 252 if (rp->rc_flag & RC_WANTED) { 253 rp->rc_flag &= ~RC_WANTED; 254 wakeup((caddr_t)rp); 255 } 256 return (RC_DOIT); 257 } 258 259 /* 260 * Update a request cache entry after the rpc has been done 261 */ 262 void 263 nfsrv_updatecache(nam, nd, repvalid, repmbuf) 264 struct mbuf *nam; 265 register struct nfsd *nd; 266 int repvalid; 267 struct mbuf *repmbuf; 268 { 269 register struct nfsrvcache *rp; 270 271 if (nd->nd_nqlflag != NQL_NOVAL) 272 return; 273 loop: 274 for (rp = rheadhtbl[NFSRCHASH(nd->nd_retxid)]; rp; rp = rp->rc_forw) { 275 if (nd->nd_retxid == rp->rc_xid && nd->nd_procnum == rp->rc_proc && 276 netaddr_match(NETFAMILY(rp), &rp->rc_haddr, nam)) { 277 if ((rp->rc_flag & RC_LOCKED) != 0) { 278 rp->rc_flag |= RC_WANTED; 279 (void) tsleep((caddr_t)rp, PZERO-1, "nfsrc", 0); 280 goto loop; 281 } 282 rp->rc_flag |= RC_LOCKED; 283 rp->rc_state = RC_DONE; 284 /* 285 * If we have a valid reply update status and save 286 * the reply for non-idempotent rpc's. 287 * Otherwise invalidate entry by setting the timestamp 288 * to nil. 289 */ 290 if (repvalid) { 291 rp->rc_timestamp = time.tv_sec; 292 if (nonidempotent[nd->nd_procnum]) { 293 if (repliesstatus[nd->nd_procnum]) { 294 rp->rc_status = nd->nd_repstat; 295 rp->rc_flag |= RC_REPSTATUS; 296 } else { 297 rp->rc_reply = m_copym(repmbuf, 298 0, M_COPYALL, M_WAIT); 299 rp->rc_flag |= RC_REPMBUF; 300 } 301 } 302 } else { 303 rp->rc_timestamp = 0; 304 } 305 rp->rc_flag &= ~RC_LOCKED; 306 if (rp->rc_flag & RC_WANTED) { 307 rp->rc_flag &= ~RC_WANTED; 308 wakeup((caddr_t)rp); 309 } 310 return; 311 } 312 } 313 } 314 315 /* 316 * Clean out the cache. Called when the last nfsd terminates. 317 */ 318 void 319 nfsrv_cleancache() 320 { 321 register struct nfsrvcache *rp, *nextrp; 322 323 for (rp = nfsrvlruhead; rp; rp = nextrp) { 324 nextrp = rp->rc_next; 325 free(rp, M_NFSD); 326 } 327 bzero((char *)rheadhtbl, (rheadhash + 1) * sizeof(void *)); 328 numnfsrvcache = 0; 329 } 330