Home | History | Annotate | Line # | Download | only in nfs
nfs_srvcache.c revision 1.1.1.3
      1 /*
      2  * Copyright (c) 1989, 1993
      3  *	The Regents of the University of California.  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, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	@(#)nfs_srvcache.c	8.3 (Berkeley) 3/30/95
     37  */
     38 
     39 /*
     40  * Reference: Chet Juszczak, "Improving the Performance and Correctness
     41  *		of an NFS Server", in Proc. Winter 1989 USENIX Conference,
     42  *		pages 53-63. San Diego, February 1989.
     43  */
     44 #include <sys/param.h>
     45 #include <sys/vnode.h>
     46 #include <sys/mount.h>
     47 #include <sys/kernel.h>
     48 #include <sys/systm.h>
     49 #include <sys/proc.h>
     50 #include <sys/mbuf.h>
     51 #include <sys/malloc.h>
     52 #include <sys/socket.h>
     53 #include <sys/socketvar.h>
     54 
     55 #include <netinet/in.h>
     56 #ifdef ISO
     57 #include <netiso/iso.h>
     58 #endif
     59 #include <nfs/nfsm_subs.h>
     60 #include <nfs/rpcv2.h>
     61 #include <nfs/nfsproto.h>
     62 #include <nfs/nfs.h>
     63 #include <nfs/nfsrvcache.h>
     64 #include <nfs/nqnfs.h>
     65 
     66 extern struct nfsstats nfsstats;
     67 extern int nfsv2_procid[NFS_NPROCS];
     68 long numnfsrvcache, desirednfsrvcache = NFSRVCACHESIZ;
     69 
     70 #define	NFSRCHASH(xid) \
     71 	(&nfsrvhashtbl[((xid) + ((xid) >> 24)) & nfsrvhash])
     72 LIST_HEAD(nfsrvhash, nfsrvcache) *nfsrvhashtbl;
     73 TAILQ_HEAD(nfsrvlru, nfsrvcache) nfsrvlruhead;
     74 u_long nfsrvhash;
     75 
     76 #define TRUE	1
     77 #define	FALSE	0
     78 
     79 #define	NETFAMILY(rp) \
     80 		(((rp)->rc_flag & RC_INETADDR) ? AF_INET : AF_ISO)
     81 
     82 /*
     83  * Static array that defines which nfs rpc's are nonidempotent
     84  */
     85 int nonidempotent[NFS_NPROCS] = {
     86 	FALSE,
     87 	FALSE,
     88 	TRUE,
     89 	FALSE,
     90 	FALSE,
     91 	FALSE,
     92 	FALSE,
     93 	TRUE,
     94 	TRUE,
     95 	TRUE,
     96 	TRUE,
     97 	TRUE,
     98 	TRUE,
     99 	TRUE,
    100 	TRUE,
    101 	TRUE,
    102 	FALSE,
    103 	FALSE,
    104 	FALSE,
    105 	FALSE,
    106 	FALSE,
    107 	FALSE,
    108 	FALSE,
    109 	FALSE,
    110 	FALSE,
    111 	FALSE,
    112 };
    113 
    114 /* True iff the rpc reply is an nfs status ONLY! */
    115 static int nfsv2_repstat[NFS_NPROCS] = {
    116 	FALSE,
    117 	FALSE,
    118 	FALSE,
    119 	FALSE,
    120 	FALSE,
    121 	FALSE,
    122 	FALSE,
    123 	FALSE,
    124 	FALSE,
    125 	FALSE,
    126 	TRUE,
    127 	TRUE,
    128 	TRUE,
    129 	TRUE,
    130 	FALSE,
    131 	TRUE,
    132 	FALSE,
    133 	FALSE,
    134 };
    135 
    136 /*
    137  * Initialize the server request cache list
    138  */
    139 void
    140 nfsrv_initcache()
    141 {
    142 
    143 	nfsrvhashtbl = hashinit(desirednfsrvcache, M_NFSD, &nfsrvhash);
    144 	TAILQ_INIT(&nfsrvlruhead);
    145 }
    146 
    147 /*
    148  * Look for the request in the cache
    149  * If found then
    150  *    return action and optionally reply
    151  * else
    152  *    insert it in the cache
    153  *
    154  * The rules are as follows:
    155  * - if in progress, return DROP request
    156  * - if completed within DELAY of the current time, return DROP it
    157  * - if completed a longer time ago return REPLY if the reply was cached or
    158  *   return DOIT
    159  * Update/add new request at end of lru list
    160  */
    161 int
    162 nfsrv_getcache(nd, slp, repp)
    163 	register struct nfsrv_descript *nd;
    164 	struct nfssvc_sock *slp;
    165 	struct mbuf **repp;
    166 {
    167 	register struct nfsrvcache *rp;
    168 	struct mbuf *mb;
    169 	struct sockaddr_in *saddr;
    170 	caddr_t bpos;
    171 	int ret;
    172 
    173 	/*
    174 	 * Don't cache recent requests for reliable transport protocols.
    175 	 * (Maybe we should for the case of a reconnect, but..)
    176 	 */
    177 	if (!nd->nd_nam2)
    178 		return (RC_DOIT);
    179 loop:
    180 	for (rp = NFSRCHASH(nd->nd_retxid)->lh_first; rp != 0;
    181 	    rp = rp->rc_hash.le_next) {
    182 	    if (nd->nd_retxid == rp->rc_xid && nd->nd_procnum == rp->rc_proc &&
    183 		netaddr_match(NETFAMILY(rp), &rp->rc_haddr, nd->nd_nam)) {
    184 			if ((rp->rc_flag & RC_LOCKED) != 0) {
    185 				rp->rc_flag |= RC_WANTED;
    186 				(void) tsleep((caddr_t)rp, PZERO-1, "nfsrc", 0);
    187 				goto loop;
    188 			}
    189 			rp->rc_flag |= RC_LOCKED;
    190 			/* If not at end of LRU chain, move it there */
    191 			if (rp->rc_lru.tqe_next) {
    192 				TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
    193 				TAILQ_INSERT_TAIL(&nfsrvlruhead, rp, rc_lru);
    194 			}
    195 			if (rp->rc_state == RC_UNUSED)
    196 				panic("nfsrv cache");
    197 			if (rp->rc_state == RC_INPROG) {
    198 				nfsstats.srvcache_inproghits++;
    199 				ret = RC_DROPIT;
    200 			} else if (rp->rc_flag & RC_REPSTATUS) {
    201 				nfsstats.srvcache_nonidemdonehits++;
    202 				nfs_rephead(0, nd, slp, rp->rc_status,
    203 				   0, (u_quad_t *)0, repp, &mb, &bpos);
    204 				ret = RC_REPLY;
    205 			} else if (rp->rc_flag & RC_REPMBUF) {
    206 				nfsstats.srvcache_nonidemdonehits++;
    207 				*repp = m_copym(rp->rc_reply, 0, M_COPYALL,
    208 						M_WAIT);
    209 				ret = RC_REPLY;
    210 			} else {
    211 				nfsstats.srvcache_idemdonehits++;
    212 				rp->rc_state = RC_INPROG;
    213 				ret = RC_DOIT;
    214 			}
    215 			rp->rc_flag &= ~RC_LOCKED;
    216 			if (rp->rc_flag & RC_WANTED) {
    217 				rp->rc_flag &= ~RC_WANTED;
    218 				wakeup((caddr_t)rp);
    219 			}
    220 			return (ret);
    221 		}
    222 	}
    223 	nfsstats.srvcache_misses++;
    224 	if (numnfsrvcache < desirednfsrvcache) {
    225 		rp = (struct nfsrvcache *)malloc((u_long)sizeof *rp,
    226 		    M_NFSD, M_WAITOK);
    227 		bzero((char *)rp, sizeof *rp);
    228 		numnfsrvcache++;
    229 		rp->rc_flag = RC_LOCKED;
    230 	} else {
    231 		rp = nfsrvlruhead.tqh_first;
    232 		while ((rp->rc_flag & RC_LOCKED) != 0) {
    233 			rp->rc_flag |= RC_WANTED;
    234 			(void) tsleep((caddr_t)rp, PZERO-1, "nfsrc", 0);
    235 			rp = nfsrvlruhead.tqh_first;
    236 		}
    237 		rp->rc_flag |= RC_LOCKED;
    238 		LIST_REMOVE(rp, rc_hash);
    239 		TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
    240 		if (rp->rc_flag & RC_REPMBUF)
    241 			m_freem(rp->rc_reply);
    242 		if (rp->rc_flag & RC_NAM)
    243 			MFREE(rp->rc_nam, mb);
    244 		rp->rc_flag &= (RC_LOCKED | RC_WANTED);
    245 	}
    246 	TAILQ_INSERT_TAIL(&nfsrvlruhead, rp, rc_lru);
    247 	rp->rc_state = RC_INPROG;
    248 	rp->rc_xid = nd->nd_retxid;
    249 	saddr = mtod(nd->nd_nam, struct sockaddr_in *);
    250 	switch (saddr->sin_family) {
    251 	case AF_INET:
    252 		rp->rc_flag |= RC_INETADDR;
    253 		rp->rc_inetaddr = saddr->sin_addr.s_addr;
    254 		break;
    255 	case AF_ISO:
    256 	default:
    257 		rp->rc_flag |= RC_NAM;
    258 		rp->rc_nam = m_copym(nd->nd_nam, 0, M_COPYALL, M_WAIT);
    259 		break;
    260 	};
    261 	rp->rc_proc = nd->nd_procnum;
    262 	LIST_INSERT_HEAD(NFSRCHASH(nd->nd_retxid), rp, rc_hash);
    263 	rp->rc_flag &= ~RC_LOCKED;
    264 	if (rp->rc_flag & RC_WANTED) {
    265 		rp->rc_flag &= ~RC_WANTED;
    266 		wakeup((caddr_t)rp);
    267 	}
    268 	return (RC_DOIT);
    269 }
    270 
    271 /*
    272  * Update a request cache entry after the rpc has been done
    273  */
    274 void
    275 nfsrv_updatecache(nd, repvalid, repmbuf)
    276 	register struct nfsrv_descript *nd;
    277 	int repvalid;
    278 	struct mbuf *repmbuf;
    279 {
    280 	register struct nfsrvcache *rp;
    281 
    282 	if (!nd->nd_nam2)
    283 		return;
    284 loop:
    285 	for (rp = NFSRCHASH(nd->nd_retxid)->lh_first; rp != 0;
    286 	    rp = rp->rc_hash.le_next) {
    287 	    if (nd->nd_retxid == rp->rc_xid && nd->nd_procnum == rp->rc_proc &&
    288 		netaddr_match(NETFAMILY(rp), &rp->rc_haddr, nd->nd_nam)) {
    289 			if ((rp->rc_flag & RC_LOCKED) != 0) {
    290 				rp->rc_flag |= RC_WANTED;
    291 				(void) tsleep((caddr_t)rp, PZERO-1, "nfsrc", 0);
    292 				goto loop;
    293 			}
    294 			rp->rc_flag |= RC_LOCKED;
    295 			rp->rc_state = RC_DONE;
    296 			/*
    297 			 * If we have a valid reply update status and save
    298 			 * the reply for non-idempotent rpc's.
    299 			 */
    300 			if (repvalid && nonidempotent[nd->nd_procnum]) {
    301 				if ((nd->nd_flag & ND_NFSV3) == 0 &&
    302 				  nfsv2_repstat[nfsv2_procid[nd->nd_procnum]]) {
    303 					rp->rc_status = nd->nd_repstat;
    304 					rp->rc_flag |= RC_REPSTATUS;
    305 				} else {
    306 					rp->rc_reply = m_copym(repmbuf,
    307 						0, M_COPYALL, M_WAIT);
    308 					rp->rc_flag |= RC_REPMBUF;
    309 				}
    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 struct nfsrvcache *rp, *nextrp;
    328 
    329 	for (rp = nfsrvlruhead.tqh_first; rp != 0; rp = nextrp) {
    330 		nextrp = rp->rc_lru.tqe_next;
    331 		LIST_REMOVE(rp, rc_hash);
    332 		TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
    333 		free(rp, M_NFSD);
    334 	}
    335 	numnfsrvcache = 0;
    336 }
    337