13 #include <linux/types.h>
16 #include <linux/slab.h>
17 #include <linux/signal.h>
18 #include <linux/sched.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>
27 #include <linux/net.h>
31 #include <asm/ioctls.h>
38 #define RPCDBG_FACILITY RPCDBG_CACHE
45 time_t now = seconds_since_boot();
69 for (hp=head; *hp !=
NULL ; hp = &(*hp)->
next) {
71 if (detail->
match(tmp, key)) {
72 if (cache_is_expired(detail, tmp))
83 new = detail->
alloc();
91 detail->
init(
new, key);
96 for (hp=head; *hp !=
NULL ; hp = &(*hp)->
next) {
98 if (detail->
match(tmp, key)) {
99 if (cache_is_expired(detail, tmp)) {
108 cache_put(
new, detail);
119 cache_put(freeme, detail);
139 cache_revisit_request(head);
140 cache_dequeue(detail, head);
163 cache_fresh_unlocked(old, detail);
169 tmp = detail->
alloc();
171 cache_put(old, detail);
175 detail->
init(tmp, old);
188 cache_fresh_locked(old, 0);
190 cache_fresh_unlocked(tmp, detail);
191 cache_fresh_unlocked(old, detail);
192 cache_put(old, detail);
230 rv = cache_is_valid(detail, h);
238 cache_fresh_unlocked(h, detail);
260 long refresh_age, age;
263 rv = cache_is_valid(detail, h);
272 }
else if (rv == -
EAGAIN || age > refresh_age/2) {
273 dprintk(
"RPC: Want update, refage=%ld, age=%ld\n",
276 switch (cache_make_upcall(detail, h)) {
279 cache_revisit_request(h);
280 rv = try_to_negate_entry(detail, h);
284 cache_revisit_request(h);
291 if (!cache_defer_req(rqstp, h)) {
296 rv = cache_is_valid(detail, h);
302 cache_put(h, detail);
342 static int current_index;
350 INIT_LIST_HEAD(&cd->
queue);
351 spin_lock(&cache_list_lock);
357 list_add(&cd->
others, &cache_list);
358 spin_unlock(&cache_list_lock);
368 spin_lock(&cache_list_lock);
372 spin_unlock(&cache_list_lock);
375 if (current_detail == cd)
376 current_detail =
NULL;
377 list_del_init(&cd->
others);
379 spin_unlock(&cache_list_lock);
380 if (list_empty(&cache_list)) {
396 static int cache_clean(
void)
401 spin_lock(&cache_list_lock);
404 while (current_detail ==
NULL ||
405 current_index >= current_detail->
hash_size) {
407 next = current_detail->
others.next;
409 next = cache_list.
next;
410 if (next == &cache_list) {
411 current_detail =
NULL;
412 spin_unlock(&cache_list_lock);
416 if (current_detail->
nextcheck > seconds_since_boot())
417 current_index = current_detail->
hash_size;
420 current_detail->
nextcheck = seconds_since_boot()+30*60;
425 while (current_detail &&
426 current_index < current_detail->hash_size &&
432 if (current_detail && current_index < current_detail->hash_size) {
440 cp = & current_detail->
hash_table[current_index];
441 for (ch = *cp ; ch ; cp = & ch->
next, ch = *
cp) {
444 if (!cache_is_expired(current_detail, ch))
458 spin_unlock(&cache_list_lock);
461 cache_dequeue(current_detail, ch);
462 cache_revisit_request(ch);
466 spin_unlock(&cache_list_lock);
477 if (cache_clean() == -1)
480 if (list_empty(&cache_list))
495 while (cache_clean() != -1)
497 while (cache_clean() != -1)
505 detail->
nextcheck = seconds_since_boot();
527 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
528 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
535 static int cache_defer_cnt;
539 hlist_del_init(&dreq->
hash);
540 if (!list_empty(&dreq->
recent)) {
541 list_del_init(&dreq->
recent);
550 INIT_LIST_HEAD(&dreq->
recent);
551 hlist_add_head(&dreq->
hash, &cache_defer_hash[hash]);
561 spin_lock(&cache_defer_lock);
563 __hash_deferred_req(dreq, item);
567 list_add(&dreq->
recent, &cache_defer_list);
570 spin_unlock(&cache_defer_lock);
592 dreq->
revisit = cache_restart_thread;
594 setup_deferral(dreq, item, 0);
602 spin_lock(&cache_defer_lock);
603 if (!hlist_unhashed(&
sleeper.handle.hash)) {
604 __unhash_deferred_req(&
sleeper.handle);
605 spin_unlock(&cache_defer_lock);
612 spin_unlock(&cache_defer_lock);
618 static void cache_limit_defers(
void)
625 if (cache_defer_cnt <=
DFR_MAX)
628 spin_lock(&cache_defer_lock);
631 if (cache_defer_cnt >
DFR_MAX) {
638 __unhash_deferred_req(discard);
640 spin_unlock(&cache_defer_lock);
651 cache_wait_req(req, item);
655 dreq = req->
defer(req);
658 setup_deferral(dreq, item, 1);
663 cache_revisit_request(item);
665 cache_limit_defers();
669 static void cache_revisit_request(
struct cache_head *item)
676 INIT_LIST_HEAD(&pending);
677 spin_lock(&cache_defer_lock);
680 if (dreq->item == item) {
681 __unhash_deferred_req(dreq);
682 list_add(&dreq->
recent, &pending);
685 spin_unlock(&cache_defer_lock);
687 while (!list_empty(&pending)) {
689 list_del_init(&dreq->
recent);
700 INIT_LIST_HEAD(&pending);
701 spin_lock(&cache_defer_lock);
704 if (dreq->
owner == owner) {
705 __unhash_deferred_req(dreq);
706 list_add(&dreq->
recent, &pending);
709 spin_unlock(&cache_defer_lock);
711 while (!list_empty(&pending)) {
713 list_del_init(&dreq->
recent);
767 spin_lock(&queue_lock);
769 while (rp->
q.list.next != &cd->
queue &&
773 list_move(&rp->
q.list, next);
775 if (rp->
q.list.next == &cd->
queue) {
776 spin_unlock(&queue_lock);
785 spin_unlock(&queue_lock);
789 spin_lock(&queue_lock);
790 list_move(&rp->
q.list, &rq->
q.list);
791 spin_unlock(&queue_lock);
801 spin_lock(&queue_lock);
802 list_move(&rp->
q.list, &rq->
q.list);
803 spin_unlock(&queue_lock);
810 spin_lock(&queue_lock);
815 spin_unlock(&queue_lock);
816 cache_put(rq->
item, cd);
820 spin_unlock(&queue_lock);
825 return err ? err :
count;
828 static ssize_t cache_do_downcall(
char *kaddr,
const char __user *buf,
844 static ssize_t cache_slow_downcall(
const char __user *buf,
850 if (count >=
sizeof(write_buf))
853 ret = cache_do_downcall(write_buf, buf, count, cd);
860 const char __user *buf,
875 ret = cache_do_downcall(kaddr, buf, count, cd);
881 return cache_slow_downcall(buf, count, cd);
884 static ssize_t cache_write(
struct file *filp,
const char __user *buf,
885 size_t count, loff_t *ppos,
889 struct inode *inode = filp->
f_path.dentry->d_inode;
896 ret = cache_downcall(mapping, buf, count, cd);
911 poll_wait(filp, &queue_wait, wait);
919 spin_lock(&queue_lock);
927 spin_unlock(&queue_lock);
931 static int cache_ioctl(
struct inode *
ino,
struct file *filp,
932 unsigned int cmd,
unsigned long arg,
942 spin_lock(&queue_lock);
955 spin_unlock(&queue_lock);
957 return put_user(len, (
int __user *)arg);
960 static int cache_open(
struct inode *inode,
struct file *filp,
965 if (!cd || !try_module_get(cd->
owner))
975 spin_lock(&queue_lock);
976 list_add(&rp->
q.list, &cd->
queue);
977 spin_unlock(&queue_lock);
983 static int cache_release(
struct inode *inode,
struct file *filp,
989 spin_lock(&queue_lock);
1002 spin_unlock(&queue_lock);
1010 module_put(cd->
owner);
1019 spin_lock(&queue_lock);
1028 spin_unlock(&queue_lock);
1029 cache_put(cr->
item, detail);
1034 spin_unlock(&queue_lock);
1052 if (len < 0)
return;
1054 while ((c=*str++) && len)
1062 *bp++ =
'0' + ((c & 0300)>>6);
1063 *bp++ =
'0' + ((c & 0070)>>3);
1064 *bp++ =
'0' + ((c & 0007)>>0);
1072 if (c || len <1) len = -1;
1087 if (len < 0)
return;
1093 while (blen && len >= 2) {
1094 unsigned char c = *buf++;
1095 *bp++ =
'0' + ((c&0xf0)>>4) + (c>=0xa0)*(
'a'-
'9'-1);
1096 *bp++ =
'0' + (c&0x0f) + ((c&0x0f)>=0x0a)*(
'a'-
'9'-1);
1101 if (blen || len<1) len = -1;
1111 static void warn_no_listener(
struct cache_detail *detail)
1120 static bool cache_listeners_exist(
struct cache_detail *detail)
1127 if (detail->
last_close < seconds_since_boot() - 30)
1155 if (!cache_listeners_exist(detail)) {
1156 warn_no_listener(detail);
1180 crq->
item = cache_get(h);
1184 spin_lock(&queue_lock);
1186 spin_unlock(&queue_lock);
1204 #define isodigit(c) (isdigit(c) && c <= '7')
1211 while (*bp ==
' ') bp++;
1213 if (bp[0] ==
'\\' && bp[1] ==
'x') {
1216 while (len < bufsize) {
1227 *dest++ = (h << 4) | l;
1233 while (*bp !=
' ' && *bp !=
'\n' && *bp && len < bufsize-1) {
1235 isodigit(bp[1]) && (bp[1] <=
'3') &&
1238 int byte = (*++bp -
'0');
1240 byte = (byte << 3) | (*bp++ -
'0');
1241 byte = (byte << 3) | (*bp++ -
'0');
1251 if (*bp !=
' ' && *bp !=
'\n' && *bp !=
'\0')
1253 while (*bp ==
' ') bp++;
1285 entry = n & ((1
LL<<32) - 1);
1290 n &= ~((1
LL<<32) - 1);
1294 }
while(hash < cd->hash_size &&
1302 static void *c_next(
struct seq_file *
m,
void *
p, loff_t *
pos)
1305 int hash = (*pos >> 32);
1317 *pos &= ~((1
LL<<32) - 1);
1318 while (hash < cd->hash_size &&
1329 static void c_stop(
struct seq_file *m,
void *p)
1336 static int c_show(
struct seq_file *m,
void *p)
1345 seq_printf(m, "
# expiry=%ld refcnt=%d flags=%lx\n",
1353 if (cache_is_expired(cd, cp))
1368 static int content_open(
struct inode *inode,
struct file *
file,
1373 if (!cd || !try_module_get(cd->
owner))
1377 module_put(cd->
owner);
1385 static int content_release(
struct inode *inode,
struct file *file,
1389 module_put(cd->
owner);
1393 static int open_flush(
struct inode *inode,
struct file *file,
1396 if (!cd || !try_module_get(cd->
owner))
1401 static int release_flush(
struct inode *inode,
struct file *file,
1404 module_put(cd->
owner);
1408 static ssize_t read_flush(
struct file *file,
char __user *buf,
1409 size_t count, loff_t *ppos,
1413 unsigned long p = *ppos;
1429 static ssize_t write_flush(
struct file *file,
const char __user *buf,
1430 size_t count, loff_t *ppos,
1436 if (*ppos || count >
sizeof(tbuf)-1)
1442 if (*ep && *ep !=
'\n')
1454 static ssize_t cache_read_procfs(
struct file *filp,
char __user *buf,
1455 size_t count, loff_t *ppos)
1459 return cache_read(filp, buf, count, ppos, cd);
1462 static ssize_t cache_write_procfs(
struct file *filp,
const char __user *buf,
1463 size_t count, loff_t *ppos)
1467 return cache_write(filp, buf, count, ppos, cd);
1470 static unsigned int cache_poll_procfs(
struct file *filp,
poll_table *wait)
1474 return cache_poll(filp, wait, cd);
1477 static long cache_ioctl_procfs(
struct file *filp,
1478 unsigned int cmd,
unsigned long arg)
1480 struct inode *inode = filp->
f_path.dentry->d_inode;
1483 return cache_ioctl(inode, filp, cmd, arg, cd);
1486 static int cache_open_procfs(
struct inode *inode,
struct file *filp)
1490 return cache_open(inode, filp, cd);
1493 static int cache_release_procfs(
struct inode *inode,
struct file *filp)
1497 return cache_release(inode, filp, cd);
1503 .read = cache_read_procfs,
1504 .write = cache_write_procfs,
1505 .poll = cache_poll_procfs,
1506 .unlocked_ioctl = cache_ioctl_procfs,
1507 .open = cache_open_procfs,
1508 .release = cache_release_procfs,
1511 static int content_open_procfs(
struct inode *inode,
struct file *filp)
1515 return content_open(inode, filp, cd);
1518 static int content_release_procfs(
struct inode *inode,
struct file *filp)
1522 return content_release(inode, filp, cd);
1525 static const struct file_operations content_file_operations_procfs = {
1526 .open = content_open_procfs,
1529 .release = content_release_procfs,
1532 static int open_flush_procfs(
struct inode *inode,
struct file *filp)
1536 return open_flush(inode, filp, cd);
1539 static int release_flush_procfs(
struct inode *inode,
struct file *filp)
1543 return release_flush(inode, filp, cd);
1546 static ssize_t read_flush_procfs(
struct file *filp,
char __user *buf,
1547 size_t count, loff_t *ppos)
1551 return read_flush(filp, buf, count, ppos, cd);
1554 static ssize_t write_flush_procfs(
struct file *filp,
1555 const char __user *buf,
1556 size_t count, loff_t *ppos)
1560 return write_flush(filp, buf, count, ppos, cd);
1564 .open = open_flush_procfs,
1565 .read = read_flush_procfs,
1566 .write = write_flush_procfs,
1567 .release = release_flush_procfs,
1588 #ifdef CONFIG_PROC_FS
1589 static int create_cache_proc_entries(
struct cache_detail *cd,
struct net *net)
1603 &cache_flush_operations_procfs, cd);
1611 &cache_file_operations_procfs, cd);
1619 &content_file_operations_procfs, cd);
1626 remove_cache_proc_entries(cd, net);
1630 static int create_cache_proc_entries(
struct cache_detail *cd,
struct net *net)
1646 ret = create_cache_proc_entries(cd, net);
1655 remove_cache_proc_entries(cd, net);
1686 static ssize_t cache_read_pipefs(
struct file *filp,
char __user *buf,
1687 size_t count, loff_t *ppos)
1691 return cache_read(filp, buf, count, ppos, cd);
1694 static ssize_t cache_write_pipefs(
struct file *filp,
const char __user *buf,
1695 size_t count, loff_t *ppos)
1699 return cache_write(filp, buf, count, ppos, cd);
1702 static unsigned int cache_poll_pipefs(
struct file *filp,
poll_table *wait)
1706 return cache_poll(filp, wait, cd);
1709 static long cache_ioctl_pipefs(
struct file *filp,
1710 unsigned int cmd,
unsigned long arg)
1712 struct inode *inode = filp->f_dentry->d_inode;
1715 return cache_ioctl(inode, filp, cmd, arg, cd);
1718 static int cache_open_pipefs(
struct inode *inode,
struct file *filp)
1722 return cache_open(inode, filp, cd);
1725 static int cache_release_pipefs(
struct inode *inode,
struct file *filp)
1729 return cache_release(inode, filp, cd);
1735 .read = cache_read_pipefs,
1736 .write = cache_write_pipefs,
1737 .poll = cache_poll_pipefs,
1738 .unlocked_ioctl = cache_ioctl_pipefs,
1739 .open = cache_open_pipefs,
1740 .release = cache_release_pipefs,
1743 static int content_open_pipefs(
struct inode *inode,
struct file *filp)
1747 return content_open(inode, filp, cd);
1750 static int content_release_pipefs(
struct inode *inode,
struct file *filp)
1754 return content_release(inode, filp, cd);
1758 .open = content_open_pipefs,
1761 .release = content_release_pipefs,
1764 static int open_flush_pipefs(
struct inode *inode,
struct file *filp)
1768 return open_flush(inode, filp, cd);
1771 static int release_flush_pipefs(
struct inode *inode,
struct file *filp)
1775 return release_flush(inode, filp, cd);
1778 static ssize_t read_flush_pipefs(
struct file *filp,
char __user *buf,
1779 size_t count, loff_t *ppos)
1783 return read_flush(filp, buf, count, ppos, cd);
1786 static ssize_t write_flush_pipefs(
struct file *filp,
1787 const char __user *buf,
1788 size_t count, loff_t *ppos)
1792 return write_flush(filp, buf, count, ppos, cd);
1796 .open = open_flush_pipefs,
1797 .read = read_flush_pipefs,
1798 .write = write_flush_pipefs,
1799 .release = release_flush_pipefs,