nfscache.c 7.6 KB

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