nfs_srvcache.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /* $OpenBSD: nfs_srvcache.c,v 1.27 2015/03/14 03:38:52 jsg Exp $ */
  2. /* $NetBSD: nfs_srvcache.c,v 1.12 1996/02/18 11:53:49 fvdl Exp $ */
  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. * Reference: Chet Juszczak, "Improving the Performance and Correctness
  38. * of an NFS Server", in Proc. Winter 1989 USENIX Conference,
  39. * pages 53-63. San Diego, February 1989.
  40. */
  41. #include <sys/param.h>
  42. #include <sys/mount.h>
  43. #include <sys/kernel.h>
  44. #include <sys/systm.h>
  45. #include <sys/mbuf.h>
  46. #include <sys/malloc.h>
  47. #include <sys/socket.h>
  48. #include <sys/queue.h>
  49. #include <crypto/siphash.h>
  50. #include <netinet/in.h>
  51. #include <nfs/nfsproto.h>
  52. #include <nfs/nfs.h>
  53. #include <nfs/nfsrvcache.h>
  54. #include <nfs/nfs_var.h>
  55. extern struct nfsstats nfsstats;
  56. extern int nfsv2_procid[NFS_NPROCS];
  57. long numnfsrvcache, desirednfsrvcache = NFSRVCACHESIZ;
  58. struct nfsrvcache *nfsrv_lookupcache(struct nfsrv_descript *);
  59. void nfsrv_cleanentry(struct nfsrvcache *);
  60. LIST_HEAD(nfsrvhash, nfsrvcache) *nfsrvhashtbl;
  61. SIPHASH_KEY nfsrvhashkey;
  62. TAILQ_HEAD(nfsrvlru, nfsrvcache) nfsrvlruhead;
  63. u_long nfsrvhash;
  64. #define NFSRCHASH(xid) \
  65. (&nfsrvhashtbl[SipHash24(&nfsrvhashkey, &(xid), sizeof(xid)) & nfsrvhash])
  66. #define NETFAMILY(rp) \
  67. (((rp)->rc_flag & RC_INETADDR) ? AF_INET : AF_UNSPEC)
  68. /* Array that defines which nfs rpc's are nonidempotent */
  69. int nonidempotent[NFS_NPROCS] = {
  70. 0, 0, 1, 0, 0, 0, 0, 1,
  71. 1, 1, 1, 1, 1, 1, 1, 1,
  72. 0, 0, 0, 0, 0, 0, 0
  73. };
  74. /* True iff the rpc reply is an nfs status ONLY! */
  75. int nfsv2_repstat[NFS_NPROCS] = {
  76. 0, 0, 0, 0, 0, 0, 0, 0,
  77. 0, 0, 1, 1, 1, 1, 0, 1,
  78. 0, 0
  79. };
  80. void
  81. nfsrv_cleanentry(struct nfsrvcache *rp)
  82. {
  83. if ((rp->rc_flag & RC_REPMBUF) != 0)
  84. m_freem(rp->rc_reply);
  85. if ((rp->rc_flag & RC_NAM) != 0)
  86. m_free(rp->rc_nam);
  87. rp->rc_flag &= ~(RC_REPSTATUS|RC_REPMBUF);
  88. }
  89. /* Initialize the server request cache list */
  90. void
  91. nfsrv_initcache(void)
  92. {
  93. nfsrvhashtbl = hashinit(desirednfsrvcache, M_NFSD, M_WAITOK, &nfsrvhash);
  94. arc4random_buf(&nfsrvhashkey, sizeof(nfsrvhashkey));
  95. TAILQ_INIT(&nfsrvlruhead);
  96. }
  97. /*
  98. * Look for the request in the cache
  99. * If found then
  100. * return action and optionally reply
  101. * else
  102. * insert it in the cache
  103. *
  104. * The rules are as follows:
  105. * - if in progress, return DROP request
  106. * - if completed within DELAY of the current time, return DROP it
  107. * - if completed a longer time ago return REPLY if the reply was cached or
  108. * return DOIT
  109. * Update/add new request at end of lru list
  110. */
  111. int
  112. nfsrv_getcache(struct nfsrv_descript *nd, struct nfssvc_sock *slp,
  113. struct mbuf **repp)
  114. {
  115. struct nfsrvhash *hash;
  116. struct nfsrvcache *rp;
  117. struct mbuf *mb;
  118. struct sockaddr_in *saddr;
  119. int ret;
  120. /*
  121. * Don't cache recent requests for reliable transport protocols.
  122. * (Maybe we should for the case of a reconnect, but..)
  123. */
  124. if (!nd->nd_nam2)
  125. return (RC_DOIT);
  126. rp = nfsrv_lookupcache(nd);
  127. if (rp) {
  128. /* If not at end of LRU chain, move it there */
  129. if (TAILQ_NEXT(rp, rc_lru)) {
  130. TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
  131. TAILQ_INSERT_TAIL(&nfsrvlruhead, rp, rc_lru);
  132. }
  133. if (rp->rc_state == RC_UNUSED)
  134. panic("nfsrv cache");
  135. if (rp->rc_state == RC_INPROG) {
  136. nfsstats.srvcache_inproghits++;
  137. ret = RC_DROPIT;
  138. } else if (rp->rc_flag & RC_REPSTATUS) {
  139. nfsstats.srvcache_nonidemdonehits++;
  140. nfs_rephead(0, nd, slp, rp->rc_status, repp, &mb);
  141. ret = RC_REPLY;
  142. } else if (rp->rc_flag & RC_REPMBUF) {
  143. nfsstats.srvcache_nonidemdonehits++;
  144. *repp = m_copym(rp->rc_reply, 0, M_COPYALL, M_WAIT);
  145. ret = RC_REPLY;
  146. } else {
  147. nfsstats.srvcache_idemdonehits++;
  148. rp->rc_state = RC_INPROG;
  149. ret = RC_DOIT;
  150. }
  151. rp->rc_flag &= ~RC_LOCKED;
  152. if (rp->rc_flag & RC_WANTED) {
  153. rp->rc_flag &= ~RC_WANTED;
  154. wakeup(rp);
  155. }
  156. return (ret);
  157. }
  158. nfsstats.srvcache_misses++;
  159. if (numnfsrvcache < desirednfsrvcache) {
  160. rp = malloc(sizeof(*rp), M_NFSD, M_WAITOK|M_ZERO);
  161. numnfsrvcache++;
  162. rp->rc_flag = RC_LOCKED;
  163. } else {
  164. rp = TAILQ_FIRST(&nfsrvlruhead);
  165. while ((rp->rc_flag & RC_LOCKED) != 0) {
  166. rp->rc_flag |= RC_WANTED;
  167. tsleep(rp, PZERO-1, "nfsrc", 0);
  168. rp = TAILQ_FIRST(&nfsrvlruhead);
  169. }
  170. rp->rc_flag |= RC_LOCKED;
  171. LIST_REMOVE(rp, rc_hash);
  172. TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
  173. nfsrv_cleanentry(rp);
  174. rp->rc_flag &= (RC_LOCKED | RC_WANTED);
  175. }
  176. TAILQ_INSERT_TAIL(&nfsrvlruhead, rp, rc_lru);
  177. rp->rc_state = RC_INPROG;
  178. rp->rc_xid = nd->nd_retxid;
  179. saddr = mtod(nd->nd_nam, struct sockaddr_in *);
  180. switch (saddr->sin_family) {
  181. case AF_INET:
  182. rp->rc_flag |= RC_INETADDR;
  183. rp->rc_inetaddr = saddr->sin_addr.s_addr;
  184. break;
  185. default:
  186. rp->rc_flag |= RC_NAM;
  187. rp->rc_nam = m_copym(nd->nd_nam, 0, M_COPYALL, M_WAIT);
  188. break;
  189. };
  190. rp->rc_proc = nd->nd_procnum;
  191. hash = NFSRCHASH(nd->nd_retxid);
  192. LIST_INSERT_HEAD(hash, rp, rc_hash);
  193. rp->rc_flag &= ~RC_LOCKED;
  194. if (rp->rc_flag & RC_WANTED) {
  195. rp->rc_flag &= ~RC_WANTED;
  196. wakeup(rp);
  197. }
  198. return (RC_DOIT);
  199. }
  200. /* Update a request cache entry after the rpc has been done */
  201. void
  202. nfsrv_updatecache(struct nfsrv_descript *nd, int repvalid,
  203. struct mbuf *repmbuf)
  204. {
  205. struct nfsrvcache *rp;
  206. if (!nd->nd_nam2)
  207. return;
  208. rp = nfsrv_lookupcache(nd);
  209. if (rp) {
  210. nfsrv_cleanentry(rp);
  211. rp->rc_state = RC_DONE;
  212. /*
  213. * If we have a valid reply update status and save
  214. * the reply for non-idempotent rpc's.
  215. */
  216. if (repvalid && nonidempotent[nd->nd_procnum]) {
  217. if ((nd->nd_flag & ND_NFSV3) == 0 &&
  218. nfsv2_repstat[nfsv2_procid[nd->nd_procnum]]) {
  219. rp->rc_status = nd->nd_repstat;
  220. rp->rc_flag |= RC_REPSTATUS;
  221. } else {
  222. rp->rc_reply = m_copym(repmbuf, 0, M_COPYALL,
  223. M_WAIT);
  224. rp->rc_flag |= RC_REPMBUF;
  225. }
  226. }
  227. rp->rc_flag &= ~RC_LOCKED;
  228. if (rp->rc_flag & RC_WANTED) {
  229. rp->rc_flag &= ~RC_WANTED;
  230. wakeup(rp);
  231. }
  232. return;
  233. }
  234. }
  235. /* Clean out the cache. Called when the last nfsd terminates. */
  236. void
  237. nfsrv_cleancache(void)
  238. {
  239. struct nfsrvcache *rp, *nextrp;
  240. for (rp = TAILQ_FIRST(&nfsrvlruhead); rp != NULL; rp = nextrp) {
  241. nextrp = TAILQ_NEXT(rp, rc_lru);
  242. LIST_REMOVE(rp, rc_hash);
  243. TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
  244. nfsrv_cleanentry(rp);
  245. free(rp, M_NFSD, 0);
  246. }
  247. numnfsrvcache = 0;
  248. }
  249. struct nfsrvcache *
  250. nfsrv_lookupcache(struct nfsrv_descript *nd)
  251. {
  252. struct nfsrvhash *hash;
  253. struct nfsrvcache *rp;
  254. hash = NFSRCHASH(nd->nd_retxid);
  255. loop:
  256. LIST_FOREACH(rp, hash, rc_hash) {
  257. if (nd->nd_retxid == rp->rc_xid &&
  258. nd->nd_procnum == rp->rc_proc &&
  259. netaddr_match(NETFAMILY(rp), &rp->rc_haddr, nd->nd_nam)) {
  260. if ((rp->rc_flag & RC_LOCKED)) {
  261. rp->rc_flag |= RC_WANTED;
  262. tsleep(rp, PZERO - 1, "nfsrc", 0);
  263. goto loop;
  264. }
  265. rp->rc_flag |= RC_LOCKED;
  266. return (rp);
  267. }
  268. }
  269. return (NULL);
  270. }