nfscache.c 7.8 KB

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