nfscache.c 7.8 KB

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