nfscache.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * Request reply cache. This is currently a global cache, but this may
  3. * change in the future and be a per-client cache.
  4. *
  5. * This code is heavily inspired by the 44BSD implementation, although
  6. * it does things a bit differently.
  7. *
  8. * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
  9. */
  10. #include <linux/slab.h>
  11. #include <linux/sunrpc/clnt.h>
  12. #include "nfsd.h"
  13. #include "cache.h"
  14. /* Size of reply cache. Common values are:
  15. * 4.3BSD: 128
  16. * 4.4BSD: 256
  17. * Solaris2: 1024
  18. * DEC Unix: 512-4096
  19. */
  20. #define CACHESIZE 1024
  21. #define HASHSIZE 64
  22. static struct hlist_head * cache_hash;
  23. static struct list_head lru_head;
  24. static int cache_disabled = 1;
  25. /*
  26. * Calculate the hash index from an XID.
  27. */
  28. static inline u32 request_hash(u32 xid)
  29. {
  30. u32 h = xid;
  31. h ^= (xid >> 24);
  32. return h & (HASHSIZE-1);
  33. }
  34. static int nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *vec);
  35. /*
  36. * locking for the reply cache:
  37. * A cache entry is "single use" if c_state == RC_INPROG
  38. * Otherwise, it when accessing _prev or _next, the lock must be held.
  39. */
  40. static DEFINE_SPINLOCK(cache_lock);
  41. int nfsd_reply_cache_init(void)
  42. {
  43. struct svc_cacherep *rp;
  44. int i;
  45. INIT_LIST_HEAD(&lru_head);
  46. i = CACHESIZE;
  47. while (i) {
  48. rp = kmalloc(sizeof(*rp), GFP_KERNEL);
  49. if (!rp)
  50. goto out_nomem;
  51. list_add(&rp->c_lru, &lru_head);
  52. rp->c_state = RC_UNUSED;
  53. rp->c_type = RC_NOCACHE;
  54. INIT_HLIST_NODE(&rp->c_hash);
  55. i--;
  56. }
  57. cache_hash = kcalloc (HASHSIZE, sizeof(struct hlist_head), GFP_KERNEL);
  58. if (!cache_hash)
  59. goto out_nomem;
  60. cache_disabled = 0;
  61. return 0;
  62. out_nomem:
  63. printk(KERN_ERR "nfsd: failed to allocate reply cache\n");
  64. nfsd_reply_cache_shutdown();
  65. return -ENOMEM;
  66. }
  67. void nfsd_reply_cache_shutdown(void)
  68. {
  69. struct svc_cacherep *rp;
  70. while (!list_empty(&lru_head)) {
  71. rp = list_entry(lru_head.next, struct svc_cacherep, c_lru);
  72. if (rp->c_state == RC_DONE && rp->c_type == RC_REPLBUFF)
  73. kfree(rp->c_replvec.iov_base);
  74. list_del(&rp->c_lru);
  75. kfree(rp);
  76. }
  77. cache_disabled = 1;
  78. kfree (cache_hash);
  79. cache_hash = NULL;
  80. }
  81. /*
  82. * Move cache entry to end of LRU list
  83. */
  84. static void
  85. lru_put_end(struct svc_cacherep *rp)
  86. {
  87. list_move_tail(&rp->c_lru, &lru_head);
  88. }
  89. /*
  90. * Move a cache entry from one hash list to another
  91. */
  92. static void
  93. hash_refile(struct svc_cacherep *rp)
  94. {
  95. hlist_del_init(&rp->c_hash);
  96. hlist_add_head(&rp->c_hash, cache_hash + request_hash(rp->c_xid));
  97. }
  98. /*
  99. * Try to find an entry matching the current call in the cache. When none
  100. * is found, we grab the oldest unlocked entry off the LRU list.
  101. * Note that no operation within the loop may sleep.
  102. */
  103. int
  104. nfsd_cache_lookup(struct svc_rqst *rqstp)
  105. {
  106. struct hlist_node *hn;
  107. struct hlist_head *rh;
  108. struct svc_cacherep *rp;
  109. __be32 xid = rqstp->rq_xid;
  110. u32 proto = rqstp->rq_prot,
  111. vers = rqstp->rq_vers,
  112. proc = rqstp->rq_proc;
  113. unsigned long age;
  114. int type = rqstp->rq_cachetype;
  115. int rtn;
  116. rqstp->rq_cacherep = NULL;
  117. if (cache_disabled || type == RC_NOCACHE) {
  118. nfsdstats.rcnocache++;
  119. return RC_DOIT;
  120. }
  121. spin_lock(&cache_lock);
  122. rtn = RC_DOIT;
  123. rh = &cache_hash[request_hash(xid)];
  124. hlist_for_each_entry(rp, hn, rh, c_hash) {
  125. if (rp->c_state != RC_UNUSED &&
  126. xid == rp->c_xid && proc == rp->c_proc &&
  127. proto == rp->c_prot && vers == rp->c_vers &&
  128. time_before(jiffies, rp->c_timestamp + 120*HZ) &&
  129. rpc_cmp_addr(svc_addr(rqstp), (struct sockaddr *)&rp->c_addr) &&
  130. rpc_get_port(svc_addr(rqstp)) == rpc_get_port((struct sockaddr *)&rp->c_addr)) {
  131. nfsdstats.rchits++;
  132. goto found_entry;
  133. }
  134. }
  135. nfsdstats.rcmisses++;
  136. /* This loop shouldn't take more than a few iterations normally */
  137. {
  138. int safe = 0;
  139. list_for_each_entry(rp, &lru_head, c_lru) {
  140. if (rp->c_state != RC_INPROG)
  141. break;
  142. if (safe++ > CACHESIZE) {
  143. printk("nfsd: loop in repcache LRU list\n");
  144. cache_disabled = 1;
  145. goto out;
  146. }
  147. }
  148. }
  149. /* All entries on the LRU are in-progress. This should not happen */
  150. if (&rp->c_lru == &lru_head) {
  151. static int complaints;
  152. printk(KERN_WARNING "nfsd: all repcache entries locked!\n");
  153. if (++complaints > 5) {
  154. printk(KERN_WARNING "nfsd: disabling repcache.\n");
  155. cache_disabled = 1;
  156. }
  157. goto out;
  158. }
  159. rqstp->rq_cacherep = rp;
  160. rp->c_state = RC_INPROG;
  161. rp->c_xid = xid;
  162. rp->c_proc = proc;
  163. rpc_copy_addr((struct sockaddr *)&rp->c_addr, svc_addr(rqstp));
  164. rpc_set_port((struct sockaddr *)&rp->c_addr, rpc_get_port(svc_addr(rqstp)));
  165. rp->c_prot = proto;
  166. rp->c_vers = vers;
  167. rp->c_timestamp = jiffies;
  168. hash_refile(rp);
  169. /* release any buffer */
  170. if (rp->c_type == RC_REPLBUFF) {
  171. kfree(rp->c_replvec.iov_base);
  172. rp->c_replvec.iov_base = NULL;
  173. }
  174. rp->c_type = RC_NOCACHE;
  175. out:
  176. spin_unlock(&cache_lock);
  177. return rtn;
  178. found_entry:
  179. /* We found a matching entry which is either in progress or done. */
  180. age = jiffies - rp->c_timestamp;
  181. rp->c_timestamp = jiffies;
  182. lru_put_end(rp);
  183. rtn = RC_DROPIT;
  184. /* Request being processed or excessive rexmits */
  185. if (rp->c_state == RC_INPROG || age < RC_DELAY)
  186. goto out;
  187. /* From the hall of fame of impractical attacks:
  188. * Is this a user who tries to snoop on the cache? */
  189. rtn = RC_DOIT;
  190. if (!rqstp->rq_secure && rp->c_secure)
  191. goto out;
  192. /* Compose RPC reply header */
  193. switch (rp->c_type) {
  194. case RC_NOCACHE:
  195. break;
  196. case RC_REPLSTAT:
  197. svc_putu32(&rqstp->rq_res.head[0], rp->c_replstat);
  198. rtn = RC_REPLY;
  199. break;
  200. case RC_REPLBUFF:
  201. if (!nfsd_cache_append(rqstp, &rp->c_replvec))
  202. goto out; /* should not happen */
  203. rtn = RC_REPLY;
  204. break;
  205. default:
  206. printk(KERN_WARNING "nfsd: bad repcache type %d\n", rp->c_type);
  207. rp->c_state = RC_UNUSED;
  208. }
  209. goto out;
  210. }
  211. /*
  212. * Update a cache entry. This is called from nfsd_dispatch when
  213. * the procedure has been executed and the complete reply is in
  214. * rqstp->rq_res.
  215. *
  216. * We're copying around data here rather than swapping buffers because
  217. * the toplevel loop requires max-sized buffers, which would be a waste
  218. * of memory for a cache with a max reply size of 100 bytes (diropokres).
  219. *
  220. * If we should start to use different types of cache entries tailored
  221. * specifically for attrstat and fh's, we may save even more space.
  222. *
  223. * Also note that a cachetype of RC_NOCACHE can legally be passed when
  224. * nfsd failed to encode a reply that otherwise would have been cached.
  225. * In this case, nfsd_cache_update is called with statp == NULL.
  226. */
  227. void
  228. nfsd_cache_update(struct svc_rqst *rqstp, int cachetype, __be32 *statp)
  229. {
  230. struct svc_cacherep *rp;
  231. struct kvec *resv = &rqstp->rq_res.head[0], *cachv;
  232. int len;
  233. if (!(rp = rqstp->rq_cacherep) || cache_disabled)
  234. return;
  235. len = resv->iov_len - ((char*)statp - (char*)resv->iov_base);
  236. len >>= 2;
  237. /* Don't cache excessive amounts of data and XDR failures */
  238. if (!statp || len > (256 >> 2)) {
  239. rp->c_state = RC_UNUSED;
  240. return;
  241. }
  242. switch (cachetype) {
  243. case RC_REPLSTAT:
  244. if (len != 1)
  245. printk("nfsd: RC_REPLSTAT/reply len %d!\n",len);
  246. rp->c_replstat = *statp;
  247. break;
  248. case RC_REPLBUFF:
  249. cachv = &rp->c_replvec;
  250. cachv->iov_base = kmalloc(len << 2, GFP_KERNEL);
  251. if (!cachv->iov_base) {
  252. spin_lock(&cache_lock);
  253. rp->c_state = RC_UNUSED;
  254. spin_unlock(&cache_lock);
  255. return;
  256. }
  257. cachv->iov_len = len << 2;
  258. memcpy(cachv->iov_base, statp, len << 2);
  259. break;
  260. }
  261. spin_lock(&cache_lock);
  262. lru_put_end(rp);
  263. rp->c_secure = rqstp->rq_secure;
  264. rp->c_type = cachetype;
  265. rp->c_state = RC_DONE;
  266. rp->c_timestamp = jiffies;
  267. spin_unlock(&cache_lock);
  268. return;
  269. }
  270. /*
  271. * Copy cached reply to current reply buffer. Should always fit.
  272. * FIXME as reply is in a page, we should just attach the page, and
  273. * keep a refcount....
  274. */
  275. static int
  276. nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *data)
  277. {
  278. struct kvec *vec = &rqstp->rq_res.head[0];
  279. if (vec->iov_len + data->iov_len > PAGE_SIZE) {
  280. printk(KERN_WARNING "nfsd: cached reply too large (%Zd).\n",
  281. data->iov_len);
  282. return 0;
  283. }
  284. memcpy((char*)vec->iov_base + vec->iov_len, data->iov_base, data->iov_len);
  285. vec->iov_len += data->iov_len;
  286. return 1;
  287. }