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