1
2
3
4
5
6
7
8
9
10
11
12
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 <asm/ioctls.h>
30#include <linux/sunrpc/types.h>
31#include <linux/sunrpc/cache.h>
32#include <linux/sunrpc/stats.h>
33
34#define RPCDBG_FACILITY RPCDBG_CACHE
35
36static void cache_defer_req(struct cache_req *req, struct cache_head *item);
37static void cache_revisit_request(struct cache_head *item);
38
39void cache_init(struct cache_head *h)
40{
41 time_t now = get_seconds();
42 h->next = NULL;
43 h->flags = 0;
44 atomic_set(&h->refcnt, 1);
45 h->expiry_time = now + CACHE_NEW_EXPIRY;
46 h->last_refresh = now;
47}
48
49
50static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h);
51
52
53
54
55
56
57
58
59
60
61
62int cache_check(struct cache_detail *detail,
63 struct cache_head *h, struct cache_req *rqstp)
64{
65 int rv;
66 long refresh_age, age;
67
68
69 if (!test_bit(CACHE_VALID, &h->flags) ||
70 h->expiry_time < get_seconds())
71 rv = -EAGAIN;
72 else if (detail->flush_time > h->last_refresh)
73 rv = -EAGAIN;
74 else {
75
76 if (test_bit(CACHE_NEGATIVE, &h->flags))
77 rv = -ENOENT;
78 else rv = 0;
79 }
80
81
82 refresh_age = (h->expiry_time - h->last_refresh);
83 age = get_seconds() - h->last_refresh;
84
85 if (rqstp == NULL) {
86 if (rv == -EAGAIN)
87 rv = -ENOENT;
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)) {
92 case -EINVAL:
93 clear_bit(CACHE_PENDING, &h->flags);
94 if (rv == -EAGAIN) {
95 set_bit(CACHE_NEGATIVE, &h->flags);
96 cache_fresh(detail, h, get_seconds()+CACHE_NEW_EXPIRY);
97 rv = -ENOENT;
98 }
99 break;
100
101 case -EAGAIN:
102 clear_bit(CACHE_PENDING, &h->flags);
103 cache_revisit_request(h);
104 break;
105 }
106 }
107 }
108
109 if (rv == -EAGAIN)
110 cache_defer_req(rqstp, h);
111
112 if (rv && h)
113 detail->cache_put(h, detail);
114 return rv;
115}
116
117static void queue_loose(struct cache_detail *detail, struct cache_head *ch);
118
119void cache_fresh(struct cache_detail *detail,
120 struct cache_head *head, time_t expiry)
121{
122
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);
129}
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163static LIST_HEAD(cache_list);
164static DEFINE_SPINLOCK(cache_list_lock);
165static struct cache_detail *current_detail;
166static int current_index;
167
168static struct file_operations cache_file_operations;
169static struct file_operations content_file_operations;
170static struct file_operations cache_flush_operations;
171
172static void do_cache_clean(void *data);
173static DECLARE_WORK(cache_cleaner, do_cache_clean, NULL);
174
175void cache_register(struct cache_detail *cd)
176{
177 cd->proc_ent = proc_mkdir(cd->name, proc_net_rpc);
178 if (cd->proc_ent) {
179 struct proc_dir_entry *p;
180 cd->proc_ent->owner = THIS_MODULE;
181 cd->channel_ent = cd->content_ent = NULL;
182
183 p = create_proc_entry("flush", S_IFREG|S_IRUSR|S_IWUSR,
184 cd->proc_ent);
185 cd->flush_ent = p;
186 if (p) {
187 p->proc_fops = &cache_flush_operations;
188 p->owner = THIS_MODULE;
189 p->data = cd;
190 }
191
192 if (cd->cache_request || cd->cache_parse) {
193 p = create_proc_entry("channel", S_IFREG|S_IRUSR|S_IWUSR,
194 cd->proc_ent);
195 cd->channel_ent = p;
196 if (p) {
197 p->proc_fops = &cache_file_operations;
198 p->owner = THIS_MODULE;
199 p->data = cd;
200 }
201 }
202 if (cd->cache_show) {
203 p = create_proc_entry("content", S_IFREG|S_IRUSR|S_IWUSR,
204 cd->proc_ent);
205 cd->content_ent = p;
206 if (p) {
207 p->proc_fops = &content_file_operations;
208 p->owner = THIS_MODULE;
209 p->data = cd;
210 }
211 }
212 }
213 rwlock_init(&cd->hash_lock);
214 INIT_LIST_HEAD(&cd->queue);
215 spin_lock(&cache_list_lock);
216 cd->nextcheck = 0;
217 cd->entries = 0;
218 atomic_set(&cd->readers, 0);
219 cd->last_close = 0;
220 cd->last_warn = -1;
221 list_add(&cd->others, &cache_list);
222 spin_unlock(&cache_list_lock);
223
224
225 schedule_work(&cache_cleaner);
226}
227
228int cache_unregister(struct cache_detail *cd)
229{
230 cache_purge(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);
236 return -EBUSY;
237 }
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);
243 if (cd->proc_ent) {
244 if (cd->flush_ent)
245 remove_proc_entry("flush", cd->proc_ent);
246 if (cd->channel_ent)
247 remove_proc_entry("channel", cd->proc_ent);
248 if (cd->content_ent)
249 remove_proc_entry("content", cd->proc_ent);
250
251 cd->proc_ent = NULL;
252 remove_proc_entry(cd->name, proc_net_rpc);
253 }
254 if (list_empty(&cache_list)) {
255
256 cancel_delayed_work(&cache_cleaner);
257 flush_scheduled_work();
258 }
259 return 0;
260}
261
262
263
264
265
266
267
268static int cache_clean(void)
269{
270 int rv = 0;
271 struct list_head *next;
272
273 spin_lock(&cache_list_lock);
274
275
276 while (current_detail == NULL ||
277 current_index >= current_detail->hash_size) {
278 if (current_detail)
279 next = current_detail->others.next;
280 else
281 next = cache_list.next;
282 if (next == &cache_list) {
283 current_detail = NULL;
284 spin_unlock(&cache_list_lock);
285 return -1;
286 }
287 current_detail = list_entry(next, struct cache_detail, others);
288 if (current_detail->nextcheck > get_seconds())
289 current_index = current_detail->hash_size;
290 else {
291 current_index = 0;
292 current_detail->nextcheck = get_seconds()+30*60;
293 }
294 }
295
296
297 while (current_detail &&
298 current_index < current_detail->hash_size &&
299 current_detail->hash_table[current_index] == NULL)
300 current_index++;
301
302
303
304 if (current_detail && current_index < current_detail->hash_size) {
305 struct cache_head *ch, **cp;
306 struct cache_detail *d;
307
308 write_lock(¤t_detail->hash_lock);
309
310
311
312 cp = & current_detail->hash_table[current_index];
313 ch = *cp;
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
319 )
320 continue;
321 if (test_and_clear_bit(CACHE_PENDING, &ch->flags))
322 queue_loose(current_detail, ch);
323
324 if (!atomic_read(&ch->refcnt))
325 break;
326 }
327 if (ch) {
328 cache_get(ch);
329 clear_bit(CACHE_HASHED, &ch->flags);
330 *cp = ch->next;
331 ch->next = NULL;
332 current_detail->entries--;
333 rv = 1;
334 }
335 write_unlock(¤t_detail->hash_lock);
336 d = current_detail;
337 if (!ch)
338 current_index ++;
339 spin_unlock(&cache_list_lock);
340 if (ch)
341 d->cache_put(ch, d);
342 } else
343 spin_unlock(&cache_list_lock);
344
345 return rv;
346}
347
348
349
350
351static void do_cache_clean(void *data)
352{
353 int delay = 5;
354 if (cache_clean() == -1)
355 delay = 30*HZ;
356
357 if (list_empty(&cache_list))
358 delay = 0;
359
360 if (delay)
361 schedule_delayed_work(&cache_cleaner, delay);
362}
363
364
365
366
367
368
369
370void cache_flush(void)
371{
372 while (cache_clean() != -1)
373 cond_resched();
374 while (cache_clean() != -1)
375 cond_resched();
376}
377
378void cache_purge(struct cache_detail *detail)
379{
380 detail->flush_time = LONG_MAX;
381 detail->nextcheck = get_seconds();
382 cache_flush();
383 detail->flush_time = 1;
384}
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403#define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
404#define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
405
406#define DFR_MAX 300
407
408static DEFINE_SPINLOCK(cache_defer_lock);
409static LIST_HEAD(cache_defer_list);
410static struct list_head cache_defer_hash[DFR_HASHSIZE];
411static int cache_defer_cnt;
412
413static void cache_defer_req(struct cache_req *req, struct cache_head *item)
414{
415 struct cache_deferred_req *dreq;
416 int hash = DFR_HASH(item);
417
418 dreq = req->defer(req);
419 if (dreq == NULL)
420 return;
421
422 dreq->item = item;
423 dreq->recv_time = get_seconds();
424
425 spin_lock(&cache_defer_lock);
426
427 list_add(&dreq->recent, &cache_defer_list);
428
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]);
432
433
434 dreq = NULL;
435 if (++cache_defer_cnt > DFR_MAX) {
436
437
438
439 if (net_random()&1)
440 dreq = list_entry(cache_defer_list.next,
441 struct cache_deferred_req,
442 recent);
443 else
444 dreq = list_entry(cache_defer_list.prev,
445 struct cache_deferred_req,
446 recent);
447 list_del(&dreq->recent);
448 list_del(&dreq->hash);
449 cache_defer_cnt--;
450 }
451 spin_unlock(&cache_defer_lock);
452
453 if (dreq) {
454
455 dreq->revisit(dreq, 1);
456 }
457 if (test_bit(CACHE_VALID, &item->flags)) {
458
459 cache_revisit_request(item);
460 }
461}
462
463static void cache_revisit_request(struct cache_head *item)
464{
465 struct cache_deferred_req *dreq;
466 struct list_head pending;
467
468 struct list_head *lp;
469 int hash = DFR_HASH(item);
470
471 INIT_LIST_HEAD(&pending);
472 spin_lock(&cache_defer_lock);
473
474 lp = cache_defer_hash[hash].next;
475 if (lp) {
476 while (lp != &cache_defer_hash[hash]) {
477 dreq = list_entry(lp, struct cache_deferred_req, hash);
478 lp = lp->next;
479 if (dreq->item == item) {
480 list_del(&dreq->hash);
481 list_move(&dreq->recent, &pending);
482 cache_defer_cnt--;
483 }
484 }
485 }
486 spin_unlock(&cache_defer_lock);
487
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);
492 }
493}
494
495void cache_clean_deferred(void *owner)
496{
497 struct cache_deferred_req *dreq, *tmp;
498 struct list_head pending;
499
500
501 INIT_LIST_HEAD(&pending);
502 spin_lock(&cache_defer_lock);
503
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);
508 cache_defer_cnt--;
509 }
510 }
511 spin_unlock(&cache_defer_lock);
512
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);
517 }
518}
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536static DEFINE_SPINLOCK(queue_lock);
537static DECLARE_MUTEX(queue_io_sem);
538
539struct cache_queue {
540 struct list_head list;
541 int reader;
542};
543struct cache_request {
544 struct cache_queue q;
545 struct cache_head *item;
546 char * buf;
547 int len;
548 int readers;
549};
550struct cache_reader {
551 struct cache_queue q;
552 int offset;
553};
554
555static ssize_t
556cache_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
557{
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;
561 int err;
562
563 if (count == 0)
564 return 0;
565
566 down(&queue_io_sem);
567
568 again:
569 spin_lock(&queue_lock);
570
571 while (rp->q.list.next != &cd->queue &&
572 list_entry(rp->q.list.next, struct cache_queue, list)
573 ->reader) {
574 struct list_head *next = rp->q.list.next;
575 list_move(&rp->q.list, next);
576 }
577 if (rp->q.list.next == &cd->queue) {
578 spin_unlock(&queue_lock);
579 up(&queue_io_sem);
580 if (rp->offset)
581 BUG();
582 return 0;
583 }
584 rq = container_of(rp->q.list.next, struct cache_request, q.list);
585 if (rq->q.reader) BUG();
586 if (rp->offset == 0)
587 rq->readers++;
588 spin_unlock(&queue_lock);
589
590 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
591 err = -EAGAIN;
592 spin_lock(&queue_lock);
593 list_move(&rp->q.list, &rq->q.list);
594 spin_unlock(&queue_lock);
595 } else {
596 if (rp->offset + count > rq->len)
597 count = rq->len - rp->offset;
598 err = -EFAULT;
599 if (copy_to_user(buf, rq->buf + rp->offset, count))
600 goto out;
601 rp->offset += count;
602 if (rp->offset >= rq->len) {
603 rp->offset = 0;
604 spin_lock(&queue_lock);
605 list_move(&rp->q.list, &rq->q.list);
606 spin_unlock(&queue_lock);
607 }
608 err = 0;
609 }
610 out:
611 if (rp->offset == 0) {
612
613 spin_lock(&queue_lock);
614 rq->readers--;
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);
620 kfree(rq->buf);
621 kfree(rq);
622 } else
623 spin_unlock(&queue_lock);
624 }
625 if (err == -EAGAIN)
626 goto again;
627 up(&queue_io_sem);
628 return err ? err : count;
629}
630
631static char write_buf[8192];
632
633static ssize_t
634cache_write(struct file *filp, const char __user *buf, size_t count,
635 loff_t *ppos)
636{
637 int err;
638 struct cache_detail *cd = PDE(filp->f_dentry->d_inode)->data;
639
640 if (count == 0)
641 return 0;
642 if (count >= sizeof(write_buf))
643 return -EINVAL;
644
645 down(&queue_io_sem);
646
647 if (copy_from_user(write_buf, buf, count)) {
648 up(&queue_io_sem);
649 return -EFAULT;
650 }
651 write_buf[count] = '\0';
652 if (cd->cache_parse)
653 err = cd->cache_parse(cd, write_buf, count);
654 else
655 err = -EINVAL;
656
657 up(&queue_io_sem);
658 return err ? err : count;
659}
660
661static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
662
663static unsigned int
664cache_poll(struct file *filp, poll_table *wait)
665{
666 unsigned int mask;
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;
670
671 poll_wait(filp, &queue_wait, wait);
672
673
674 mask = POLL_OUT | POLLWRNORM;
675
676 if (!rp)
677 return mask;
678
679 spin_lock(&queue_lock);
680
681 for (cq= &rp->q; &cq->list != &cd->queue;
682 cq = list_entry(cq->list.next, struct cache_queue, list))
683 if (!cq->reader) {
684 mask |= POLLIN | POLLRDNORM;
685 break;
686 }
687 spin_unlock(&queue_lock);
688 return mask;
689}
690
691static int
692cache_ioctl(struct inode *ino, struct file *filp,
693 unsigned int cmd, unsigned long arg)
694{
695 int len = 0;
696 struct cache_reader *rp = filp->private_data;
697 struct cache_queue *cq;
698 struct cache_detail *cd = PDE(ino)->data;
699
700 if (cmd != FIONREAD || !rp)
701 return -EINVAL;
702
703 spin_lock(&queue_lock);
704
705
706
707
708 for (cq= &rp->q; &cq->list != &cd->queue;
709 cq = list_entry(cq->list.next, struct cache_queue, list))
710 if (!cq->reader) {
711 struct cache_request *cr =
712 container_of(cq, struct cache_request, q);
713 len = cr->len - rp->offset;
714 break;
715 }
716 spin_unlock(&queue_lock);
717
718 return put_user(len, (int __user *)arg);
719}
720
721static int
722cache_open(struct inode *inode, struct file *filp)
723{
724 struct cache_reader *rp = NULL;
725
726 nonseekable_open(inode, filp);
727 if (filp->f_mode & FMODE_READ) {
728 struct cache_detail *cd = PDE(inode)->data;
729
730 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
731 if (!rp)
732 return -ENOMEM;
733 rp->offset = 0;
734 rp->q.reader = 1;
735 atomic_inc(&cd->readers);
736 spin_lock(&queue_lock);
737 list_add(&rp->q.list, &cd->queue);
738 spin_unlock(&queue_lock);
739 }
740 filp->private_data = rp;
741 return 0;
742}
743
744static int
745cache_release(struct inode *inode, struct file *filp)
746{
747 struct cache_reader *rp = filp->private_data;
748 struct cache_detail *cd = PDE(inode)->data;
749
750 if (rp) {
751 spin_lock(&queue_lock);
752 if (rp->offset) {
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))
756 if (!cq->reader) {
757 container_of(cq, struct cache_request, q)
758 ->readers--;
759 break;
760 }
761 rp->offset = 0;
762 }
763 list_del(&rp->q.list);
764 spin_unlock(&queue_lock);
765
766 filp->private_data = NULL;
767 kfree(rp);
768
769 cd->last_close = get_seconds();
770 atomic_dec(&cd->readers);
771 }
772 return 0;
773}
774
775
776
777static struct file_operations cache_file_operations = {
778 .owner = THIS_MODULE,
779 .llseek = no_llseek,
780 .read = cache_read,
781 .write = cache_write,
782 .poll = cache_poll,
783 .ioctl = cache_ioctl,
784 .open = cache_open,
785 .release = cache_release,
786};
787
788
789static void queue_loose(struct cache_detail *detail, struct cache_head *ch)
790{
791 struct cache_queue *cq;
792 spin_lock(&queue_lock);
793 list_for_each_entry(cq, &detail->queue, list)
794 if (!cq->reader) {
795 struct cache_request *cr = container_of(cq, struct cache_request, q);
796 if (cr->item != ch)
797 continue;
798 if (cr->readers != 0)
799 break;
800 list_del(&cr->q.list);
801 spin_unlock(&queue_lock);
802 detail->cache_put(cr->item, detail);
803 kfree(cr->buf);
804 kfree(cr);
805 return;
806 }
807 spin_unlock(&queue_lock);
808}
809
810
811
812
813
814
815
816
817
818
819void qword_add(char **bpp, int *lp, char *str)
820{
821 char *bp = *bpp;
822 int len = *lp;
823 char c;
824
825 if (len < 0) return;
826
827 while ((c=*str++) && len)
828 switch(c) {
829 case ' ':
830 case '\t':
831 case '\n':
832 case '\\':
833 if (len >= 4) {
834 *bp++ = '\\';
835 *bp++ = '0' + ((c & 0300)>>6);
836 *bp++ = '0' + ((c & 0070)>>3);
837 *bp++ = '0' + ((c & 0007)>>0);
838 }
839 len -= 4;
840 break;
841 default:
842 *bp++ = c;
843 len--;
844 }
845 if (c || len <1) len = -1;
846 else {
847 *bp++ = ' ';
848 len--;
849 }
850 *bpp = bp;
851 *lp = len;
852}
853
854void qword_addhex(char **bpp, int *lp, char *buf, int blen)
855{
856 char *bp = *bpp;
857 int len = *lp;
858
859 if (len < 0) return;
860
861 if (len > 2) {
862 *bp++ = '\\';
863 *bp++ = 'x';
864 len -= 2;
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);
869 len -= 2;
870 blen--;
871 }
872 }
873 if (blen || len<1) len = -1;
874 else {
875 *bp++ = ' ';
876 len--;
877 }
878 *bpp = bp;
879 *lp = len;
880}
881
882static void warn_no_listener(struct cache_detail *detail)
883{
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);
888 }
889}
890
891
892
893
894
895static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h)
896{
897
898 char *buf;
899 struct cache_request *crq;
900 char *bp;
901 int len;
902
903 if (detail->cache_request == NULL)
904 return -EINVAL;
905
906 if (atomic_read(&detail->readers) == 0 &&
907 detail->last_close < get_seconds() - 30) {
908 warn_no_listener(detail);
909 return -EINVAL;
910 }
911
912 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
913 if (!buf)
914 return -EAGAIN;
915
916 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
917 if (!crq) {
918 kfree(buf);
919 return -EAGAIN;
920 }
921
922 bp = buf; len = PAGE_SIZE;
923
924 detail->cache_request(detail, h, &bp, &len);
925
926 if (len < 0) {
927 kfree(buf);
928 kfree(crq);
929 return -EAGAIN;
930 }
931 crq->q.reader = 0;
932 crq->item = cache_get(h);
933 crq->buf = buf;
934 crq->len = PAGE_SIZE - len;
935 crq->readers = 0;
936 spin_lock(&queue_lock);
937 list_add_tail(&crq->q.list, &detail->queue);
938 spin_unlock(&queue_lock);
939 wake_up(&queue_wait);
940 return 0;
941}
942
943
944
945
946
947
948
949
950
951
952
953
954
955#define isodigit(c) (isdigit(c) && c <= '7')
956int qword_get(char **bpp, char *dest, int bufsize)
957{
958
959 char *bp = *bpp;
960 int len = 0;
961
962 while (*bp == ' ') bp++;
963
964 if (bp[0] == '\\' && bp[1] == 'x') {
965
966 bp += 2;
967 while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) {
968 int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
969 bp++;
970 byte <<= 4;
971 byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
972 *dest++ = byte;
973 bp++;
974 len++;
975 }
976 } else {
977
978 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
979 if (*bp == '\\' &&
980 isodigit(bp[1]) && (bp[1] <= '3') &&
981 isodigit(bp[2]) &&
982 isodigit(bp[3])) {
983 int byte = (*++bp -'0');
984 bp++;
985 byte = (byte << 3) | (*bp++ - '0');
986 byte = (byte << 3) | (*bp++ - '0');
987 *dest++ = byte;
988 len++;
989 } else {
990 *dest++ = *bp++;
991 len++;
992 }
993 }
994 }
995
996 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
997 return -1;
998 while (*bp == ' ') bp++;
999 *bpp = bp;
1000 *dest = '\0';
1001 return len;
1002}
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012struct handle {
1013 struct cache_detail *cd;
1014};
1015
1016static void *c_start(struct seq_file *m, loff_t *pos)
1017{
1018 loff_t n = *pos;
1019 unsigned hash, entry;
1020 struct cache_head *ch;
1021 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1022
1023
1024 read_lock(&cd->hash_lock);
1025 if (!n--)
1026 return SEQ_START_TOKEN;
1027 hash = n >> 32;
1028 entry = n & ((1LL<<32) - 1);
1029
1030 for (ch=cd->hash_table[hash]; ch; ch=ch->next)
1031 if (!entry--)
1032 return ch;
1033 n &= ~((1LL<<32) - 1);
1034 do {
1035 hash++;
1036 n += 1LL<<32;
1037 } while(hash < cd->hash_size &&
1038 cd->hash_table[hash]==NULL);
1039 if (hash >= cd->hash_size)
1040 return NULL;
1041 *pos = n+1;
1042 return cd->hash_table[hash];
1043}
1044
1045static void *c_next(struct seq_file *m, void *p, loff_t *pos)
1046{
1047 struct cache_head *ch = p;
1048 int hash = (*pos >> 32);
1049 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1050
1051 if (p == SEQ_START_TOKEN)
1052 hash = 0;
1053 else if (ch->next == NULL) {
1054 hash++;
1055 *pos += 1LL<<32;
1056 } else {
1057 ++*pos;
1058 return ch->next;
1059 }
1060 *pos &= ~((1LL<<32) - 1);
1061 while (hash < cd->hash_size &&
1062 cd->hash_table[hash] == NULL) {
1063 hash++;
1064 *pos += 1LL<<32;
1065 }
1066 if (hash >= cd->hash_size)
1067 return NULL;
1068 ++*pos;
1069 return cd->hash_table[hash];
1070}
1071
1072static void c_stop(struct seq_file *m, void *p)
1073{
1074 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1075 read_unlock(&cd->hash_lock);
1076}
1077
1078static int c_show(struct seq_file *m, void *p)
1079{
1080 struct cache_head *cp = p;
1081 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1082
1083 if (p == SEQ_START_TOKEN)
1084 return cd->cache_show(m, cd, NULL);
1085
1086 ifdebug(CACHE)
1087 seq_printf(m, "# expiry=%ld refcnt=%d\n",
1088 cp->expiry_time, atomic_read(&cp->refcnt));
1089 cache_get(cp);
1090 if (cache_check(cd, cp, NULL))
1091
1092 seq_printf(m, "# ");
1093 else
1094 cache_put(cp, cd);
1095
1096 return cd->cache_show(m, cd, cp);
1097}
1098
1099static struct seq_operations cache_content_op = {
1100 .start = c_start,
1101 .next = c_next,
1102 .stop = c_stop,
1103 .show = c_show,
1104};
1105
1106static int content_open(struct inode *inode, struct file *file)
1107{
1108 int res;
1109 struct handle *han;
1110 struct cache_detail *cd = PDE(inode)->data;
1111
1112 han = kmalloc(sizeof(*han), GFP_KERNEL);
1113 if (han == NULL)
1114 return -ENOMEM;
1115
1116 han->cd = cd;
1117
1118 res = seq_open(file, &cache_content_op);
1119 if (res)
1120 kfree(han);
1121 else
1122 ((struct seq_file *)file->private_data)->private = han;
1123
1124 return res;
1125}
1126static int content_release(struct inode *inode, struct file *file)
1127{
1128 struct seq_file *m = (struct seq_file *)file->private_data;
1129 struct handle *han = m->private;
1130 kfree(han);
1131 m->private = NULL;
1132 return seq_release(inode, file);
1133}
1134
1135static struct file_operations content_file_operations = {
1136 .open = content_open,
1137 .read = seq_read,
1138 .llseek = seq_lseek,
1139 .release = content_release,
1140};
1141
1142static ssize_t read_flush(struct file *file, char __user *buf,
1143 size_t count, loff_t *ppos)
1144{
1145 struct cache_detail *cd = PDE(file->f_dentry->d_inode)->data;
1146 char tbuf[20];
1147 unsigned long p = *ppos;
1148 int len;
1149
1150 sprintf(tbuf, "%lu\n", cd->flush_time);
1151 len = strlen(tbuf);
1152 if (p >= len)
1153 return 0;
1154 len -= p;
1155 if (len > count) len = count;
1156 if (copy_to_user(buf, (void*)(tbuf+p), len))
1157 len = -EFAULT;
1158 else
1159 *ppos += len;
1160 return len;
1161}
1162
1163static ssize_t write_flush(struct file * file, const char __user * buf,
1164 size_t count, loff_t *ppos)
1165{
1166 struct cache_detail *cd = PDE(file->f_dentry->d_inode)->data;
1167 char tbuf[20];
1168 char *ep;
1169 long flushtime;
1170 if (*ppos || count > sizeof(tbuf)-1)
1171 return -EINVAL;
1172 if (copy_from_user(tbuf, buf, count))
1173 return -EFAULT;
1174 tbuf[count] = 0;
1175 flushtime = simple_strtoul(tbuf, &ep, 0);
1176 if (*ep && *ep != '\n')
1177 return -EINVAL;
1178
1179 cd->flush_time = flushtime;
1180 cd->nextcheck = get_seconds();
1181 cache_flush();
1182
1183 *ppos += count;
1184 return count;
1185}
1186
1187static struct file_operations cache_flush_operations = {
1188 .open = nonseekable_open,
1189 .read = read_flush,
1190 .write = write_flush,
1191};
1192