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 * Redistribution and use in source and binary forms are permitted 9 * provided that the above copyright notice and this paragraph are 10 * duplicated in all such forms and that any documentation, 11 * advertising materials, and other materials related to such 12 * distribution and use acknowledge that the software was developed 13 * by the University of California, Berkeley. The name of the 14 * University may not be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 19 * 20 * @(#)nfs_srvcache.c 7.5 (Berkeley) 03/01/90 21 */ 22 23 #include "param.h" 24 #include "user.h" 25 #include "vnode.h" 26 #include "mount.h" 27 #include "kernel.h" 28 #include "systm.h" 29 #include "mbuf.h" 30 #include "socket.h" 31 #include "socketvar.h" 32 #include "../netinet/in.h" 33 #include "nfsm_subs.h" 34 #include "nfsv2.h" 35 #include "nfsrvcache.h" 36 #include "nfs.h" 37 38 #if ((NFSRCHSZ&(NFSRCHSZ-1)) == 0) 39 #define NFSRCHASH(xid) (((xid)+((xid)>>16))&(NFSRCHSZ-1)) 40 #else 41 #define NFSRCHASH(xid) (((unsigned)((xid)+((xid)>>16)))%NFSRCHSZ) 42 #endif 43 44 union rhead { 45 union rhead *rh_head[2]; 46 struct nfsrvcache *rh_chain[2]; 47 } rhead[NFSRCHSZ]; 48 49 static struct nfsrvcache nfsrvcachehead; 50 static struct nfsrvcache nfsrvcache[NFSRVCACHESIZ]; 51 52 #define TRUE 1 53 #define FALSE 0 54 55 /* 56 * Static array that defines which nfs rpc's are nonidempotent 57 */ 58 int nonidempotent[NFS_NPROCS] = { 59 FALSE, 60 FALSE, 61 TRUE, 62 FALSE, 63 FALSE, 64 FALSE, 65 FALSE, 66 FALSE, 67 TRUE, 68 TRUE, 69 TRUE, 70 TRUE, 71 TRUE, 72 TRUE, 73 TRUE, 74 TRUE, 75 FALSE, 76 FALSE, 77 }; 78 79 /* True iff the rpc reply is an nfs status ONLY! */ 80 static int repliesstatus[NFS_NPROCS] = { 81 FALSE, 82 FALSE, 83 FALSE, 84 FALSE, 85 FALSE, 86 FALSE, 87 FALSE, 88 FALSE, 89 FALSE, 90 FALSE, 91 TRUE, 92 TRUE, 93 TRUE, 94 TRUE, 95 FALSE, 96 TRUE, 97 FALSE, 98 FALSE, 99 }; 100 101 /* 102 * Initialize the server request cache list 103 */ 104 nfsrv_initcache() 105 { 106 register int i; 107 register struct nfsrvcache *rp = nfsrvcache; 108 register struct nfsrvcache *hp = &nfsrvcachehead; 109 register union rhead *rh = rhead; 110 111 for (i = NFSRCHSZ; --i >= 0; rh++) { 112 rh->rh_head[0] = rh; 113 rh->rh_head[1] = rh; 114 } 115 hp->rc_next = hp->rc_prev = hp; 116 for (i = NFSRVCACHESIZ; i-- > 0; ) { 117 rp->rc_state = RC_UNUSED; 118 rp->rc_flag = 0; 119 rp->rc_forw = rp; 120 rp->rc_back = rp; 121 rp->rc_next = hp->rc_next; 122 hp->rc_next->rc_prev = rp; 123 rp->rc_prev = hp; 124 hp->rc_next = rp; 125 rp++; 126 } 127 } 128 129 /* 130 * Look for the request in the cache 131 * If found then 132 * return action and optionally reply 133 * else 134 * insert it in the cache 135 * 136 * The rules are as follows: 137 * - if in progress, return DROP request 138 * - if completed within DELAY of the current time, return DROP it 139 * - if completed a longer time ago return REPLY if the reply was cached or 140 * return DOIT 141 * Update/add new request at end of lru list 142 */ 143 nfsrv_getcache(nam, xid, proc, repp) 144 struct mbuf *nam; 145 u_long xid; 146 int proc; 147 struct mbuf **repp; 148 { 149 register struct nfsrvcache *rp; 150 register union rhead *rh; 151 register u_long saddr; 152 struct mbuf *mb; 153 caddr_t bpos; 154 int ret; 155 156 rh = &rhead[NFSRCHASH(xid)]; 157 saddr = mtod(nam, struct sockaddr_in *)->sin_addr.s_addr; 158 loop: 159 for (rp = rh->rh_chain[0]; rp != (struct nfsrvcache *)rh; rp = rp->rc_forw) { 160 if (xid == rp->rc_xid && saddr == rp->rc_saddr && 161 proc == rp->rc_proc) { 162 if ((rp->rc_flag & RC_LOCKED) != 0) { 163 rp->rc_flag |= RC_WANTED; 164 sleep((caddr_t)rp, PZERO-1); 165 goto loop; 166 } 167 rp->rc_flag |= RC_LOCKED; 168 put_at_head(rp); 169 if (rp->rc_state == RC_UNUSED) 170 panic("nfsrv cache"); 171 if (rp->rc_state == RC_INPROG || 172 (time.tv_sec - rp->rc_timestamp) < RC_DELAY) { 173 nfsstats.srvcache_inproghits++; 174 ret = RC_DROPIT; 175 } else if (rp->rc_flag & RC_REPSTATUS) { 176 nfsstats.srvcache_idemdonehits++; 177 nfs_rephead(0, xid, rp->rc_status, repp, &mb, 178 &bpos); 179 rp->rc_timestamp = time.tv_sec; 180 ret = RC_REPLY; 181 } else if (rp->rc_flag & RC_REPMBUF) { 182 nfsstats.srvcache_idemdonehits++; 183 *repp = NFSMCOPY(rp->rc_reply, 0, M_COPYALL, 184 M_WAIT); 185 rp->rc_timestamp = time.tv_sec; 186 ret = RC_REPLY; 187 } else { 188 nfsstats.srvcache_nonidemdonehits++; 189 rp->rc_state = RC_INPROG; 190 ret = RC_DOIT; 191 } 192 rp->rc_flag &= ~RC_LOCKED; 193 if (rp->rc_flag & RC_WANTED) { 194 rp->rc_flag &= ~RC_WANTED; 195 wakeup((caddr_t)rp); 196 } 197 return (ret); 198 } 199 } 200 nfsstats.srvcache_misses++; 201 rp = nfsrvcachehead.rc_prev; 202 while ((rp->rc_flag & RC_LOCKED) != 0) { 203 rp->rc_flag |= RC_WANTED; 204 sleep((caddr_t)rp, PZERO-1); 205 } 206 remque(rp); 207 put_at_head(rp); 208 if (rp->rc_flag & RC_REPMBUF) 209 mb = rp->rc_reply; 210 else 211 mb = (struct mbuf *)0; 212 rp->rc_flag = 0; 213 rp->rc_state = RC_INPROG; 214 rp->rc_xid = xid; 215 rp->rc_saddr = saddr; 216 rp->rc_proc = proc; 217 insque(rp, rh); 218 if (mb) 219 m_freem(mb); 220 return (RC_DOIT); 221 } 222 223 /* 224 * Update a request cache entry after the rpc has been done 225 */ 226 nfsrv_updatecache(nam, xid, proc, repvalid, repstat, repmbuf) 227 struct mbuf *nam; 228 u_long xid; 229 int proc; 230 int repvalid; 231 int repstat; 232 struct mbuf *repmbuf; 233 { 234 register struct nfsrvcache *rp; 235 register union rhead *rh; 236 register u_long saddr; 237 238 rh = &rhead[NFSRCHASH(xid)]; 239 saddr = mtod(nam, struct sockaddr_in *)->sin_addr.s_addr; 240 loop: 241 for (rp = rh->rh_chain[0]; rp != (struct nfsrvcache *)rh; rp = rp->rc_forw) { 242 if (xid == rp->rc_xid && saddr == rp->rc_saddr && 243 proc == rp->rc_proc) { 244 if ((rp->rc_flag & RC_LOCKED) != 0) { 245 rp->rc_flag |= RC_WANTED; 246 sleep((caddr_t)rp, PZERO-1); 247 goto loop; 248 } 249 rp->rc_flag |= RC_LOCKED; 250 rp->rc_state = RC_DONE; 251 /* 252 * If we have a valid reply update status and save 253 * the reply for non-idempotent rpc's. Otherwise 254 * invalidate entry by setting the timestamp to nil. 255 */ 256 if (repvalid) { 257 rp->rc_timestamp = time.tv_sec; 258 if (nonidempotent[proc]) { 259 if (repliesstatus[proc]) { 260 rp->rc_status = repstat; 261 rp->rc_flag |= RC_REPSTATUS; 262 } else { 263 rp->rc_reply = NFSMCOPY(repmbuf, 264 0, M_COPYALL, M_WAIT); 265 rp->rc_flag |= RC_REPMBUF; 266 } 267 } 268 } else { 269 rp->rc_timestamp = 0; 270 } 271 rp->rc_flag &= ~RC_LOCKED; 272 if (rp->rc_flag & RC_WANTED) { 273 rp->rc_flag &= ~RC_WANTED; 274 wakeup((caddr_t)rp); 275 } 276 return; 277 } 278 } 279 } 280