cache.c 31.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * net/sunrpc/cache.c
 *
 * Generic code for various authentication-related caches
 * used by sunrpc clients and servers.
 *
 * Copyright (C) 2002 Neil Brown <neilb@cse.unsw.edu.au>
 *
 * Released under terms in GPL version 2.  See COPYING.
 *
 */

#include <linux/types.h>
#include <linux/fs.h>
#include <linux/file.h>
#include <linux/slab.h>
#include <linux/signal.h>
#include <linux/sched.h>
#include <linux/kmod.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/ctype.h>
#include <asm/uaccess.h>
#include <linux/poll.h>
#include <linux/seq_file.h>
#include <linux/proc_fs.h>
#include <linux/net.h>
#include <linux/workqueue.h>
A
Arjan van de Ven 已提交
29
#include <linux/mutex.h>
L
Linus Torvalds 已提交
30 31 32 33 34 35 36
#include <asm/ioctls.h>
#include <linux/sunrpc/types.h>
#include <linux/sunrpc/cache.h>
#include <linux/sunrpc/stats.h>

#define	 RPCDBG_FACILITY RPCDBG_CACHE

37
static int cache_defer_req(struct cache_req *req, struct cache_head *item);
L
Linus Torvalds 已提交
38 39
static void cache_revisit_request(struct cache_head *item);

40
static void cache_init(struct cache_head *h)
L
Linus Torvalds 已提交
41 42 43 44
{
	time_t now = get_seconds();
	h->next = NULL;
	h->flags = 0;
45
	kref_init(&h->ref);
L
Linus Torvalds 已提交
46 47 48 49
	h->expiry_time = now + CACHE_NEW_EXPIRY;
	h->last_refresh = now;
}

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail,
				       struct cache_head *key, int hash)
{
	struct cache_head **head,  **hp;
	struct cache_head *new = NULL;

	head = &detail->hash_table[hash];

	read_lock(&detail->hash_lock);

	for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
		struct cache_head *tmp = *hp;
		if (detail->match(tmp, key)) {
			cache_get(tmp);
			read_unlock(&detail->hash_lock);
			return tmp;
		}
	}
	read_unlock(&detail->hash_lock);
	/* Didn't find anything, insert an empty entry */

	new = detail->alloc();
	if (!new)
		return NULL;
74 75 76 77
	/* must fully initialise 'new', else
	 * we might get lose if we need to
	 * cache_put it soon.
	 */
78
	cache_init(new);
79
	detail->init(new, key);
80 81 82 83 84 85 86 87 88

	write_lock(&detail->hash_lock);

	/* check if entry appeared while we slept */
	for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
		struct cache_head *tmp = *hp;
		if (detail->match(tmp, key)) {
			cache_get(tmp);
			write_unlock(&detail->hash_lock);
89
			cache_put(new, detail);
90 91 92 93 94 95 96 97 98 99 100
			return tmp;
		}
	}
	new->next = *head;
	*head = new;
	detail->entries++;
	cache_get(new);
	write_unlock(&detail->hash_lock);

	return new;
}
101
EXPORT_SYMBOL_GPL(sunrpc_cache_lookup);
102

103

104
static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch);
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

static int cache_fresh_locked(struct cache_head *head, time_t expiry)
{
	head->expiry_time = expiry;
	head->last_refresh = get_seconds();
	return !test_and_set_bit(CACHE_VALID, &head->flags);
}

static void cache_fresh_unlocked(struct cache_head *head,
			struct cache_detail *detail, int new)
{
	if (new)
		cache_revisit_request(head);
	if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
		cache_revisit_request(head);
120
		cache_dequeue(detail, head);
121 122 123
	}
}

124 125 126 127 128 129 130 131 132
struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
				       struct cache_head *new, struct cache_head *old, int hash)
{
	/* The 'old' entry is to be replaced by 'new'.
	 * If 'old' is not VALID, we update it directly,
	 * otherwise we need to replace it
	 */
	struct cache_head **head;
	struct cache_head *tmp;
133
	int is_new;
134 135 136 137 138 139 140 141

	if (!test_bit(CACHE_VALID, &old->flags)) {
		write_lock(&detail->hash_lock);
		if (!test_bit(CACHE_VALID, &old->flags)) {
			if (test_bit(CACHE_NEGATIVE, &new->flags))
				set_bit(CACHE_NEGATIVE, &old->flags);
			else
				detail->update(old, new);
142
			is_new = cache_fresh_locked(old, new->expiry_time);
143
			write_unlock(&detail->hash_lock);
144
			cache_fresh_unlocked(old, detail, is_new);
145 146 147 148 149 150 151
			return old;
		}
		write_unlock(&detail->hash_lock);
	}
	/* We need to insert a new entry */
	tmp = detail->alloc();
	if (!tmp) {
152
		cache_put(old, detail);
153 154 155 156 157 158 159 160 161 162 163 164 165
		return NULL;
	}
	cache_init(tmp);
	detail->init(tmp, old);
	head = &detail->hash_table[hash];

	write_lock(&detail->hash_lock);
	if (test_bit(CACHE_NEGATIVE, &new->flags))
		set_bit(CACHE_NEGATIVE, &tmp->flags);
	else
		detail->update(tmp, new);
	tmp->next = *head;
	*head = tmp;
166
	detail->entries++;
167
	cache_get(tmp);
168 169
	is_new = cache_fresh_locked(tmp, new->expiry_time);
	cache_fresh_locked(old, 0);
170
	write_unlock(&detail->hash_lock);
171 172
	cache_fresh_unlocked(tmp, detail, is_new);
	cache_fresh_unlocked(old, detail, 0);
173
	cache_put(old, detail);
174 175
	return tmp;
}
176
EXPORT_SYMBOL_GPL(sunrpc_cache_update);
L
Linus Torvalds 已提交
177 178

static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h);
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

static inline int cache_is_valid(struct cache_detail *detail, struct cache_head *h)
{
	if (!test_bit(CACHE_VALID, &h->flags) ||
	    h->expiry_time < get_seconds())
		return -EAGAIN;
	else if (detail->flush_time > h->last_refresh)
		return -EAGAIN;
	else {
		/* entry is valid */
		if (test_bit(CACHE_NEGATIVE, &h->flags))
			return -ENOENT;
		else
			return 0;
	}
}
L
Linus Torvalds 已提交
195 196 197 198 199 200 201 202
/*
 * This is the generic cache management routine for all
 * the authentication caches.
 * It checks the currency of a cache item and will (later)
 * initiate an upcall to fill it if needed.
 *
 *
 * Returns 0 if the cache_head can be used, or cache_puts it and returns
203 204 205 206
 * -EAGAIN if upcall is pending and request has been queued
 * -ETIMEDOUT if upcall failed or request could not be queue or
 *           upcall completed but item is still invalid (implying that
 *           the cache item has been replaced with a newer one).
L
Linus Torvalds 已提交
207 208 209 210 211 212 213 214 215
 * -ENOENT if cache entry was negative
 */
int cache_check(struct cache_detail *detail,
		    struct cache_head *h, struct cache_req *rqstp)
{
	int rv;
	long refresh_age, age;

	/* First decide return status as best we can */
216
	rv = cache_is_valid(detail, h);
L
Linus Torvalds 已提交
217 218 219 220 221 222 223 224 225

	/* now see if we want to start an upcall */
	refresh_age = (h->expiry_time - h->last_refresh);
	age = get_seconds() - h->last_refresh;

	if (rqstp == NULL) {
		if (rv == -EAGAIN)
			rv = -ENOENT;
	} else if (rv == -EAGAIN || age > refresh_age/2) {
226 227
		dprintk("RPC:       Want update, refage=%ld, age=%ld\n",
				refresh_age, age);
L
Linus Torvalds 已提交
228 229 230 231
		if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
			switch (cache_make_upcall(detail, h)) {
			case -EINVAL:
				clear_bit(CACHE_PENDING, &h->flags);
232
				cache_revisit_request(h);
L
Linus Torvalds 已提交
233 234
				if (rv == -EAGAIN) {
					set_bit(CACHE_NEGATIVE, &h->flags);
235 236
					cache_fresh_unlocked(h, detail,
					     cache_fresh_locked(h, get_seconds()+CACHE_NEW_EXPIRY));
L
Linus Torvalds 已提交
237 238 239 240 241 242 243 244 245 246 247 248
					rv = -ENOENT;
				}
				break;

			case -EAGAIN:
				clear_bit(CACHE_PENDING, &h->flags);
				cache_revisit_request(h);
				break;
			}
		}
	}

249 250 251 252 253 254 255 256
	if (rv == -EAGAIN) {
		if (cache_defer_req(rqstp, h) == 0) {
			/* Request is not deferred */
			rv = cache_is_valid(detail, h);
			if (rv == -EAGAIN)
				rv = -ETIMEDOUT;
		}
	}
257
	if (rv)
258
		cache_put(h, detail);
L
Linus Torvalds 已提交
259 260
	return rv;
}
261
EXPORT_SYMBOL_GPL(cache_check);
L
Linus Torvalds 已提交
262 263 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

/*
 * caches need to be periodically cleaned.
 * For this we maintain a list of cache_detail and
 * a current pointer into that list and into the table
 * for that entry.
 *
 * Each time clean_cache is called it finds the next non-empty entry
 * in the current table and walks the list in that entry
 * looking for entries that can be removed.
 *
 * An entry gets removed if:
 * - The expiry is before current time
 * - The last_refresh time is before the flush_time for that cache
 *
 * later we might drop old entries with non-NEVER expiry if that table
 * is getting 'full' for some definition of 'full'
 *
 * The question of "how often to scan a table" is an interesting one
 * and is answered in part by the use of the "nextcheck" field in the
 * cache_detail.
 * When a scan of a table begins, the nextcheck field is set to a time
 * that is well into the future.
 * While scanning, if an expiry time is found that is earlier than the
 * current nextcheck time, nextcheck is set to that expiry time.
 * If the flush_time is ever set to a time earlier than the nextcheck
 * time, the nextcheck time is then set to that flush_time.
 *
 * A table is then only scanned if the current time is at least
 * the nextcheck time.
292
 *
L
Linus Torvalds 已提交
293 294 295 296 297 298 299
 */

static LIST_HEAD(cache_list);
static DEFINE_SPINLOCK(cache_list_lock);
static struct cache_detail *current_detail;
static int current_index;

300 301 302
static const struct file_operations cache_file_operations;
static const struct file_operations content_file_operations;
static const struct file_operations cache_flush_operations;
L
Linus Torvalds 已提交
303

304 305
static void do_cache_clean(struct work_struct *work);
static DECLARE_DELAYED_WORK(cache_cleaner, do_cache_clean);
L
Linus Torvalds 已提交
306

307
static void remove_cache_proc_entries(struct cache_detail *cd)
L
Linus Torvalds 已提交
308
{
309 310 311 312 313 314 315 316 317 318 319
	if (cd->proc_ent == NULL)
		return;
	if (cd->flush_ent)
		remove_proc_entry("flush", cd->proc_ent);
	if (cd->channel_ent)
		remove_proc_entry("channel", cd->proc_ent);
	if (cd->content_ent)
		remove_proc_entry("content", cd->proc_ent);
	cd->proc_ent = NULL;
	remove_proc_entry(cd->name, proc_net_rpc);
}
320

321 322
#ifdef CONFIG_PROC_FS
static int create_cache_proc_entries(struct cache_detail *cd)
323 324
{
	struct proc_dir_entry *p;
325

326 327
	cd->proc_ent = proc_mkdir(cd->name, proc_net_rpc);
	if (cd->proc_ent == NULL)
328
		goto out_nomem;
329 330
	cd->channel_ent = cd->content_ent = NULL;

331 332
	p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR,
			     cd->proc_ent, &cache_flush_operations, cd);
333 334
	cd->flush_ent = p;
	if (p == NULL)
335
		goto out_nomem;
336 337

	if (cd->cache_request || cd->cache_parse) {
338 339
		p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR,
				     cd->proc_ent, &cache_file_operations, cd);
340 341
		cd->channel_ent = p;
		if (p == NULL)
342
			goto out_nomem;
L
Linus Torvalds 已提交
343
	}
344
	if (cd->cache_show) {
345 346
		p = proc_create_data("content", S_IFREG|S_IRUSR|S_IWUSR,
				cd->proc_ent, &content_file_operations, cd);
347 348
		cd->content_ent = p;
		if (p == NULL)
349
			goto out_nomem;
350
	}
351 352 353 354
	return 0;
out_nomem:
	remove_cache_proc_entries(cd);
	return -ENOMEM;
355
}
356 357 358 359 360 361
#else /* CONFIG_PROC_FS */
static int create_cache_proc_entries(struct cache_detail *cd)
{
	return 0;
}
#endif
362

363
int cache_register(struct cache_detail *cd)
364
{
365 366 367 368 369
	int ret;

	ret = create_cache_proc_entries(cd);
	if (ret)
		return ret;
L
Linus Torvalds 已提交
370 371 372 373 374 375 376 377 378 379 380 381
	rwlock_init(&cd->hash_lock);
	INIT_LIST_HEAD(&cd->queue);
	spin_lock(&cache_list_lock);
	cd->nextcheck = 0;
	cd->entries = 0;
	atomic_set(&cd->readers, 0);
	cd->last_close = 0;
	cd->last_warn = -1;
	list_add(&cd->others, &cache_list);
	spin_unlock(&cache_list_lock);

	/* start the cleaning process */
382
	schedule_delayed_work(&cache_cleaner, 0);
383
	return 0;
L
Linus Torvalds 已提交
384
}
385
EXPORT_SYMBOL_GPL(cache_register);
L
Linus Torvalds 已提交
386

387
void cache_unregister(struct cache_detail *cd)
L
Linus Torvalds 已提交
388 389 390 391 392 393 394
{
	cache_purge(cd);
	spin_lock(&cache_list_lock);
	write_lock(&cd->hash_lock);
	if (cd->entries || atomic_read(&cd->inuse)) {
		write_unlock(&cd->hash_lock);
		spin_unlock(&cache_list_lock);
395
		goto out;
L
Linus Torvalds 已提交
396 397 398 399 400 401
	}
	if (current_detail == cd)
		current_detail = NULL;
	list_del_init(&cd->others);
	write_unlock(&cd->hash_lock);
	spin_unlock(&cache_list_lock);
402
	remove_cache_proc_entries(cd);
L
Linus Torvalds 已提交
403 404
	if (list_empty(&cache_list)) {
		/* module must be being unloaded so its safe to kill the worker */
405
		cancel_delayed_work_sync(&cache_cleaner);
L
Linus Torvalds 已提交
406
	}
407 408 409
	return;
out:
	printk(KERN_ERR "nfsd: failed to unregister %s cache\n", cd->name);
L
Linus Torvalds 已提交
410
}
411
EXPORT_SYMBOL_GPL(cache_unregister);
L
Linus Torvalds 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453

/* clean cache tries to find something to clean
 * and cleans it.
 * It returns 1 if it cleaned something,
 *            0 if it didn't find anything this time
 *           -1 if it fell off the end of the list.
 */
static int cache_clean(void)
{
	int rv = 0;
	struct list_head *next;

	spin_lock(&cache_list_lock);

	/* find a suitable table if we don't already have one */
	while (current_detail == NULL ||
	    current_index >= current_detail->hash_size) {
		if (current_detail)
			next = current_detail->others.next;
		else
			next = cache_list.next;
		if (next == &cache_list) {
			current_detail = NULL;
			spin_unlock(&cache_list_lock);
			return -1;
		}
		current_detail = list_entry(next, struct cache_detail, others);
		if (current_detail->nextcheck > get_seconds())
			current_index = current_detail->hash_size;
		else {
			current_index = 0;
			current_detail->nextcheck = get_seconds()+30*60;
		}
	}

	/* find a non-empty bucket in the table */
	while (current_detail &&
	       current_index < current_detail->hash_size &&
	       current_detail->hash_table[current_index] == NULL)
		current_index++;

	/* find a cleanable entry in the bucket and clean it, or set to next bucket */
454

L
Linus Torvalds 已提交
455 456 457
	if (current_detail && current_index < current_detail->hash_size) {
		struct cache_head *ch, **cp;
		struct cache_detail *d;
458

L
Linus Torvalds 已提交
459 460 461
		write_lock(&current_detail->hash_lock);

		/* Ok, now to clean this strand */
462

L
Linus Torvalds 已提交
463 464 465 466 467 468 469 470 471 472
		cp = & current_detail->hash_table[current_index];
		ch = *cp;
		for (; ch; cp= & ch->next, ch= *cp) {
			if (current_detail->nextcheck > ch->expiry_time)
				current_detail->nextcheck = ch->expiry_time+1;
			if (ch->expiry_time >= get_seconds()
			    && ch->last_refresh >= current_detail->flush_time
				)
				continue;
			if (test_and_clear_bit(CACHE_PENDING, &ch->flags))
473
				cache_dequeue(current_detail, ch);
L
Linus Torvalds 已提交
474

475
			if (atomic_read(&ch->ref.refcount) == 1)
L
Linus Torvalds 已提交
476 477 478 479 480 481 482 483 484 485 486 487 488
				break;
		}
		if (ch) {
			*cp = ch->next;
			ch->next = NULL;
			current_detail->entries--;
			rv = 1;
		}
		write_unlock(&current_detail->hash_lock);
		d = current_detail;
		if (!ch)
			current_index ++;
		spin_unlock(&cache_list_lock);
489 490
		if (ch) {
			cache_revisit_request(ch);
491
			cache_put(ch, d);
492
		}
L
Linus Torvalds 已提交
493 494 495 496 497 498 499 500 501
	} else
		spin_unlock(&cache_list_lock);

	return rv;
}

/*
 * We want to regularly clean the cache, so we need to schedule some work ...
 */
502
static void do_cache_clean(struct work_struct *work)
L
Linus Torvalds 已提交
503 504 505
{
	int delay = 5;
	if (cache_clean() == -1)
506
		delay = round_jiffies_relative(30*HZ);
L
Linus Torvalds 已提交
507 508 509 510 511 512 513 514 515

	if (list_empty(&cache_list))
		delay = 0;

	if (delay)
		schedule_delayed_work(&cache_cleaner, delay);
}


516
/*
L
Linus Torvalds 已提交
517
 * Clean all caches promptly.  This just calls cache_clean
518
 * repeatedly until we are sure that every cache has had a chance to
L
Linus Torvalds 已提交
519 520 521 522 523 524 525 526 527
 * be fully cleaned
 */
void cache_flush(void)
{
	while (cache_clean() != -1)
		cond_resched();
	while (cache_clean() != -1)
		cond_resched();
}
528
EXPORT_SYMBOL_GPL(cache_flush);
L
Linus Torvalds 已提交
529 530 531 532 533 534 535 536

void cache_purge(struct cache_detail *detail)
{
	detail->flush_time = LONG_MAX;
	detail->nextcheck = get_seconds();
	cache_flush();
	detail->flush_time = 1;
}
537
EXPORT_SYMBOL_GPL(cache_purge);
L
Linus Torvalds 已提交
538 539 540 541 542 543 544 545 546 547


/*
 * Deferral and Revisiting of Requests.
 *
 * If a cache lookup finds a pending entry, we
 * need to defer the request and revisit it later.
 * All deferred requests are stored in a hash table,
 * indexed by "struct cache_head *".
 * As it may be wasteful to store a whole request
548
 * structure, we allow the request to provide a
L
Linus Torvalds 已提交
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
 * deferred form, which must contain a
 * 'struct cache_deferred_req'
 * This cache_deferred_req contains a method to allow
 * it to be revisited when cache info is available
 */

#define	DFR_HASHSIZE	(PAGE_SIZE/sizeof(struct list_head))
#define	DFR_HASH(item)	((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)

#define	DFR_MAX	300	/* ??? */

static DEFINE_SPINLOCK(cache_defer_lock);
static LIST_HEAD(cache_defer_list);
static struct list_head cache_defer_hash[DFR_HASHSIZE];
static int cache_defer_cnt;

565
static int cache_defer_req(struct cache_req *req, struct cache_head *item)
L
Linus Torvalds 已提交
566 567 568 569
{
	struct cache_deferred_req *dreq;
	int hash = DFR_HASH(item);

570 571 572 573 574
	if (cache_defer_cnt >= DFR_MAX) {
		/* too much in the cache, randomly drop this one,
		 * or continue and drop the oldest below
		 */
		if (net_random()&1)
575
			return 0;
576
	}
L
Linus Torvalds 已提交
577 578
	dreq = req->defer(req);
	if (dreq == NULL)
579
		return 0;
L
Linus Torvalds 已提交
580 581 582 583 584 585 586 587 588 589 590 591 592 593

	dreq->item = item;

	spin_lock(&cache_defer_lock);

	list_add(&dreq->recent, &cache_defer_list);

	if (cache_defer_hash[hash].next == NULL)
		INIT_LIST_HEAD(&cache_defer_hash[hash]);
	list_add(&dreq->hash, &cache_defer_hash[hash]);

	/* it is in, now maybe clean up */
	dreq = NULL;
	if (++cache_defer_cnt > DFR_MAX) {
594 595
		dreq = list_entry(cache_defer_list.prev,
				  struct cache_deferred_req, recent);
L
Linus Torvalds 已提交
596 597 598 599 600 601 602 603 604 605
		list_del(&dreq->recent);
		list_del(&dreq->hash);
		cache_defer_cnt--;
	}
	spin_unlock(&cache_defer_lock);

	if (dreq) {
		/* there was one too many */
		dreq->revisit(dreq, 1);
	}
606
	if (!test_bit(CACHE_PENDING, &item->flags)) {
L
Linus Torvalds 已提交
607 608
		/* must have just been validated... */
		cache_revisit_request(item);
609
		return 0;
L
Linus Torvalds 已提交
610
	}
611
	return 1;
L
Linus Torvalds 已提交
612 613 614 615 616 617 618 619 620 621 622 623
}

static void cache_revisit_request(struct cache_head *item)
{
	struct cache_deferred_req *dreq;
	struct list_head pending;

	struct list_head *lp;
	int hash = DFR_HASH(item);

	INIT_LIST_HEAD(&pending);
	spin_lock(&cache_defer_lock);
624

L
Linus Torvalds 已提交
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
	lp = cache_defer_hash[hash].next;
	if (lp) {
		while (lp != &cache_defer_hash[hash]) {
			dreq = list_entry(lp, struct cache_deferred_req, hash);
			lp = lp->next;
			if (dreq->item == item) {
				list_del(&dreq->hash);
				list_move(&dreq->recent, &pending);
				cache_defer_cnt--;
			}
		}
	}
	spin_unlock(&cache_defer_lock);

	while (!list_empty(&pending)) {
		dreq = list_entry(pending.next, struct cache_deferred_req, recent);
		list_del_init(&dreq->recent);
		dreq->revisit(dreq, 0);
	}
}

void cache_clean_deferred(void *owner)
{
	struct cache_deferred_req *dreq, *tmp;
	struct list_head pending;


	INIT_LIST_HEAD(&pending);
	spin_lock(&cache_defer_lock);
654

L
Linus Torvalds 已提交
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
	list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
		if (dreq->owner == owner) {
			list_del(&dreq->hash);
			list_move(&dreq->recent, &pending);
			cache_defer_cnt--;
		}
	}
	spin_unlock(&cache_defer_lock);

	while (!list_empty(&pending)) {
		dreq = list_entry(pending.next, struct cache_deferred_req, recent);
		list_del_init(&dreq->recent);
		dreq->revisit(dreq, 1);
	}
}

/*
 * communicate with user-space
 *
J
J. Bruce Fields 已提交
674 675 676 677
 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
 * On read, you get a full request, or block.
 * On write, an update request is processed.
 * Poll works if anything to read, and always allows write.
L
Linus Torvalds 已提交
678
 *
679
 * Implemented by linked list of requests.  Each open file has
J
J. Bruce Fields 已提交
680
 * a ->private that also exists in this list.  New requests are added
L
Linus Torvalds 已提交
681 682 683 684 685 686 687
 * to the end and may wakeup and preceding readers.
 * New readers are added to the head.  If, on read, an item is found with
 * CACHE_UPCALLING clear, we free it from the list.
 *
 */

static DEFINE_SPINLOCK(queue_lock);
A
Arjan van de Ven 已提交
688
static DEFINE_MUTEX(queue_io_mutex);
L
Linus Torvalds 已提交
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710

struct cache_queue {
	struct list_head	list;
	int			reader;	/* if 0, then request */
};
struct cache_request {
	struct cache_queue	q;
	struct cache_head	*item;
	char			* buf;
	int			len;
	int			readers;
};
struct cache_reader {
	struct cache_queue	q;
	int			offset;	/* if non-0, we have a refcnt on next request */
};

static ssize_t
cache_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
{
	struct cache_reader *rp = filp->private_data;
	struct cache_request *rq;
J
Josef Sipek 已提交
711
	struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
L
Linus Torvalds 已提交
712 713 714 715 716
	int err;

	if (count == 0)
		return 0;

A
Arjan van de Ven 已提交
717
	mutex_lock(&queue_io_mutex); /* protect against multiple concurrent
L
Linus Torvalds 已提交
718 719 720 721 722 723 724 725 726 727 728 729
			      * readers on this file */
 again:
	spin_lock(&queue_lock);
	/* need to find next request */
	while (rp->q.list.next != &cd->queue &&
	       list_entry(rp->q.list.next, struct cache_queue, list)
	       ->reader) {
		struct list_head *next = rp->q.list.next;
		list_move(&rp->q.list, next);
	}
	if (rp->q.list.next == &cd->queue) {
		spin_unlock(&queue_lock);
A
Arjan van de Ven 已提交
730
		mutex_unlock(&queue_io_mutex);
731
		BUG_ON(rp->offset);
L
Linus Torvalds 已提交
732 733 734
		return 0;
	}
	rq = container_of(rp->q.list.next, struct cache_request, q.list);
735
	BUG_ON(rq->q.reader);
L
Linus Torvalds 已提交
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
	if (rp->offset == 0)
		rq->readers++;
	spin_unlock(&queue_lock);

	if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
		err = -EAGAIN;
		spin_lock(&queue_lock);
		list_move(&rp->q.list, &rq->q.list);
		spin_unlock(&queue_lock);
	} else {
		if (rp->offset + count > rq->len)
			count = rq->len - rp->offset;
		err = -EFAULT;
		if (copy_to_user(buf, rq->buf + rp->offset, count))
			goto out;
		rp->offset += count;
		if (rp->offset >= rq->len) {
			rp->offset = 0;
			spin_lock(&queue_lock);
			list_move(&rp->q.list, &rq->q.list);
			spin_unlock(&queue_lock);
		}
		err = 0;
	}
 out:
	if (rp->offset == 0) {
		/* need to release rq */
		spin_lock(&queue_lock);
		rq->readers--;
		if (rq->readers == 0 &&
		    !test_bit(CACHE_PENDING, &rq->item->flags)) {
			list_del(&rq->q.list);
			spin_unlock(&queue_lock);
769
			cache_put(rq->item, cd);
L
Linus Torvalds 已提交
770 771 772 773 774 775 776
			kfree(rq->buf);
			kfree(rq);
		} else
			spin_unlock(&queue_lock);
	}
	if (err == -EAGAIN)
		goto again;
A
Arjan van de Ven 已提交
777
	mutex_unlock(&queue_io_mutex);
L
Linus Torvalds 已提交
778 779 780
	return err ? err :  count;
}

A
Arjan van de Ven 已提交
781
static char write_buf[8192]; /* protected by queue_io_mutex */
L
Linus Torvalds 已提交
782 783 784 785 786 787

static ssize_t
cache_write(struct file *filp, const char __user *buf, size_t count,
	    loff_t *ppos)
{
	int err;
J
Josef Sipek 已提交
788
	struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
L
Linus Torvalds 已提交
789 790 791 792 793 794

	if (count == 0)
		return 0;
	if (count >= sizeof(write_buf))
		return -EINVAL;

A
Arjan van de Ven 已提交
795
	mutex_lock(&queue_io_mutex);
L
Linus Torvalds 已提交
796 797

	if (copy_from_user(write_buf, buf, count)) {
A
Arjan van de Ven 已提交
798
		mutex_unlock(&queue_io_mutex);
L
Linus Torvalds 已提交
799 800 801 802 803 804 805 806
		return -EFAULT;
	}
	write_buf[count] = '\0';
	if (cd->cache_parse)
		err = cd->cache_parse(cd, write_buf, count);
	else
		err = -EINVAL;

A
Arjan van de Ven 已提交
807
	mutex_unlock(&queue_io_mutex);
L
Linus Torvalds 已提交
808 809 810 811 812 813 814 815 816 817 818
	return err ? err : count;
}

static DECLARE_WAIT_QUEUE_HEAD(queue_wait);

static unsigned int
cache_poll(struct file *filp, poll_table *wait)
{
	unsigned int mask;
	struct cache_reader *rp = filp->private_data;
	struct cache_queue *cq;
J
Josef Sipek 已提交
819
	struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
L
Linus Torvalds 已提交
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926

	poll_wait(filp, &queue_wait, wait);

	/* alway allow write */
	mask = POLL_OUT | POLLWRNORM;

	if (!rp)
		return mask;

	spin_lock(&queue_lock);

	for (cq= &rp->q; &cq->list != &cd->queue;
	     cq = list_entry(cq->list.next, struct cache_queue, list))
		if (!cq->reader) {
			mask |= POLLIN | POLLRDNORM;
			break;
		}
	spin_unlock(&queue_lock);
	return mask;
}

static int
cache_ioctl(struct inode *ino, struct file *filp,
	    unsigned int cmd, unsigned long arg)
{
	int len = 0;
	struct cache_reader *rp = filp->private_data;
	struct cache_queue *cq;
	struct cache_detail *cd = PDE(ino)->data;

	if (cmd != FIONREAD || !rp)
		return -EINVAL;

	spin_lock(&queue_lock);

	/* only find the length remaining in current request,
	 * or the length of the next request
	 */
	for (cq= &rp->q; &cq->list != &cd->queue;
	     cq = list_entry(cq->list.next, struct cache_queue, list))
		if (!cq->reader) {
			struct cache_request *cr =
				container_of(cq, struct cache_request, q);
			len = cr->len - rp->offset;
			break;
		}
	spin_unlock(&queue_lock);

	return put_user(len, (int __user *)arg);
}

static int
cache_open(struct inode *inode, struct file *filp)
{
	struct cache_reader *rp = NULL;

	nonseekable_open(inode, filp);
	if (filp->f_mode & FMODE_READ) {
		struct cache_detail *cd = PDE(inode)->data;

		rp = kmalloc(sizeof(*rp), GFP_KERNEL);
		if (!rp)
			return -ENOMEM;
		rp->offset = 0;
		rp->q.reader = 1;
		atomic_inc(&cd->readers);
		spin_lock(&queue_lock);
		list_add(&rp->q.list, &cd->queue);
		spin_unlock(&queue_lock);
	}
	filp->private_data = rp;
	return 0;
}

static int
cache_release(struct inode *inode, struct file *filp)
{
	struct cache_reader *rp = filp->private_data;
	struct cache_detail *cd = PDE(inode)->data;

	if (rp) {
		spin_lock(&queue_lock);
		if (rp->offset) {
			struct cache_queue *cq;
			for (cq= &rp->q; &cq->list != &cd->queue;
			     cq = list_entry(cq->list.next, struct cache_queue, list))
				if (!cq->reader) {
					container_of(cq, struct cache_request, q)
						->readers--;
					break;
				}
			rp->offset = 0;
		}
		list_del(&rp->q.list);
		spin_unlock(&queue_lock);

		filp->private_data = NULL;
		kfree(rp);

		cd->last_close = get_seconds();
		atomic_dec(&cd->readers);
	}
	return 0;
}



927
static const struct file_operations cache_file_operations = {
L
Linus Torvalds 已提交
928 929 930 931 932 933 934 935 936 937 938
	.owner		= THIS_MODULE,
	.llseek		= no_llseek,
	.read		= cache_read,
	.write		= cache_write,
	.poll		= cache_poll,
	.ioctl		= cache_ioctl, /* for FIONREAD */
	.open		= cache_open,
	.release	= cache_release,
};


939
static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
L
Linus Torvalds 已提交
940 941 942 943 944 945 946 947 948
{
	struct cache_queue *cq;
	spin_lock(&queue_lock);
	list_for_each_entry(cq, &detail->queue, list)
		if (!cq->reader) {
			struct cache_request *cr = container_of(cq, struct cache_request, q);
			if (cr->item != ch)
				continue;
			if (cr->readers != 0)
949
				continue;
L
Linus Torvalds 已提交
950 951
			list_del(&cr->q.list);
			spin_unlock(&queue_lock);
952
			cache_put(cr->item, detail);
L
Linus Torvalds 已提交
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
			kfree(cr->buf);
			kfree(cr);
			return;
		}
	spin_unlock(&queue_lock);
}

/*
 * Support routines for text-based upcalls.
 * Fields are separated by spaces.
 * Fields are either mangled to quote space tab newline slosh with slosh
 * or a hexified with a leading \x
 * Record is terminated with newline.
 *
 */

void qword_add(char **bpp, int *lp, char *str)
{
	char *bp = *bpp;
	int len = *lp;
	char c;

	if (len < 0) return;

	while ((c=*str++) && len)
		switch(c) {
		case ' ':
		case '\t':
		case '\n':
		case '\\':
			if (len >= 4) {
				*bp++ = '\\';
				*bp++ = '0' + ((c & 0300)>>6);
				*bp++ = '0' + ((c & 0070)>>3);
				*bp++ = '0' + ((c & 0007)>>0);
			}
			len -= 4;
			break;
		default:
			*bp++ = c;
			len--;
		}
	if (c || len <1) len = -1;
	else {
		*bp++ = ' ';
		len--;
	}
	*bpp = bp;
	*lp = len;
}
1003
EXPORT_SYMBOL_GPL(qword_add);
L
Linus Torvalds 已提交
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031

void qword_addhex(char **bpp, int *lp, char *buf, int blen)
{
	char *bp = *bpp;
	int len = *lp;

	if (len < 0) return;

	if (len > 2) {
		*bp++ = '\\';
		*bp++ = 'x';
		len -= 2;
		while (blen && len >= 2) {
			unsigned char c = *buf++;
			*bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
			*bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
			len -= 2;
			blen--;
		}
	}
	if (blen || len<1) len = -1;
	else {
		*bp++ = ' ';
		len--;
	}
	*bpp = bp;
	*lp = len;
}
1032
EXPORT_SYMBOL_GPL(qword_addhex);
L
Linus Torvalds 已提交
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100

static void warn_no_listener(struct cache_detail *detail)
{
	if (detail->last_warn != detail->last_close) {
		detail->last_warn = detail->last_close;
		if (detail->warn_no_listener)
			detail->warn_no_listener(detail);
	}
}

/*
 * register an upcall request to user-space.
 * Each request is at most one page long.
 */
static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h)
{

	char *buf;
	struct cache_request *crq;
	char *bp;
	int len;

	if (detail->cache_request == NULL)
		return -EINVAL;

	if (atomic_read(&detail->readers) == 0 &&
	    detail->last_close < get_seconds() - 30) {
			warn_no_listener(detail);
			return -EINVAL;
	}

	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
	if (!buf)
		return -EAGAIN;

	crq = kmalloc(sizeof (*crq), GFP_KERNEL);
	if (!crq) {
		kfree(buf);
		return -EAGAIN;
	}

	bp = buf; len = PAGE_SIZE;

	detail->cache_request(detail, h, &bp, &len);

	if (len < 0) {
		kfree(buf);
		kfree(crq);
		return -EAGAIN;
	}
	crq->q.reader = 0;
	crq->item = cache_get(h);
	crq->buf = buf;
	crq->len = PAGE_SIZE - len;
	crq->readers = 0;
	spin_lock(&queue_lock);
	list_add_tail(&crq->q.list, &detail->queue);
	spin_unlock(&queue_lock);
	wake_up(&queue_wait);
	return 0;
}

/*
 * parse a message from user-space and pass it
 * to an appropriate cache
 * Messages are, like requests, separated into fields by
 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
 *
1101
 * Message is
L
Linus Torvalds 已提交
1102 1103
 *   reply cachename expiry key ... content....
 *
1104
 * key and content are both parsed by cache
L
Linus Torvalds 已提交
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
 */

#define isodigit(c) (isdigit(c) && c <= '7')
int qword_get(char **bpp, char *dest, int bufsize)
{
	/* return bytes copied, or -1 on error */
	char *bp = *bpp;
	int len = 0;

	while (*bp == ' ') bp++;

	if (bp[0] == '\\' && bp[1] == 'x') {
		/* HEX STRING */
		bp += 2;
		while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) {
			int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
			bp++;
			byte <<= 4;
			byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
			*dest++ = byte;
			bp++;
			len++;
		}
	} else {
		/* text with \nnn octal quoting */
		while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
			if (*bp == '\\' &&
			    isodigit(bp[1]) && (bp[1] <= '3') &&
			    isodigit(bp[2]) &&
			    isodigit(bp[3])) {
				int byte = (*++bp -'0');
				bp++;
				byte = (byte << 3) | (*bp++ - '0');
				byte = (byte << 3) | (*bp++ - '0');
				*dest++ = byte;
				len++;
			} else {
				*dest++ = *bp++;
				len++;
			}
		}
	}

	if (*bp != ' ' && *bp != '\n' && *bp != '\0')
		return -1;
	while (*bp == ' ') bp++;
	*bpp = bp;
	*dest = '\0';
	return len;
}
1155
EXPORT_SYMBOL_GPL(qword_get);
L
Linus Torvalds 已提交
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169


/*
 * support /proc/sunrpc/cache/$CACHENAME/content
 * as a seqfile.
 * We call ->cache_show passing NULL for the item to
 * get a header, then pass each real item in the cache
 */

struct handle {
	struct cache_detail *cd;
};

static void *c_start(struct seq_file *m, loff_t *pos)
1170
	__acquires(cd->hash_lock)
L
Linus Torvalds 已提交
1171 1172 1173 1174 1175
{
	loff_t n = *pos;
	unsigned hash, entry;
	struct cache_head *ch;
	struct cache_detail *cd = ((struct handle*)m->private)->cd;
1176

L
Linus Torvalds 已提交
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190

	read_lock(&cd->hash_lock);
	if (!n--)
		return SEQ_START_TOKEN;
	hash = n >> 32;
	entry = n & ((1LL<<32) - 1);

	for (ch=cd->hash_table[hash]; ch; ch=ch->next)
		if (!entry--)
			return ch;
	n &= ~((1LL<<32) - 1);
	do {
		hash++;
		n += 1LL<<32;
1191
	} while(hash < cd->hash_size &&
L
Linus Torvalds 已提交
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
		cd->hash_table[hash]==NULL);
	if (hash >= cd->hash_size)
		return NULL;
	*pos = n+1;
	return cd->hash_table[hash];
}

static void *c_next(struct seq_file *m, void *p, loff_t *pos)
{
	struct cache_head *ch = p;
	int hash = (*pos >> 32);
	struct cache_detail *cd = ((struct handle*)m->private)->cd;

	if (p == SEQ_START_TOKEN)
		hash = 0;
	else if (ch->next == NULL) {
		hash++;
		*pos += 1LL<<32;
	} else {
		++*pos;
		return ch->next;
	}
	*pos &= ~((1LL<<32) - 1);
	while (hash < cd->hash_size &&
	       cd->hash_table[hash] == NULL) {
		hash++;
		*pos += 1LL<<32;
	}
	if (hash >= cd->hash_size)
		return NULL;
	++*pos;
	return cd->hash_table[hash];
}

static void c_stop(struct seq_file *m, void *p)
1227
	__releases(cd->hash_lock)
L
Linus Torvalds 已提交
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
{
	struct cache_detail *cd = ((struct handle*)m->private)->cd;
	read_unlock(&cd->hash_lock);
}

static int c_show(struct seq_file *m, void *p)
{
	struct cache_head *cp = p;
	struct cache_detail *cd = ((struct handle*)m->private)->cd;

	if (p == SEQ_START_TOKEN)
		return cd->cache_show(m, cd, NULL);

	ifdebug(CACHE)
1242
		seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
1243
			   cp->expiry_time, atomic_read(&cp->ref.refcount), cp->flags);
L
Linus Torvalds 已提交
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
	cache_get(cp);
	if (cache_check(cd, cp, NULL))
		/* cache_check does a cache_put on failure */
		seq_printf(m, "# ");
	else
		cache_put(cp, cd);

	return cd->cache_show(m, cd, cp);
}

1254
static const struct seq_operations cache_content_op = {
L
Linus Torvalds 已提交
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
	.start	= c_start,
	.next	= c_next,
	.stop	= c_stop,
	.show	= c_show,
};

static int content_open(struct inode *inode, struct file *file)
{
	struct handle *han;
	struct cache_detail *cd = PDE(inode)->data;

1266
	han = __seq_open_private(file, &cache_content_op, sizeof(*han));
L
Linus Torvalds 已提交
1267 1268 1269 1270
	if (han == NULL)
		return -ENOMEM;

	han->cd = cd;
1271
	return 0;
L
Linus Torvalds 已提交
1272 1273
}

1274
static const struct file_operations content_file_operations = {
L
Linus Torvalds 已提交
1275 1276 1277
	.open		= content_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
1278
	.release	= seq_release_private,
L
Linus Torvalds 已提交
1279 1280 1281 1282 1283
};

static ssize_t read_flush(struct file *file, char __user *buf,
			    size_t count, loff_t *ppos)
{
J
Josef Sipek 已提交
1284
	struct cache_detail *cd = PDE(file->f_path.dentry->d_inode)->data;
L
Linus Torvalds 已提交
1285 1286
	char tbuf[20];
	unsigned long p = *ppos;
1287
	size_t len;
L
Linus Torvalds 已提交
1288 1289 1290 1291 1292 1293

	sprintf(tbuf, "%lu\n", cd->flush_time);
	len = strlen(tbuf);
	if (p >= len)
		return 0;
	len -= p;
1294 1295
	if (len > count)
		len = count;
L
Linus Torvalds 已提交
1296
	if (copy_to_user(buf, (void*)(tbuf+p), len))
1297 1298
		return -EFAULT;
	*ppos += len;
L
Linus Torvalds 已提交
1299 1300 1301 1302 1303 1304
	return len;
}

static ssize_t write_flush(struct file * file, const char __user * buf,
			     size_t count, loff_t *ppos)
{
J
Josef Sipek 已提交
1305
	struct cache_detail *cd = PDE(file->f_path.dentry->d_inode)->data;
L
Linus Torvalds 已提交
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
	char tbuf[20];
	char *ep;
	long flushtime;
	if (*ppos || count > sizeof(tbuf)-1)
		return -EINVAL;
	if (copy_from_user(tbuf, buf, count))
		return -EFAULT;
	tbuf[count] = 0;
	flushtime = simple_strtoul(tbuf, &ep, 0);
	if (*ep && *ep != '\n')
		return -EINVAL;

	cd->flush_time = flushtime;
	cd->nextcheck = get_seconds();
	cache_flush();

	*ppos += count;
	return count;
}

1326
static const struct file_operations cache_flush_operations = {
L
Linus Torvalds 已提交
1327 1328 1329 1330
	.open		= nonseekable_open,
	.read		= read_flush,
	.write		= write_flush,
};