nfscache.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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) ((((xid) >> 24) ^ (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 = kmalloc (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. memset(hash_list, 0, HASHSIZE * sizeof(struct hlist_head));
  67. cache_disabled = 0;
  68. }
  69. void
  70. nfsd_cache_shutdown(void)
  71. {
  72. struct svc_cacherep *rp;
  73. while (!list_empty(&lru_head)) {
  74. rp = list_entry(lru_head.next, struct svc_cacherep, c_lru);
  75. if (rp->c_state == RC_DONE && rp->c_type == RC_REPLBUFF)
  76. kfree(rp->c_replvec.iov_base);
  77. list_del(&rp->c_lru);
  78. kfree(rp);
  79. }
  80. cache_disabled = 1;
  81. kfree (hash_list);
  82. hash_list = NULL;
  83. }
  84. /*
  85. * Move cache entry to end of LRU list
  86. */
  87. static void
  88. lru_put_end(struct svc_cacherep *rp)
  89. {
  90. list_move_tail(&rp->c_lru, &lru_head);
  91. }
  92. /*
  93. * Move a cache entry from one hash list to another
  94. */
  95. static void
  96. hash_refile(struct svc_cacherep *rp)
  97. {
  98. hlist_del_init(&rp->c_hash);
  99. hlist_add_head(&rp->c_hash, hash_list + REQHASH(rp->c_xid));
  100. }
  101. /*
  102. * Try to find an entry matching the current call in the cache. When none
  103. * is found, we grab the oldest unlocked entry off the LRU list.
  104. * Note that no operation within the loop may sleep.
  105. */
  106. int
  107. nfsd_cache_lookup(struct svc_rqst *rqstp, int type)
  108. {
  109. struct hlist_node *hn;
  110. struct hlist_head *rh;
  111. struct svc_cacherep *rp;
  112. u32 xid = rqstp->rq_xid,
  113. proto = rqstp->rq_prot,
  114. vers = rqstp->rq_vers,
  115. proc = rqstp->rq_proc;
  116. unsigned long age;
  117. int rtn;
  118. rqstp->rq_cacherep = NULL;
  119. if (cache_disabled || type == RC_NOCACHE) {
  120. nfsdstats.rcnocache++;
  121. return RC_DOIT;
  122. }
  123. spin_lock(&cache_lock);
  124. rtn = RC_DOIT;
  125. rh = &hash_list[REQHASH(xid)];
  126. hlist_for_each_entry(rp, hn, rh, c_hash) {
  127. if (rp->c_state != RC_UNUSED &&
  128. xid == rp->c_xid && proc == rp->c_proc &&
  129. proto == rp->c_prot && vers == rp->c_vers &&
  130. time_before(jiffies, rp->c_timestamp + 120*HZ) &&
  131. memcmp((char*)&rqstp->rq_addr, (char*)&rp->c_addr, sizeof(rp->c_addr))==0) {
  132. nfsdstats.rchits++;
  133. goto found_entry;
  134. }
  135. }
  136. nfsdstats.rcmisses++;
  137. /* This loop shouldn't take more than a few iterations normally */
  138. {
  139. int safe = 0;
  140. list_for_each_entry(rp, &lru_head, c_lru) {
  141. if (rp->c_state != RC_INPROG)
  142. break;
  143. if (safe++ > CACHESIZE) {
  144. printk("nfsd: loop in repcache LRU list\n");
  145. cache_disabled = 1;
  146. goto out;
  147. }
  148. }
  149. }
  150. /* This should not happen */
  151. if (rp == NULL) {
  152. static int complaints;
  153. printk(KERN_WARNING "nfsd: all repcache entries locked!\n");
  154. if (++complaints > 5) {
  155. printk(KERN_WARNING "nfsd: disabling repcache.\n");
  156. cache_disabled = 1;
  157. }
  158. goto out;
  159. }
  160. rqstp->rq_cacherep = rp;
  161. rp->c_state = RC_INPROG;
  162. rp->c_xid = xid;
  163. rp->c_proc = proc;
  164. rp->c_addr = rqstp->rq_addr;
  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, u32 *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. }