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>
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 <asm/ioctls.h>
30 #include <linux/sunrpc/types.h>
31 #include <linux/sunrpc/cache.h>
32 #include <linux/sunrpc/stats.h>
34 #define RPCDBG_FACILITY RPCDBG_CACHE
36 void cache_init(struct cache_head *h)
38 time_t now = get_seconds();
41 atomic_set(&h->refcnt, 0);
42 h->expiry_time = now + CACHE_NEW_EXPIRY;
43 h->last_refresh = now;
47 static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h);
49 * This is the generic cache management routine for all
50 * the authentication caches.
51 * It checks the currency of a cache item and will (later)
52 * initiate an upcall to fill it if needed.
55 * Returns 0 if the cache_head can be used, or cache_puts it and returns
56 * -EAGAIN if upcall is pending,
57 * -ENOENT if cache entry was negative
59 int cache_check(struct cache_detail *detail,
60 struct cache_head *h, struct cache_req *rqstp)
63 long refresh_age, age;
65 /* First decide return status as best we can */
66 if (!test_bit(CACHE_VALID, &h->flags) ||
67 h->expiry_time < get_seconds())
69 else if (detail->flush_time > h->last_refresh)
73 if (test_bit(CACHE_NEGATIVE, &h->flags))
78 /* now see if we want to start an upcall */
79 refresh_age = (h->expiry_time - h->last_refresh);
80 age = get_seconds() - h->last_refresh;
85 } else if (rv == -EAGAIN || age > refresh_age/2) {
86 dprintk("Want update, refage=%ld, age=%ld\n", refresh_age, age);
87 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
88 switch (cache_make_upcall(detail, h)) {
90 clear_bit(CACHE_PENDING, &h->flags);
92 set_bit(CACHE_NEGATIVE, &h->flags);
93 cache_fresh(detail, h, get_seconds()+CACHE_NEW_EXPIRY);
99 clear_bit(CACHE_PENDING, &h->flags);
100 cache_revisit_request(h);
107 cache_defer_req(rqstp, h);
110 detail->cache_put(h, detail);
114 static void queue_loose(struct cache_detail *detail, struct cache_head *ch);
116 void cache_fresh(struct cache_detail *detail,
117 struct cache_head *head, time_t expiry)
120 head->expiry_time = expiry;
121 head->last_refresh = get_seconds();
122 if (!test_and_set_bit(CACHE_VALID, &head->flags))
123 cache_revisit_request(head);
124 if (test_and_clear_bit(CACHE_PENDING, &head->flags))
125 queue_loose(detail, head);
129 * caches need to be periodically cleaned.
130 * For this we maintain a list of cache_detail and
131 * a current pointer into that list and into the table
134 * Each time clean_cache is called it finds the next non-empty entry
135 * in the current table and walks the list in that entry
136 * looking for entries that can be removed.
138 * An entry gets removed if:
139 * - The expiry is before current time
140 * - The last_refresh time is before the flush_time for that cache
142 * later we might drop old entries with non-NEVER expiry if that table
143 * is getting 'full' for some definition of 'full'
145 * The question of "how often to scan a table" is an interesting one
146 * and is answered in part by the use of the "nextcheck" field in the
148 * When a scan of a table begins, the nextcheck field is set to a time
149 * that is well into the future.
150 * While scanning, if an expiry time is found that is earlier than the
151 * current nextcheck time, nextcheck is set to that expiry time.
152 * If the flush_time is ever set to a time earlier than the nextcheck
153 * time, the nextcheck time is then set to that flush_time.
155 * A table is then only scanned if the current time is at least
156 * the nextcheck time.
160 static LIST_HEAD(cache_list);
161 static spinlock_t cache_list_lock = SPIN_LOCK_UNLOCKED;
162 static struct cache_detail *current_detail;
163 static int current_index;
165 static struct file_operations cache_file_operations;
166 static struct file_operations content_file_operations;
167 static struct file_operations cache_flush_operations;
169 static void do_cache_clean(void *data);
170 static DECLARE_WORK(cache_cleaner, do_cache_clean, NULL);
172 void cache_register(struct cache_detail *cd)
174 cd->proc_ent = proc_mkdir(cd->name, proc_net_rpc);
176 struct proc_dir_entry *p;
177 cd->proc_ent->owner = THIS_MODULE;
178 cd->channel_ent = cd->content_ent = NULL;
180 p = create_proc_entry("flush", S_IFREG|S_IRUSR|S_IWUSR,
184 p->proc_fops = &cache_flush_operations;
185 p->owner = THIS_MODULE;
189 if (cd->cache_request || cd->cache_parse) {
190 p = create_proc_entry("channel", S_IFREG|S_IRUSR|S_IWUSR,
194 p->proc_fops = &cache_file_operations;
195 p->owner = THIS_MODULE;
199 if (cd->cache_show) {
200 p = create_proc_entry("content", S_IFREG|S_IRUSR|S_IWUSR,
204 p->proc_fops = &content_file_operations;
205 p->owner = THIS_MODULE;
210 rwlock_init(&cd->hash_lock);
211 INIT_LIST_HEAD(&cd->queue);
212 spin_lock(&cache_list_lock);
215 atomic_set(&cd->readers, 0);
218 list_add(&cd->others, &cache_list);
219 spin_unlock(&cache_list_lock);
221 /* start the cleaning process */
222 schedule_work(&cache_cleaner);
225 int cache_unregister(struct cache_detail *cd)
228 spin_lock(&cache_list_lock);
229 write_lock(&cd->hash_lock);
230 if (cd->entries || atomic_read(&cd->inuse)) {
231 write_unlock(&cd->hash_lock);
232 spin_unlock(&cache_list_lock);
235 if (current_detail == cd)
236 current_detail = NULL;
237 list_del_init(&cd->others);
238 write_unlock(&cd->hash_lock);
239 spin_unlock(&cache_list_lock);
242 remove_proc_entry("flush", cd->proc_ent);
244 remove_proc_entry("channel", cd->proc_ent);
246 remove_proc_entry("content", cd->proc_ent);
249 remove_proc_entry(cd->name, proc_net_rpc);
251 if (list_empty(&cache_list)) {
252 /* module must be being unloaded so its safe to kill the worker */
253 cancel_delayed_work(&cache_cleaner);
254 flush_scheduled_work();
259 struct cache_detail *cache_find(char *name)
263 spin_lock(&cache_list_lock);
264 list_for_each(l, &cache_list) {
265 struct cache_detail *cd = list_entry(l, struct cache_detail, others);
267 if (strcmp(cd->name, name)==0) {
268 atomic_inc(&cd->inuse);
269 spin_unlock(&cache_list_lock);
273 spin_unlock(&cache_list_lock);
277 /* cache_drop must be called on any cache returned by
278 * cache_find, after it has been used
280 void cache_drop(struct cache_detail *detail)
282 atomic_dec(&detail->inuse);
285 /* clean cache tries to find something to clean
287 * It returns 1 if it cleaned something,
288 * 0 if it didn't find anything this time
289 * -1 if it fell off the end of the list.
291 int cache_clean(void)
294 struct list_head *next;
296 spin_lock(&cache_list_lock);
298 /* find a suitable table if we don't already have one */
299 while (current_detail == NULL ||
300 current_index >= current_detail->hash_size) {
302 next = current_detail->others.next;
304 next = cache_list.next;
305 if (next == &cache_list) {
306 current_detail = NULL;
307 spin_unlock(&cache_list_lock);
310 current_detail = list_entry(next, struct cache_detail, others);
311 if (current_detail->nextcheck > get_seconds())
312 current_index = current_detail->hash_size;
315 current_detail->nextcheck = get_seconds()+30*60;
319 /* find a non-empty bucket in the table */
320 while (current_detail &&
321 current_index < current_detail->hash_size &&
322 current_detail->hash_table[current_index] == NULL)
325 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
327 if (current_detail && current_index < current_detail->hash_size) {
328 struct cache_head *ch, **cp;
329 struct cache_detail *d;
331 write_lock(¤t_detail->hash_lock);
333 /* Ok, now to clean this strand */
335 cp = & current_detail->hash_table[current_index];
337 for (; ch; cp= & ch->next, ch= *cp) {
338 if (current_detail->nextcheck > ch->expiry_time)
339 current_detail->nextcheck = ch->expiry_time+1;
340 if (ch->expiry_time >= get_seconds()
341 && ch->last_refresh >= current_detail->flush_time
344 if (test_and_clear_bit(CACHE_PENDING, &ch->flags))
345 queue_loose(current_detail, ch);
347 if (!atomic_read(&ch->refcnt))
352 clear_bit(CACHE_HASHED, &ch->flags);
355 current_detail->entries--;
358 write_unlock(¤t_detail->hash_lock);
362 spin_unlock(&cache_list_lock);
366 spin_unlock(&cache_list_lock);
372 * We want to regularly clean the cache, so we need to schedule some work ...
374 static void do_cache_clean(void *data)
377 if (cache_clean() == -1)
380 if (list_empty(&cache_list))
384 schedule_delayed_work(&cache_cleaner, delay);
389 * Clean all caches promptly. This just calls cache_clean
390 * repeatedly until we are sure that every cache has had a chance to
393 void cache_flush(void)
395 while (cache_clean() != -1)
397 while (cache_clean() != -1)
401 void cache_purge(struct cache_detail *detail)
403 detail->flush_time = get_seconds()+1;
404 detail->nextcheck = get_seconds();
411 * Deferral and Revisiting of Requests.
413 * If a cache lookup finds a pending entry, we
414 * need to defer the request and revisit it later.
415 * All deferred requests are stored in a hash table,
416 * indexed by "struct cache_head *".
417 * As it may be wasteful to store a whole request
418 * structure, we allow the request to provide a
419 * deferred form, which must contain a
420 * 'struct cache_deferred_req'
421 * This cache_deferred_req contains a method to allow
422 * it to be revisited when cache info is available
425 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
426 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
428 #define DFR_MAX 300 /* ??? */
430 spinlock_t cache_defer_lock = SPIN_LOCK_UNLOCKED;
431 static LIST_HEAD(cache_defer_list);
432 static struct list_head cache_defer_hash[DFR_HASHSIZE];
433 static int cache_defer_cnt;
435 void cache_defer_req(struct cache_req *req, struct cache_head *item)
437 struct cache_deferred_req *dreq;
438 int hash = DFR_HASH(item);
440 dreq = req->defer(req);
445 dreq->recv_time = get_seconds();
447 spin_lock(&cache_defer_lock);
449 list_add(&dreq->recent, &cache_defer_list);
451 if (cache_defer_hash[hash].next == NULL)
452 INIT_LIST_HEAD(&cache_defer_hash[hash]);
453 list_add(&dreq->hash, &cache_defer_hash[hash]);
455 /* it is in, now maybe clean up */
457 if (++cache_defer_cnt > DFR_MAX) {
458 /* too much in the cache, randomly drop
462 dreq = list_entry(cache_defer_list.next,
463 struct cache_deferred_req,
466 dreq = list_entry(cache_defer_list.prev,
467 struct cache_deferred_req,
469 list_del(&dreq->recent);
470 list_del(&dreq->hash);
473 spin_unlock(&cache_defer_lock);
476 /* there was one too many */
477 dreq->revisit(dreq, 1);
479 if (test_bit(CACHE_VALID, &item->flags)) {
480 /* must have just been validated... */
481 cache_revisit_request(item);
485 void cache_revisit_request(struct cache_head *item)
487 struct cache_deferred_req *dreq;
488 struct list_head pending;
490 struct list_head *lp;
491 int hash = DFR_HASH(item);
493 INIT_LIST_HEAD(&pending);
494 spin_lock(&cache_defer_lock);
496 lp = cache_defer_hash[hash].next;
498 while (lp != &cache_defer_hash[hash]) {
499 dreq = list_entry(lp, struct cache_deferred_req, hash);
501 if (dreq->item == item) {
502 list_del(&dreq->hash);
503 list_move(&dreq->recent, &pending);
508 spin_unlock(&cache_defer_lock);
510 while (!list_empty(&pending)) {
511 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
512 list_del_init(&dreq->recent);
513 dreq->revisit(dreq, 0);
517 void cache_clean_deferred(void *owner)
519 struct cache_deferred_req *dreq, *tmp;
520 struct list_head pending;
523 INIT_LIST_HEAD(&pending);
524 spin_lock(&cache_defer_lock);
526 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
527 if (dreq->owner == owner) {
528 list_del(&dreq->hash);
529 list_move(&dreq->recent, &pending);
533 spin_unlock(&cache_defer_lock);
535 while (!list_empty(&pending)) {
536 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
537 list_del_init(&dreq->recent);
538 dreq->revisit(dreq, 1);
543 * communicate with user-space
545 * We have a magic /proc file - /proc/sunrpc/cache
546 * On read, you get a full request, or block
547 * On write, an update request is processed
548 * Poll works if anything to read, and always allows write
550 * Implemented by linked list of requests. Each open file has
551 * a ->private that also exists in this list. New request are added
552 * to the end and may wakeup and preceding readers.
553 * New readers are added to the head. If, on read, an item is found with
554 * CACHE_UPCALLING clear, we free it from the list.
558 static spinlock_t queue_lock = SPIN_LOCK_UNLOCKED;
559 static DECLARE_MUTEX(queue_io_sem);
562 struct list_head list;
563 int reader; /* if 0, then request */
565 struct cache_request {
566 struct cache_queue q;
567 struct cache_head *item;
572 struct cache_reader {
573 struct cache_queue q;
574 int offset; /* if non-0, we have a refcnt on next request */
578 cache_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
580 struct cache_reader *rp = filp->private_data;
581 struct cache_request *rq;
582 struct cache_detail *cd = PDE(filp->f_dentry->d_inode)->data;
585 if (ppos != &filp->f_pos)
591 down(&queue_io_sem); /* protect against multiple concurrent
592 * readers on this file */
594 spin_lock(&queue_lock);
595 /* need to find next request */
596 while (rp->q.list.next != &cd->queue &&
597 list_entry(rp->q.list.next, struct cache_queue, list)
599 struct list_head *next = rp->q.list.next;
600 list_move(&rp->q.list, next);
602 if (rp->q.list.next == &cd->queue) {
603 spin_unlock(&queue_lock);
609 rq = container_of(rp->q.list.next, struct cache_request, q.list);
610 if (rq->q.reader) BUG();
613 spin_unlock(&queue_lock);
615 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
617 spin_lock(&queue_lock);
618 list_move(&rp->q.list, &rq->q.list);
619 spin_unlock(&queue_lock);
621 if (rp->offset + count > rq->len)
622 count = rq->len - rp->offset;
624 if (copy_to_user(buf, rq->buf + rp->offset, count))
627 if (rp->offset >= rq->len) {
629 spin_lock(&queue_lock);
630 list_move(&rp->q.list, &rq->q.list);
631 spin_unlock(&queue_lock);
636 if (rp->offset == 0) {
637 /* need to release rq */
638 spin_lock(&queue_lock);
640 if (rq->readers == 0 &&
641 !test_bit(CACHE_PENDING, &rq->item->flags)) {
642 list_del(&rq->q.list);
643 spin_unlock(&queue_lock);
644 cd->cache_put(rq->item, cd);
648 spin_unlock(&queue_lock);
653 return err ? err : count;
656 static char write_buf[8192]; /* protected by queue_io_sem */
659 cache_write(struct file *filp, const char __user *buf, size_t count,
663 struct cache_detail *cd = PDE(filp->f_dentry->d_inode)->data;
665 if (ppos != &filp->f_pos)
670 if (count >= sizeof(write_buf))
675 if (copy_from_user(write_buf, buf, count)) {
679 write_buf[count] = '\0';
681 err = cd->cache_parse(cd, write_buf, count);
686 return err ? err : count;
689 static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
692 cache_poll(struct file *filp, poll_table *wait)
695 struct cache_reader *rp = filp->private_data;
696 struct cache_queue *cq;
697 struct cache_detail *cd = PDE(filp->f_dentry->d_inode)->data;
699 poll_wait(filp, &queue_wait, wait);
701 /* alway allow write */
702 mask = POLL_OUT | POLLWRNORM;
707 spin_lock(&queue_lock);
709 for (cq= &rp->q; &cq->list != &cd->queue;
710 cq = list_entry(cq->list.next, struct cache_queue, list))
712 mask |= POLLIN | POLLRDNORM;
715 spin_unlock(&queue_lock);
720 cache_ioctl(struct inode *ino, struct file *filp,
721 unsigned int cmd, unsigned long arg)
724 struct cache_reader *rp = filp->private_data;
725 struct cache_queue *cq;
726 struct cache_detail *cd = PDE(ino)->data;
728 if (cmd != FIONREAD || !rp)
731 spin_lock(&queue_lock);
733 /* only find the length remaining in current request,
734 * or the length of the next request
736 for (cq= &rp->q; &cq->list != &cd->queue;
737 cq = list_entry(cq->list.next, struct cache_queue, list))
739 struct cache_request *cr =
740 container_of(cq, struct cache_request, q);
741 len = cr->len - rp->offset;
744 spin_unlock(&queue_lock);
746 return put_user(len, (int __user *)arg);
750 cache_open(struct inode *inode, struct file *filp)
752 struct cache_reader *rp = NULL;
754 if (filp->f_mode & FMODE_READ) {
755 struct cache_detail *cd = PDE(inode)->data;
757 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
762 atomic_inc(&cd->readers);
763 spin_lock(&queue_lock);
764 list_add(&rp->q.list, &cd->queue);
765 spin_unlock(&queue_lock);
767 filp->private_data = rp;
772 cache_release(struct inode *inode, struct file *filp)
774 struct cache_reader *rp = filp->private_data;
775 struct cache_detail *cd = PDE(inode)->data;
778 spin_lock(&queue_lock);
780 struct cache_queue *cq;
781 for (cq= &rp->q; &cq->list != &cd->queue;
782 cq = list_entry(cq->list.next, struct cache_queue, list))
784 container_of(cq, struct cache_request, q)
790 list_del(&rp->q.list);
791 spin_unlock(&queue_lock);
793 filp->private_data = NULL;
796 cd->last_close = get_seconds();
797 atomic_dec(&cd->readers);
804 static struct file_operations cache_file_operations = {
805 .owner = THIS_MODULE,
808 .write = cache_write,
810 .ioctl = cache_ioctl, /* for FIONREAD */
812 .release = cache_release,
816 static void queue_loose(struct cache_detail *detail, struct cache_head *ch)
818 struct cache_queue *cq;
819 spin_lock(&queue_lock);
820 list_for_each_entry(cq, &detail->queue, list)
822 struct cache_request *cr = container_of(cq, struct cache_request, q);
825 if (cr->readers != 0)
827 list_del(&cr->q.list);
828 spin_unlock(&queue_lock);
829 detail->cache_put(cr->item, detail);
834 spin_unlock(&queue_lock);
838 * Support routines for text-based upcalls.
839 * Fields are separated by spaces.
840 * Fields are either mangled to quote space tab newline slosh with slosh
841 * or a hexified with a leading \x
842 * Record is terminated with newline.
846 void qword_add(char **bpp, int *lp, char *str)
854 while ((c=*str++) && len)
862 *bp++ = '0' + ((c & 0300)>>6);
863 *bp++ = '0' + ((c & 0070)>>3);
864 *bp++ = '0' + ((c & 0007)>>0);
872 if (c || len <1) len = -1;
881 void qword_addhex(char **bpp, int *lp, char *buf, int blen)
892 while (blen && len >= 2) {
893 unsigned char c = *buf++;
894 *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
895 *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
900 if (blen || len<1) len = -1;
909 void warn_no_listener(struct cache_detail *detail)
911 if (detail->last_warn != detail->last_close) {
912 detail->last_warn = detail->last_close;
913 if (detail->warn_no_listener)
914 detail->warn_no_listener(detail);
919 * register an upcall request to user-space.
920 * Each request is at most one page long.
922 static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h)
926 struct cache_request *crq;
930 if (detail->cache_request == NULL)
933 if (atomic_read(&detail->readers) == 0 &&
934 detail->last_close < get_seconds() - 30) {
935 warn_no_listener(detail);
939 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
943 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
949 bp = buf; len = PAGE_SIZE;
951 detail->cache_request(detail, h, &bp, &len);
959 crq->item = cache_get(h);
961 crq->len = PAGE_SIZE - len;
963 spin_lock(&queue_lock);
964 list_add_tail(&crq->q.list, &detail->queue);
965 spin_unlock(&queue_lock);
966 wake_up(&queue_wait);
971 * parse a message from user-space and pass it
972 * to an appropriate cache
973 * Messages are, like requests, separated into fields by
974 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
977 * reply cachename expiry key ... content....
979 * key and content are both parsed by cache
982 #define isodigit(c) (isdigit(c) && c <= '7')
983 int qword_get(char **bpp, char *dest, int bufsize)
985 /* return bytes copied, or -1 on error */
989 while (*bp == ' ') bp++;
991 if (bp[0] == '\\' && bp[1] == 'x') {
994 while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) {
995 int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
998 byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
1004 /* text with \nnn octal quoting */
1005 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1007 isodigit(bp[1]) && (bp[1] <= '3') &&
1010 int byte = (*++bp -'0');
1012 byte = (byte << 3) | (*bp++ - '0');
1013 byte = (byte << 3) | (*bp++ - '0');
1023 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1025 while (*bp == ' ') bp++;
1033 * support /proc/sunrpc/cache/$CACHENAME/content
1035 * We call ->cache_show passing NULL for the item to
1036 * get a header, then pass each real item in the cache
1040 struct cache_detail *cd;
1043 static void *c_start(struct seq_file *m, loff_t *pos)
1046 unsigned hash, entry;
1047 struct cache_head *ch;
1048 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1051 read_lock(&cd->hash_lock);
1053 return SEQ_START_TOKEN;
1055 entry = n & ((1LL<<32) - 1);
1057 for (ch=cd->hash_table[hash]; ch; ch=ch->next)
1060 n &= ~((1LL<<32) - 1);
1064 } while(hash < cd->hash_size &&
1065 cd->hash_table[hash]==NULL);
1066 if (hash >= cd->hash_size)
1069 return cd->hash_table[hash];
1072 static void *c_next(struct seq_file *m, void *p, loff_t *pos)
1074 struct cache_head *ch = p;
1075 int hash = (*pos >> 32);
1076 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1078 if (p == SEQ_START_TOKEN)
1080 else if (ch->next == NULL) {
1087 *pos &= ~((1LL<<32) - 1);
1088 while (hash < cd->hash_size &&
1089 cd->hash_table[hash] == NULL) {
1093 if (hash >= cd->hash_size)
1096 return cd->hash_table[hash];
1099 static void c_stop(struct seq_file *m, void *p)
1101 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1102 read_unlock(&cd->hash_lock);
1105 static int c_show(struct seq_file *m, void *p)
1107 struct cache_head *cp = p;
1108 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1110 if (p == SEQ_START_TOKEN)
1111 return cd->cache_show(m, cd, NULL);
1114 seq_printf(m, "# expiry=%ld refcnt=%d\n",
1115 cp->expiry_time, atomic_read(&cp->refcnt));
1117 if (cache_check(cd, cp, NULL))
1118 /* cache_check does a cache_put on failure */
1119 seq_printf(m, "# ");
1123 return cd->cache_show(m, cd, cp);
1126 struct seq_operations cache_content_op = {
1133 static int content_open(struct inode *inode, struct file *file)
1137 struct cache_detail *cd = PDE(inode)->data;
1139 han = kmalloc(sizeof(*han), GFP_KERNEL);
1145 res = seq_open(file, &cache_content_op);
1149 ((struct seq_file *)file->private_data)->private = han;
1153 static int content_release(struct inode *inode, struct file *file)
1155 struct seq_file *m = (struct seq_file *)file->private_data;
1156 struct handle *han = m->private;
1159 return seq_release(inode, file);
1162 static struct file_operations content_file_operations = {
1163 .open = content_open,
1165 .llseek = seq_lseek,
1166 .release = content_release,
1169 static ssize_t read_flush(struct file *file, char __user *buf,
1170 size_t count, loff_t *ppos)
1172 struct cache_detail *cd = PDE(file->f_dentry->d_inode)->data;
1174 unsigned long p = *ppos;
1177 sprintf(tbuf, "%lu\n", cd->flush_time);
1182 if (len > count) len = count;
1183 if (copy_to_user(buf, (void*)(tbuf+p), len))
1190 static ssize_t write_flush(struct file * file, const char __user * buf,
1191 size_t count, loff_t *ppos)
1193 struct cache_detail *cd = PDE(file->f_dentry->d_inode)->data;
1197 if (*ppos || count > sizeof(tbuf)-1)
1199 if (copy_from_user(tbuf, buf, count))
1202 flushtime = simple_strtoul(tbuf, &ep, 0);
1203 if (*ep && *ep != '\n')
1206 cd->flush_time = flushtime;
1207 cd->nextcheck = get_seconds();
1214 static struct file_operations cache_flush_operations = {
1216 .write = write_flush,