nfscache.c 9.7 KB

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