nfscache.c 7.8 KB

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