nfscache.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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/addr.h>
  12. #include <linux/highmem.h>
  13. #include <net/checksum.h>
  14. #include "nfsd.h"
  15. #include "cache.h"
  16. #define NFSDDBG_FACILITY NFSDDBG_REPCACHE
  17. #define HASHSIZE 64
  18. static struct hlist_head * cache_hash;
  19. static struct list_head lru_head;
  20. static struct kmem_cache *drc_slab;
  21. /* max number of entries allowed in the cache */
  22. static unsigned int max_drc_entries;
  23. /*
  24. * Stats and other tracking of on the duplicate reply cache. All of these and
  25. * the "rc" fields in nfsdstats are protected by the cache_lock
  26. */
  27. /* total number of entries */
  28. static unsigned int num_drc_entries;
  29. /* cache misses due only to checksum comparison failures */
  30. static unsigned int payload_misses;
  31. /*
  32. * Calculate the hash index from an XID.
  33. */
  34. static inline u32 request_hash(u32 xid)
  35. {
  36. u32 h = xid;
  37. h ^= (xid >> 24);
  38. return h & (HASHSIZE-1);
  39. }
  40. static int nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *vec);
  41. static void cache_cleaner_func(struct work_struct *unused);
  42. static int nfsd_reply_cache_shrink(struct shrinker *shrink,
  43. struct shrink_control *sc);
  44. struct shrinker nfsd_reply_cache_shrinker = {
  45. .shrink = nfsd_reply_cache_shrink,
  46. .seeks = 1,
  47. };
  48. /*
  49. * locking for the reply cache:
  50. * A cache entry is "single use" if c_state == RC_INPROG
  51. * Otherwise, it when accessing _prev or _next, the lock must be held.
  52. */
  53. static DEFINE_SPINLOCK(cache_lock);
  54. static DECLARE_DELAYED_WORK(cache_cleaner, cache_cleaner_func);
  55. /*
  56. * Put a cap on the size of the DRC based on the amount of available
  57. * low memory in the machine.
  58. *
  59. * 64MB: 8192
  60. * 128MB: 11585
  61. * 256MB: 16384
  62. * 512MB: 23170
  63. * 1GB: 32768
  64. * 2GB: 46340
  65. * 4GB: 65536
  66. * 8GB: 92681
  67. * 16GB: 131072
  68. *
  69. * ...with a hard cap of 256k entries. In the worst case, each entry will be
  70. * ~1k, so the above numbers should give a rough max of the amount of memory
  71. * used in k.
  72. */
  73. static unsigned int
  74. nfsd_cache_size_limit(void)
  75. {
  76. unsigned int limit;
  77. unsigned long low_pages = totalram_pages - totalhigh_pages;
  78. limit = (16 * int_sqrt(low_pages)) << (PAGE_SHIFT-10);
  79. return min_t(unsigned int, limit, 256*1024);
  80. }
  81. static struct svc_cacherep *
  82. nfsd_reply_cache_alloc(void)
  83. {
  84. struct svc_cacherep *rp;
  85. rp = kmem_cache_alloc(drc_slab, GFP_KERNEL);
  86. if (rp) {
  87. rp->c_state = RC_UNUSED;
  88. rp->c_type = RC_NOCACHE;
  89. INIT_LIST_HEAD(&rp->c_lru);
  90. INIT_HLIST_NODE(&rp->c_hash);
  91. }
  92. return rp;
  93. }
  94. static void
  95. nfsd_reply_cache_free_locked(struct svc_cacherep *rp)
  96. {
  97. if (rp->c_type == RC_REPLBUFF)
  98. kfree(rp->c_replvec.iov_base);
  99. if (!hlist_unhashed(&rp->c_hash))
  100. hlist_del(&rp->c_hash);
  101. list_del(&rp->c_lru);
  102. --num_drc_entries;
  103. kmem_cache_free(drc_slab, rp);
  104. }
  105. static void
  106. nfsd_reply_cache_free(struct svc_cacherep *rp)
  107. {
  108. spin_lock(&cache_lock);
  109. nfsd_reply_cache_free_locked(rp);
  110. spin_unlock(&cache_lock);
  111. }
  112. int nfsd_reply_cache_init(void)
  113. {
  114. INIT_LIST_HEAD(&lru_head);
  115. max_drc_entries = nfsd_cache_size_limit();
  116. num_drc_entries = 0;
  117. register_shrinker(&nfsd_reply_cache_shrinker);
  118. drc_slab = kmem_cache_create("nfsd_drc", sizeof(struct svc_cacherep),
  119. 0, 0, NULL);
  120. if (!drc_slab)
  121. goto out_nomem;
  122. cache_hash = kcalloc(HASHSIZE, sizeof(struct hlist_head), GFP_KERNEL);
  123. if (!cache_hash)
  124. goto out_nomem;
  125. return 0;
  126. out_nomem:
  127. printk(KERN_ERR "nfsd: failed to allocate reply cache\n");
  128. nfsd_reply_cache_shutdown();
  129. return -ENOMEM;
  130. }
  131. void nfsd_reply_cache_shutdown(void)
  132. {
  133. struct svc_cacherep *rp;
  134. unregister_shrinker(&nfsd_reply_cache_shrinker);
  135. cancel_delayed_work_sync(&cache_cleaner);
  136. while (!list_empty(&lru_head)) {
  137. rp = list_entry(lru_head.next, struct svc_cacherep, c_lru);
  138. nfsd_reply_cache_free_locked(rp);
  139. }
  140. kfree (cache_hash);
  141. cache_hash = NULL;
  142. if (drc_slab) {
  143. kmem_cache_destroy(drc_slab);
  144. drc_slab = NULL;
  145. }
  146. }
  147. /*
  148. * Move cache entry to end of LRU list, and queue the cleaner to run if it's
  149. * not already scheduled.
  150. */
  151. static void
  152. lru_put_end(struct svc_cacherep *rp)
  153. {
  154. rp->c_timestamp = jiffies;
  155. list_move_tail(&rp->c_lru, &lru_head);
  156. schedule_delayed_work(&cache_cleaner, RC_EXPIRE);
  157. }
  158. /*
  159. * Move a cache entry from one hash list to another
  160. */
  161. static void
  162. hash_refile(struct svc_cacherep *rp)
  163. {
  164. hlist_del_init(&rp->c_hash);
  165. hlist_add_head(&rp->c_hash, cache_hash + request_hash(rp->c_xid));
  166. }
  167. static inline bool
  168. nfsd_cache_entry_expired(struct svc_cacherep *rp)
  169. {
  170. return rp->c_state != RC_INPROG &&
  171. time_after(jiffies, rp->c_timestamp + RC_EXPIRE);
  172. }
  173. /*
  174. * Walk the LRU list and prune off entries that are older than RC_EXPIRE.
  175. * Also prune the oldest ones when the total exceeds the max number of entries.
  176. */
  177. static void
  178. prune_cache_entries(void)
  179. {
  180. struct svc_cacherep *rp, *tmp;
  181. list_for_each_entry_safe(rp, tmp, &lru_head, c_lru) {
  182. if (!nfsd_cache_entry_expired(rp) &&
  183. num_drc_entries <= max_drc_entries)
  184. break;
  185. nfsd_reply_cache_free_locked(rp);
  186. }
  187. /*
  188. * Conditionally rearm the job. If we cleaned out the list, then
  189. * cancel any pending run (since there won't be any work to do).
  190. * Otherwise, we rearm the job or modify the existing one to run in
  191. * RC_EXPIRE since we just ran the pruner.
  192. */
  193. if (list_empty(&lru_head))
  194. cancel_delayed_work(&cache_cleaner);
  195. else
  196. mod_delayed_work(system_wq, &cache_cleaner, RC_EXPIRE);
  197. }
  198. static void
  199. cache_cleaner_func(struct work_struct *unused)
  200. {
  201. spin_lock(&cache_lock);
  202. prune_cache_entries();
  203. spin_unlock(&cache_lock);
  204. }
  205. static int
  206. nfsd_reply_cache_shrink(struct shrinker *shrink, struct shrink_control *sc)
  207. {
  208. unsigned int num;
  209. spin_lock(&cache_lock);
  210. if (sc->nr_to_scan)
  211. prune_cache_entries();
  212. num = num_drc_entries;
  213. spin_unlock(&cache_lock);
  214. return num;
  215. }
  216. /*
  217. * Walk an xdr_buf and get a CRC for at most the first RC_CSUMLEN bytes
  218. */
  219. static __wsum
  220. nfsd_cache_csum(struct svc_rqst *rqstp)
  221. {
  222. int idx;
  223. unsigned int base;
  224. __wsum csum;
  225. struct xdr_buf *buf = &rqstp->rq_arg;
  226. const unsigned char *p = buf->head[0].iov_base;
  227. size_t csum_len = min_t(size_t, buf->head[0].iov_len + buf->page_len,
  228. RC_CSUMLEN);
  229. size_t len = min(buf->head[0].iov_len, csum_len);
  230. /* rq_arg.head first */
  231. csum = csum_partial(p, len, 0);
  232. csum_len -= len;
  233. /* Continue into page array */
  234. idx = buf->page_base / PAGE_SIZE;
  235. base = buf->page_base & ~PAGE_MASK;
  236. while (csum_len) {
  237. p = page_address(buf->pages[idx]) + base;
  238. len = min_t(size_t, PAGE_SIZE - base, csum_len);
  239. csum = csum_partial(p, len, csum);
  240. csum_len -= len;
  241. base = 0;
  242. ++idx;
  243. }
  244. return csum;
  245. }
  246. static bool
  247. nfsd_cache_match(struct svc_rqst *rqstp, __wsum csum, struct svc_cacherep *rp)
  248. {
  249. /* Check RPC header info first */
  250. if (rqstp->rq_xid != rp->c_xid || rqstp->rq_proc != rp->c_proc ||
  251. rqstp->rq_prot != rp->c_prot || rqstp->rq_vers != rp->c_vers ||
  252. rqstp->rq_arg.len != rp->c_len ||
  253. !rpc_cmp_addr(svc_addr(rqstp), (struct sockaddr *)&rp->c_addr) ||
  254. rpc_get_port(svc_addr(rqstp)) != rpc_get_port((struct sockaddr *)&rp->c_addr))
  255. return false;
  256. /* compare checksum of NFS data */
  257. if (csum != rp->c_csum) {
  258. ++payload_misses;
  259. return false;
  260. }
  261. return true;
  262. }
  263. /*
  264. * Search the request hash for an entry that matches the given rqstp.
  265. * Must be called with cache_lock held. Returns the found entry or
  266. * NULL on failure.
  267. */
  268. static struct svc_cacherep *
  269. nfsd_cache_search(struct svc_rqst *rqstp, __wsum csum)
  270. {
  271. struct svc_cacherep *rp;
  272. struct hlist_head *rh;
  273. rh = &cache_hash[request_hash(rqstp->rq_xid)];
  274. hlist_for_each_entry(rp, rh, c_hash) {
  275. if (nfsd_cache_match(rqstp, csum, rp))
  276. return rp;
  277. }
  278. return NULL;
  279. }
  280. /*
  281. * Try to find an entry matching the current call in the cache. When none
  282. * is found, we try to grab the oldest expired entry off the LRU list. If
  283. * a suitable one isn't there, then drop the cache_lock and allocate a
  284. * new one, then search again in case one got inserted while this thread
  285. * didn't hold the lock.
  286. */
  287. int
  288. nfsd_cache_lookup(struct svc_rqst *rqstp)
  289. {
  290. struct svc_cacherep *rp, *found;
  291. __be32 xid = rqstp->rq_xid;
  292. u32 proto = rqstp->rq_prot,
  293. vers = rqstp->rq_vers,
  294. proc = rqstp->rq_proc;
  295. __wsum csum;
  296. unsigned long age;
  297. int type = rqstp->rq_cachetype;
  298. int rtn = RC_DOIT;
  299. rqstp->rq_cacherep = NULL;
  300. if (type == RC_NOCACHE) {
  301. nfsdstats.rcnocache++;
  302. return rtn;
  303. }
  304. csum = nfsd_cache_csum(rqstp);
  305. /*
  306. * Since the common case is a cache miss followed by an insert,
  307. * preallocate an entry. First, try to reuse the first entry on the LRU
  308. * if it works, then go ahead and prune the LRU list.
  309. */
  310. spin_lock(&cache_lock);
  311. if (!list_empty(&lru_head)) {
  312. rp = list_first_entry(&lru_head, struct svc_cacherep, c_lru);
  313. if (nfsd_cache_entry_expired(rp) ||
  314. num_drc_entries >= max_drc_entries) {
  315. lru_put_end(rp);
  316. prune_cache_entries();
  317. goto search_cache;
  318. }
  319. }
  320. /* No expired ones available, allocate a new one. */
  321. spin_unlock(&cache_lock);
  322. rp = nfsd_reply_cache_alloc();
  323. spin_lock(&cache_lock);
  324. if (likely(rp))
  325. ++num_drc_entries;
  326. search_cache:
  327. found = nfsd_cache_search(rqstp, csum);
  328. if (found) {
  329. if (likely(rp))
  330. nfsd_reply_cache_free_locked(rp);
  331. rp = found;
  332. goto found_entry;
  333. }
  334. if (!rp) {
  335. dprintk("nfsd: unable to allocate DRC entry!\n");
  336. goto out;
  337. }
  338. /*
  339. * We're keeping the one we just allocated. Are we now over the
  340. * limit? Prune one off the tip of the LRU in trade for the one we
  341. * just allocated if so.
  342. */
  343. if (num_drc_entries >= max_drc_entries)
  344. nfsd_reply_cache_free_locked(list_first_entry(&lru_head,
  345. struct svc_cacherep, c_lru));
  346. nfsdstats.rcmisses++;
  347. rqstp->rq_cacherep = rp;
  348. rp->c_state = RC_INPROG;
  349. rp->c_xid = xid;
  350. rp->c_proc = proc;
  351. rpc_copy_addr((struct sockaddr *)&rp->c_addr, svc_addr(rqstp));
  352. rpc_set_port((struct sockaddr *)&rp->c_addr, rpc_get_port(svc_addr(rqstp)));
  353. rp->c_prot = proto;
  354. rp->c_vers = vers;
  355. rp->c_len = rqstp->rq_arg.len;
  356. rp->c_csum = csum;
  357. hash_refile(rp);
  358. lru_put_end(rp);
  359. /* release any buffer */
  360. if (rp->c_type == RC_REPLBUFF) {
  361. kfree(rp->c_replvec.iov_base);
  362. rp->c_replvec.iov_base = NULL;
  363. }
  364. rp->c_type = RC_NOCACHE;
  365. out:
  366. spin_unlock(&cache_lock);
  367. return rtn;
  368. found_entry:
  369. nfsdstats.rchits++;
  370. /* We found a matching entry which is either in progress or done. */
  371. age = jiffies - rp->c_timestamp;
  372. lru_put_end(rp);
  373. rtn = RC_DROPIT;
  374. /* Request being processed or excessive rexmits */
  375. if (rp->c_state == RC_INPROG || age < RC_DELAY)
  376. goto out;
  377. /* From the hall of fame of impractical attacks:
  378. * Is this a user who tries to snoop on the cache? */
  379. rtn = RC_DOIT;
  380. if (!rqstp->rq_secure && rp->c_secure)
  381. goto out;
  382. /* Compose RPC reply header */
  383. switch (rp->c_type) {
  384. case RC_NOCACHE:
  385. break;
  386. case RC_REPLSTAT:
  387. svc_putu32(&rqstp->rq_res.head[0], rp->c_replstat);
  388. rtn = RC_REPLY;
  389. break;
  390. case RC_REPLBUFF:
  391. if (!nfsd_cache_append(rqstp, &rp->c_replvec))
  392. goto out; /* should not happen */
  393. rtn = RC_REPLY;
  394. break;
  395. default:
  396. printk(KERN_WARNING "nfsd: bad repcache type %d\n", rp->c_type);
  397. nfsd_reply_cache_free_locked(rp);
  398. }
  399. goto out;
  400. }
  401. /*
  402. * Update a cache entry. This is called from nfsd_dispatch when
  403. * the procedure has been executed and the complete reply is in
  404. * rqstp->rq_res.
  405. *
  406. * We're copying around data here rather than swapping buffers because
  407. * the toplevel loop requires max-sized buffers, which would be a waste
  408. * of memory for a cache with a max reply size of 100 bytes (diropokres).
  409. *
  410. * If we should start to use different types of cache entries tailored
  411. * specifically for attrstat and fh's, we may save even more space.
  412. *
  413. * Also note that a cachetype of RC_NOCACHE can legally be passed when
  414. * nfsd failed to encode a reply that otherwise would have been cached.
  415. * In this case, nfsd_cache_update is called with statp == NULL.
  416. */
  417. void
  418. nfsd_cache_update(struct svc_rqst *rqstp, int cachetype, __be32 *statp)
  419. {
  420. struct svc_cacherep *rp = rqstp->rq_cacherep;
  421. struct kvec *resv = &rqstp->rq_res.head[0], *cachv;
  422. int len;
  423. if (!rp)
  424. return;
  425. len = resv->iov_len - ((char*)statp - (char*)resv->iov_base);
  426. len >>= 2;
  427. /* Don't cache excessive amounts of data and XDR failures */
  428. if (!statp || len > (256 >> 2)) {
  429. nfsd_reply_cache_free(rp);
  430. return;
  431. }
  432. switch (cachetype) {
  433. case RC_REPLSTAT:
  434. if (len != 1)
  435. printk("nfsd: RC_REPLSTAT/reply len %d!\n",len);
  436. rp->c_replstat = *statp;
  437. break;
  438. case RC_REPLBUFF:
  439. cachv = &rp->c_replvec;
  440. cachv->iov_base = kmalloc(len << 2, GFP_KERNEL);
  441. if (!cachv->iov_base) {
  442. nfsd_reply_cache_free(rp);
  443. return;
  444. }
  445. cachv->iov_len = len << 2;
  446. memcpy(cachv->iov_base, statp, len << 2);
  447. break;
  448. case RC_NOCACHE:
  449. nfsd_reply_cache_free(rp);
  450. return;
  451. }
  452. spin_lock(&cache_lock);
  453. lru_put_end(rp);
  454. rp->c_secure = rqstp->rq_secure;
  455. rp->c_type = cachetype;
  456. rp->c_state = RC_DONE;
  457. spin_unlock(&cache_lock);
  458. return;
  459. }
  460. /*
  461. * Copy cached reply to current reply buffer. Should always fit.
  462. * FIXME as reply is in a page, we should just attach the page, and
  463. * keep a refcount....
  464. */
  465. static int
  466. nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *data)
  467. {
  468. struct kvec *vec = &rqstp->rq_res.head[0];
  469. if (vec->iov_len + data->iov_len > PAGE_SIZE) {
  470. printk(KERN_WARNING "nfsd: cached reply too large (%Zd).\n",
  471. data->iov_len);
  472. return 0;
  473. }
  474. memcpy((char*)vec->iov_base + vec->iov_len, data->iov_base, data->iov_len);
  475. vec->iov_len += data->iov_len;
  476. return 1;
  477. }