nfscache.c 8.8 KB

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