nfscache.c 11 KB

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