Home | History | Annotate | Line # | Download | only in nfs
nfs_srvcache.c revision 1.31.12.1
      1 /*	$NetBSD: nfs_srvcache.c,v 1.31.12.1 2006/12/30 20:50:52 yamt Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Rick Macklem at The University of Guelph.
      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. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  *
     34  *	@(#)nfs_srvcache.c	8.3 (Berkeley) 3/30/95
     35  */
     36 
     37 /*
     38  * Reference: Chet Juszczak, "Improving the Performance and Correctness
     39  *		of an NFS Server", in Proc. Winter 1989 USENIX Conference,
     40  *		pages 53-63. San Diego, February 1989.
     41  */
     42 
     43 #include <sys/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: nfs_srvcache.c,v 1.31.12.1 2006/12/30 20:50:52 yamt Exp $");
     45 
     46 #include "opt_iso.h"
     47 
     48 #include <sys/param.h>
     49 #include <sys/vnode.h>
     50 #include <sys/mount.h>
     51 #include <sys/kernel.h>
     52 #include <sys/systm.h>
     53 #include <sys/lock.h>
     54 #include <sys/proc.h>
     55 #include <sys/pool.h>
     56 #include <sys/mbuf.h>
     57 #include <sys/malloc.h>
     58 #include <sys/socket.h>
     59 #include <sys/socketvar.h>
     60 
     61 #include <netinet/in.h>
     62 #ifdef ISO
     63 #include <netiso/iso.h>
     64 #endif
     65 #include <nfs/nfsm_subs.h>
     66 #include <nfs/rpcv2.h>
     67 #include <nfs/nfsproto.h>
     68 #include <nfs/nfs.h>
     69 #include <nfs/nfsrvcache.h>
     70 #include <nfs/nfs_var.h>
     71 
     72 extern struct nfsstats nfsstats;
     73 extern const int nfsv2_procid[NFS_NPROCS];
     74 long numnfsrvcache, desirednfsrvcache = NFSRVCACHESIZ;
     75 struct pool nfs_reqcache_pool;
     76 
     77 #define	NFSRCHASH(xid) \
     78 	(&nfsrvhashtbl[((xid) + ((xid) >> 24)) & nfsrvhash])
     79 LIST_HEAD(nfsrvhash, nfsrvcache) *nfsrvhashtbl;
     80 TAILQ_HEAD(nfsrvlru, nfsrvcache) nfsrvlruhead;
     81 struct simplelock nfsrv_reqcache_lock = SIMPLELOCK_INITIALIZER;
     82 u_long nfsrvhash;
     83 
     84 #define	NETFAMILY(rp) \
     85 		(((rp)->rc_flag & RC_INETADDR) ? AF_INET : AF_ISO)
     86 
     87 static struct nfsrvcache *nfsrv_lookupcache(struct nfsrv_descript *nd);
     88 static void nfsrv_unlockcache(struct nfsrvcache *rp);
     89 
     90 /*
     91  * Static array that defines which nfs rpc's are nonidempotent
     92  */
     93 const int nonidempotent[NFS_NPROCS] = {
     94 	FALSE,	/* NULL */
     95 	FALSE,	/* GETATTR */
     96 	TRUE,	/* SETATTR */
     97 	FALSE,	/* LOOKUP */
     98 	FALSE,	/* ACCESS */
     99 	FALSE,	/* READLINK */
    100 	FALSE,	/* READ */
    101 	TRUE,	/* WRITE */
    102 	TRUE,	/* CREATE */
    103 	TRUE,	/* MKDIR */
    104 	TRUE,	/* SYMLINK */
    105 	TRUE,	/* MKNOD */
    106 	TRUE,	/* REMOVE */
    107 	TRUE,	/* RMDIR */
    108 	TRUE,	/* RENAME */
    109 	TRUE,	/* LINK */
    110 	FALSE,	/* READDIR */
    111 	FALSE,	/* READDIRPLUS */
    112 	FALSE,	/* FSSTAT */
    113 	FALSE,	/* FSINFO */
    114 	FALSE,	/* PATHCONF */
    115 	FALSE,	/* COMMIT */
    116 	FALSE,	/* NOOP */
    117 };
    118 
    119 /* True iff the rpc reply is an nfs status ONLY! */
    120 static const int nfsv2_repstat[NFS_NPROCS] = {
    121 	FALSE,	/* NULL */
    122 	FALSE,	/* GETATTR */
    123 	FALSE,	/* SETATTR */
    124 	FALSE,	/* NOOP */
    125 	FALSE,	/* LOOKUP */
    126 	FALSE,	/* READLINK */
    127 	FALSE,	/* READ */
    128 	FALSE,	/* Obsolete WRITECACHE */
    129 	FALSE,	/* WRITE */
    130 	FALSE,	/* CREATE */
    131 	TRUE,	/* REMOVE */
    132 	TRUE,	/* RENAME */
    133 	TRUE,	/* LINK */
    134 	TRUE,	/* SYMLINK */
    135 	FALSE,	/* MKDIR */
    136 	TRUE,	/* RMDIR */
    137 	FALSE,	/* READDIR */
    138 	FALSE,	/* STATFS */
    139 };
    140 
    141 /*
    142  * Initialize the server request cache list
    143  */
    144 void
    145 nfsrv_initcache()
    146 {
    147 
    148 	nfsrvhashtbl = hashinit(desirednfsrvcache, HASH_LIST, M_NFSD,
    149 	    M_WAITOK, &nfsrvhash);
    150 	TAILQ_INIT(&nfsrvlruhead);
    151 	pool_init(&nfs_reqcache_pool, sizeof(struct nfsrvcache), 0, 0, 0,
    152 	    "nfsreqcachepl", &pool_allocator_nointr);
    153 }
    154 
    155 /*
    156  * Lookup a cache and lock it
    157  */
    158 static struct nfsrvcache *
    159 nfsrv_lookupcache(nd)
    160 	struct nfsrv_descript *nd;
    161 {
    162 	struct nfsrvcache *rp;
    163 
    164 	LOCK_ASSERT(simple_lock_held(&nfsrv_reqcache_lock));
    165 
    166 loop:
    167 	LIST_FOREACH(rp, NFSRCHASH(nd->nd_retxid), rc_hash) {
    168 		if (nd->nd_retxid == rp->rc_xid &&
    169 		    nd->nd_procnum == rp->rc_proc &&
    170 		    netaddr_match(NETFAMILY(rp), &rp->rc_haddr, nd->nd_nam)) {
    171 			if ((rp->rc_flag & RC_LOCKED) != 0) {
    172 				rp->rc_flag |= RC_WANTED;
    173 				(void) ltsleep(rp, PZERO - 1, "nfsrc", 0,
    174 				    &nfsrv_reqcache_lock);
    175 				goto loop;
    176 			}
    177 			rp->rc_flag |= RC_LOCKED;
    178 			break;
    179 		}
    180 	}
    181 
    182 	return rp;
    183 }
    184 
    185 /*
    186  * Unlock a cache
    187  */
    188 static void
    189 nfsrv_unlockcache(rp)
    190 	struct nfsrvcache *rp;
    191 {
    192 
    193 	LOCK_ASSERT(simple_lock_held(&nfsrv_reqcache_lock));
    194 
    195 	rp->rc_flag &= ~RC_LOCKED;
    196 	if (rp->rc_flag & RC_WANTED) {
    197 		rp->rc_flag &= ~RC_WANTED;
    198 		wakeup(rp);
    199 	}
    200 }
    201 
    202 /*
    203  * Look for the request in the cache
    204  * If found then
    205  *    return action and optionally reply
    206  * else
    207  *    insert it in the cache
    208  *
    209  * The rules are as follows:
    210  * - if in progress, return DROP request
    211  * - if completed within DELAY of the current time, return DROP it
    212  * - if completed a longer time ago return REPLY if the reply was cached or
    213  *   return DOIT
    214  * Update/add new request at end of lru list
    215  */
    216 int
    217 nfsrv_getcache(nd, slp, repp)
    218 	struct nfsrv_descript *nd;
    219 	struct nfssvc_sock *slp;
    220 	struct mbuf **repp;
    221 {
    222 	struct nfsrvcache *rp, *rpdup;
    223 	struct mbuf *mb;
    224 	struct sockaddr_in *saddr;
    225 	caddr_t bpos;
    226 	int ret;
    227 
    228 	simple_lock(&nfsrv_reqcache_lock);
    229 	rp = nfsrv_lookupcache(nd);
    230 	if (rp) {
    231 		simple_unlock(&nfsrv_reqcache_lock);
    232 found:
    233 		/* If not at end of LRU chain, move it there */
    234 		if (TAILQ_NEXT(rp, rc_lru)) { /* racy but ok */
    235 			simple_lock(&nfsrv_reqcache_lock);
    236 			TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
    237 			TAILQ_INSERT_TAIL(&nfsrvlruhead, rp, rc_lru);
    238 			simple_unlock(&nfsrv_reqcache_lock);
    239 		}
    240 		if (rp->rc_state == RC_UNUSED)
    241 			panic("nfsrv cache");
    242 		if (rp->rc_state == RC_INPROG) {
    243 			nfsstats.srvcache_inproghits++;
    244 			ret = RC_DROPIT;
    245 		} else if (rp->rc_flag & RC_REPSTATUS) {
    246 			nfsstats.srvcache_nonidemdonehits++;
    247 			nfs_rephead(0, nd, slp, rp->rc_status,
    248 			   0, (u_quad_t *)0, repp, &mb, &bpos);
    249 			ret = RC_REPLY;
    250 		} else if (rp->rc_flag & RC_REPMBUF) {
    251 			nfsstats.srvcache_nonidemdonehits++;
    252 			*repp = m_copym(rp->rc_reply, 0, M_COPYALL,
    253 					M_WAIT);
    254 			ret = RC_REPLY;
    255 		} else {
    256 			nfsstats.srvcache_idemdonehits++;
    257 			rp->rc_state = RC_INPROG;
    258 			ret = RC_DOIT;
    259 		}
    260 		simple_lock(&nfsrv_reqcache_lock);
    261 		nfsrv_unlockcache(rp);
    262 		simple_unlock(&nfsrv_reqcache_lock);
    263 		return ret;
    264 	}
    265 	nfsstats.srvcache_misses++;
    266 	if (numnfsrvcache < desirednfsrvcache) {
    267 		numnfsrvcache++;
    268 		simple_unlock(&nfsrv_reqcache_lock);
    269 		rp = pool_get(&nfs_reqcache_pool, PR_WAITOK);
    270 		memset(rp, 0, sizeof *rp);
    271 		rp->rc_flag = RC_LOCKED;
    272 	} else {
    273 		rp = TAILQ_FIRST(&nfsrvlruhead);
    274 		while ((rp->rc_flag & RC_LOCKED) != 0) {
    275 			rp->rc_flag |= RC_WANTED;
    276 			(void) ltsleep(rp, PZERO-1, "nfsrc", 0,
    277 			    &nfsrv_reqcache_lock);
    278 			rp = TAILQ_FIRST(&nfsrvlruhead);
    279 		}
    280 		rp->rc_flag |= RC_LOCKED;
    281 		LIST_REMOVE(rp, rc_hash);
    282 		TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
    283 		simple_unlock(&nfsrv_reqcache_lock);
    284 		if (rp->rc_flag & RC_REPMBUF)
    285 			m_freem(rp->rc_reply);
    286 		if (rp->rc_flag & RC_NAM)
    287 			(void) m_free(rp->rc_nam);
    288 		rp->rc_flag &= (RC_LOCKED | RC_WANTED);
    289 	}
    290 	rp->rc_state = RC_INPROG;
    291 	rp->rc_xid = nd->nd_retxid;
    292 	saddr = mtod(nd->nd_nam, struct sockaddr_in *);
    293 	switch (saddr->sin_family) {
    294 	case AF_INET:
    295 		rp->rc_flag |= RC_INETADDR;
    296 		rp->rc_inetaddr = saddr->sin_addr.s_addr;
    297 		break;
    298 	case AF_ISO:
    299 	default:
    300 		rp->rc_flag |= RC_NAM;
    301 		rp->rc_nam = m_copym(nd->nd_nam, 0, M_COPYALL, M_WAIT);
    302 		break;
    303 	};
    304 	rp->rc_proc = nd->nd_procnum;
    305 	simple_lock(&nfsrv_reqcache_lock);
    306 	rpdup = nfsrv_lookupcache(nd);
    307 	if (rpdup != NULL) {
    308 		/*
    309 		 * other thread made duplicate cache entry.
    310 		 */
    311 		simple_unlock(&nfsrv_reqcache_lock);
    312 		pool_put(&nfs_reqcache_pool, rp);
    313 		rp = rpdup;
    314 		goto found;
    315 	}
    316 	TAILQ_INSERT_TAIL(&nfsrvlruhead, rp, rc_lru);
    317 	LIST_INSERT_HEAD(NFSRCHASH(nd->nd_retxid), rp, rc_hash);
    318 	nfsrv_unlockcache(rp);
    319 	simple_unlock(&nfsrv_reqcache_lock);
    320 	return RC_DOIT;
    321 }
    322 
    323 /*
    324  * Update a request cache entry after the rpc has been done
    325  */
    326 void
    327 nfsrv_updatecache(nd, repvalid, repmbuf)
    328 	struct nfsrv_descript *nd;
    329 	int repvalid;
    330 	struct mbuf *repmbuf;
    331 {
    332 	struct nfsrvcache *rp;
    333 
    334 	if (!nd->nd_nam2)
    335 		return;
    336 	simple_lock(&nfsrv_reqcache_lock);
    337 	rp = nfsrv_lookupcache(nd);
    338 	simple_unlock(&nfsrv_reqcache_lock);
    339 	if (rp) {
    340 		rp->rc_state = RC_DONE;
    341 		/*
    342 		 * If we have a valid reply update status and save
    343 		 * the reply for non-idempotent rpc's.
    344 		 */
    345 		if (repvalid && nonidempotent[nd->nd_procnum]) {
    346 			if ((nd->nd_flag & ND_NFSV3) == 0 &&
    347 			  nfsv2_repstat[nfsv2_procid[nd->nd_procnum]]) {
    348 				rp->rc_status = nd->nd_repstat;
    349 				rp->rc_flag |= RC_REPSTATUS;
    350 			} else {
    351 				rp->rc_reply = m_copym(repmbuf,
    352 					0, M_COPYALL, M_WAIT);
    353 				rp->rc_flag |= RC_REPMBUF;
    354 			}
    355 		}
    356 		simple_lock(&nfsrv_reqcache_lock);
    357 		nfsrv_unlockcache(rp);
    358 		simple_unlock(&nfsrv_reqcache_lock);
    359 	}
    360 }
    361 
    362 /*
    363  * Clean out the cache. Called when the last nfsd terminates.
    364  */
    365 void
    366 nfsrv_cleancache()
    367 {
    368 	struct nfsrvcache *rp, *nextrp;
    369 
    370 	simple_lock(&nfsrv_reqcache_lock);
    371 	for (rp = TAILQ_FIRST(&nfsrvlruhead); rp != 0; rp = nextrp) {
    372 		nextrp = TAILQ_NEXT(rp, rc_lru);
    373 		LIST_REMOVE(rp, rc_hash);
    374 		TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
    375 		pool_put(&nfs_reqcache_pool, rp);
    376 	}
    377 	numnfsrvcache = 0;
    378 	simple_unlock(&nfsrv_reqcache_lock);
    379 }
    380