nfscache.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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 8
  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. static void
  207. lru_put_end(struct nfsd_drc_bucket *b, struct nfsd_cacherep *rp)
  208. {
  209. rp->c_timestamp = jiffies;
  210. list_move_tail(&rp->c_lru, &b->lru_head);
  211. }
  212. static noinline struct nfsd_drc_bucket *
  213. nfsd_cache_bucket_find(__be32 xid, struct nfsd_net *nn)
  214. {
  215. unsigned int hash = hash_32((__force u32)xid, nn->maskbits);
  216. return &nn->drc_hashtbl[hash];
  217. }
  218. /*
  219. * Remove and return no more than @max expired entries in bucket @b.
  220. * If @max is zero, do not limit the number of removed entries.
  221. */
  222. static void
  223. nfsd_prune_bucket_locked(struct nfsd_net *nn, struct nfsd_drc_bucket *b,
  224. unsigned int max, struct list_head *dispose)
  225. {
  226. unsigned long expiry = jiffies - RC_EXPIRE;
  227. struct nfsd_cacherep *rp, *tmp;
  228. unsigned int freed = 0;
  229. lockdep_assert_held(&b->cache_lock);
  230. /* The bucket LRU is ordered oldest-first. */
  231. list_for_each_entry_safe(rp, tmp, &b->lru_head, c_lru) {
  232. if (atomic_read(&nn->num_drc_entries) <= nn->max_drc_entries &&
  233. time_before(expiry, rp->c_timestamp))
  234. break;
  235. nfsd_cacherep_unlink_locked(nn, b, rp);
  236. list_add(&rp->c_lru, dispose);
  237. if (max && ++freed > max)
  238. break;
  239. }
  240. }
  241. /**
  242. * nfsd_reply_cache_count - count_objects method for the DRC shrinker
  243. * @shrink: our registered shrinker context
  244. * @sc: garbage collection parameters
  245. *
  246. * Returns the total number of entries in the duplicate reply cache. To
  247. * keep things simple and quick, this is not the number of expired entries
  248. * in the cache (ie, the number that would be removed by a call to
  249. * nfsd_reply_cache_scan).
  250. */
  251. static unsigned long
  252. nfsd_reply_cache_count(struct shrinker *shrink, struct shrink_control *sc)
  253. {
  254. struct nfsd_net *nn = shrink->private_data;
  255. return atomic_read(&nn->num_drc_entries);
  256. }
  257. /**
  258. * nfsd_reply_cache_scan - scan_objects method for the DRC shrinker
  259. * @shrink: our registered shrinker context
  260. * @sc: garbage collection parameters
  261. *
  262. * Free expired entries on each bucket's LRU list until we've released
  263. * nr_to_scan freed objects. Nothing will be released if the cache
  264. * has not exceeded it's max_drc_entries limit.
  265. *
  266. * Returns the number of entries released by this call.
  267. */
  268. static unsigned long
  269. nfsd_reply_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
  270. {
  271. struct nfsd_net *nn = shrink->private_data;
  272. unsigned long freed = 0;
  273. LIST_HEAD(dispose);
  274. unsigned int i;
  275. for (i = 0; i < nn->drc_hashsize; i++) {
  276. struct nfsd_drc_bucket *b = &nn->drc_hashtbl[i];
  277. if (list_empty(&b->lru_head))
  278. continue;
  279. spin_lock(&b->cache_lock);
  280. nfsd_prune_bucket_locked(nn, b, 0, &dispose);
  281. spin_unlock(&b->cache_lock);
  282. freed += nfsd_cacherep_dispose(&dispose);
  283. if (freed > sc->nr_to_scan)
  284. break;
  285. }
  286. return freed;
  287. }
  288. /**
  289. * nfsd_cache_csum - Checksum incoming NFS Call arguments
  290. * @buf: buffer containing a whole RPC Call message
  291. * @start: starting byte of the NFS Call header
  292. * @remaining: size of the NFS Call header, in bytes
  293. *
  294. * Compute a weak checksum of the leading bytes of an NFS procedure
  295. * call header to help verify that a retransmitted Call matches an
  296. * entry in the duplicate reply cache.
  297. *
  298. * To avoid assumptions about how the RPC message is laid out in
  299. * @buf and what else it might contain (eg, a GSS MIC suffix), the
  300. * caller passes us the exact location and length of the NFS Call
  301. * header.
  302. *
  303. * Returns a 32-bit checksum value, as defined in RFC 793.
  304. */
  305. static __wsum nfsd_cache_csum(struct xdr_buf *buf, unsigned int start,
  306. unsigned int remaining)
  307. {
  308. unsigned int base, len;
  309. struct xdr_buf subbuf;
  310. __wsum csum = 0;
  311. void *p;
  312. int idx;
  313. if (remaining > RC_CSUMLEN)
  314. remaining = RC_CSUMLEN;
  315. if (xdr_buf_subsegment(buf, &subbuf, start, remaining))
  316. return csum;
  317. /* rq_arg.head first */
  318. if (subbuf.head[0].iov_len) {
  319. len = min_t(unsigned int, subbuf.head[0].iov_len, remaining);
  320. csum = csum_partial(subbuf.head[0].iov_base, len, csum);
  321. remaining -= len;
  322. }
  323. /* Continue into page array */
  324. idx = subbuf.page_base / PAGE_SIZE;
  325. base = subbuf.page_base & ~PAGE_MASK;
  326. while (remaining) {
  327. p = page_address(subbuf.pages[idx]) + base;
  328. len = min_t(unsigned int, PAGE_SIZE - base, remaining);
  329. csum = csum_partial(p, len, csum);
  330. remaining -= len;
  331. base = 0;
  332. ++idx;
  333. }
  334. return csum;
  335. }
  336. static int
  337. nfsd_cache_key_cmp(const struct nfsd_cacherep *key,
  338. const struct nfsd_cacherep *rp, struct nfsd_net *nn)
  339. {
  340. if (key->c_key.k_xid == rp->c_key.k_xid &&
  341. key->c_key.k_csum != rp->c_key.k_csum) {
  342. nfsd_stats_payload_misses_inc(nn);
  343. trace_nfsd_drc_mismatch(nn, key, rp);
  344. }
  345. return memcmp(&key->c_key, &rp->c_key, sizeof(key->c_key));
  346. }
  347. /*
  348. * Search the request hash for an entry that matches the given rqstp.
  349. * Must be called with cache_lock held. Returns the found entry or
  350. * inserts an empty key on failure.
  351. */
  352. static struct nfsd_cacherep *
  353. nfsd_cache_insert(struct nfsd_drc_bucket *b, struct nfsd_cacherep *key,
  354. struct nfsd_net *nn)
  355. {
  356. struct nfsd_cacherep *rp, *ret = key;
  357. struct rb_node **p = &b->rb_head.rb_node,
  358. *parent = NULL;
  359. unsigned int entries = 0;
  360. int cmp;
  361. while (*p != NULL) {
  362. ++entries;
  363. parent = *p;
  364. rp = rb_entry(parent, struct nfsd_cacherep, c_node);
  365. cmp = nfsd_cache_key_cmp(key, rp, nn);
  366. if (cmp < 0)
  367. p = &parent->rb_left;
  368. else if (cmp > 0)
  369. p = &parent->rb_right;
  370. else {
  371. ret = rp;
  372. goto out;
  373. }
  374. }
  375. rb_link_node(&key->c_node, parent, p);
  376. rb_insert_color(&key->c_node, &b->rb_head);
  377. out:
  378. /* tally hash chain length stats */
  379. if (entries > nn->longest_chain) {
  380. nn->longest_chain = entries;
  381. nn->longest_chain_cachesize = atomic_read(&nn->num_drc_entries);
  382. } else if (entries == nn->longest_chain) {
  383. /* prefer to keep the smallest cachesize possible here */
  384. nn->longest_chain_cachesize = min_t(unsigned int,
  385. nn->longest_chain_cachesize,
  386. atomic_read(&nn->num_drc_entries));
  387. }
  388. return ret;
  389. }
  390. /**
  391. * nfsd_cache_lookup - Find an entry in the duplicate reply cache
  392. * @rqstp: Incoming Call to find
  393. * @start: starting byte in @rqstp->rq_arg of the NFS Call header
  394. * @len: size of the NFS Call header, in bytes
  395. * @cacherep: OUT: DRC entry for this request
  396. *
  397. * Try to find an entry matching the current call in the cache. When none
  398. * is found, we try to grab the oldest expired entry off the LRU list. If
  399. * a suitable one isn't there, then drop the cache_lock and allocate a
  400. * new one, then search again in case one got inserted while this thread
  401. * didn't hold the lock.
  402. *
  403. * Return values:
  404. * %RC_DOIT: Process the request normally
  405. * %RC_REPLY: Reply from cache
  406. * %RC_DROPIT: Do not process the request further
  407. */
  408. int nfsd_cache_lookup(struct svc_rqst *rqstp, unsigned int start,
  409. unsigned int len, struct nfsd_cacherep **cacherep)
  410. {
  411. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  412. struct nfsd_cacherep *rp, *found;
  413. __wsum csum;
  414. struct nfsd_drc_bucket *b;
  415. int type = rqstp->rq_cachetype;
  416. LIST_HEAD(dispose);
  417. int rtn = RC_DOIT;
  418. if (type == RC_NOCACHE) {
  419. nfsd_stats_rc_nocache_inc(nn);
  420. goto out;
  421. }
  422. csum = nfsd_cache_csum(&rqstp->rq_arg, start, len);
  423. /*
  424. * Since the common case is a cache miss followed by an insert,
  425. * preallocate an entry.
  426. */
  427. rp = nfsd_cacherep_alloc(rqstp, csum, nn);
  428. if (!rp)
  429. goto out;
  430. b = nfsd_cache_bucket_find(rqstp->rq_xid, nn);
  431. spin_lock(&b->cache_lock);
  432. found = nfsd_cache_insert(b, rp, nn);
  433. if (found != rp)
  434. goto found_entry;
  435. *cacherep = rp;
  436. rp->c_state = RC_INPROG;
  437. nfsd_prune_bucket_locked(nn, b, 3, &dispose);
  438. spin_unlock(&b->cache_lock);
  439. nfsd_cacherep_dispose(&dispose);
  440. nfsd_stats_rc_misses_inc(nn);
  441. atomic_inc(&nn->num_drc_entries);
  442. nfsd_stats_drc_mem_usage_add(nn, sizeof(*rp));
  443. goto out;
  444. found_entry:
  445. /* We found a matching entry which is either in progress or done. */
  446. nfsd_reply_cache_free_locked(NULL, rp, nn);
  447. nfsd_stats_rc_hits_inc(nn);
  448. rtn = RC_DROPIT;
  449. rp = found;
  450. /* Request being processed */
  451. if (rp->c_state == RC_INPROG)
  452. goto out_trace;
  453. /* From the hall of fame of impractical attacks:
  454. * Is this a user who tries to snoop on the cache? */
  455. rtn = RC_DOIT;
  456. if (!test_bit(RQ_SECURE, &rqstp->rq_flags) && rp->c_secure)
  457. goto out_trace;
  458. /* Compose RPC reply header */
  459. switch (rp->c_type) {
  460. case RC_NOCACHE:
  461. break;
  462. case RC_REPLSTAT:
  463. xdr_stream_encode_be32(&rqstp->rq_res_stream, rp->c_replstat);
  464. rtn = RC_REPLY;
  465. break;
  466. case RC_REPLBUFF:
  467. if (!nfsd_cache_append(rqstp, &rp->c_replvec))
  468. goto out_unlock; /* should not happen */
  469. rtn = RC_REPLY;
  470. break;
  471. default:
  472. WARN_ONCE(1, "nfsd: bad repcache type %d\n", rp->c_type);
  473. }
  474. out_trace:
  475. trace_nfsd_drc_found(nn, rqstp, rtn);
  476. out_unlock:
  477. spin_unlock(&b->cache_lock);
  478. out:
  479. return rtn;
  480. }
  481. /**
  482. * nfsd_cache_update - Update an entry in the duplicate reply cache.
  483. * @rqstp: svc_rqst with a finished Reply
  484. * @rp: IN: DRC entry for this request
  485. * @cachetype: which cache to update
  486. * @statp: pointer to Reply's NFS status code, or NULL
  487. *
  488. * This is called from nfsd_dispatch when the procedure has been
  489. * executed and the complete reply is in rqstp->rq_res.
  490. *
  491. * We're copying around data here rather than swapping buffers because
  492. * the toplevel loop requires max-sized buffers, which would be a waste
  493. * of memory for a cache with a max reply size of 100 bytes (diropokres).
  494. *
  495. * If we should start to use different types of cache entries tailored
  496. * specifically for attrstat and fh's, we may save even more space.
  497. *
  498. * Also note that a cachetype of RC_NOCACHE can legally be passed when
  499. * nfsd failed to encode a reply that otherwise would have been cached.
  500. * In this case, nfsd_cache_update is called with statp == NULL.
  501. */
  502. void nfsd_cache_update(struct svc_rqst *rqstp, struct nfsd_cacherep *rp,
  503. int cachetype, __be32 *statp)
  504. {
  505. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  506. struct kvec *resv = &rqstp->rq_res.head[0], *cachv;
  507. struct nfsd_drc_bucket *b;
  508. int len;
  509. size_t bufsize = 0;
  510. if (!rp)
  511. return;
  512. b = nfsd_cache_bucket_find(rp->c_key.k_xid, nn);
  513. len = resv->iov_len - ((char*)statp - (char*)resv->iov_base);
  514. len >>= 2;
  515. /* Don't cache excessive amounts of data and XDR failures */
  516. if (!statp || len > (256 >> 2)) {
  517. nfsd_reply_cache_free(b, rp, nn);
  518. return;
  519. }
  520. switch (cachetype) {
  521. case RC_REPLSTAT:
  522. if (len != 1)
  523. printk("nfsd: RC_REPLSTAT/reply len %d!\n",len);
  524. rp->c_replstat = *statp;
  525. break;
  526. case RC_REPLBUFF:
  527. cachv = &rp->c_replvec;
  528. bufsize = len << 2;
  529. cachv->iov_base = kmalloc(bufsize, GFP_KERNEL);
  530. if (!cachv->iov_base) {
  531. nfsd_reply_cache_free(b, rp, nn);
  532. return;
  533. }
  534. cachv->iov_len = bufsize;
  535. memcpy(cachv->iov_base, statp, bufsize);
  536. break;
  537. case RC_NOCACHE:
  538. nfsd_reply_cache_free(b, rp, nn);
  539. return;
  540. }
  541. spin_lock(&b->cache_lock);
  542. nfsd_stats_drc_mem_usage_add(nn, bufsize);
  543. lru_put_end(b, rp);
  544. rp->c_secure = test_bit(RQ_SECURE, &rqstp->rq_flags);
  545. rp->c_type = cachetype;
  546. rp->c_state = RC_DONE;
  547. spin_unlock(&b->cache_lock);
  548. return;
  549. }
  550. static int
  551. nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *data)
  552. {
  553. __be32 *p;
  554. p = xdr_reserve_space(&rqstp->rq_res_stream, data->iov_len);
  555. if (unlikely(!p))
  556. return false;
  557. memcpy(p, data->iov_base, data->iov_len);
  558. xdr_commit_encode(&rqstp->rq_res_stream);
  559. return true;
  560. }
  561. /*
  562. * Note that fields may be added, removed or reordered in the future. Programs
  563. * scraping this file for info should test the labels to ensure they're
  564. * getting the correct field.
  565. */
  566. int nfsd_reply_cache_stats_show(struct seq_file *m, void *v)
  567. {
  568. struct nfsd_net *nn = net_generic(file_inode(m->file)->i_sb->s_fs_info,
  569. nfsd_net_id);
  570. seq_printf(m, "max entries: %u\n", nn->max_drc_entries);
  571. seq_printf(m, "num entries: %u\n",
  572. atomic_read(&nn->num_drc_entries));
  573. seq_printf(m, "hash buckets: %u\n", 1 << nn->maskbits);
  574. seq_printf(m, "mem usage: %lld\n",
  575. percpu_counter_sum_positive(&nn->counter[NFSD_STATS_DRC_MEM_USAGE]));
  576. seq_printf(m, "cache hits: %lld\n",
  577. percpu_counter_sum_positive(&nn->counter[NFSD_STATS_RC_HITS]));
  578. seq_printf(m, "cache misses: %lld\n",
  579. percpu_counter_sum_positive(&nn->counter[NFSD_STATS_RC_MISSES]));
  580. seq_printf(m, "not cached: %lld\n",
  581. percpu_counter_sum_positive(&nn->counter[NFSD_STATS_RC_NOCACHE]));
  582. seq_printf(m, "payload misses: %lld\n",
  583. percpu_counter_sum_positive(&nn->counter[NFSD_STATS_PAYLOAD_MISSES]));
  584. seq_printf(m, "longest chain len: %u\n", nn->longest_chain);
  585. seq_printf(m, "cachesize at longest: %u\n", nn->longest_chain_cachesize);
  586. return 0;
  587. }