CPUFREQ: Fix some more CPU hotplug locking.
[linux/fpc-iii.git] / net / sunrpc / cache.c
blob00cb388ece032cec8aeba948bb97c0e653eede5e
1 /*
2 * net/sunrpc/cache.c
4 * Generic code for various authentication-related caches
5 * used by sunrpc clients and servers.
7 * Copyright (C) 2002 Neil Brown <neilb@cse.unsw.edu.au>
9 * Released under terms in GPL version 2. See COPYING.
13 #include <linux/types.h>
14 #include <linux/fs.h>
15 #include <linux/file.h>
16 #include <linux/slab.h>
17 #include <linux/signal.h>
18 #include <linux/sched.h>
19 #include <linux/kmod.h>
20 #include <linux/list.h>
21 #include <linux/module.h>
22 #include <linux/ctype.h>
23 #include <asm/uaccess.h>
24 #include <linux/poll.h>
25 #include <linux/seq_file.h>
26 #include <linux/proc_fs.h>
27 #include <linux/net.h>
28 #include <linux/workqueue.h>
29 #include <linux/mutex.h>
30 #include <asm/ioctls.h>
31 #include <linux/sunrpc/types.h>
32 #include <linux/sunrpc/cache.h>
33 #include <linux/sunrpc/stats.h>
35 #define RPCDBG_FACILITY RPCDBG_CACHE
37 static void cache_defer_req(struct cache_req *req, struct cache_head *item);
38 static void cache_revisit_request(struct cache_head *item);
40 static void cache_init(struct cache_head *h)
42 time_t now = get_seconds();
43 h->next = NULL;
44 h->flags = 0;
45 kref_init(&h->ref);
46 h->expiry_time = now + CACHE_NEW_EXPIRY;
47 h->last_refresh = now;
50 struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail,
51 struct cache_head *key, int hash)
53 struct cache_head **head, **hp;
54 struct cache_head *new = NULL;
56 head = &detail->hash_table[hash];
58 read_lock(&detail->hash_lock);
60 for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
61 struct cache_head *tmp = *hp;
62 if (detail->match(tmp, key)) {
63 cache_get(tmp);
64 read_unlock(&detail->hash_lock);
65 return tmp;
68 read_unlock(&detail->hash_lock);
69 /* Didn't find anything, insert an empty entry */
71 new = detail->alloc();
72 if (!new)
73 return NULL;
74 /* must fully initialise 'new', else
75 * we might get lose if we need to
76 * cache_put it soon.
78 cache_init(new);
79 detail->init(new, key);
81 write_lock(&detail->hash_lock);
83 /* check if entry appeared while we slept */
84 for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
85 struct cache_head *tmp = *hp;
86 if (detail->match(tmp, key)) {
87 cache_get(tmp);
88 write_unlock(&detail->hash_lock);
89 cache_put(new, detail);
90 return tmp;
93 new->next = *head;
94 *head = new;
95 detail->entries++;
96 cache_get(new);
97 write_unlock(&detail->hash_lock);
99 return new;
101 EXPORT_SYMBOL(sunrpc_cache_lookup);
104 static void queue_loose(struct cache_detail *detail, struct cache_head *ch);
106 static int cache_fresh_locked(struct cache_head *head, time_t expiry)
108 head->expiry_time = expiry;
109 head->last_refresh = get_seconds();
110 return !test_and_set_bit(CACHE_VALID, &head->flags);
113 static void cache_fresh_unlocked(struct cache_head *head,
114 struct cache_detail *detail, int new)
116 if (new)
117 cache_revisit_request(head);
118 if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
119 cache_revisit_request(head);
120 queue_loose(detail, head);
124 struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
125 struct cache_head *new, struct cache_head *old, int hash)
127 /* The 'old' entry is to be replaced by 'new'.
128 * If 'old' is not VALID, we update it directly,
129 * otherwise we need to replace it
131 struct cache_head **head;
132 struct cache_head *tmp;
133 int is_new;
135 if (!test_bit(CACHE_VALID, &old->flags)) {
136 write_lock(&detail->hash_lock);
137 if (!test_bit(CACHE_VALID, &old->flags)) {
138 if (test_bit(CACHE_NEGATIVE, &new->flags))
139 set_bit(CACHE_NEGATIVE, &old->flags);
140 else
141 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 return old;
147 write_unlock(&detail->hash_lock);
149 /* We need to insert a new entry */
150 tmp = detail->alloc();
151 if (!tmp) {
152 cache_put(old, detail);
153 return NULL;
155 cache_init(tmp);
156 detail->init(tmp, old);
157 head = &detail->hash_table[hash];
159 write_lock(&detail->hash_lock);
160 if (test_bit(CACHE_NEGATIVE, &new->flags))
161 set_bit(CACHE_NEGATIVE, &tmp->flags);
162 else
163 detail->update(tmp, new);
164 tmp->next = *head;
165 *head = tmp;
166 detail->entries++;
167 cache_get(tmp);
168 is_new = cache_fresh_locked(tmp, new->expiry_time);
169 cache_fresh_locked(old, 0);
170 write_unlock(&detail->hash_lock);
171 cache_fresh_unlocked(tmp, detail, is_new);
172 cache_fresh_unlocked(old, detail, 0);
173 cache_put(old, detail);
174 return tmp;
176 EXPORT_SYMBOL(sunrpc_cache_update);
178 static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h);
180 * This is the generic cache management routine for all
181 * the authentication caches.
182 * It checks the currency of a cache item and will (later)
183 * initiate an upcall to fill it if needed.
186 * Returns 0 if the cache_head can be used, or cache_puts it and returns
187 * -EAGAIN if upcall is pending,
188 * -ENOENT if cache entry was negative
190 int cache_check(struct cache_detail *detail,
191 struct cache_head *h, struct cache_req *rqstp)
193 int rv;
194 long refresh_age, age;
196 /* First decide return status as best we can */
197 if (!test_bit(CACHE_VALID, &h->flags) ||
198 h->expiry_time < get_seconds())
199 rv = -EAGAIN;
200 else if (detail->flush_time > h->last_refresh)
201 rv = -EAGAIN;
202 else {
203 /* entry is valid */
204 if (test_bit(CACHE_NEGATIVE, &h->flags))
205 rv = -ENOENT;
206 else rv = 0;
209 /* now see if we want to start an upcall */
210 refresh_age = (h->expiry_time - h->last_refresh);
211 age = get_seconds() - h->last_refresh;
213 if (rqstp == NULL) {
214 if (rv == -EAGAIN)
215 rv = -ENOENT;
216 } else if (rv == -EAGAIN || age > refresh_age/2) {
217 dprintk("Want update, refage=%ld, age=%ld\n", refresh_age, age);
218 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
219 switch (cache_make_upcall(detail, h)) {
220 case -EINVAL:
221 clear_bit(CACHE_PENDING, &h->flags);
222 if (rv == -EAGAIN) {
223 set_bit(CACHE_NEGATIVE, &h->flags);
224 cache_fresh_unlocked(h, detail,
225 cache_fresh_locked(h, get_seconds()+CACHE_NEW_EXPIRY));
226 rv = -ENOENT;
228 break;
230 case -EAGAIN:
231 clear_bit(CACHE_PENDING, &h->flags);
232 cache_revisit_request(h);
233 break;
238 if (rv == -EAGAIN)
239 cache_defer_req(rqstp, h);
241 if (rv)
242 cache_put(h, detail);
243 return rv;
247 * caches need to be periodically cleaned.
248 * For this we maintain a list of cache_detail and
249 * a current pointer into that list and into the table
250 * for that entry.
252 * Each time clean_cache is called it finds the next non-empty entry
253 * in the current table and walks the list in that entry
254 * looking for entries that can be removed.
256 * An entry gets removed if:
257 * - The expiry is before current time
258 * - The last_refresh time is before the flush_time for that cache
260 * later we might drop old entries with non-NEVER expiry if that table
261 * is getting 'full' for some definition of 'full'
263 * The question of "how often to scan a table" is an interesting one
264 * and is answered in part by the use of the "nextcheck" field in the
265 * cache_detail.
266 * When a scan of a table begins, the nextcheck field is set to a time
267 * that is well into the future.
268 * While scanning, if an expiry time is found that is earlier than the
269 * current nextcheck time, nextcheck is set to that expiry time.
270 * If the flush_time is ever set to a time earlier than the nextcheck
271 * time, the nextcheck time is then set to that flush_time.
273 * A table is then only scanned if the current time is at least
274 * the nextcheck time.
278 static LIST_HEAD(cache_list);
279 static DEFINE_SPINLOCK(cache_list_lock);
280 static struct cache_detail *current_detail;
281 static int current_index;
283 static struct file_operations cache_file_operations;
284 static struct file_operations content_file_operations;
285 static struct file_operations cache_flush_operations;
287 static void do_cache_clean(void *data);
288 static DECLARE_WORK(cache_cleaner, do_cache_clean, NULL);
290 void cache_register(struct cache_detail *cd)
292 cd->proc_ent = proc_mkdir(cd->name, proc_net_rpc);
293 if (cd->proc_ent) {
294 struct proc_dir_entry *p;
295 cd->proc_ent->owner = cd->owner;
296 cd->channel_ent = cd->content_ent = NULL;
298 p = create_proc_entry("flush", S_IFREG|S_IRUSR|S_IWUSR,
299 cd->proc_ent);
300 cd->flush_ent = p;
301 if (p) {
302 p->proc_fops = &cache_flush_operations;
303 p->owner = cd->owner;
304 p->data = cd;
307 if (cd->cache_request || cd->cache_parse) {
308 p = create_proc_entry("channel", S_IFREG|S_IRUSR|S_IWUSR,
309 cd->proc_ent);
310 cd->channel_ent = p;
311 if (p) {
312 p->proc_fops = &cache_file_operations;
313 p->owner = cd->owner;
314 p->data = cd;
317 if (cd->cache_show) {
318 p = create_proc_entry("content", S_IFREG|S_IRUSR|S_IWUSR,
319 cd->proc_ent);
320 cd->content_ent = p;
321 if (p) {
322 p->proc_fops = &content_file_operations;
323 p->owner = cd->owner;
324 p->data = cd;
328 rwlock_init(&cd->hash_lock);
329 INIT_LIST_HEAD(&cd->queue);
330 spin_lock(&cache_list_lock);
331 cd->nextcheck = 0;
332 cd->entries = 0;
333 atomic_set(&cd->readers, 0);
334 cd->last_close = 0;
335 cd->last_warn = -1;
336 list_add(&cd->others, &cache_list);
337 spin_unlock(&cache_list_lock);
339 /* start the cleaning process */
340 schedule_work(&cache_cleaner);
343 int cache_unregister(struct cache_detail *cd)
345 cache_purge(cd);
346 spin_lock(&cache_list_lock);
347 write_lock(&cd->hash_lock);
348 if (cd->entries || atomic_read(&cd->inuse)) {
349 write_unlock(&cd->hash_lock);
350 spin_unlock(&cache_list_lock);
351 return -EBUSY;
353 if (current_detail == cd)
354 current_detail = NULL;
355 list_del_init(&cd->others);
356 write_unlock(&cd->hash_lock);
357 spin_unlock(&cache_list_lock);
358 if (cd->proc_ent) {
359 if (cd->flush_ent)
360 remove_proc_entry("flush", cd->proc_ent);
361 if (cd->channel_ent)
362 remove_proc_entry("channel", cd->proc_ent);
363 if (cd->content_ent)
364 remove_proc_entry("content", cd->proc_ent);
366 cd->proc_ent = NULL;
367 remove_proc_entry(cd->name, proc_net_rpc);
369 if (list_empty(&cache_list)) {
370 /* module must be being unloaded so its safe to kill the worker */
371 cancel_delayed_work(&cache_cleaner);
372 flush_scheduled_work();
374 return 0;
377 /* clean cache tries to find something to clean
378 * and cleans it.
379 * It returns 1 if it cleaned something,
380 * 0 if it didn't find anything this time
381 * -1 if it fell off the end of the list.
383 static int cache_clean(void)
385 int rv = 0;
386 struct list_head *next;
388 spin_lock(&cache_list_lock);
390 /* find a suitable table if we don't already have one */
391 while (current_detail == NULL ||
392 current_index >= current_detail->hash_size) {
393 if (current_detail)
394 next = current_detail->others.next;
395 else
396 next = cache_list.next;
397 if (next == &cache_list) {
398 current_detail = NULL;
399 spin_unlock(&cache_list_lock);
400 return -1;
402 current_detail = list_entry(next, struct cache_detail, others);
403 if (current_detail->nextcheck > get_seconds())
404 current_index = current_detail->hash_size;
405 else {
406 current_index = 0;
407 current_detail->nextcheck = get_seconds()+30*60;
411 /* find a non-empty bucket in the table */
412 while (current_detail &&
413 current_index < current_detail->hash_size &&
414 current_detail->hash_table[current_index] == NULL)
415 current_index++;
417 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
419 if (current_detail && current_index < current_detail->hash_size) {
420 struct cache_head *ch, **cp;
421 struct cache_detail *d;
423 write_lock(&current_detail->hash_lock);
425 /* Ok, now to clean this strand */
427 cp = & current_detail->hash_table[current_index];
428 ch = *cp;
429 for (; ch; cp= & ch->next, ch= *cp) {
430 if (current_detail->nextcheck > ch->expiry_time)
431 current_detail->nextcheck = ch->expiry_time+1;
432 if (ch->expiry_time >= get_seconds()
433 && ch->last_refresh >= current_detail->flush_time
435 continue;
436 if (test_and_clear_bit(CACHE_PENDING, &ch->flags))
437 queue_loose(current_detail, ch);
439 if (atomic_read(&ch->ref.refcount) == 1)
440 break;
442 if (ch) {
443 *cp = ch->next;
444 ch->next = NULL;
445 current_detail->entries--;
446 rv = 1;
448 write_unlock(&current_detail->hash_lock);
449 d = current_detail;
450 if (!ch)
451 current_index ++;
452 spin_unlock(&cache_list_lock);
453 if (ch)
454 cache_put(ch, d);
455 } else
456 spin_unlock(&cache_list_lock);
458 return rv;
462 * We want to regularly clean the cache, so we need to schedule some work ...
464 static void do_cache_clean(void *data)
466 int delay = 5;
467 if (cache_clean() == -1)
468 delay = 30*HZ;
470 if (list_empty(&cache_list))
471 delay = 0;
473 if (delay)
474 schedule_delayed_work(&cache_cleaner, delay);
479 * Clean all caches promptly. This just calls cache_clean
480 * repeatedly until we are sure that every cache has had a chance to
481 * be fully cleaned
483 void cache_flush(void)
485 while (cache_clean() != -1)
486 cond_resched();
487 while (cache_clean() != -1)
488 cond_resched();
491 void cache_purge(struct cache_detail *detail)
493 detail->flush_time = LONG_MAX;
494 detail->nextcheck = get_seconds();
495 cache_flush();
496 detail->flush_time = 1;
502 * Deferral and Revisiting of Requests.
504 * If a cache lookup finds a pending entry, we
505 * need to defer the request and revisit it later.
506 * All deferred requests are stored in a hash table,
507 * indexed by "struct cache_head *".
508 * As it may be wasteful to store a whole request
509 * structure, we allow the request to provide a
510 * deferred form, which must contain a
511 * 'struct cache_deferred_req'
512 * This cache_deferred_req contains a method to allow
513 * it to be revisited when cache info is available
516 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
517 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
519 #define DFR_MAX 300 /* ??? */
521 static DEFINE_SPINLOCK(cache_defer_lock);
522 static LIST_HEAD(cache_defer_list);
523 static struct list_head cache_defer_hash[DFR_HASHSIZE];
524 static int cache_defer_cnt;
526 static void cache_defer_req(struct cache_req *req, struct cache_head *item)
528 struct cache_deferred_req *dreq;
529 int hash = DFR_HASH(item);
531 dreq = req->defer(req);
532 if (dreq == NULL)
533 return;
535 dreq->item = item;
536 dreq->recv_time = get_seconds();
538 spin_lock(&cache_defer_lock);
540 list_add(&dreq->recent, &cache_defer_list);
542 if (cache_defer_hash[hash].next == NULL)
543 INIT_LIST_HEAD(&cache_defer_hash[hash]);
544 list_add(&dreq->hash, &cache_defer_hash[hash]);
546 /* it is in, now maybe clean up */
547 dreq = NULL;
548 if (++cache_defer_cnt > DFR_MAX) {
549 /* too much in the cache, randomly drop
550 * first or last
552 if (net_random()&1)
553 dreq = list_entry(cache_defer_list.next,
554 struct cache_deferred_req,
555 recent);
556 else
557 dreq = list_entry(cache_defer_list.prev,
558 struct cache_deferred_req,
559 recent);
560 list_del(&dreq->recent);
561 list_del(&dreq->hash);
562 cache_defer_cnt--;
564 spin_unlock(&cache_defer_lock);
566 if (dreq) {
567 /* there was one too many */
568 dreq->revisit(dreq, 1);
570 if (!test_bit(CACHE_PENDING, &item->flags)) {
571 /* must have just been validated... */
572 cache_revisit_request(item);
576 static void cache_revisit_request(struct cache_head *item)
578 struct cache_deferred_req *dreq;
579 struct list_head pending;
581 struct list_head *lp;
582 int hash = DFR_HASH(item);
584 INIT_LIST_HEAD(&pending);
585 spin_lock(&cache_defer_lock);
587 lp = cache_defer_hash[hash].next;
588 if (lp) {
589 while (lp != &cache_defer_hash[hash]) {
590 dreq = list_entry(lp, struct cache_deferred_req, hash);
591 lp = lp->next;
592 if (dreq->item == item) {
593 list_del(&dreq->hash);
594 list_move(&dreq->recent, &pending);
595 cache_defer_cnt--;
599 spin_unlock(&cache_defer_lock);
601 while (!list_empty(&pending)) {
602 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
603 list_del_init(&dreq->recent);
604 dreq->revisit(dreq, 0);
608 void cache_clean_deferred(void *owner)
610 struct cache_deferred_req *dreq, *tmp;
611 struct list_head pending;
614 INIT_LIST_HEAD(&pending);
615 spin_lock(&cache_defer_lock);
617 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
618 if (dreq->owner == owner) {
619 list_del(&dreq->hash);
620 list_move(&dreq->recent, &pending);
621 cache_defer_cnt--;
624 spin_unlock(&cache_defer_lock);
626 while (!list_empty(&pending)) {
627 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
628 list_del_init(&dreq->recent);
629 dreq->revisit(dreq, 1);
634 * communicate with user-space
636 * We have a magic /proc file - /proc/sunrpc/cache
637 * On read, you get a full request, or block
638 * On write, an update request is processed
639 * Poll works if anything to read, and always allows write
641 * Implemented by linked list of requests. Each open file has
642 * a ->private that also exists in this list. New request are added
643 * to the end and may wakeup and preceding readers.
644 * New readers are added to the head. If, on read, an item is found with
645 * CACHE_UPCALLING clear, we free it from the list.
649 static DEFINE_SPINLOCK(queue_lock);
650 static DEFINE_MUTEX(queue_io_mutex);
652 struct cache_queue {
653 struct list_head list;
654 int reader; /* if 0, then request */
656 struct cache_request {
657 struct cache_queue q;
658 struct cache_head *item;
659 char * buf;
660 int len;
661 int readers;
663 struct cache_reader {
664 struct cache_queue q;
665 int offset; /* if non-0, we have a refcnt on next request */
668 static ssize_t
669 cache_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
671 struct cache_reader *rp = filp->private_data;
672 struct cache_request *rq;
673 struct cache_detail *cd = PDE(filp->f_dentry->d_inode)->data;
674 int err;
676 if (count == 0)
677 return 0;
679 mutex_lock(&queue_io_mutex); /* protect against multiple concurrent
680 * readers on this file */
681 again:
682 spin_lock(&queue_lock);
683 /* need to find next request */
684 while (rp->q.list.next != &cd->queue &&
685 list_entry(rp->q.list.next, struct cache_queue, list)
686 ->reader) {
687 struct list_head *next = rp->q.list.next;
688 list_move(&rp->q.list, next);
690 if (rp->q.list.next == &cd->queue) {
691 spin_unlock(&queue_lock);
692 mutex_unlock(&queue_io_mutex);
693 BUG_ON(rp->offset);
694 return 0;
696 rq = container_of(rp->q.list.next, struct cache_request, q.list);
697 BUG_ON(rq->q.reader);
698 if (rp->offset == 0)
699 rq->readers++;
700 spin_unlock(&queue_lock);
702 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
703 err = -EAGAIN;
704 spin_lock(&queue_lock);
705 list_move(&rp->q.list, &rq->q.list);
706 spin_unlock(&queue_lock);
707 } else {
708 if (rp->offset + count > rq->len)
709 count = rq->len - rp->offset;
710 err = -EFAULT;
711 if (copy_to_user(buf, rq->buf + rp->offset, count))
712 goto out;
713 rp->offset += count;
714 if (rp->offset >= rq->len) {
715 rp->offset = 0;
716 spin_lock(&queue_lock);
717 list_move(&rp->q.list, &rq->q.list);
718 spin_unlock(&queue_lock);
720 err = 0;
722 out:
723 if (rp->offset == 0) {
724 /* need to release rq */
725 spin_lock(&queue_lock);
726 rq->readers--;
727 if (rq->readers == 0 &&
728 !test_bit(CACHE_PENDING, &rq->item->flags)) {
729 list_del(&rq->q.list);
730 spin_unlock(&queue_lock);
731 cache_put(rq->item, cd);
732 kfree(rq->buf);
733 kfree(rq);
734 } else
735 spin_unlock(&queue_lock);
737 if (err == -EAGAIN)
738 goto again;
739 mutex_unlock(&queue_io_mutex);
740 return err ? err : count;
743 static char write_buf[8192]; /* protected by queue_io_mutex */
745 static ssize_t
746 cache_write(struct file *filp, const char __user *buf, size_t count,
747 loff_t *ppos)
749 int err;
750 struct cache_detail *cd = PDE(filp->f_dentry->d_inode)->data;
752 if (count == 0)
753 return 0;
754 if (count >= sizeof(write_buf))
755 return -EINVAL;
757 mutex_lock(&queue_io_mutex);
759 if (copy_from_user(write_buf, buf, count)) {
760 mutex_unlock(&queue_io_mutex);
761 return -EFAULT;
763 write_buf[count] = '\0';
764 if (cd->cache_parse)
765 err = cd->cache_parse(cd, write_buf, count);
766 else
767 err = -EINVAL;
769 mutex_unlock(&queue_io_mutex);
770 return err ? err : count;
773 static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
775 static unsigned int
776 cache_poll(struct file *filp, poll_table *wait)
778 unsigned int mask;
779 struct cache_reader *rp = filp->private_data;
780 struct cache_queue *cq;
781 struct cache_detail *cd = PDE(filp->f_dentry->d_inode)->data;
783 poll_wait(filp, &queue_wait, wait);
785 /* alway allow write */
786 mask = POLL_OUT | POLLWRNORM;
788 if (!rp)
789 return mask;
791 spin_lock(&queue_lock);
793 for (cq= &rp->q; &cq->list != &cd->queue;
794 cq = list_entry(cq->list.next, struct cache_queue, list))
795 if (!cq->reader) {
796 mask |= POLLIN | POLLRDNORM;
797 break;
799 spin_unlock(&queue_lock);
800 return mask;
803 static int
804 cache_ioctl(struct inode *ino, struct file *filp,
805 unsigned int cmd, unsigned long arg)
807 int len = 0;
808 struct cache_reader *rp = filp->private_data;
809 struct cache_queue *cq;
810 struct cache_detail *cd = PDE(ino)->data;
812 if (cmd != FIONREAD || !rp)
813 return -EINVAL;
815 spin_lock(&queue_lock);
817 /* only find the length remaining in current request,
818 * or the length of the next request
820 for (cq= &rp->q; &cq->list != &cd->queue;
821 cq = list_entry(cq->list.next, struct cache_queue, list))
822 if (!cq->reader) {
823 struct cache_request *cr =
824 container_of(cq, struct cache_request, q);
825 len = cr->len - rp->offset;
826 break;
828 spin_unlock(&queue_lock);
830 return put_user(len, (int __user *)arg);
833 static int
834 cache_open(struct inode *inode, struct file *filp)
836 struct cache_reader *rp = NULL;
838 nonseekable_open(inode, filp);
839 if (filp->f_mode & FMODE_READ) {
840 struct cache_detail *cd = PDE(inode)->data;
842 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
843 if (!rp)
844 return -ENOMEM;
845 rp->offset = 0;
846 rp->q.reader = 1;
847 atomic_inc(&cd->readers);
848 spin_lock(&queue_lock);
849 list_add(&rp->q.list, &cd->queue);
850 spin_unlock(&queue_lock);
852 filp->private_data = rp;
853 return 0;
856 static int
857 cache_release(struct inode *inode, struct file *filp)
859 struct cache_reader *rp = filp->private_data;
860 struct cache_detail *cd = PDE(inode)->data;
862 if (rp) {
863 spin_lock(&queue_lock);
864 if (rp->offset) {
865 struct cache_queue *cq;
866 for (cq= &rp->q; &cq->list != &cd->queue;
867 cq = list_entry(cq->list.next, struct cache_queue, list))
868 if (!cq->reader) {
869 container_of(cq, struct cache_request, q)
870 ->readers--;
871 break;
873 rp->offset = 0;
875 list_del(&rp->q.list);
876 spin_unlock(&queue_lock);
878 filp->private_data = NULL;
879 kfree(rp);
881 cd->last_close = get_seconds();
882 atomic_dec(&cd->readers);
884 return 0;
889 static struct file_operations cache_file_operations = {
890 .owner = THIS_MODULE,
891 .llseek = no_llseek,
892 .read = cache_read,
893 .write = cache_write,
894 .poll = cache_poll,
895 .ioctl = cache_ioctl, /* for FIONREAD */
896 .open = cache_open,
897 .release = cache_release,
901 static void queue_loose(struct cache_detail *detail, struct cache_head *ch)
903 struct cache_queue *cq;
904 spin_lock(&queue_lock);
905 list_for_each_entry(cq, &detail->queue, list)
906 if (!cq->reader) {
907 struct cache_request *cr = container_of(cq, struct cache_request, q);
908 if (cr->item != ch)
909 continue;
910 if (cr->readers != 0)
911 continue;
912 list_del(&cr->q.list);
913 spin_unlock(&queue_lock);
914 cache_put(cr->item, detail);
915 kfree(cr->buf);
916 kfree(cr);
917 return;
919 spin_unlock(&queue_lock);
923 * Support routines for text-based upcalls.
924 * Fields are separated by spaces.
925 * Fields are either mangled to quote space tab newline slosh with slosh
926 * or a hexified with a leading \x
927 * Record is terminated with newline.
931 void qword_add(char **bpp, int *lp, char *str)
933 char *bp = *bpp;
934 int len = *lp;
935 char c;
937 if (len < 0) return;
939 while ((c=*str++) && len)
940 switch(c) {
941 case ' ':
942 case '\t':
943 case '\n':
944 case '\\':
945 if (len >= 4) {
946 *bp++ = '\\';
947 *bp++ = '0' + ((c & 0300)>>6);
948 *bp++ = '0' + ((c & 0070)>>3);
949 *bp++ = '0' + ((c & 0007)>>0);
951 len -= 4;
952 break;
953 default:
954 *bp++ = c;
955 len--;
957 if (c || len <1) len = -1;
958 else {
959 *bp++ = ' ';
960 len--;
962 *bpp = bp;
963 *lp = len;
966 void qword_addhex(char **bpp, int *lp, char *buf, int blen)
968 char *bp = *bpp;
969 int len = *lp;
971 if (len < 0) return;
973 if (len > 2) {
974 *bp++ = '\\';
975 *bp++ = 'x';
976 len -= 2;
977 while (blen && len >= 2) {
978 unsigned char c = *buf++;
979 *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
980 *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
981 len -= 2;
982 blen--;
985 if (blen || len<1) len = -1;
986 else {
987 *bp++ = ' ';
988 len--;
990 *bpp = bp;
991 *lp = len;
994 static void warn_no_listener(struct cache_detail *detail)
996 if (detail->last_warn != detail->last_close) {
997 detail->last_warn = detail->last_close;
998 if (detail->warn_no_listener)
999 detail->warn_no_listener(detail);
1004 * register an upcall request to user-space.
1005 * Each request is at most one page long.
1007 static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h)
1010 char *buf;
1011 struct cache_request *crq;
1012 char *bp;
1013 int len;
1015 if (detail->cache_request == NULL)
1016 return -EINVAL;
1018 if (atomic_read(&detail->readers) == 0 &&
1019 detail->last_close < get_seconds() - 30) {
1020 warn_no_listener(detail);
1021 return -EINVAL;
1024 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1025 if (!buf)
1026 return -EAGAIN;
1028 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1029 if (!crq) {
1030 kfree(buf);
1031 return -EAGAIN;
1034 bp = buf; len = PAGE_SIZE;
1036 detail->cache_request(detail, h, &bp, &len);
1038 if (len < 0) {
1039 kfree(buf);
1040 kfree(crq);
1041 return -EAGAIN;
1043 crq->q.reader = 0;
1044 crq->item = cache_get(h);
1045 crq->buf = buf;
1046 crq->len = PAGE_SIZE - len;
1047 crq->readers = 0;
1048 spin_lock(&queue_lock);
1049 list_add_tail(&crq->q.list, &detail->queue);
1050 spin_unlock(&queue_lock);
1051 wake_up(&queue_wait);
1052 return 0;
1056 * parse a message from user-space and pass it
1057 * to an appropriate cache
1058 * Messages are, like requests, separated into fields by
1059 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1061 * Message is
1062 * reply cachename expiry key ... content....
1064 * key and content are both parsed by cache
1067 #define isodigit(c) (isdigit(c) && c <= '7')
1068 int qword_get(char **bpp, char *dest, int bufsize)
1070 /* return bytes copied, or -1 on error */
1071 char *bp = *bpp;
1072 int len = 0;
1074 while (*bp == ' ') bp++;
1076 if (bp[0] == '\\' && bp[1] == 'x') {
1077 /* HEX STRING */
1078 bp += 2;
1079 while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) {
1080 int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
1081 bp++;
1082 byte <<= 4;
1083 byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
1084 *dest++ = byte;
1085 bp++;
1086 len++;
1088 } else {
1089 /* text with \nnn octal quoting */
1090 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1091 if (*bp == '\\' &&
1092 isodigit(bp[1]) && (bp[1] <= '3') &&
1093 isodigit(bp[2]) &&
1094 isodigit(bp[3])) {
1095 int byte = (*++bp -'0');
1096 bp++;
1097 byte = (byte << 3) | (*bp++ - '0');
1098 byte = (byte << 3) | (*bp++ - '0');
1099 *dest++ = byte;
1100 len++;
1101 } else {
1102 *dest++ = *bp++;
1103 len++;
1108 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1109 return -1;
1110 while (*bp == ' ') bp++;
1111 *bpp = bp;
1112 *dest = '\0';
1113 return len;
1118 * support /proc/sunrpc/cache/$CACHENAME/content
1119 * as a seqfile.
1120 * We call ->cache_show passing NULL for the item to
1121 * get a header, then pass each real item in the cache
1124 struct handle {
1125 struct cache_detail *cd;
1128 static void *c_start(struct seq_file *m, loff_t *pos)
1130 loff_t n = *pos;
1131 unsigned hash, entry;
1132 struct cache_head *ch;
1133 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1136 read_lock(&cd->hash_lock);
1137 if (!n--)
1138 return SEQ_START_TOKEN;
1139 hash = n >> 32;
1140 entry = n & ((1LL<<32) - 1);
1142 for (ch=cd->hash_table[hash]; ch; ch=ch->next)
1143 if (!entry--)
1144 return ch;
1145 n &= ~((1LL<<32) - 1);
1146 do {
1147 hash++;
1148 n += 1LL<<32;
1149 } while(hash < cd->hash_size &&
1150 cd->hash_table[hash]==NULL);
1151 if (hash >= cd->hash_size)
1152 return NULL;
1153 *pos = n+1;
1154 return cd->hash_table[hash];
1157 static void *c_next(struct seq_file *m, void *p, loff_t *pos)
1159 struct cache_head *ch = p;
1160 int hash = (*pos >> 32);
1161 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1163 if (p == SEQ_START_TOKEN)
1164 hash = 0;
1165 else if (ch->next == NULL) {
1166 hash++;
1167 *pos += 1LL<<32;
1168 } else {
1169 ++*pos;
1170 return ch->next;
1172 *pos &= ~((1LL<<32) - 1);
1173 while (hash < cd->hash_size &&
1174 cd->hash_table[hash] == NULL) {
1175 hash++;
1176 *pos += 1LL<<32;
1178 if (hash >= cd->hash_size)
1179 return NULL;
1180 ++*pos;
1181 return cd->hash_table[hash];
1184 static void c_stop(struct seq_file *m, void *p)
1186 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1187 read_unlock(&cd->hash_lock);
1190 static int c_show(struct seq_file *m, void *p)
1192 struct cache_head *cp = p;
1193 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1195 if (p == SEQ_START_TOKEN)
1196 return cd->cache_show(m, cd, NULL);
1198 ifdebug(CACHE)
1199 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
1200 cp->expiry_time, atomic_read(&cp->ref.refcount), cp->flags);
1201 cache_get(cp);
1202 if (cache_check(cd, cp, NULL))
1203 /* cache_check does a cache_put on failure */
1204 seq_printf(m, "# ");
1205 else
1206 cache_put(cp, cd);
1208 return cd->cache_show(m, cd, cp);
1211 static struct seq_operations cache_content_op = {
1212 .start = c_start,
1213 .next = c_next,
1214 .stop = c_stop,
1215 .show = c_show,
1218 static int content_open(struct inode *inode, struct file *file)
1220 int res;
1221 struct handle *han;
1222 struct cache_detail *cd = PDE(inode)->data;
1224 han = kmalloc(sizeof(*han), GFP_KERNEL);
1225 if (han == NULL)
1226 return -ENOMEM;
1228 han->cd = cd;
1230 res = seq_open(file, &cache_content_op);
1231 if (res)
1232 kfree(han);
1233 else
1234 ((struct seq_file *)file->private_data)->private = han;
1236 return res;
1238 static int content_release(struct inode *inode, struct file *file)
1240 struct seq_file *m = (struct seq_file *)file->private_data;
1241 struct handle *han = m->private;
1242 kfree(han);
1243 m->private = NULL;
1244 return seq_release(inode, file);
1247 static struct file_operations content_file_operations = {
1248 .open = content_open,
1249 .read = seq_read,
1250 .llseek = seq_lseek,
1251 .release = content_release,
1254 static ssize_t read_flush(struct file *file, char __user *buf,
1255 size_t count, loff_t *ppos)
1257 struct cache_detail *cd = PDE(file->f_dentry->d_inode)->data;
1258 char tbuf[20];
1259 unsigned long p = *ppos;
1260 int len;
1262 sprintf(tbuf, "%lu\n", cd->flush_time);
1263 len = strlen(tbuf);
1264 if (p >= len)
1265 return 0;
1266 len -= p;
1267 if (len > count) len = count;
1268 if (copy_to_user(buf, (void*)(tbuf+p), len))
1269 len = -EFAULT;
1270 else
1271 *ppos += len;
1272 return len;
1275 static ssize_t write_flush(struct file * file, const char __user * buf,
1276 size_t count, loff_t *ppos)
1278 struct cache_detail *cd = PDE(file->f_dentry->d_inode)->data;
1279 char tbuf[20];
1280 char *ep;
1281 long flushtime;
1282 if (*ppos || count > sizeof(tbuf)-1)
1283 return -EINVAL;
1284 if (copy_from_user(tbuf, buf, count))
1285 return -EFAULT;
1286 tbuf[count] = 0;
1287 flushtime = simple_strtoul(tbuf, &ep, 0);
1288 if (*ep && *ep != '\n')
1289 return -EINVAL;
1291 cd->flush_time = flushtime;
1292 cd->nextcheck = get_seconds();
1293 cache_flush();
1295 *ppos += count;
1296 return count;
1299 static struct file_operations cache_flush_operations = {
1300 .open = nonseekable_open,
1301 .read = read_flush,
1302 .write = write_flush,