nfscache.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 "nfsd.h"
  11. #include "cache.h"
  12. /* Size of reply cache. Common values are:
  13. * 4.3BSD: 128
  14. * 4.4BSD: 256
  15. * Solaris2: 1024
  16. * DEC Unix: 512-4096
  17. */
  18. #define CACHESIZE 1024
  19. #define HASHSIZE 64
  20. static struct hlist_head * cache_hash;
  21. static struct list_head lru_head;
  22. static int cache_disabled = 1;
  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. /*
  34. * locking for the reply cache:
  35. * A cache entry is "single use" if c_state == RC_INPROG
  36. * Otherwise, it when accessing _prev or _next, the lock must be held.
  37. */
  38. static DEFINE_SPINLOCK(cache_lock);
  39. int nfsd_reply_cache_init(void)
  40. {
  41. struct svc_cacherep *rp;
  42. int i;
  43. INIT_LIST_HEAD(&lru_head);
  44. i = CACHESIZE;
  45. while (i) {
  46. rp = kmalloc(sizeof(*rp), GFP_KERNEL);
  47. if (!rp)
  48. goto out_nomem;
  49. list_add(&rp->c_lru, &lru_head);
  50. rp->c_state = RC_UNUSED;
  51. rp->c_type = RC_NOCACHE;
  52. INIT_HLIST_NODE(&rp->c_hash);
  53. i--;
  54. }
  55. cache_hash = kcalloc (HASHSIZE, sizeof(struct hlist_head), GFP_KERNEL);
  56. if (!cache_hash)
  57. goto out_nomem;
  58. cache_disabled = 0;
  59. return 0;
  60. out_nomem:
  61. printk(KERN_ERR "nfsd: failed to allocate reply cache\n");
  62. nfsd_reply_cache_shutdown();
  63. return -ENOMEM;
  64. }
  65. void nfsd_reply_cache_shutdown(void)
  66. {
  67. struct svc_cacherep *rp;
  68. while (!list_empty(&lru_head)) {
  69. rp = list_entry(lru_head.next, struct svc_cacherep, c_lru);
  70. if (rp->c_state == RC_DONE && rp->c_type == RC_REPLBUFF)
  71. kfree(rp->c_replvec.iov_base);
  72. list_del(&rp->c_lru);
  73. kfree(rp);
  74. }
  75. cache_disabled = 1;
  76. kfree (cache_hash);
  77. cache_hash = NULL;
  78. }
  79. /*
  80. * Move cache entry to end of LRU list
  81. */
  82. static void
  83. lru_put_end(struct svc_cacherep *rp)
  84. {
  85. list_move_tail(&rp->c_lru, &lru_head);
  86. }
  87. /*
  88. * Move a cache entry from one hash list to another
  89. */
  90. static void
  91. hash_refile(struct svc_cacherep *rp)
  92. {
  93. hlist_del_init(&rp->c_hash);
  94. hlist_add_head(&rp->c_hash, cache_hash + request_hash(rp->c_xid));
  95. }
  96. /*
  97. * Try to find an entry matching the current call in the cache. When none
  98. * is found, we grab the oldest unlocked entry off the LRU list.
  99. * Note that no operation within the loop may sleep.
  100. */
  101. int
  102. nfsd_cache_lookup(struct svc_rqst *rqstp, int type)
  103. {
  104. struct hlist_node *hn;
  105. struct hlist_head *rh;
  106. struct svc_cacherep *rp;
  107. __be32 xid = rqstp->rq_xid;
  108. u32 proto = rqstp->rq_prot,
  109. vers = rqstp->rq_vers,
  110. proc = rqstp->rq_proc;
  111. unsigned long age;
  112. int rtn;
  113. rqstp->rq_cacherep = NULL;
  114. if (cache_disabled || type == RC_NOCACHE) {
  115. nfsdstats.rcnocache++;
  116. return RC_DOIT;
  117. }
  118. spin_lock(&cache_lock);
  119. rtn = RC_DOIT;
  120. rh = &cache_hash[request_hash(xid)];
  121. hlist_for_each_entry(rp, hn, rh, c_hash) {
  122. if (rp->c_state != RC_UNUSED &&
  123. xid == rp->c_xid && proc == rp->c_proc &&
  124. proto == rp->c_prot && vers == rp->c_vers &&
  125. time_before(jiffies, rp->c_timestamp + 120*HZ) &&
  126. memcmp((char*)&rqstp->rq_addr, (char*)&rp->c_addr, sizeof(rp->c_addr))==0) {
  127. nfsdstats.rchits++;
  128. goto found_entry;
  129. }
  130. }
  131. nfsdstats.rcmisses++;
  132. /* This loop shouldn't take more than a few iterations normally */
  133. {
  134. int safe = 0;
  135. list_for_each_entry(rp, &lru_head, c_lru) {
  136. if (rp->c_state != RC_INPROG)
  137. break;
  138. if (safe++ > CACHESIZE) {
  139. printk("nfsd: loop in repcache LRU list\n");
  140. cache_disabled = 1;
  141. goto out;
  142. }
  143. }
  144. }
  145. /* All entries on the LRU are in-progress. This should not happen */
  146. if (&rp->c_lru == &lru_head) {
  147. static int complaints;
  148. printk(KERN_WARNING "nfsd: all repcache entries locked!\n");
  149. if (++complaints > 5) {
  150. printk(KERN_WARNING "nfsd: disabling repcache.\n");
  151. cache_disabled = 1;
  152. }
  153. goto out;
  154. }
  155. rqstp->rq_cacherep = rp;
  156. rp->c_state = RC_INPROG;
  157. rp->c_xid = xid;
  158. rp->c_proc = proc;
  159. memcpy(&rp->c_addr, svc_addr_in(rqstp), sizeof(rp->c_addr));
  160. rp->c_prot = proto;
  161. rp->c_vers = vers;
  162. rp->c_timestamp = jiffies;
  163. hash_refile(rp);
  164. /* release any buffer */
  165. if (rp->c_type == RC_REPLBUFF) {
  166. kfree(rp->c_replvec.iov_base);
  167. rp->c_replvec.iov_base = NULL;
  168. }
  169. rp->c_type = RC_NOCACHE;
  170. out:
  171. spin_unlock(&cache_lock);
  172. return rtn;
  173. found_entry:
  174. /* We found a matching entry which is either in progress or done. */
  175. age = jiffies - rp->c_timestamp;
  176. rp->c_timestamp = jiffies;
  177. lru_put_end(rp);
  178. rtn = RC_DROPIT;
  179. /* Request being processed or excessive rexmits */
  180. if (rp->c_state == RC_INPROG || age < RC_DELAY)
  181. goto out;
  182. /* From the hall of fame of impractical attacks:
  183. * Is this a user who tries to snoop on the cache? */
  184. rtn = RC_DOIT;
  185. if (!rqstp->rq_secure && rp->c_secure)
  186. goto out;
  187. /* Compose RPC reply header */
  188. switch (rp->c_type) {
  189. case RC_NOCACHE:
  190. break;
  191. case RC_REPLSTAT:
  192. svc_putu32(&rqstp->rq_res.head[0], rp->c_replstat);
  193. rtn = RC_REPLY;
  194. break;
  195. case RC_REPLBUFF:
  196. if (!nfsd_cache_append(rqstp, &rp->c_replvec))
  197. goto out; /* should not happen */
  198. rtn = RC_REPLY;
  199. break;
  200. default:
  201. printk(KERN_WARNING "nfsd: bad repcache type %d\n", rp->c_type);
  202. rp->c_state = RC_UNUSED;
  203. }
  204. goto out;
  205. }
  206. /*
  207. * Update a cache entry. This is called from nfsd_dispatch when
  208. * the procedure has been executed and the complete reply is in
  209. * rqstp->rq_res.
  210. *
  211. * We're copying around data here rather than swapping buffers because
  212. * the toplevel loop requires max-sized buffers, which would be a waste
  213. * of memory for a cache with a max reply size of 100 bytes (diropokres).
  214. *
  215. * If we should start to use different types of cache entries tailored
  216. * specifically for attrstat and fh's, we may save even more space.
  217. *
  218. * Also note that a cachetype of RC_NOCACHE can legally be passed when
  219. * nfsd failed to encode a reply that otherwise would have been cached.
  220. * In this case, nfsd_cache_update is called with statp == NULL.
  221. */
  222. void
  223. nfsd_cache_update(struct svc_rqst *rqstp, int cachetype, __be32 *statp)
  224. {
  225. struct svc_cacherep *rp;
  226. struct kvec *resv = &rqstp->rq_res.head[0], *cachv;
  227. int len;
  228. if (!(rp = rqstp->rq_cacherep) || cache_disabled)
  229. return;
  230. len = resv->iov_len - ((char*)statp - (char*)resv->iov_base);
  231. len >>= 2;
  232. /* Don't cache excessive amounts of data and XDR failures */
  233. if (!statp || len > (256 >> 2)) {
  234. rp->c_state = RC_UNUSED;
  235. return;
  236. }
  237. switch (cachetype) {
  238. case RC_REPLSTAT:
  239. if (len != 1)
  240. printk("nfsd: RC_REPLSTAT/reply len %d!\n",len);
  241. rp->c_replstat = *statp;
  242. break;
  243. case RC_REPLBUFF:
  244. cachv = &rp->c_replvec;
  245. cachv->iov_base = kmalloc(len << 2, GFP_KERNEL);
  246. if (!cachv->iov_base) {
  247. spin_lock(&cache_lock);
  248. rp->c_state = RC_UNUSED;
  249. spin_unlock(&cache_lock);
  250. return;
  251. }
  252. cachv->iov_len = len << 2;
  253. memcpy(cachv->iov_base, statp, len << 2);
  254. break;
  255. }
  256. spin_lock(&cache_lock);
  257. lru_put_end(rp);
  258. rp->c_secure = rqstp->rq_secure;
  259. rp->c_type = cachetype;
  260. rp->c_state = RC_DONE;
  261. rp->c_timestamp = jiffies;
  262. spin_unlock(&cache_lock);
  263. return;
  264. }
  265. /*
  266. * Copy cached reply to current reply buffer. Should always fit.
  267. * FIXME as reply is in a page, we should just attach the page, and
  268. * keep a refcount....
  269. */
  270. static int
  271. nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *data)
  272. {
  273. struct kvec *vec = &rqstp->rq_res.head[0];
  274. if (vec->iov_len + data->iov_len > PAGE_SIZE) {
  275. printk(KERN_WARNING "nfsd: cached reply too large (%Zd).\n",
  276. data->iov_len);
  277. return 0;
  278. }
  279. memcpy((char*)vec->iov_base + vec->iov_len, data->iov_base, data->iov_len);
  280. vec->iov_len += data->iov_len;
  281. return 1;
  282. }