nfscache.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 <linux/sunrpc/clnt.h>
  12. #include "nfsd.h"
  13. #include "cache.h"
  14. /* Size of reply cache. Common values are:
  15. * 4.3BSD: 128
  16. * 4.4BSD: 256
  17. * Solaris2: 1024
  18. * DEC Unix: 512-4096
  19. */
  20. #define CACHESIZE 1024
  21. #define HASHSIZE 64
  22. static struct hlist_head * cache_hash;
  23. static struct list_head lru_head;
  24. static int cache_disabled = 1;
  25. static struct kmem_cache *drc_slab;
  26. /*
  27. * Calculate the hash index from an XID.
  28. */
  29. static inline u32 request_hash(u32 xid)
  30. {
  31. u32 h = xid;
  32. h ^= (xid >> 24);
  33. return h & (HASHSIZE-1);
  34. }
  35. static int nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *vec);
  36. /*
  37. * locking for the reply cache:
  38. * A cache entry is "single use" if c_state == RC_INPROG
  39. * Otherwise, it when accessing _prev or _next, the lock must be held.
  40. */
  41. static DEFINE_SPINLOCK(cache_lock);
  42. static struct svc_cacherep *
  43. nfsd_reply_cache_alloc(void)
  44. {
  45. struct svc_cacherep *rp;
  46. rp = kmem_cache_alloc(drc_slab, GFP_KERNEL);
  47. if (rp) {
  48. rp->c_state = RC_UNUSED;
  49. rp->c_type = RC_NOCACHE;
  50. INIT_LIST_HEAD(&rp->c_lru);
  51. INIT_HLIST_NODE(&rp->c_hash);
  52. }
  53. return rp;
  54. }
  55. static void
  56. nfsd_reply_cache_free_locked(struct svc_cacherep *rp)
  57. {
  58. if (rp->c_state == RC_DONE && rp->c_type == RC_REPLBUFF)
  59. kfree(rp->c_replvec.iov_base);
  60. list_del(&rp->c_lru);
  61. kmem_cache_free(drc_slab, rp);
  62. }
  63. int nfsd_reply_cache_init(void)
  64. {
  65. int i;
  66. struct svc_cacherep *rp;
  67. drc_slab = kmem_cache_create("nfsd_drc", sizeof(struct svc_cacherep),
  68. 0, 0, NULL);
  69. if (!drc_slab)
  70. goto out_nomem;
  71. INIT_LIST_HEAD(&lru_head);
  72. i = CACHESIZE;
  73. while (i) {
  74. rp = nfsd_reply_cache_alloc();
  75. if (!rp)
  76. goto out_nomem;
  77. list_add(&rp->c_lru, &lru_head);
  78. i--;
  79. }
  80. cache_hash = kcalloc (HASHSIZE, sizeof(struct hlist_head), GFP_KERNEL);
  81. if (!cache_hash)
  82. goto out_nomem;
  83. cache_disabled = 0;
  84. return 0;
  85. out_nomem:
  86. printk(KERN_ERR "nfsd: failed to allocate reply cache\n");
  87. nfsd_reply_cache_shutdown();
  88. return -ENOMEM;
  89. }
  90. void nfsd_reply_cache_shutdown(void)
  91. {
  92. struct svc_cacherep *rp;
  93. while (!list_empty(&lru_head)) {
  94. rp = list_entry(lru_head.next, struct svc_cacherep, c_lru);
  95. nfsd_reply_cache_free_locked(rp);
  96. }
  97. cache_disabled = 1;
  98. kfree (cache_hash);
  99. cache_hash = NULL;
  100. if (drc_slab) {
  101. kmem_cache_destroy(drc_slab);
  102. drc_slab = NULL;
  103. }
  104. }
  105. /*
  106. * Move cache entry to end of LRU list
  107. */
  108. static void
  109. lru_put_end(struct svc_cacherep *rp)
  110. {
  111. list_move_tail(&rp->c_lru, &lru_head);
  112. }
  113. /*
  114. * Move a cache entry from one hash list to another
  115. */
  116. static void
  117. hash_refile(struct svc_cacherep *rp)
  118. {
  119. hlist_del_init(&rp->c_hash);
  120. hlist_add_head(&rp->c_hash, cache_hash + request_hash(rp->c_xid));
  121. }
  122. /*
  123. * Try to find an entry matching the current call in the cache. When none
  124. * is found, we grab the oldest unlocked entry off the LRU list.
  125. * Note that no operation within the loop may sleep.
  126. */
  127. int
  128. nfsd_cache_lookup(struct svc_rqst *rqstp)
  129. {
  130. struct hlist_node *hn;
  131. struct hlist_head *rh;
  132. struct svc_cacherep *rp;
  133. __be32 xid = rqstp->rq_xid;
  134. u32 proto = rqstp->rq_prot,
  135. vers = rqstp->rq_vers,
  136. proc = rqstp->rq_proc;
  137. unsigned long age;
  138. int type = rqstp->rq_cachetype;
  139. int rtn;
  140. rqstp->rq_cacherep = NULL;
  141. if (cache_disabled || type == RC_NOCACHE) {
  142. nfsdstats.rcnocache++;
  143. return RC_DOIT;
  144. }
  145. spin_lock(&cache_lock);
  146. rtn = RC_DOIT;
  147. rh = &cache_hash[request_hash(xid)];
  148. hlist_for_each_entry(rp, hn, rh, c_hash) {
  149. if (rp->c_state != RC_UNUSED &&
  150. xid == rp->c_xid && proc == rp->c_proc &&
  151. proto == rp->c_prot && vers == rp->c_vers &&
  152. time_before(jiffies, rp->c_timestamp + 120*HZ) &&
  153. rpc_cmp_addr(svc_addr(rqstp), (struct sockaddr *)&rp->c_addr) &&
  154. rpc_get_port(svc_addr(rqstp)) == rpc_get_port((struct sockaddr *)&rp->c_addr)) {
  155. nfsdstats.rchits++;
  156. goto found_entry;
  157. }
  158. }
  159. nfsdstats.rcmisses++;
  160. /* This loop shouldn't take more than a few iterations normally */
  161. {
  162. int safe = 0;
  163. list_for_each_entry(rp, &lru_head, c_lru) {
  164. if (rp->c_state != RC_INPROG)
  165. break;
  166. if (safe++ > CACHESIZE) {
  167. printk("nfsd: loop in repcache LRU list\n");
  168. cache_disabled = 1;
  169. goto out;
  170. }
  171. }
  172. }
  173. /* All entries on the LRU are in-progress. This should not happen */
  174. if (&rp->c_lru == &lru_head) {
  175. static int complaints;
  176. printk(KERN_WARNING "nfsd: all repcache entries locked!\n");
  177. if (++complaints > 5) {
  178. printk(KERN_WARNING "nfsd: disabling repcache.\n");
  179. cache_disabled = 1;
  180. }
  181. goto out;
  182. }
  183. rqstp->rq_cacherep = rp;
  184. rp->c_state = RC_INPROG;
  185. rp->c_xid = xid;
  186. rp->c_proc = proc;
  187. rpc_copy_addr((struct sockaddr *)&rp->c_addr, svc_addr(rqstp));
  188. rpc_set_port((struct sockaddr *)&rp->c_addr, rpc_get_port(svc_addr(rqstp)));
  189. rp->c_prot = proto;
  190. rp->c_vers = vers;
  191. rp->c_timestamp = jiffies;
  192. hash_refile(rp);
  193. /* release any buffer */
  194. if (rp->c_type == RC_REPLBUFF) {
  195. kfree(rp->c_replvec.iov_base);
  196. rp->c_replvec.iov_base = NULL;
  197. }
  198. rp->c_type = RC_NOCACHE;
  199. out:
  200. spin_unlock(&cache_lock);
  201. return rtn;
  202. found_entry:
  203. /* We found a matching entry which is either in progress or done. */
  204. age = jiffies - rp->c_timestamp;
  205. rp->c_timestamp = jiffies;
  206. lru_put_end(rp);
  207. rtn = RC_DROPIT;
  208. /* Request being processed or excessive rexmits */
  209. if (rp->c_state == RC_INPROG || age < RC_DELAY)
  210. goto out;
  211. /* From the hall of fame of impractical attacks:
  212. * Is this a user who tries to snoop on the cache? */
  213. rtn = RC_DOIT;
  214. if (!rqstp->rq_secure && rp->c_secure)
  215. goto out;
  216. /* Compose RPC reply header */
  217. switch (rp->c_type) {
  218. case RC_NOCACHE:
  219. break;
  220. case RC_REPLSTAT:
  221. svc_putu32(&rqstp->rq_res.head[0], rp->c_replstat);
  222. rtn = RC_REPLY;
  223. break;
  224. case RC_REPLBUFF:
  225. if (!nfsd_cache_append(rqstp, &rp->c_replvec))
  226. goto out; /* should not happen */
  227. rtn = RC_REPLY;
  228. break;
  229. default:
  230. printk(KERN_WARNING "nfsd: bad repcache type %d\n", rp->c_type);
  231. rp->c_state = RC_UNUSED;
  232. }
  233. goto out;
  234. }
  235. /*
  236. * Update a cache entry. This is called from nfsd_dispatch when
  237. * the procedure has been executed and the complete reply is in
  238. * rqstp->rq_res.
  239. *
  240. * We're copying around data here rather than swapping buffers because
  241. * the toplevel loop requires max-sized buffers, which would be a waste
  242. * of memory for a cache with a max reply size of 100 bytes (diropokres).
  243. *
  244. * If we should start to use different types of cache entries tailored
  245. * specifically for attrstat and fh's, we may save even more space.
  246. *
  247. * Also note that a cachetype of RC_NOCACHE can legally be passed when
  248. * nfsd failed to encode a reply that otherwise would have been cached.
  249. * In this case, nfsd_cache_update is called with statp == NULL.
  250. */
  251. void
  252. nfsd_cache_update(struct svc_rqst *rqstp, int cachetype, __be32 *statp)
  253. {
  254. struct svc_cacherep *rp;
  255. struct kvec *resv = &rqstp->rq_res.head[0], *cachv;
  256. int len;
  257. if (!(rp = rqstp->rq_cacherep) || cache_disabled)
  258. return;
  259. len = resv->iov_len - ((char*)statp - (char*)resv->iov_base);
  260. len >>= 2;
  261. /* Don't cache excessive amounts of data and XDR failures */
  262. if (!statp || len > (256 >> 2)) {
  263. rp->c_state = RC_UNUSED;
  264. return;
  265. }
  266. switch (cachetype) {
  267. case RC_REPLSTAT:
  268. if (len != 1)
  269. printk("nfsd: RC_REPLSTAT/reply len %d!\n",len);
  270. rp->c_replstat = *statp;
  271. break;
  272. case RC_REPLBUFF:
  273. cachv = &rp->c_replvec;
  274. cachv->iov_base = kmalloc(len << 2, GFP_KERNEL);
  275. if (!cachv->iov_base) {
  276. rp->c_state = RC_UNUSED;
  277. return;
  278. }
  279. cachv->iov_len = len << 2;
  280. memcpy(cachv->iov_base, statp, len << 2);
  281. break;
  282. }
  283. spin_lock(&cache_lock);
  284. lru_put_end(rp);
  285. rp->c_secure = rqstp->rq_secure;
  286. rp->c_type = cachetype;
  287. rp->c_state = RC_DONE;
  288. rp->c_timestamp = jiffies;
  289. spin_unlock(&cache_lock);
  290. return;
  291. }
  292. /*
  293. * Copy cached reply to current reply buffer. Should always fit.
  294. * FIXME as reply is in a page, we should just attach the page, and
  295. * keep a refcount....
  296. */
  297. static int
  298. nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *data)
  299. {
  300. struct kvec *vec = &rqstp->rq_res.head[0];
  301. if (vec->iov_len + data->iov_len > PAGE_SIZE) {
  302. printk(KERN_WARNING "nfsd: cached reply too large (%Zd).\n",
  303. data->iov_len);
  304. return 0;
  305. }
  306. memcpy((char*)vec->iov_base + vec->iov_len, data->iov_base, data->iov_len);
  307. vec->iov_len += data->iov_len;
  308. return 1;
  309. }