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 static void cache_defer_req(struct cache_req *req, struct cache_head *item);
37 static void cache_revisit_request(struct cache_head *item);
39 void cache_init(struct cache_head *h)
41 time_t now = get_seconds();
44 atomic_set(&h->refcnt, 1);
45 h->expiry_time = now + CACHE_NEW_EXPIRY;
46 h->last_refresh = now;
50 static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h);
52 * This is the generic cache management routine for all
53 * the authentication caches.
54 * It checks the currency of a cache item and will (later)
55 * initiate an upcall to fill it if needed.
58 * Returns 0 if the cache_head can be used, or cache_puts it and returns
59 * -EAGAIN if upcall is pending,
60 * -ENOENT if cache entry was negative
62 int cache_check(struct cache_detail *detail,
63 struct cache_head *h, struct cache_req *rqstp)
66 long refresh_age, age;
68 /* First decide return status as best we can */
69 if (!test_bit(CACHE_VALID, &h->flags) ||
70 h->expiry_time < get_seconds())
72 else if (detail->flush_time > h->last_refresh)
76 if (test_bit(CACHE_NEGATIVE, &h->flags))
81 /* now see if we want to start an upcall */
82 refresh_age = (h->expiry_time - h->last_refresh);
83 age = get_seconds() - h->last_refresh;
88 } else if (rv == -EAGAIN || age > refresh_age/2) {
89 dprintk("Want update, refage=%ld, age=%ld\n", refresh_age, age);
90 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
91 switch (cache_make_upcall(detail, h)) {
93 clear_bit(CACHE_PENDING, &h->flags);
95 set_bit(CACHE_NEGATIVE, &h->flags);
96 cache_fresh(detail, h, get_seconds()+CACHE_NEW_EXPIRY);
102 clear_bit(CACHE_PENDING, &h->flags);
103 cache_revisit_request(h);
110 cache_defer_req(rqstp, h);
113 detail->cache_put(h, detail);
117 static void queue_loose(struct cache_detail *detail, struct cache_head *ch);
119 void cache_fresh(struct cache_detail *detail,
120 struct cache_head *head, time_t expiry)
123 head->expiry_time = expiry;
124 head->last_refresh = get_seconds();
125 if (!test_and_set_bit(CACHE_VALID, &head->flags))
126 cache_revisit_request(head);
127 if (test_and_clear_bit(CACHE_PENDING, &head->flags))
128 queue_loose(detail, head);
132 * caches need to be periodically cleaned.
133 * For this we maintain a list of cache_detail and
134 * a current pointer into that list and into the table
137 * Each time clean_cache is called it finds the next non-empty entry
138 * in the current table and walks the list in that entry
139 * looking for entries that can be removed.
141 * An entry gets removed if:
142 * - The expiry is before current time
143 * - The last_refresh time is before the flush_time for that cache
145 * later we might drop old entries with non-NEVER expiry if that table
146 * is getting 'full' for some definition of 'full'
148 * The question of "how often to scan a table" is an interesting one
149 * and is answered in part by the use of the "nextcheck" field in the
151 * When a scan of a table begins, the nextcheck field is set to a time
152 * that is well into the future.
153 * While scanning, if an expiry time is found that is earlier than the
154 * current nextcheck time, nextcheck is set to that expiry time.
155 * If the flush_time is ever set to a time earlier than the nextcheck
156 * time, the nextcheck time is then set to that flush_time.
158 * A table is then only scanned if the current time is at least
159 * the nextcheck time.
163 static LIST_HEAD(cache_list);
164 static DEFINE_SPINLOCK(cache_list_lock);
165 static struct cache_detail *current_detail;
166 static int current_index;
168 static struct file_operations cache_file_operations;
169 static struct file_operations content_file_operations;
170 static struct file_operations cache_flush_operations;
172 static void do_cache_clean(void *data);
173 static DECLARE_WORK(cache_cleaner, do_cache_clean, NULL);
175 void cache_register(struct cache_detail *cd)
177 cd->proc_ent = proc_mkdir(cd->name, proc_net_rpc);
179 struct proc_dir_entry *p;
180 cd->proc_ent->owner = THIS_MODULE;
181 cd->channel_ent = cd->content_ent = NULL;
183 p = create_proc_entry("flush", S_IFREG|S_IRUSR|S_IWUSR,
187 p->proc_fops = &cache_flush_operations;
188 p->owner = THIS_MODULE;
192 if (cd->cache_request || cd->cache_parse) {
193 p = create_proc_entry("channel", S_IFREG|S_IRUSR|S_IWUSR,
197 p->proc_fops = &cache_file_operations;
198 p->owner = THIS_MODULE;
202 if (cd->cache_show) {
203 p = create_proc_entry("content", S_IFREG|S_IRUSR|S_IWUSR,
207 p->proc_fops = &content_file_operations;
208 p->owner = THIS_MODULE;
213 rwlock_init(&cd->hash_lock);
214 INIT_LIST_HEAD(&cd->queue);
215 spin_lock(&cache_list_lock);
218 atomic_set(&cd->readers, 0);
221 list_add(&cd->others, &cache_list);
222 spin_unlock(&cache_list_lock);
224 /* start the cleaning process */
225 schedule_work(&cache_cleaner);
228 int cache_unregister(struct cache_detail *cd)
231 spin_lock(&cache_list_lock);
232 write_lock(&cd->hash_lock);
233 if (cd->entries || atomic_read(&cd->inuse)) {
234 write_unlock(&cd->hash_lock);
235 spin_unlock(&cache_list_lock);
238 if (current_detail == cd)
239 current_detail = NULL;
240 list_del_init(&cd->others);
241 write_unlock(&cd->hash_lock);
242 spin_unlock(&cache_list_lock);
245 remove_proc_entry("flush", cd->proc_ent);
247 remove_proc_entry("channel", cd->proc_ent);
249 remove_proc_entry("content", cd->proc_ent);
252 remove_proc_entry(cd->name, proc_net_rpc);
254 if (list_empty(&cache_list)) {
255 /* module must be being unloaded so its safe to kill the worker */
256 cancel_delayed_work(&cache_cleaner);
257 flush_scheduled_work();
262 /* clean cache tries to find something to clean
264 * It returns 1 if it cleaned something,
265 * 0 if it didn't find anything this time
266 * -1 if it fell off the end of the list.
268 static int cache_clean(void)
271 struct list_head *next;
273 spin_lock(&cache_list_lock);
275 /* find a suitable table if we don't already have one */
276 while (current_detail == NULL ||
277 current_index >= current_detail->hash_size) {
279 next = current_detail->others.next;
281 next = cache_list.next;
282 if (next == &cache_list) {
283 current_detail = NULL;
284 spin_unlock(&cache_list_lock);
287 current_detail = list_entry(next, struct cache_detail, others);
288 if (current_detail->nextcheck > get_seconds())
289 current_index = current_detail->hash_size;
292 current_detail->nextcheck = get_seconds()+30*60;
296 /* find a non-empty bucket in the table */
297 while (current_detail &&
298 current_index < current_detail->hash_size &&
299 current_detail->hash_table[current_index] == NULL)
302 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
304 if (current_detail && current_index < current_detail->hash_size) {
305 struct cache_head *ch, **cp;
306 struct cache_detail *d;
308 write_lock(¤t_detail->hash_lock);
310 /* Ok, now to clean this strand */
312 cp = & current_detail->hash_table[current_index];
314 for (; ch; cp= & ch->next, ch= *cp) {
315 if (current_detail->nextcheck > ch->expiry_time)
316 current_detail->nextcheck = ch->expiry_time+1;
317 if (ch->expiry_time >= get_seconds()
318 && ch->last_refresh >= current_detail->flush_time
321 if (test_and_clear_bit(CACHE_PENDING, &ch->flags))
322 queue_loose(current_detail, ch);
324 if (!atomic_read(&ch->refcnt))
329 clear_bit(CACHE_HASHED, &ch->flags);
332 current_detail->entries--;
335 write_unlock(¤t_detail->hash_lock);
339 spin_unlock(&cache_list_lock);
343 spin_unlock(&cache_list_lock);
349 * We want to regularly clean the cache, so we need to schedule some work ...
351 static void do_cache_clean(void *data)
354 if (cache_clean() == -1)
357 if (list_empty(&cache_list))
361 schedule_delayed_work(&cache_cleaner, delay);
366 * Clean all caches promptly. This just calls cache_clean
367 * repeatedly until we are sure that every cache has had a chance to
370 void cache_flush(void)
372 while (cache_clean() != -1)
374 while (cache_clean() != -1)
378 void cache_purge(struct cache_detail *detail)
380 detail->flush_time = LONG_MAX;
381 detail->nextcheck = get_seconds();
383 detail->flush_time = 1;
389 * Deferral and Revisiting of Requests.
391 * If a cache lookup finds a pending entry, we
392 * need to defer the request and revisit it later.
393 * All deferred requests are stored in a hash table,
394 * indexed by "struct cache_head *".
395 * As it may be wasteful to store a whole request
396 * structure, we allow the request to provide a
397 * deferred form, which must contain a
398 * 'struct cache_deferred_req'
399 * This cache_deferred_req contains a method to allow
400 * it to be revisited when cache info is available
403 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
404 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
406 #define DFR_MAX 300 /* ??? */
408 static DEFINE_SPINLOCK(cache_defer_lock);
409 static LIST_HEAD(cache_defer_list);
410 static struct list_head cache_defer_hash[DFR_HASHSIZE];
411 static int cache_defer_cnt;
413 static void cache_defer_req(struct cache_req *req, struct cache_head *item)
415 struct cache_deferred_req *dreq;
416 int hash = DFR_HASH(item);
418 dreq = req->defer(req);
423 dreq->recv_time = get_seconds();
425 spin_lock(&cache_defer_lock);
427 list_add(&dreq->recent, &cache_defer_list);
429 if (cache_defer_hash[hash].next == NULL)
430 INIT_LIST_HEAD(&cache_defer_hash[hash]);
431 list_add(&dreq->hash, &cache_defer_hash[hash]);
433 /* it is in, now maybe clean up */
435 if (++cache_defer_cnt > DFR_MAX) {
436 /* too much in the cache, randomly drop
440 dreq = list_entry(cache_defer_list.next,
441 struct cache_deferred_req,
444 dreq = list_entry(cache_defer_list.prev,
445 struct cache_deferred_req,
447 list_del(&dreq->recent);
448 list_del(&dreq->hash);
451 spin_unlock(&cache_defer_lock);
454 /* there was one too many */
455 dreq->revisit(dreq, 1);
457 if (test_bit(CACHE_VALID, &item->flags)) {
458 /* must have just been validated... */
459 cache_revisit_request(item);
463 static void cache_revisit_request(struct cache_head *item)
465 struct cache_deferred_req *dreq;
466 struct list_head pending;
468 struct list_head *lp;
469 int hash = DFR_HASH(item);
471 INIT_LIST_HEAD(&pending);
472 spin_lock(&cache_defer_lock);
474 lp = cache_defer_hash[hash].next;
476 while (lp != &cache_defer_hash[hash]) {
477 dreq = list_entry(lp, struct cache_deferred_req, hash);
479 if (dreq->item == item) {
480 list_del(&dreq->hash);
481 list_move(&dreq->recent, &pending);
486 spin_unlock(&cache_defer_lock);
488 while (!list_empty(&pending)) {
489 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
490 list_del_init(&dreq->recent);
491 dreq->revisit(dreq, 0);
495 void cache_clean_deferred(void *owner)
497 struct cache_deferred_req *dreq, *tmp;
498 struct list_head pending;
501 INIT_LIST_HEAD(&pending);
502 spin_lock(&cache_defer_lock);
504 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
505 if (dreq->owner == owner) {
506 list_del(&dreq->hash);
507 list_move(&dreq->recent, &pending);
511 spin_unlock(&cache_defer_lock);
513 while (!list_empty(&pending)) {
514 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
515 list_del_init(&dreq->recent);
516 dreq->revisit(dreq, 1);
521 * communicate with user-space
523 * We have a magic /proc file - /proc/sunrpc/cache
524 * On read, you get a full request, or block
525 * On write, an update request is processed
526 * Poll works if anything to read, and always allows write
528 * Implemented by linked list of requests. Each open file has
529 * a ->private that also exists in this list. New request are added
530 * to the end and may wakeup and preceding readers.
531 * New readers are added to the head. If, on read, an item is found with
532 * CACHE_UPCALLING clear, we free it from the list.
536 static DEFINE_SPINLOCK(queue_lock);
537 static DECLARE_MUTEX(queue_io_sem);
540 struct list_head list;
541 int reader; /* if 0, then request */
543 struct cache_request {
544 struct cache_queue q;
545 struct cache_head *item;
550 struct cache_reader {
551 struct cache_queue q;
552 int offset; /* if non-0, we have a refcnt on next request */
556 cache_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
558 struct cache_reader *rp = filp->private_data;
559 struct cache_request *rq;
560 struct cache_detail *cd = PDE(filp->f_dentry->d_inode)->data;
566 down(&queue_io_sem); /* protect against multiple concurrent
567 * readers on this file */
569 spin_lock(&queue_lock);
570 /* need to find next request */
571 while (rp->q.list.next != &cd->queue &&
572 list_entry(rp->q.list.next, struct cache_queue, list)
574 struct list_head *next = rp->q.list.next;
575 list_move(&rp->q.list, next);
577 if (rp->q.list.next == &cd->queue) {
578 spin_unlock(&queue_lock);
584 rq = container_of(rp->q.list.next, struct cache_request, q.list);
585 if (rq->q.reader) BUG();
588 spin_unlock(&queue_lock);
590 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
592 spin_lock(&queue_lock);
593 list_move(&rp->q.list, &rq->q.list);
594 spin_unlock(&queue_lock);
596 if (rp->offset + count > rq->len)
597 count = rq->len - rp->offset;
599 if (copy_to_user(buf, rq->buf + rp->offset, count))
602 if (rp->offset >= rq->len) {
604 spin_lock(&queue_lock);
605 list_move(&rp->q.list, &rq->q.list);
606 spin_unlock(&queue_lock);
611 if (rp->offset == 0) {
612 /* need to release rq */
613 spin_lock(&queue_lock);
615 if (rq->readers == 0 &&
616 !test_bit(CACHE_PENDING, &rq->item->flags)) {
617 list_del(&rq->q.list);
618 spin_unlock(&queue_lock);
619 cd->cache_put(rq->item, cd);
623 spin_unlock(&queue_lock);
628 return err ? err : count;
631 static char write_buf[8192]; /* protected by queue_io_sem */
634 cache_write(struct file *filp, const char __user *buf, size_t count,
638 struct cache_detail *cd = PDE(filp->f_dentry->d_inode)->data;
642 if (count >= sizeof(write_buf))
647 if (copy_from_user(write_buf, buf, count)) {
651 write_buf[count] = '\0';
653 err = cd->cache_parse(cd, write_buf, count);
658 return err ? err : count;
661 static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
664 cache_poll(struct file *filp, poll_table *wait)
667 struct cache_reader *rp = filp->private_data;
668 struct cache_queue *cq;
669 struct cache_detail *cd = PDE(filp->f_dentry->d_inode)->data;
671 poll_wait(filp, &queue_wait, wait);
673 /* alway allow write */
674 mask = POLL_OUT | POLLWRNORM;
679 spin_lock(&queue_lock);
681 for (cq= &rp->q; &cq->list != &cd->queue;
682 cq = list_entry(cq->list.next, struct cache_queue, list))
684 mask |= POLLIN | POLLRDNORM;
687 spin_unlock(&queue_lock);
692 cache_ioctl(struct inode *ino, struct file *filp,
693 unsigned int cmd, unsigned long arg)
696 struct cache_reader *rp = filp->private_data;
697 struct cache_queue *cq;
698 struct cache_detail *cd = PDE(ino)->data;
700 if (cmd != FIONREAD || !rp)
703 spin_lock(&queue_lock);
705 /* only find the length remaining in current request,
706 * or the length of the next request
708 for (cq= &rp->q; &cq->list != &cd->queue;
709 cq = list_entry(cq->list.next, struct cache_queue, list))
711 struct cache_request *cr =
712 container_of(cq, struct cache_request, q);
713 len = cr->len - rp->offset;
716 spin_unlock(&queue_lock);
718 return put_user(len, (int __user *)arg);
722 cache_open(struct inode *inode, struct file *filp)
724 struct cache_reader *rp = NULL;
726 nonseekable_open(inode, filp);
727 if (filp->f_mode & FMODE_READ) {
728 struct cache_detail *cd = PDE(inode)->data;
730 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
735 atomic_inc(&cd->readers);
736 spin_lock(&queue_lock);
737 list_add(&rp->q.list, &cd->queue);
738 spin_unlock(&queue_lock);
740 filp->private_data = rp;
745 cache_release(struct inode *inode, struct file *filp)
747 struct cache_reader *rp = filp->private_data;
748 struct cache_detail *cd = PDE(inode)->data;
751 spin_lock(&queue_lock);
753 struct cache_queue *cq;
754 for (cq= &rp->q; &cq->list != &cd->queue;
755 cq = list_entry(cq->list.next, struct cache_queue, list))
757 container_of(cq, struct cache_request, q)
763 list_del(&rp->q.list);
764 spin_unlock(&queue_lock);
766 filp->private_data = NULL;
769 cd->last_close = get_seconds();
770 atomic_dec(&cd->readers);
777 static struct file_operations cache_file_operations = {
778 .owner = THIS_MODULE,
781 .write = cache_write,
783 .ioctl = cache_ioctl, /* for FIONREAD */
785 .release = cache_release,
789 static void queue_loose(struct cache_detail *detail, struct cache_head *ch)
791 struct cache_queue *cq;
792 spin_lock(&queue_lock);
793 list_for_each_entry(cq, &detail->queue, list)
795 struct cache_request *cr = container_of(cq, struct cache_request, q);
798 if (cr->readers != 0)
800 list_del(&cr->q.list);
801 spin_unlock(&queue_lock);
802 detail->cache_put(cr->item, detail);
807 spin_unlock(&queue_lock);
811 * Support routines for text-based upcalls.
812 * Fields are separated by spaces.
813 * Fields are either mangled to quote space tab newline slosh with slosh
814 * or a hexified with a leading \x
815 * Record is terminated with newline.
819 void qword_add(char **bpp, int *lp, char *str)
827 while ((c=*str++) && len)
835 *bp++ = '0' + ((c & 0300)>>6);
836 *bp++ = '0' + ((c & 0070)>>3);
837 *bp++ = '0' + ((c & 0007)>>0);
845 if (c || len <1) len = -1;
854 void qword_addhex(char **bpp, int *lp, char *buf, int blen)
865 while (blen && len >= 2) {
866 unsigned char c = *buf++;
867 *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
868 *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
873 if (blen || len<1) len = -1;
882 static void warn_no_listener(struct cache_detail *detail)
884 if (detail->last_warn != detail->last_close) {
885 detail->last_warn = detail->last_close;
886 if (detail->warn_no_listener)
887 detail->warn_no_listener(detail);
892 * register an upcall request to user-space.
893 * Each request is at most one page long.
895 static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h)
899 struct cache_request *crq;
903 if (detail->cache_request == NULL)
906 if (atomic_read(&detail->readers) == 0 &&
907 detail->last_close < get_seconds() - 30) {
908 warn_no_listener(detail);
912 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
916 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
922 bp = buf; len = PAGE_SIZE;
924 detail->cache_request(detail, h, &bp, &len);
932 crq->item = cache_get(h);
934 crq->len = PAGE_SIZE - len;
936 spin_lock(&queue_lock);
937 list_add_tail(&crq->q.list, &detail->queue);
938 spin_unlock(&queue_lock);
939 wake_up(&queue_wait);
944 * parse a message from user-space and pass it
945 * to an appropriate cache
946 * Messages are, like requests, separated into fields by
947 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
950 * reply cachename expiry key ... content....
952 * key and content are both parsed by cache
955 #define isodigit(c) (isdigit(c) && c <= '7')
956 int qword_get(char **bpp, char *dest, int bufsize)
958 /* return bytes copied, or -1 on error */
962 while (*bp == ' ') bp++;
964 if (bp[0] == '\\' && bp[1] == 'x') {
967 while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) {
968 int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
971 byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
977 /* text with \nnn octal quoting */
978 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
980 isodigit(bp[1]) && (bp[1] <= '3') &&
983 int byte = (*++bp -'0');
985 byte = (byte << 3) | (*bp++ - '0');
986 byte = (byte << 3) | (*bp++ - '0');
996 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
998 while (*bp == ' ') bp++;
1006 * support /proc/sunrpc/cache/$CACHENAME/content
1008 * We call ->cache_show passing NULL for the item to
1009 * get a header, then pass each real item in the cache
1013 struct cache_detail *cd;
1016 static void *c_start(struct seq_file *m, loff_t *pos)
1019 unsigned hash, entry;
1020 struct cache_head *ch;
1021 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1024 read_lock(&cd->hash_lock);
1026 return SEQ_START_TOKEN;
1028 entry = n & ((1LL<<32) - 1);
1030 for (ch=cd->hash_table[hash]; ch; ch=ch->next)
1033 n &= ~((1LL<<32) - 1);
1037 } while(hash < cd->hash_size &&
1038 cd->hash_table[hash]==NULL);
1039 if (hash >= cd->hash_size)
1042 return cd->hash_table[hash];
1045 static void *c_next(struct seq_file *m, void *p, loff_t *pos)
1047 struct cache_head *ch = p;
1048 int hash = (*pos >> 32);
1049 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1051 if (p == SEQ_START_TOKEN)
1053 else if (ch->next == NULL) {
1060 *pos &= ~((1LL<<32) - 1);
1061 while (hash < cd->hash_size &&
1062 cd->hash_table[hash] == NULL) {
1066 if (hash >= cd->hash_size)
1069 return cd->hash_table[hash];
1072 static void c_stop(struct seq_file *m, void *p)
1074 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1075 read_unlock(&cd->hash_lock);
1078 static int c_show(struct seq_file *m, void *p)
1080 struct cache_head *cp = p;
1081 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1083 if (p == SEQ_START_TOKEN)
1084 return cd->cache_show(m, cd, NULL);
1087 seq_printf(m, "# expiry=%ld refcnt=%d\n",
1088 cp->expiry_time, atomic_read(&cp->refcnt));
1090 if (cache_check(cd, cp, NULL))
1091 /* cache_check does a cache_put on failure */
1092 seq_printf(m, "# ");
1096 return cd->cache_show(m, cd, cp);
1099 static struct seq_operations cache_content_op = {
1106 static int content_open(struct inode *inode, struct file *file)
1110 struct cache_detail *cd = PDE(inode)->data;
1112 han = kmalloc(sizeof(*han), GFP_KERNEL);
1118 res = seq_open(file, &cache_content_op);
1122 ((struct seq_file *)file->private_data)->private = han;
1126 static int content_release(struct inode *inode, struct file *file)
1128 struct seq_file *m = (struct seq_file *)file->private_data;
1129 struct handle *han = m->private;
1132 return seq_release(inode, file);
1135 static struct file_operations content_file_operations = {
1136 .open = content_open,
1138 .llseek = seq_lseek,
1139 .release = content_release,
1142 static ssize_t read_flush(struct file *file, char __user *buf,
1143 size_t count, loff_t *ppos)
1145 struct cache_detail *cd = PDE(file->f_dentry->d_inode)->data;
1147 unsigned long p = *ppos;
1150 sprintf(tbuf, "%lu\n", cd->flush_time);
1155 if (len > count) len = count;
1156 if (copy_to_user(buf, (void*)(tbuf+p), len))
1163 static ssize_t write_flush(struct file * file, const char __user * buf,
1164 size_t count, loff_t *ppos)
1166 struct cache_detail *cd = PDE(file->f_dentry->d_inode)->data;
1170 if (*ppos || count > sizeof(tbuf)-1)
1172 if (copy_from_user(tbuf, buf, count))
1175 flushtime = simple_strtoul(tbuf, &ep, 0);
1176 if (*ep && *ep != '\n')
1179 cd->flush_time = flushtime;
1180 cd->nextcheck = get_seconds();
1187 static struct file_operations cache_flush_operations = {
1188 .open = nonseekable_open,
1190 .write = write_flush,