nfscache.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Request reply cache. This is currently a global cache, but this may
  4. * change in the future and be a per-client cache.
  5. *
  6. * This code is heavily inspired by the 44BSD implementation, although
  7. * it does things a bit differently.
  8. *
  9. * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
  10. */
  11. #include <linux/sunrpc/svc_xprt.h>
  12. #include <linux/slab.h>
  13. #include <linux/vmalloc.h>
  14. #include <linux/sunrpc/addr.h>
  15. #include <linux/highmem.h>
  16. #include <linux/log2.h>
  17. #include <linux/hash.h>
  18. #include <net/checksum.h>
  19. #include "nfsd.h"
  20. #include "cache.h"
  21. #include "trace.h"
  22. /*
  23. * We use this value to determine the number of hash buckets from the max
  24. * cache size, the idea being that when the cache is at its maximum number
  25. * of entries, then this should be the average number of entries per bucket.
  26. */
  27. #define TARGET_BUCKET_SIZE 64
  28. struct nfsd_drc_bucket {
  29. struct rb_root rb_head;
  30. struct list_head lru_head;
  31. spinlock_t cache_lock;
  32. };
  33. static struct kmem_cache *drc_slab;
  34. static int nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *vec);
  35. static unsigned long nfsd_reply_cache_count(struct shrinker *shrink,
  36. struct shrink_control *sc);
  37. static unsigned long nfsd_reply_cache_scan(struct shrinker *shrink,
  38. struct shrink_control *sc);
  39. /*
  40. * Put a cap on the size of the DRC based on the amount of available
  41. * low memory in the machine.
  42. *
  43. * 64MB: 8192
  44. * 128MB: 11585
  45. * 256MB: 16384
  46. * 512MB: 23170
  47. * 1GB: 32768
  48. * 2GB: 46340
  49. * 4GB: 65536
  50. * 8GB: 92681
  51. * 16GB: 131072
  52. *
  53. * ...with a hard cap of 256k entries. In the worst case, each entry will be
  54. * ~1k, so the above numbers should give a rough max of the amount of memory
  55. * used in k.
  56. *
  57. * XXX: these limits are per-container, so memory used will increase
  58. * linearly with number of containers. Maybe that's OK.
  59. */
  60. static unsigned int
  61. nfsd_cache_size_limit(void)
  62. {
  63. unsigned int limit;
  64. unsigned long low_pages = totalram_pages() - totalhigh_pages();
  65. limit = (16 * int_sqrt(low_pages)) << (PAGE_SHIFT-10);
  66. return min_t(unsigned int, limit, 256*1024);
  67. }
  68. /*
  69. * Compute the number of hash buckets we need. Divide the max cachesize by
  70. * the "target" max bucket size, and round up to next power of two.
  71. */
  72. static unsigned int
  73. nfsd_hashsize(unsigned int limit)
  74. {
  75. return roundup_pow_of_two(limit / TARGET_BUCKET_SIZE);
  76. }
  77. static struct nfsd_cacherep *
  78. nfsd_cacherep_alloc(struct svc_rqst *rqstp, __wsum csum,
  79. struct nfsd_net *nn)
  80. {
  81. struct nfsd_cacherep *rp;
  82. rp = kmem_cache_alloc(drc_slab, GFP_KERNEL);
  83. if (rp) {
  84. rp->c_state = RC_UNUSED;
  85. rp->c_type = RC_NOCACHE;
  86. RB_CLEAR_NODE(&rp->c_node);
  87. INIT_LIST_HEAD(&rp->c_lru);
  88. memset(&rp->c_key, 0, sizeof(rp->c_key));
  89. rp->c_key.k_xid = rqstp->rq_xid;
  90. rp->c_key.k_proc = rqstp->rq_proc;
  91. rpc_copy_addr((struct sockaddr *)&rp->c_key.k_addr, svc_addr(rqstp));
  92. rpc_set_port((struct sockaddr *)&rp->c_key.k_addr, rpc_get_port(svc_addr(rqstp)));
  93. rp->c_key.k_prot = rqstp->rq_prot;
  94. rp->c_key.k_vers = rqstp->rq_vers;
  95. rp->c_key.k_len = rqstp->rq_arg.len;
  96. rp->c_key.k_csum = csum;
  97. }
  98. return rp;
  99. }
  100. static void nfsd_cacherep_free(struct nfsd_cacherep *rp)
  101. {
  102. if (rp->c_type == RC_REPLBUFF)
  103. kfree(rp->c_replvec.iov_base);
  104. kmem_cache_free(drc_slab, rp);
  105. }
  106. static unsigned long
  107. nfsd_cacherep_dispose(struct list_head *dispose)
  108. {
  109. struct nfsd_cacherep *rp;
  110. unsigned long freed = 0;
  111. while (!list_empty(dispose)) {
  112. rp = list_first_entry(dispose, struct nfsd_cacherep, c_lru);
  113. list_del(&rp->c_lru);
  114. nfsd_cacherep_free(rp);
  115. freed++;
  116. }
  117. return freed;
  118. }
  119. static void
  120. nfsd_cacherep_unlink_locked(struct nfsd_net *nn, struct nfsd_drc_bucket *b,
  121. struct nfsd_cacherep *rp)
  122. {
  123. if (rp->c_type == RC_REPLBUFF && rp->c_replvec.iov_base)
  124. nfsd_stats_drc_mem_usage_sub(nn, rp->c_replvec.iov_len);
  125. if (rp->c_state != RC_UNUSED) {
  126. rb_erase(&rp->c_node, &b->rb_head);
  127. list_del(&rp->c_lru);
  128. atomic_dec(&nn->num_drc_entries);
  129. nfsd_stats_drc_mem_usage_sub(nn, sizeof(*rp));
  130. }
  131. }
  132. static void
  133. nfsd_reply_cache_free_locked(struct nfsd_drc_bucket *b, struct nfsd_cacherep *rp,
  134. struct nfsd_net *nn)
  135. {
  136. nfsd_cacherep_unlink_locked(nn, b, rp);
  137. nfsd_cacherep_free(rp);
  138. }
  139. static void
  140. nfsd_reply_cache_free(struct nfsd_drc_bucket *b, struct nfsd_cacherep *rp,
  141. struct nfsd_net *nn)
  142. {
  143. spin_lock(&b->cache_lock);
  144. nfsd_cacherep_unlink_locked(nn, b, rp);
  145. spin_unlock(&b->cache_lock);
  146. nfsd_cacherep_free(rp);
  147. }
  148. int nfsd_drc_slab_create(void)
  149. {
  150. drc_slab = KMEM_CACHE(nfsd_cacherep, 0);
  151. return drc_slab ? 0: -ENOMEM;
  152. }
  153. void nfsd_drc_slab_free(void)
  154. {
  155. kmem_cache_destroy(drc_slab);
  156. }
  157. int nfsd_reply_cache_init(struct nfsd_net *nn)
  158. {
  159. unsigned int hashsize;
  160. unsigned int i;
  161. nn->max_drc_entries = nfsd_cache_size_limit();
  162. atomic_set(&nn->num_drc_entries, 0);
  163. hashsize = nfsd_hashsize(nn->max_drc_entries);
  164. nn->maskbits = ilog2(hashsize);
  165. nn->drc_hashtbl = kvzalloc(array_size(hashsize,
  166. sizeof(*nn->drc_hashtbl)), GFP_KERNEL);
  167. if (!nn->drc_hashtbl)
  168. return -ENOMEM;
  169. nn->nfsd_reply_cache_shrinker = shrinker_alloc(0, "nfsd-reply:%s",
  170. nn->nfsd_name);
  171. if (!nn->nfsd_reply_cache_shrinker)
  172. goto out_shrinker;
  173. nn->nfsd_reply_cache_shrinker->scan_objects = nfsd_reply_cache_scan;
  174. nn->nfsd_reply_cache_shrinker->count_objects = nfsd_reply_cache_count;
  175. nn->nfsd_reply_cache_shrinker->seeks = 1;
  176. nn->nfsd_reply_cache_shrinker->private_data = nn;
  177. shrinker_register(nn->nfsd_reply_cache_shrinker);
  178. for (i = 0; i < hashsize; i++) {
  179. INIT_LIST_HEAD(&nn->drc_hashtbl[i].lru_head);
  180. spin_lock_init(&nn->drc_hashtbl[i].cache_lock);
  181. }
  182. nn->drc_hashsize = hashsize;
  183. return 0;
  184. out_shrinker:
  185. kvfree(nn->drc_hashtbl);
  186. printk(KERN_ERR "nfsd: failed to allocate reply cache\n");
  187. return -ENOMEM;
  188. }
  189. void nfsd_reply_cache_shutdown(struct nfsd_net *nn)
  190. {
  191. struct nfsd_cacherep *rp;
  192. unsigned int i;
  193. shrinker_free(nn->nfsd_reply_cache_shrinker);
  194. for (i = 0; i < nn->drc_hashsize; i++) {
  195. struct list_head *head = &nn->drc_hashtbl[i].lru_head;
  196. while (!list_empty(head)) {
  197. rp = list_first_entry(head, struct nfsd_cacherep, c_lru);
  198. nfsd_reply_cache_free_locked(&nn->drc_hashtbl[i],
  199. rp, nn);
  200. }
  201. }
  202. kvfree(nn->drc_hashtbl);
  203. nn->drc_hashtbl = NULL;
  204. nn->drc_hashsize = 0;
  205. }
  206. /*
  207. * Move cache entry to end of LRU list, and queue the cleaner to run if it's
  208. * not already scheduled.
  209. */
  210. static void
  211. lru_put_end(struct nfsd_drc_bucket *b, struct nfsd_cacherep *rp)
  212. {
  213. rp->c_timestamp = jiffies;
  214. list_move_tail(&rp->c_lru, &b->lru_head);
  215. }
  216. static noinline struct nfsd_drc_bucket *
  217. nfsd_cache_bucket_find(__be32 xid, struct nfsd_net *nn)
  218. {
  219. unsigned int hash = hash_32((__force u32)xid, nn->maskbits);
  220. return &nn->drc_hashtbl[hash];
  221. }
  222. /*
  223. * Remove and return no more than @max expired entries in bucket @b.
  224. * If @max is zero, do not limit the number of removed entries.
  225. */
  226. static void
  227. nfsd_prune_bucket_locked(struct nfsd_net *nn, struct nfsd_drc_bucket *b,
  228. unsigned int max, struct list_head *dispose)
  229. {
  230. unsigned long expiry = jiffies - RC_EXPIRE;
  231. struct nfsd_cacherep *rp, *tmp;
  232. unsigned int freed = 0;
  233. lockdep_assert_held(&b->cache_lock);
  234. /* The bucket LRU is ordered oldest-first. */
  235. list_for_each_entry_safe(rp, tmp, &b->lru_head, c_lru) {
  236. /*
  237. * Don't free entries attached to calls that are still
  238. * in-progress, but do keep scanning the list.
  239. */
  240. if (rp->c_state == RC_INPROG)
  241. continue;
  242. if (atomic_read(&nn->num_drc_entries) <= nn->max_drc_entries &&
  243. time_before(expiry, rp->c_timestamp))
  244. break;
  245. nfsd_cacherep_unlink_locked(nn, b, rp);
  246. list_add(&rp->c_lru, dispose);
  247. if (max && ++freed > max)
  248. break;
  249. }
  250. }
  251. /**
  252. * nfsd_reply_cache_count - count_objects method for the DRC shrinker
  253. * @shrink: our registered shrinker context
  254. * @sc: garbage collection parameters
  255. *
  256. * Returns the total number of entries in the duplicate reply cache. To
  257. * keep things simple and quick, this is not the number of expired entries
  258. * in the cache (ie, the number that would be removed by a call to
  259. * nfsd_reply_cache_scan).
  260. */
  261. static unsigned long
  262. nfsd_reply_cache_count(struct shrinker *shrink, struct shrink_control *sc)
  263. {
  264. struct nfsd_net *nn = shrink->private_data;
  265. return atomic_read(&nn->num_drc_entries);
  266. }
  267. /**
  268. * nfsd_reply_cache_scan - scan_objects method for the DRC shrinker
  269. * @shrink: our registered shrinker context
  270. * @sc: garbage collection parameters
  271. *
  272. * Free expired entries on each bucket's LRU list until we've released
  273. * nr_to_scan freed objects. Nothing will be released if the cache
  274. * has not exceeded it's max_drc_entries limit.
  275. *
  276. * Returns the number of entries released by this call.
  277. */
  278. static unsigned long
  279. nfsd_reply_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
  280. {
  281. struct nfsd_net *nn = shrink->private_data;
  282. unsigned long freed = 0;
  283. LIST_HEAD(dispose);
  284. unsigned int i;
  285. for (i = 0; i < nn->drc_hashsize; i++) {
  286. struct nfsd_drc_bucket *b = &nn->drc_hashtbl[i];
  287. if (list_empty(&b->lru_head))
  288. continue;
  289. spin_lock(&b->cache_lock);
  290. nfsd_prune_bucket_locked(nn, b, 0, &dispose);
  291. spin_unlock(&b->cache_lock);
  292. freed += nfsd_cacherep_dispose(&dispose);
  293. if (freed > sc->nr_to_scan)
  294. break;
  295. }
  296. return freed;
  297. }
  298. /**
  299. * nfsd_cache_csum - Checksum incoming NFS Call arguments
  300. * @buf: buffer containing a whole RPC Call message
  301. * @start: starting byte of the NFS Call header
  302. * @remaining: size of the NFS Call header, in bytes
  303. *
  304. * Compute a weak checksum of the leading bytes of an NFS procedure
  305. * call header to help verify that a retransmitted Call matches an
  306. * entry in the duplicate reply cache.
  307. *
  308. * To avoid assumptions about how the RPC message is laid out in
  309. * @buf and what else it might contain (eg, a GSS MIC suffix), the
  310. * caller passes us the exact location and length of the NFS Call
  311. * header.
  312. *
  313. * Returns a 32-bit checksum value, as defined in RFC 793.
  314. */
  315. static __wsum nfsd_cache_csum(struct xdr_buf *buf, unsigned int start,
  316. unsigned int remaining)
  317. {
  318. unsigned int base, len;
  319. struct xdr_buf subbuf;
  320. __wsum csum = 0;
  321. void *p;
  322. int idx;
  323. if (remaining > RC_CSUMLEN)
  324. remaining = RC_CSUMLEN;
  325. if (xdr_buf_subsegment(buf, &subbuf, start, remaining))
  326. return csum;
  327. /* rq_arg.head first */
  328. if (subbuf.head[0].iov_len) {
  329. len = min_t(unsigned int, subbuf.head[0].iov_len, remaining);
  330. csum = csum_partial(subbuf.head[0].iov_base, len, csum);
  331. remaining -= len;
  332. }
  333. /* Continue into page array */
  334. idx = subbuf.page_base / PAGE_SIZE;
  335. base = subbuf.page_base & ~PAGE_MASK;
  336. while (remaining) {
  337. p = page_address(subbuf.pages[idx]) + base;
  338. len = min_t(unsigned int, PAGE_SIZE - base, remaining);
  339. csum = csum_partial(p, len, csum);
  340. remaining -= len;
  341. base = 0;
  342. ++idx;
  343. }
  344. return csum;
  345. }
  346. static int
  347. nfsd_cache_key_cmp(const struct nfsd_cacherep *key,
  348. const struct nfsd_cacherep *rp, struct nfsd_net *nn)
  349. {
  350. if (key->c_key.k_xid == rp->c_key.k_xid &&
  351. key->c_key.k_csum != rp->c_key.k_csum) {
  352. nfsd_stats_payload_misses_inc(nn);
  353. trace_nfsd_drc_mismatch(nn, key, rp);
  354. }
  355. return memcmp(&key->c_key, &rp->c_key, sizeof(key->c_key));
  356. }
  357. /*
  358. * Search the request hash for an entry that matches the given rqstp.
  359. * Must be called with cache_lock held. Returns the found entry or
  360. * inserts an empty key on failure.
  361. */
  362. static struct nfsd_cacherep *
  363. nfsd_cache_insert(struct nfsd_drc_bucket *b, struct nfsd_cacherep *key,
  364. struct nfsd_net *nn)
  365. {
  366. struct nfsd_cacherep *rp, *ret = key;
  367. struct rb_node **p = &b->rb_head.rb_node,
  368. *parent = NULL;
  369. unsigned int entries = 0;
  370. int cmp;
  371. while (*p != NULL) {
  372. ++entries;
  373. parent = *p;
  374. rp = rb_entry(parent, struct nfsd_cacherep, c_node);
  375. cmp = nfsd_cache_key_cmp(key, rp, nn);
  376. if (cmp < 0)
  377. p = &parent->rb_left;
  378. else if (cmp > 0)
  379. p = &parent->rb_right;
  380. else {
  381. ret = rp;
  382. goto out;
  383. }
  384. }
  385. rb_link_node(&key->c_node, parent, p);
  386. rb_insert_color(&key->c_node, &b->rb_head);
  387. out:
  388. /* tally hash chain length stats */
  389. if (entries > nn->longest_chain) {
  390. nn->longest_chain = entries;
  391. nn->longest_chain_cachesize = atomic_read(&nn->num_drc_entries);
  392. } else if (entries == nn->longest_chain) {
  393. /* prefer to keep the smallest cachesize possible here */
  394. nn->longest_chain_cachesize = min_t(unsigned int,
  395. nn->longest_chain_cachesize,
  396. atomic_read(&nn->num_drc_entries));
  397. }
  398. lru_put_end(b, ret);
  399. return ret;
  400. }
  401. /**
  402. * nfsd_cache_lookup - Find an entry in the duplicate reply cache
  403. * @rqstp: Incoming Call to find
  404. * @start: starting byte in @rqstp->rq_arg of the NFS Call header
  405. * @len: size of the NFS Call header, in bytes
  406. * @cacherep: OUT: DRC entry for this request
  407. *
  408. * Try to find an entry matching the current call in the cache. When none
  409. * is found, we try to grab the oldest expired entry off the LRU list. If
  410. * a suitable one isn't there, then drop the cache_lock and allocate a
  411. * new one, then search again in case one got inserted while this thread
  412. * didn't hold the lock.
  413. *
  414. * Return values:
  415. * %RC_DOIT: Process the request normally
  416. * %RC_REPLY: Reply from cache
  417. * %RC_DROPIT: Do not process the request further
  418. */
  419. int nfsd_cache_lookup(struct svc_rqst *rqstp, unsigned int start,
  420. unsigned int len, struct nfsd_cacherep **cacherep)
  421. {
  422. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  423. struct nfsd_cacherep *rp, *found;
  424. __wsum csum;
  425. struct nfsd_drc_bucket *b;
  426. int type = rqstp->rq_cachetype;
  427. LIST_HEAD(dispose);
  428. int rtn = RC_DOIT;
  429. if (type == RC_NOCACHE) {
  430. nfsd_stats_rc_nocache_inc(nn);
  431. goto out;
  432. }
  433. csum = nfsd_cache_csum(&rqstp->rq_arg, start, len);
  434. /*
  435. * Since the common case is a cache miss followed by an insert,
  436. * preallocate an entry.
  437. */
  438. rp = nfsd_cacherep_alloc(rqstp, csum, nn);
  439. if (!rp)
  440. goto out;
  441. b = nfsd_cache_bucket_find(rqstp->rq_xid, nn);
  442. spin_lock(&b->cache_lock);
  443. found = nfsd_cache_insert(b, rp, nn);
  444. if (found != rp)
  445. goto found_entry;
  446. *cacherep = rp;
  447. rp->c_state = RC_INPROG;
  448. nfsd_prune_bucket_locked(nn, b, 3, &dispose);
  449. spin_unlock(&b->cache_lock);
  450. nfsd_cacherep_dispose(&dispose);
  451. nfsd_stats_rc_misses_inc(nn);
  452. atomic_inc(&nn->num_drc_entries);
  453. nfsd_stats_drc_mem_usage_add(nn, sizeof(*rp));
  454. goto out;
  455. found_entry:
  456. /* We found a matching entry which is either in progress or done. */
  457. nfsd_reply_cache_free_locked(NULL, rp, nn);
  458. nfsd_stats_rc_hits_inc(nn);
  459. rtn = RC_DROPIT;
  460. rp = found;
  461. /* Request being processed */
  462. if (rp->c_state == RC_INPROG)
  463. goto out_trace;
  464. /* From the hall of fame of impractical attacks:
  465. * Is this a user who tries to snoop on the cache? */
  466. rtn = RC_DOIT;
  467. if (!test_bit(RQ_SECURE, &rqstp->rq_flags) && rp->c_secure)
  468. goto out_trace;
  469. /* Compose RPC reply header */
  470. switch (rp->c_type) {
  471. case RC_NOCACHE:
  472. break;
  473. case RC_REPLSTAT:
  474. xdr_stream_encode_be32(&rqstp->rq_res_stream, rp->c_replstat);
  475. rtn = RC_REPLY;
  476. break;
  477. case RC_REPLBUFF:
  478. if (!nfsd_cache_append(rqstp, &rp->c_replvec))
  479. goto out_unlock; /* should not happen */
  480. rtn = RC_REPLY;
  481. break;
  482. default:
  483. WARN_ONCE(1, "nfsd: bad repcache type %d\n", rp->c_type);
  484. }
  485. out_trace:
  486. trace_nfsd_drc_found(nn, rqstp, rtn);
  487. out_unlock:
  488. spin_unlock(&b->cache_lock);
  489. out:
  490. return rtn;
  491. }
  492. /**
  493. * nfsd_cache_update - Update an entry in the duplicate reply cache.
  494. * @rqstp: svc_rqst with a finished Reply
  495. * @rp: IN: DRC entry for this request
  496. * @cachetype: which cache to update
  497. * @statp: pointer to Reply's NFS status code, or NULL
  498. *
  499. * This is called from nfsd_dispatch when the procedure has been
  500. * executed and the complete reply is in rqstp->rq_res.
  501. *
  502. * We're copying around data here rather than swapping buffers because
  503. * the toplevel loop requires max-sized buffers, which would be a waste
  504. * of memory for a cache with a max reply size of 100 bytes (diropokres).
  505. *
  506. * If we should start to use different types of cache entries tailored
  507. * specifically for attrstat and fh's, we may save even more space.
  508. *
  509. * Also note that a cachetype of RC_NOCACHE can legally be passed when
  510. * nfsd failed to encode a reply that otherwise would have been cached.
  511. * In this case, nfsd_cache_update is called with statp == NULL.
  512. */
  513. void nfsd_cache_update(struct svc_rqst *rqstp, struct nfsd_cacherep *rp,
  514. int cachetype, __be32 *statp)
  515. {
  516. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  517. struct kvec *resv = &rqstp->rq_res.head[0], *cachv;
  518. struct nfsd_drc_bucket *b;
  519. int len;
  520. size_t bufsize = 0;
  521. if (!rp)
  522. return;
  523. b = nfsd_cache_bucket_find(rp->c_key.k_xid, nn);
  524. len = resv->iov_len - ((char*)statp - (char*)resv->iov_base);
  525. len >>= 2;
  526. /* Don't cache excessive amounts of data and XDR failures */
  527. if (!statp || len > (256 >> 2)) {
  528. nfsd_reply_cache_free(b, rp, nn);
  529. return;
  530. }
  531. switch (cachetype) {
  532. case RC_REPLSTAT:
  533. if (len != 1)
  534. printk("nfsd: RC_REPLSTAT/reply len %d!\n",len);
  535. rp->c_replstat = *statp;
  536. break;
  537. case RC_REPLBUFF:
  538. cachv = &rp->c_replvec;
  539. bufsize = len << 2;
  540. cachv->iov_base = kmalloc(bufsize, GFP_KERNEL);
  541. if (!cachv->iov_base) {
  542. nfsd_reply_cache_free(b, rp, nn);
  543. return;
  544. }
  545. cachv->iov_len = bufsize;
  546. memcpy(cachv->iov_base, statp, bufsize);
  547. break;
  548. case RC_NOCACHE:
  549. nfsd_reply_cache_free(b, rp, nn);
  550. return;
  551. }
  552. spin_lock(&b->cache_lock);
  553. nfsd_stats_drc_mem_usage_add(nn, bufsize);
  554. lru_put_end(b, rp);
  555. rp->c_secure = test_bit(RQ_SECURE, &rqstp->rq_flags);
  556. rp->c_type = cachetype;
  557. rp->c_state = RC_DONE;
  558. spin_unlock(&b->cache_lock);
  559. return;
  560. }
  561. static int
  562. nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *data)
  563. {
  564. __be32 *p;
  565. p = xdr_reserve_space(&rqstp->rq_res_stream, data->iov_len);
  566. if (unlikely(!p))
  567. return false;
  568. memcpy(p, data->iov_base, data->iov_len);
  569. xdr_commit_encode(&rqstp->rq_res_stream);
  570. return true;
  571. }
  572. /*
  573. * Note that fields may be added, removed or reordered in the future. Programs
  574. * scraping this file for info should test the labels to ensure they're
  575. * getting the correct field.
  576. */
  577. int nfsd_reply_cache_stats_show(struct seq_file *m, void *v)
  578. {
  579. struct nfsd_net *nn = net_generic(file_inode(m->file)->i_sb->s_fs_info,
  580. nfsd_net_id);
  581. seq_printf(m, "max entries: %u\n", nn->max_drc_entries);
  582. seq_printf(m, "num entries: %u\n",
  583. atomic_read(&nn->num_drc_entries));
  584. seq_printf(m, "hash buckets: %u\n", 1 << nn->maskbits);
  585. seq_printf(m, "mem usage: %lld\n",
  586. percpu_counter_sum_positive(&nn->counter[NFSD_STATS_DRC_MEM_USAGE]));
  587. seq_printf(m, "cache hits: %lld\n",
  588. percpu_counter_sum_positive(&nn->counter[NFSD_STATS_RC_HITS]));
  589. seq_printf(m, "cache misses: %lld\n",
  590. percpu_counter_sum_positive(&nn->counter[NFSD_STATS_RC_MISSES]));
  591. seq_printf(m, "not cached: %lld\n",
  592. percpu_counter_sum_positive(&nn->counter[NFSD_STATS_RC_NOCACHE]));
  593. seq_printf(m, "payload misses: %lld\n",
  594. percpu_counter_sum_positive(&nn->counter[NFSD_STATS_PAYLOAD_MISSES]));
  595. seq_printf(m, "longest chain len: %u\n", nn->longest_chain);
  596. seq_printf(m, "cachesize at longest: %u\n", nn->longest_chain_cachesize);
  597. return 0;
  598. }