nfscache.c 8.9 KB

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