nfscache.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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)
  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 type = rqstp->rq_cachetype;
  114. int rtn;
  115. rqstp->rq_cacherep = NULL;
  116. if (cache_disabled || type == RC_NOCACHE) {
  117. nfsdstats.rcnocache++;
  118. return RC_DOIT;
  119. }
  120. spin_lock(&cache_lock);
  121. rtn = RC_DOIT;
  122. rh = &cache_hash[request_hash(xid)];
  123. hlist_for_each_entry(rp, hn, rh, c_hash) {
  124. if (rp->c_state != RC_UNUSED &&
  125. xid == rp->c_xid && proc == rp->c_proc &&
  126. proto == rp->c_prot && vers == rp->c_vers &&
  127. time_before(jiffies, rp->c_timestamp + 120*HZ) &&
  128. memcmp((char*)&rqstp->rq_addr, (char*)&rp->c_addr, sizeof(rp->c_addr))==0) {
  129. nfsdstats.rchits++;
  130. goto found_entry;
  131. }
  132. }
  133. nfsdstats.rcmisses++;
  134. /* This loop shouldn't take more than a few iterations normally */
  135. {
  136. int safe = 0;
  137. list_for_each_entry(rp, &lru_head, c_lru) {
  138. if (rp->c_state != RC_INPROG)
  139. break;
  140. if (safe++ > CACHESIZE) {
  141. printk("nfsd: loop in repcache LRU list\n");
  142. cache_disabled = 1;
  143. goto out;
  144. }
  145. }
  146. }
  147. /* All entries on the LRU are in-progress. This should not happen */
  148. if (&rp->c_lru == &lru_head) {
  149. static int complaints;
  150. printk(KERN_WARNING "nfsd: all repcache entries locked!\n");
  151. if (++complaints > 5) {
  152. printk(KERN_WARNING "nfsd: disabling repcache.\n");
  153. cache_disabled = 1;
  154. }
  155. goto out;
  156. }
  157. rqstp->rq_cacherep = rp;
  158. rp->c_state = RC_INPROG;
  159. rp->c_xid = xid;
  160. rp->c_proc = proc;
  161. memcpy(&rp->c_addr, svc_addr_in(rqstp), sizeof(rp->c_addr));
  162. rp->c_prot = proto;
  163. rp->c_vers = vers;
  164. rp->c_timestamp = jiffies;
  165. hash_refile(rp);
  166. /* release any buffer */
  167. if (rp->c_type == RC_REPLBUFF) {
  168. kfree(rp->c_replvec.iov_base);
  169. rp->c_replvec.iov_base = NULL;
  170. }
  171. rp->c_type = RC_NOCACHE;
  172. out:
  173. spin_unlock(&cache_lock);
  174. return rtn;
  175. found_entry:
  176. /* We found a matching entry which is either in progress or done. */
  177. age = jiffies - rp->c_timestamp;
  178. rp->c_timestamp = jiffies;
  179. lru_put_end(rp);
  180. rtn = RC_DROPIT;
  181. /* Request being processed or excessive rexmits */
  182. if (rp->c_state == RC_INPROG || age < RC_DELAY)
  183. goto out;
  184. /* From the hall of fame of impractical attacks:
  185. * Is this a user who tries to snoop on the cache? */
  186. rtn = RC_DOIT;
  187. if (!rqstp->rq_secure && rp->c_secure)
  188. goto out;
  189. /* Compose RPC reply header */
  190. switch (rp->c_type) {
  191. case RC_NOCACHE:
  192. break;
  193. case RC_REPLSTAT:
  194. svc_putu32(&rqstp->rq_res.head[0], rp->c_replstat);
  195. rtn = RC_REPLY;
  196. break;
  197. case RC_REPLBUFF:
  198. if (!nfsd_cache_append(rqstp, &rp->c_replvec))
  199. goto out; /* should not happen */
  200. rtn = RC_REPLY;
  201. break;
  202. default:
  203. printk(KERN_WARNING "nfsd: bad repcache type %d\n", rp->c_type);
  204. rp->c_state = RC_UNUSED;
  205. }
  206. goto out;
  207. }
  208. /*
  209. * Update a cache entry. This is called from nfsd_dispatch when
  210. * the procedure has been executed and the complete reply is in
  211. * rqstp->rq_res.
  212. *
  213. * We're copying around data here rather than swapping buffers because
  214. * the toplevel loop requires max-sized buffers, which would be a waste
  215. * of memory for a cache with a max reply size of 100 bytes (diropokres).
  216. *
  217. * If we should start to use different types of cache entries tailored
  218. * specifically for attrstat and fh's, we may save even more space.
  219. *
  220. * Also note that a cachetype of RC_NOCACHE can legally be passed when
  221. * nfsd failed to encode a reply that otherwise would have been cached.
  222. * In this case, nfsd_cache_update is called with statp == NULL.
  223. */
  224. void
  225. nfsd_cache_update(struct svc_rqst *rqstp, int cachetype, __be32 *statp)
  226. {
  227. struct svc_cacherep *rp;
  228. struct kvec *resv = &rqstp->rq_res.head[0], *cachv;
  229. int len;
  230. if (!(rp = rqstp->rq_cacherep) || cache_disabled)
  231. return;
  232. len = resv->iov_len - ((char*)statp - (char*)resv->iov_base);
  233. len >>= 2;
  234. /* Don't cache excessive amounts of data and XDR failures */
  235. if (!statp || len > (256 >> 2)) {
  236. rp->c_state = RC_UNUSED;
  237. return;
  238. }
  239. switch (cachetype) {
  240. case RC_REPLSTAT:
  241. if (len != 1)
  242. printk("nfsd: RC_REPLSTAT/reply len %d!\n",len);
  243. rp->c_replstat = *statp;
  244. break;
  245. case RC_REPLBUFF:
  246. cachv = &rp->c_replvec;
  247. cachv->iov_base = kmalloc(len << 2, GFP_KERNEL);
  248. if (!cachv->iov_base) {
  249. spin_lock(&cache_lock);
  250. rp->c_state = RC_UNUSED;
  251. spin_unlock(&cache_lock);
  252. return;
  253. }
  254. cachv->iov_len = len << 2;
  255. memcpy(cachv->iov_base, statp, len << 2);
  256. break;
  257. }
  258. spin_lock(&cache_lock);
  259. lru_put_end(rp);
  260. rp->c_secure = rqstp->rq_secure;
  261. rp->c_type = cachetype;
  262. rp->c_state = RC_DONE;
  263. rp->c_timestamp = jiffies;
  264. spin_unlock(&cache_lock);
  265. return;
  266. }
  267. /*
  268. * Copy cached reply to current reply buffer. Should always fit.
  269. * FIXME as reply is in a page, we should just attach the page, and
  270. * keep a refcount....
  271. */
  272. static int
  273. nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *data)
  274. {
  275. struct kvec *vec = &rqstp->rq_res.head[0];
  276. if (vec->iov_len + data->iov_len > PAGE_SIZE) {
  277. printk(KERN_WARNING "nfsd: cached reply too large (%Zd).\n",
  278. data->iov_len);
  279. return 0;
  280. }
  281. memcpy((char*)vec->iov_base + vec->iov_len, data->iov_base, data->iov_len);
  282. vec->iov_len += data->iov_len;
  283. return 1;
  284. }