nfscache.c 13 KB

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