nfscache.c 8.4 KB

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