nfscache.c 7.8 KB

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