Blame view

fs/nfsd/nfscache.c 7.61 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
5
6
7
8
9
   * Request reply cache. This is currently a global cache, but this may
   * change in the future and be a per-client cache.
   *
   * This code is heavily inspired by the 44BSD implementation, although
   * it does things a bit differently.
   *
   * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
   */
5a0e3ad6a   Tejun Heo   include cleanup: ...
10
  #include <linux/slab.h>
9a74af213   Boaz Harrosh   nfsd: Move privat...
11
12
  #include "nfsd.h"
  #include "cache.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
13
14
15
16
17
18
19
20
21
  
  /* Size of reply cache. Common values are:
   * 4.3BSD:	128
   * 4.4BSD:	256
   * Solaris2:	1024
   * DEC Unix:	512-4096
   */
  #define CACHESIZE		1024
  #define HASHSIZE		64
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
22

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

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
  	/* Don't cache excessive amounts of data and XDR failures */
  	if (!statp || len > (256 >> 2)) {
  		rp->c_state = RC_UNUSED;
  		return;
  	}
  
  	switch (cachetype) {
  	case RC_REPLSTAT:
  		if (len != 1)
  			printk("nfsd: RC_REPLSTAT/reply len %d!
  ",len);
  		rp->c_replstat = *statp;
  		break;
  	case RC_REPLBUFF:
  		cachv = &rp->c_replvec;
  		cachv->iov_base = kmalloc(len << 2, GFP_KERNEL);
  		if (!cachv->iov_base) {
  			spin_lock(&cache_lock);
  			rp->c_state = RC_UNUSED;
  			spin_unlock(&cache_lock);
  			return;
  		}
  		cachv->iov_len = len << 2;
  		memcpy(cachv->iov_base, statp, len << 2);
  		break;
  	}
  	spin_lock(&cache_lock);
  	lru_put_end(rp);
  	rp->c_secure = rqstp->rq_secure;
  	rp->c_type = cachetype;
  	rp->c_state = RC_DONE;
  	rp->c_timestamp = jiffies;
  	spin_unlock(&cache_lock);
  	return;
  }
  
  /*
   * Copy cached reply to current reply buffer. Should always fit.
   * FIXME as reply is in a page, we should just attach the page, and
   * keep a refcount....
   */
  static int
  nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *data)
  {
  	struct kvec	*vec = &rqstp->rq_res.head[0];
  
  	if (vec->iov_len + data->iov_len > PAGE_SIZE) {
  		printk(KERN_WARNING "nfsd: cached reply too large (%Zd).
  ",
  				data->iov_len);
  		return 0;
  	}
  	memcpy((char*)vec->iov_base + vec->iov_len, data->iov_base, data->iov_len);
  	vec->iov_len += data->iov_len;
  	return 1;
  }