nfscache.c 9.8 KB

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