nfscache.c 13 KB

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