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