nfscache.c 11.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10
/*
 * 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>
 */

11
#include <linux/slab.h>
12
#include <linux/sunrpc/clnt.h>
13
#include <linux/highmem.h>
14

15 16
#include "nfsd.h"
#include "cache.h"
L
Linus Torvalds 已提交
17

18 19
#define NFSDDBG_FACILITY	NFSDDBG_REPCACHE

L
Linus Torvalds 已提交
20 21
#define HASHSIZE		64

G
Greg Banks 已提交
22
static struct hlist_head *	cache_hash;
L
Linus Torvalds 已提交
23
static struct list_head 	lru_head;
24
static struct kmem_cache	*drc_slab;
25
static unsigned int		num_drc_entries;
26
static unsigned int		max_drc_entries;
L
Linus Torvalds 已提交
27

G
Greg Banks 已提交
28 29 30 31 32 33 34 35 36 37
/*
 * Calculate the hash index from an XID.
 */
static inline u32 request_hash(u32 xid)
{
	u32 h = xid;
	h ^= (xid >> 24);
	return h & (HASHSIZE-1);
}

L
Linus Torvalds 已提交
38
static int	nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *vec);
39
static void	cache_cleaner_func(struct work_struct *unused);
40 41 42 43 44 45 46
static int 	nfsd_reply_cache_shrink(struct shrinker *shrink,
					struct shrink_control *sc);

struct shrinker nfsd_reply_cache_shrinker = {
	.shrink	= nfsd_reply_cache_shrink,
	.seeks	= 1,
};
L
Linus Torvalds 已提交
47

G
Greg Banks 已提交
48
/*
L
Linus Torvalds 已提交
49 50 51 52 53
 * 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);
54
static DECLARE_DELAYED_WORK(cache_cleaner, cache_cleaner_func);
L
Linus Torvalds 已提交
55

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
/*
 * Put a cap on the size of the DRC based on the amount of available
 * low memory in the machine.
 *
 *  64MB:    8192
 * 128MB:   11585
 * 256MB:   16384
 * 512MB:   23170
 *   1GB:   32768
 *   2GB:   46340
 *   4GB:   65536
 *   8GB:   92681
 *  16GB:  131072
 *
 * ...with a hard cap of 256k entries. In the worst case, each entry will be
 * ~1k, so the above numbers should give a rough max of the amount of memory
 * used in k.
 */
static unsigned int
nfsd_cache_size_limit(void)
{
	unsigned int limit;
	unsigned long low_pages = totalram_pages - totalhigh_pages;

	limit = (16 * int_sqrt(low_pages)) << (PAGE_SHIFT-10);
	return min_t(unsigned int, limit, 256*1024);
}

84 85
static struct svc_cacherep *
nfsd_reply_cache_alloc(void)
L
Linus Torvalds 已提交
86 87
{
	struct svc_cacherep	*rp;
88 89 90 91 92 93 94 95 96 97 98 99 100 101

	rp = kmem_cache_alloc(drc_slab, GFP_KERNEL);
	if (rp) {
		rp->c_state = RC_UNUSED;
		rp->c_type = RC_NOCACHE;
		INIT_LIST_HEAD(&rp->c_lru);
		INIT_HLIST_NODE(&rp->c_hash);
	}
	return rp;
}

static void
nfsd_reply_cache_free_locked(struct svc_cacherep *rp)
{
102
	if (rp->c_type == RC_REPLBUFF)
103
		kfree(rp->c_replvec.iov_base);
104
	hlist_del(&rp->c_hash);
105
	list_del(&rp->c_lru);
106
	--num_drc_entries;
107 108 109
	kmem_cache_free(drc_slab, rp);
}

110 111 112 113 114 115 116 117
static void
nfsd_reply_cache_free(struct svc_cacherep *rp)
{
	spin_lock(&cache_lock);
	nfsd_reply_cache_free_locked(rp);
	spin_unlock(&cache_lock);
}

118 119
int nfsd_reply_cache_init(void)
{
120
	register_shrinker(&nfsd_reply_cache_shrinker);
121 122 123 124 125
	drc_slab = kmem_cache_create("nfsd_drc", sizeof(struct svc_cacherep),
					0, 0, NULL);
	if (!drc_slab)
		goto out_nomem;

126
	cache_hash = kcalloc(HASHSIZE, sizeof(struct hlist_head), GFP_KERNEL);
G
Greg Banks 已提交
127
	if (!cache_hash)
128
		goto out_nomem;
L
Linus Torvalds 已提交
129

130 131 132
	INIT_LIST_HEAD(&lru_head);
	max_drc_entries = nfsd_cache_size_limit();
	num_drc_entries = 0;
133 134 135 136 137
	return 0;
out_nomem:
	printk(KERN_ERR "nfsd: failed to allocate reply cache\n");
	nfsd_reply_cache_shutdown();
	return -ENOMEM;
L
Linus Torvalds 已提交
138 139
}

140
void nfsd_reply_cache_shutdown(void)
L
Linus Torvalds 已提交
141 142 143
{
	struct svc_cacherep	*rp;

144
	unregister_shrinker(&nfsd_reply_cache_shrinker);
145 146
	cancel_delayed_work_sync(&cache_cleaner);

L
Linus Torvalds 已提交
147 148
	while (!list_empty(&lru_head)) {
		rp = list_entry(lru_head.next, struct svc_cacherep, c_lru);
149
		nfsd_reply_cache_free_locked(rp);
L
Linus Torvalds 已提交
150 151
	}

G
Greg Banks 已提交
152 153
	kfree (cache_hash);
	cache_hash = NULL;
154 155 156 157 158

	if (drc_slab) {
		kmem_cache_destroy(drc_slab);
		drc_slab = NULL;
	}
L
Linus Torvalds 已提交
159 160 161
}

/*
162 163
 * Move cache entry to end of LRU list, and queue the cleaner to run if it's
 * not already scheduled.
L
Linus Torvalds 已提交
164 165 166 167
 */
static void
lru_put_end(struct svc_cacherep *rp)
{
168
	rp->c_timestamp = jiffies;
A
Akinobu Mita 已提交
169
	list_move_tail(&rp->c_lru, &lru_head);
170
	schedule_delayed_work(&cache_cleaner, RC_EXPIRE);
L
Linus Torvalds 已提交
171 172 173 174 175 176 177 178 179
}

/*
 * Move a cache entry from one hash list to another
 */
static void
hash_refile(struct svc_cacherep *rp)
{
	hlist_del_init(&rp->c_hash);
G
Greg Banks 已提交
180
	hlist_add_head(&rp->c_hash, cache_hash + request_hash(rp->c_xid));
L
Linus Torvalds 已提交
181 182
}

183 184 185 186 187 188 189
static inline bool
nfsd_cache_entry_expired(struct svc_cacherep *rp)
{
	return rp->c_state != RC_INPROG &&
	       time_after(jiffies, rp->c_timestamp + RC_EXPIRE);
}

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
/*
 * Walk the LRU list and prune off entries that are older than RC_EXPIRE.
 * Also prune the oldest ones when the total exceeds the max number of entries.
 */
static void
prune_cache_entries(void)
{
	struct svc_cacherep *rp, *tmp;

	list_for_each_entry_safe(rp, tmp, &lru_head, c_lru) {
		if (!nfsd_cache_entry_expired(rp) &&
		    num_drc_entries <= max_drc_entries)
			break;
		nfsd_reply_cache_free_locked(rp);
	}

	/*
	 * Conditionally rearm the job. If we cleaned out the list, then
	 * cancel any pending run (since there won't be any work to do).
	 * Otherwise, we rearm the job or modify the existing one to run in
	 * RC_EXPIRE since we just ran the pruner.
	 */
	if (list_empty(&lru_head))
		cancel_delayed_work(&cache_cleaner);
	else
		mod_delayed_work(system_wq, &cache_cleaner, RC_EXPIRE);
}

static void
cache_cleaner_func(struct work_struct *unused)
{
	spin_lock(&cache_lock);
	prune_cache_entries();
	spin_unlock(&cache_lock);
}

226 227 228 229 230 231 232 233 234 235 236 237 238 239
static int
nfsd_reply_cache_shrink(struct shrinker *shrink, struct shrink_control *sc)
{
	unsigned int num;

	spin_lock(&cache_lock);
	if (sc->nr_to_scan)
		prune_cache_entries();
	num = num_drc_entries;
	spin_unlock(&cache_lock);

	return num;
}

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
/*
 * Search the request hash for an entry that matches the given rqstp.
 * Must be called with cache_lock held. Returns the found entry or
 * NULL on failure.
 */
static struct svc_cacherep *
nfsd_cache_search(struct svc_rqst *rqstp)
{
	struct svc_cacherep	*rp;
	struct hlist_node	*hn;
	struct hlist_head 	*rh;
	__be32			xid = rqstp->rq_xid;
	u32			proto =  rqstp->rq_prot,
				vers = rqstp->rq_vers,
				proc = rqstp->rq_proc;

	rh = &cache_hash[request_hash(xid)];
	hlist_for_each_entry(rp, hn, rh, c_hash) {
258
		if (xid == rp->c_xid && proc == rp->c_proc &&
259 260 261 262 263 264 265 266
		    proto == rp->c_prot && vers == rp->c_vers &&
		    rpc_cmp_addr(svc_addr(rqstp), (struct sockaddr *)&rp->c_addr) &&
		    rpc_get_port(svc_addr(rqstp)) == rpc_get_port((struct sockaddr *)&rp->c_addr))
			return rp;
	}
	return NULL;
}

L
Linus Torvalds 已提交
267 268 269 270 271 272
/*
 * 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
273
nfsd_cache_lookup(struct svc_rqst *rqstp)
L
Linus Torvalds 已提交
274
{
275
	struct svc_cacherep	*rp, *found;
276 277
	__be32			xid = rqstp->rq_xid;
	u32			proto =  rqstp->rq_prot,
L
Linus Torvalds 已提交
278 279 280
				vers = rqstp->rq_vers,
				proc = rqstp->rq_proc;
	unsigned long		age;
281
	int type = rqstp->rq_cachetype;
L
Linus Torvalds 已提交
282 283 284
	int rtn;

	rqstp->rq_cacherep = NULL;
285
	if (type == RC_NOCACHE) {
L
Linus Torvalds 已提交
286 287 288 289 290 291 292
		nfsdstats.rcnocache++;
		return RC_DOIT;
	}

	spin_lock(&cache_lock);
	rtn = RC_DOIT;

293
	rp = nfsd_cache_search(rqstp);
294
	if (rp)
295
		goto found_entry;
296 297 298 299 300

	/* Try to use the first entry on the LRU */
	if (!list_empty(&lru_head)) {
		rp = list_first_entry(&lru_head, struct svc_cacherep, c_lru);
		if (nfsd_cache_entry_expired(rp) ||
301 302 303
		    num_drc_entries >= max_drc_entries) {
			lru_put_end(rp);
			prune_cache_entries();
304
			goto setup_entry;
305
		}
L
Linus Torvalds 已提交
306 307
	}

308 309 310 311 312
	spin_unlock(&cache_lock);
	rp = nfsd_reply_cache_alloc();
	if (!rp) {
		dprintk("nfsd: unable to allocate DRC entry!\n");
		return RC_DOIT;
L
Linus Torvalds 已提交
313
	}
314 315 316 317 318 319 320 321 322 323 324 325
	spin_lock(&cache_lock);
	++num_drc_entries;

	/*
	 * Must search again just in case someone inserted one
	 * after we dropped the lock above.
	 */
	found = nfsd_cache_search(rqstp);
	if (found) {
		nfsd_reply_cache_free_locked(rp);
		rp = found;
		goto found_entry;
L
Linus Torvalds 已提交
326 327
	}

328 329 330 331 332 333 334 335
	/*
	 * We're keeping the one we just allocated. Are we now over the
	 * limit? Prune one off the tip of the LRU in trade for the one we
	 * just allocated if so.
	 */
	if (num_drc_entries >= max_drc_entries)
		nfsd_reply_cache_free_locked(list_first_entry(&lru_head,
						struct svc_cacherep, c_lru));
L
Linus Torvalds 已提交
336

337 338
setup_entry:
	nfsdstats.rcmisses++;
L
Linus Torvalds 已提交
339 340 341 342
	rqstp->rq_cacherep = rp;
	rp->c_state = RC_INPROG;
	rp->c_xid = xid;
	rp->c_proc = proc;
343 344
	rpc_copy_addr((struct sockaddr *)&rp->c_addr, svc_addr(rqstp));
	rpc_set_port((struct sockaddr *)&rp->c_addr, rpc_get_port(svc_addr(rqstp)));
L
Linus Torvalds 已提交
345 346 347 348
	rp->c_prot = proto;
	rp->c_vers = vers;

	hash_refile(rp);
349
	lru_put_end(rp);
L
Linus Torvalds 已提交
350 351 352 353 354 355 356 357 358 359 360 361

	/* 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:
362
	nfsdstats.rchits++;
L
Linus Torvalds 已提交
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
	/* We found a matching entry which is either in progress or done. */
	age = jiffies - rp->c_timestamp;
	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\n", rp->c_type);
393
		nfsd_reply_cache_free_locked(rp);
L
Linus Torvalds 已提交
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
	}

	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
416
nfsd_cache_update(struct svc_rqst *rqstp, int cachetype, __be32 *statp)
L
Linus Torvalds 已提交
417
{
418
	struct svc_cacherep *rp = rqstp->rq_cacherep;
L
Linus Torvalds 已提交
419 420 421
	struct kvec	*resv = &rqstp->rq_res.head[0], *cachv;
	int		len;

422
	if (!rp)
L
Linus Torvalds 已提交
423 424 425 426
		return;

	len = resv->iov_len - ((char*)statp - (char*)resv->iov_base);
	len >>= 2;
G
Greg Banks 已提交
427

L
Linus Torvalds 已提交
428 429
	/* Don't cache excessive amounts of data and XDR failures */
	if (!statp || len > (256 >> 2)) {
430
		nfsd_reply_cache_free(rp);
L
Linus Torvalds 已提交
431 432 433 434 435 436 437 438 439 440 441 442 443
		return;
	}

	switch (cachetype) {
	case RC_REPLSTAT:
		if (len != 1)
			printk("nfsd: RC_REPLSTAT/reply len %d!\n",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) {
444
			nfsd_reply_cache_free(rp);
L
Linus Torvalds 已提交
445 446 447 448 449
			return;
		}
		cachv->iov_len = len << 2;
		memcpy(cachv->iov_base, statp, len << 2);
		break;
450 451 452
	case RC_NOCACHE:
		nfsd_reply_cache_free(rp);
		return;
L
Linus Torvalds 已提交
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
	}
	spin_lock(&cache_lock);
	lru_put_end(rp);
	rp->c_secure = rqstp->rq_secure;
	rp->c_type = cachetype;
	rp->c_state = RC_DONE;
	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).\n",
				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;
}