diff options
Diffstat (limited to 'net/sunrpc/cache.c')
| -rw-r--r-- | net/sunrpc/cache.c | 622 |
1 files changed, 473 insertions, 149 deletions
diff --git a/net/sunrpc/cache.c b/net/sunrpc/cache.c index ff0c23053d2f..45cdaff9b361 100644 --- a/net/sunrpc/cache.c +++ b/net/sunrpc/cache.c | |||
| @@ -27,10 +27,12 @@ | |||
| 27 | #include <linux/net.h> | 27 | #include <linux/net.h> |
| 28 | #include <linux/workqueue.h> | 28 | #include <linux/workqueue.h> |
| 29 | #include <linux/mutex.h> | 29 | #include <linux/mutex.h> |
| 30 | #include <linux/pagemap.h> | ||
| 30 | #include <asm/ioctls.h> | 31 | #include <asm/ioctls.h> |
| 31 | #include <linux/sunrpc/types.h> | 32 | #include <linux/sunrpc/types.h> |
| 32 | #include <linux/sunrpc/cache.h> | 33 | #include <linux/sunrpc/cache.h> |
| 33 | #include <linux/sunrpc/stats.h> | 34 | #include <linux/sunrpc/stats.h> |
| 35 | #include <linux/sunrpc/rpc_pipe_fs.h> | ||
| 34 | 36 | ||
| 35 | #define RPCDBG_FACILITY RPCDBG_CACHE | 37 | #define RPCDBG_FACILITY RPCDBG_CACHE |
| 36 | 38 | ||
| @@ -175,7 +177,13 @@ struct cache_head *sunrpc_cache_update(struct cache_detail *detail, | |||
| 175 | } | 177 | } |
| 176 | EXPORT_SYMBOL_GPL(sunrpc_cache_update); | 178 | EXPORT_SYMBOL_GPL(sunrpc_cache_update); |
| 177 | 179 | ||
| 178 | static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h); | 180 | static int cache_make_upcall(struct cache_detail *cd, struct cache_head *h) |
| 181 | { | ||
| 182 | if (!cd->cache_upcall) | ||
| 183 | return -EINVAL; | ||
| 184 | return cd->cache_upcall(cd, h); | ||
| 185 | } | ||
| 186 | |||
| 179 | /* | 187 | /* |
| 180 | * This is the generic cache management routine for all | 188 | * This is the generic cache management routine for all |
| 181 | * the authentication caches. | 189 | * the authentication caches. |
| @@ -284,76 +292,11 @@ static DEFINE_SPINLOCK(cache_list_lock); | |||
| 284 | static struct cache_detail *current_detail; | 292 | static struct cache_detail *current_detail; |
| 285 | static int current_index; | 293 | static int current_index; |
| 286 | 294 | ||
| 287 | static const struct file_operations cache_file_operations; | ||
| 288 | static const struct file_operations content_file_operations; | ||
| 289 | static const struct file_operations cache_flush_operations; | ||
| 290 | |||
| 291 | static void do_cache_clean(struct work_struct *work); | 295 | static void do_cache_clean(struct work_struct *work); |
| 292 | static DECLARE_DELAYED_WORK(cache_cleaner, do_cache_clean); | 296 | static DECLARE_DELAYED_WORK(cache_cleaner, do_cache_clean); |
| 293 | 297 | ||
| 294 | static void remove_cache_proc_entries(struct cache_detail *cd) | 298 | static void sunrpc_init_cache_detail(struct cache_detail *cd) |
| 295 | { | ||
| 296 | if (cd->proc_ent == NULL) | ||
| 297 | return; | ||
| 298 | if (cd->flush_ent) | ||
| 299 | remove_proc_entry("flush", cd->proc_ent); | ||
| 300 | if (cd->channel_ent) | ||
| 301 | remove_proc_entry("channel", cd->proc_ent); | ||
| 302 | if (cd->content_ent) | ||
| 303 | remove_proc_entry("content", cd->proc_ent); | ||
| 304 | cd->proc_ent = NULL; | ||
| 305 | remove_proc_entry(cd->name, proc_net_rpc); | ||
| 306 | } | ||
| 307 | |||
| 308 | #ifdef CONFIG_PROC_FS | ||
| 309 | static int create_cache_proc_entries(struct cache_detail *cd) | ||
| 310 | { | ||
| 311 | struct proc_dir_entry *p; | ||
| 312 | |||
| 313 | cd->proc_ent = proc_mkdir(cd->name, proc_net_rpc); | ||
| 314 | if (cd->proc_ent == NULL) | ||
| 315 | goto out_nomem; | ||
| 316 | cd->channel_ent = cd->content_ent = NULL; | ||
| 317 | |||
| 318 | p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR, | ||
| 319 | cd->proc_ent, &cache_flush_operations, cd); | ||
| 320 | cd->flush_ent = p; | ||
| 321 | if (p == NULL) | ||
| 322 | goto out_nomem; | ||
| 323 | |||
| 324 | if (cd->cache_request || cd->cache_parse) { | ||
| 325 | p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR, | ||
| 326 | cd->proc_ent, &cache_file_operations, cd); | ||
| 327 | cd->channel_ent = p; | ||
| 328 | if (p == NULL) | ||
| 329 | goto out_nomem; | ||
| 330 | } | ||
| 331 | if (cd->cache_show) { | ||
| 332 | p = proc_create_data("content", S_IFREG|S_IRUSR|S_IWUSR, | ||
| 333 | cd->proc_ent, &content_file_operations, cd); | ||
| 334 | cd->content_ent = p; | ||
| 335 | if (p == NULL) | ||
| 336 | goto out_nomem; | ||
| 337 | } | ||
| 338 | return 0; | ||
| 339 | out_nomem: | ||
| 340 | remove_cache_proc_entries(cd); | ||
| 341 | return -ENOMEM; | ||
| 342 | } | ||
| 343 | #else /* CONFIG_PROC_FS */ | ||
| 344 | static int create_cache_proc_entries(struct cache_detail *cd) | ||
| 345 | { | ||
| 346 | return 0; | ||
| 347 | } | ||
| 348 | #endif | ||
| 349 | |||
| 350 | int cache_register(struct cache_detail *cd) | ||
| 351 | { | 299 | { |
| 352 | int ret; | ||
| 353 | |||
| 354 | ret = create_cache_proc_entries(cd); | ||
| 355 | if (ret) | ||
| 356 | return ret; | ||
| 357 | rwlock_init(&cd->hash_lock); | 300 | rwlock_init(&cd->hash_lock); |
| 358 | INIT_LIST_HEAD(&cd->queue); | 301 | INIT_LIST_HEAD(&cd->queue); |
| 359 | spin_lock(&cache_list_lock); | 302 | spin_lock(&cache_list_lock); |
| @@ -367,11 +310,9 @@ int cache_register(struct cache_detail *cd) | |||
| 367 | 310 | ||
| 368 | /* start the cleaning process */ | 311 | /* start the cleaning process */ |
| 369 | schedule_delayed_work(&cache_cleaner, 0); | 312 | schedule_delayed_work(&cache_cleaner, 0); |
| 370 | return 0; | ||
| 371 | } | 313 | } |
| 372 | EXPORT_SYMBOL_GPL(cache_register); | ||
| 373 | 314 | ||
| 374 | void cache_unregister(struct cache_detail *cd) | 315 | static void sunrpc_destroy_cache_detail(struct cache_detail *cd) |
| 375 | { | 316 | { |
| 376 | cache_purge(cd); | 317 | cache_purge(cd); |
| 377 | spin_lock(&cache_list_lock); | 318 | spin_lock(&cache_list_lock); |
| @@ -386,7 +327,6 @@ void cache_unregister(struct cache_detail *cd) | |||
| 386 | list_del_init(&cd->others); | 327 | list_del_init(&cd->others); |
| 387 | write_unlock(&cd->hash_lock); | 328 | write_unlock(&cd->hash_lock); |
| 388 | spin_unlock(&cache_list_lock); | 329 | spin_unlock(&cache_list_lock); |
| 389 | remove_cache_proc_entries(cd); | ||
| 390 | if (list_empty(&cache_list)) { | 330 | if (list_empty(&cache_list)) { |
| 391 | /* module must be being unloaded so its safe to kill the worker */ | 331 | /* module must be being unloaded so its safe to kill the worker */ |
| 392 | cancel_delayed_work_sync(&cache_cleaner); | 332 | cancel_delayed_work_sync(&cache_cleaner); |
| @@ -395,7 +335,6 @@ void cache_unregister(struct cache_detail *cd) | |||
| 395 | out: | 335 | out: |
| 396 | printk(KERN_ERR "nfsd: failed to unregister %s cache\n", cd->name); | 336 | printk(KERN_ERR "nfsd: failed to unregister %s cache\n", cd->name); |
| 397 | } | 337 | } |
| 398 | EXPORT_SYMBOL_GPL(cache_unregister); | ||
| 399 | 338 | ||
| 400 | /* clean cache tries to find something to clean | 339 | /* clean cache tries to find something to clean |
| 401 | * and cleans it. | 340 | * and cleans it. |
| @@ -687,18 +626,18 @@ struct cache_reader { | |||
| 687 | int offset; /* if non-0, we have a refcnt on next request */ | 626 | int offset; /* if non-0, we have a refcnt on next request */ |
| 688 | }; | 627 | }; |
| 689 | 628 | ||
| 690 | static ssize_t | 629 | static ssize_t cache_read(struct file *filp, char __user *buf, size_t count, |
| 691 | cache_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos) | 630 | loff_t *ppos, struct cache_detail *cd) |
| 692 | { | 631 | { |
| 693 | struct cache_reader *rp = filp->private_data; | 632 | struct cache_reader *rp = filp->private_data; |
| 694 | struct cache_request *rq; | 633 | struct cache_request *rq; |
| 695 | struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data; | 634 | struct inode *inode = filp->f_path.dentry->d_inode; |
| 696 | int err; | 635 | int err; |
| 697 | 636 | ||
| 698 | if (count == 0) | 637 | if (count == 0) |
| 699 | return 0; | 638 | return 0; |
| 700 | 639 | ||
| 701 | mutex_lock(&queue_io_mutex); /* protect against multiple concurrent | 640 | mutex_lock(&inode->i_mutex); /* protect against multiple concurrent |
| 702 | * readers on this file */ | 641 | * readers on this file */ |
| 703 | again: | 642 | again: |
| 704 | spin_lock(&queue_lock); | 643 | spin_lock(&queue_lock); |
| @@ -711,7 +650,7 @@ cache_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos) | |||
| 711 | } | 650 | } |
| 712 | if (rp->q.list.next == &cd->queue) { | 651 | if (rp->q.list.next == &cd->queue) { |
| 713 | spin_unlock(&queue_lock); | 652 | spin_unlock(&queue_lock); |
| 714 | mutex_unlock(&queue_io_mutex); | 653 | mutex_unlock(&inode->i_mutex); |
| 715 | BUG_ON(rp->offset); | 654 | BUG_ON(rp->offset); |
| 716 | return 0; | 655 | return 0; |
| 717 | } | 656 | } |
| @@ -758,49 +697,90 @@ cache_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos) | |||
| 758 | } | 697 | } |
| 759 | if (err == -EAGAIN) | 698 | if (err == -EAGAIN) |
| 760 | goto again; | 699 | goto again; |
| 761 | mutex_unlock(&queue_io_mutex); | 700 | mutex_unlock(&inode->i_mutex); |
| 762 | return err ? err : count; | 701 | return err ? err : count; |
| 763 | } | 702 | } |
| 764 | 703 | ||
| 765 | static char write_buf[8192]; /* protected by queue_io_mutex */ | 704 | static ssize_t cache_do_downcall(char *kaddr, const char __user *buf, |
| 705 | size_t count, struct cache_detail *cd) | ||
| 706 | { | ||
| 707 | ssize_t ret; | ||
| 766 | 708 | ||
| 767 | static ssize_t | 709 | if (copy_from_user(kaddr, buf, count)) |
| 768 | cache_write(struct file *filp, const char __user *buf, size_t count, | 710 | return -EFAULT; |
| 769 | loff_t *ppos) | 711 | kaddr[count] = '\0'; |
| 712 | ret = cd->cache_parse(cd, kaddr, count); | ||
| 713 | if (!ret) | ||
| 714 | ret = count; | ||
| 715 | return ret; | ||
| 716 | } | ||
| 717 | |||
| 718 | static ssize_t cache_slow_downcall(const char __user *buf, | ||
| 719 | size_t count, struct cache_detail *cd) | ||
| 770 | { | 720 | { |
| 771 | int err; | 721 | static char write_buf[8192]; /* protected by queue_io_mutex */ |
| 772 | struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data; | 722 | ssize_t ret = -EINVAL; |
| 773 | 723 | ||
| 774 | if (count == 0) | ||
| 775 | return 0; | ||
| 776 | if (count >= sizeof(write_buf)) | 724 | if (count >= sizeof(write_buf)) |
| 777 | return -EINVAL; | 725 | goto out; |
| 778 | |||
| 779 | mutex_lock(&queue_io_mutex); | 726 | mutex_lock(&queue_io_mutex); |
| 727 | ret = cache_do_downcall(write_buf, buf, count, cd); | ||
| 728 | mutex_unlock(&queue_io_mutex); | ||
| 729 | out: | ||
| 730 | return ret; | ||
| 731 | } | ||
| 780 | 732 | ||
| 781 | if (copy_from_user(write_buf, buf, count)) { | 733 | static ssize_t cache_downcall(struct address_space *mapping, |
| 782 | mutex_unlock(&queue_io_mutex); | 734 | const char __user *buf, |
| 783 | return -EFAULT; | 735 | size_t count, struct cache_detail *cd) |
| 784 | } | 736 | { |
| 785 | write_buf[count] = '\0'; | 737 | struct page *page; |
| 786 | if (cd->cache_parse) | 738 | char *kaddr; |
| 787 | err = cd->cache_parse(cd, write_buf, count); | 739 | ssize_t ret = -ENOMEM; |
| 788 | else | 740 | |
| 789 | err = -EINVAL; | 741 | if (count >= PAGE_CACHE_SIZE) |
| 742 | goto out_slow; | ||
| 743 | |||
| 744 | page = find_or_create_page(mapping, 0, GFP_KERNEL); | ||
| 745 | if (!page) | ||
| 746 | goto out_slow; | ||
| 747 | |||
| 748 | kaddr = kmap(page); | ||
| 749 | ret = cache_do_downcall(kaddr, buf, count, cd); | ||
| 750 | kunmap(page); | ||
| 751 | unlock_page(page); | ||
| 752 | page_cache_release(page); | ||
| 753 | return ret; | ||
| 754 | out_slow: | ||
| 755 | return cache_slow_downcall(buf, count, cd); | ||
| 756 | } | ||
| 790 | 757 | ||
| 791 | mutex_unlock(&queue_io_mutex); | 758 | static ssize_t cache_write(struct file *filp, const char __user *buf, |
| 792 | return err ? err : count; | 759 | size_t count, loff_t *ppos, |
| 760 | struct cache_detail *cd) | ||
| 761 | { | ||
| 762 | struct address_space *mapping = filp->f_mapping; | ||
| 763 | struct inode *inode = filp->f_path.dentry->d_inode; | ||
| 764 | ssize_t ret = -EINVAL; | ||
| 765 | |||
| 766 | if (!cd->cache_parse) | ||
| 767 | goto out; | ||
| 768 | |||
| 769 | mutex_lock(&inode->i_mutex); | ||
| 770 | ret = cache_downcall(mapping, buf, count, cd); | ||
| 771 | mutex_unlock(&inode->i_mutex); | ||
| 772 | out: | ||
| 773 | return ret; | ||
| 793 | } | 774 | } |
| 794 | 775 | ||
| 795 | static DECLARE_WAIT_QUEUE_HEAD(queue_wait); | 776 | static DECLARE_WAIT_QUEUE_HEAD(queue_wait); |
| 796 | 777 | ||
| 797 | static unsigned int | 778 | static unsigned int cache_poll(struct file *filp, poll_table *wait, |
| 798 | cache_poll(struct file *filp, poll_table *wait) | 779 | struct cache_detail *cd) |
| 799 | { | 780 | { |
| 800 | unsigned int mask; | 781 | unsigned int mask; |
| 801 | struct cache_reader *rp = filp->private_data; | 782 | struct cache_reader *rp = filp->private_data; |
| 802 | struct cache_queue *cq; | 783 | struct cache_queue *cq; |
| 803 | struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data; | ||
| 804 | 784 | ||
| 805 | poll_wait(filp, &queue_wait, wait); | 785 | poll_wait(filp, &queue_wait, wait); |
| 806 | 786 | ||
| @@ -822,14 +802,13 @@ cache_poll(struct file *filp, poll_table *wait) | |||
| 822 | return mask; | 802 | return mask; |
| 823 | } | 803 | } |
| 824 | 804 | ||
| 825 | static int | 805 | static int cache_ioctl(struct inode *ino, struct file *filp, |
| 826 | cache_ioctl(struct inode *ino, struct file *filp, | 806 | unsigned int cmd, unsigned long arg, |
| 827 | unsigned int cmd, unsigned long arg) | 807 | struct cache_detail *cd) |
| 828 | { | 808 | { |
| 829 | int len = 0; | 809 | int len = 0; |
| 830 | struct cache_reader *rp = filp->private_data; | 810 | struct cache_reader *rp = filp->private_data; |
| 831 | struct cache_queue *cq; | 811 | struct cache_queue *cq; |
| 832 | struct cache_detail *cd = PDE(ino)->data; | ||
| 833 | 812 | ||
| 834 | if (cmd != FIONREAD || !rp) | 813 | if (cmd != FIONREAD || !rp) |
| 835 | return -EINVAL; | 814 | return -EINVAL; |
| @@ -852,15 +831,15 @@ cache_ioctl(struct inode *ino, struct file *filp, | |||
| 852 | return put_user(len, (int __user *)arg); | 831 | return put_user(len, (int __user *)arg); |
| 853 | } | 832 | } |
| 854 | 833 | ||
| 855 | static int | 834 | static int cache_open(struct inode *inode, struct file *filp, |
| 856 | cache_open(struct inode *inode, struct file *filp) | 835 | struct cache_detail *cd) |
| 857 | { | 836 | { |
| 858 | struct cache_reader *rp = NULL; | 837 | struct cache_reader *rp = NULL; |
| 859 | 838 | ||
| 839 | if (!cd || !try_module_get(cd->owner)) | ||
| 840 | return -EACCES; | ||
| 860 | nonseekable_open(inode, filp); | 841 | nonseekable_open(inode, filp); |
| 861 | if (filp->f_mode & FMODE_READ) { | 842 | if (filp->f_mode & FMODE_READ) { |
| 862 | struct cache_detail *cd = PDE(inode)->data; | ||
| 863 | |||
| 864 | rp = kmalloc(sizeof(*rp), GFP_KERNEL); | 843 | rp = kmalloc(sizeof(*rp), GFP_KERNEL); |
| 865 | if (!rp) | 844 | if (!rp) |
| 866 | return -ENOMEM; | 845 | return -ENOMEM; |
| @@ -875,11 +854,10 @@ cache_open(struct inode *inode, struct file *filp) | |||
| 875 | return 0; | 854 | return 0; |
| 876 | } | 855 | } |
| 877 | 856 | ||
| 878 | static int | 857 | static int cache_release(struct inode *inode, struct file *filp, |
| 879 | cache_release(struct inode *inode, struct file *filp) | 858 | struct cache_detail *cd) |
| 880 | { | 859 | { |
| 881 | struct cache_reader *rp = filp->private_data; | 860 | struct cache_reader *rp = filp->private_data; |
| 882 | struct cache_detail *cd = PDE(inode)->data; | ||
| 883 | 861 | ||
| 884 | if (rp) { | 862 | if (rp) { |
| 885 | spin_lock(&queue_lock); | 863 | spin_lock(&queue_lock); |
| @@ -903,23 +881,12 @@ cache_release(struct inode *inode, struct file *filp) | |||
| 903 | cd->last_close = get_seconds(); | 881 | cd->last_close = get_seconds(); |
| 904 | atomic_dec(&cd->readers); | 882 | atomic_dec(&cd->readers); |
| 905 | } | 883 | } |
| 884 | module_put(cd->owner); | ||
| 906 | return 0; | 885 | return 0; |
| 907 | } | 886 | } |
| 908 | 887 | ||
| 909 | 888 | ||
| 910 | 889 | ||
| 911 | static const struct file_operations cache_file_operations = { | ||
| 912 | .owner = THIS_MODULE, | ||
| 913 | .llseek = no_llseek, | ||
| 914 | .read = cache_read, | ||
| 915 | .write = cache_write, | ||
| 916 | .poll = cache_poll, | ||
| 917 | .ioctl = cache_ioctl, /* for FIONREAD */ | ||
| 918 | .open = cache_open, | ||
| 919 | .release = cache_release, | ||
| 920 | }; | ||
| 921 | |||
| 922 | |||
| 923 | static void queue_loose(struct cache_detail *detail, struct cache_head *ch) | 890 | static void queue_loose(struct cache_detail *detail, struct cache_head *ch) |
| 924 | { | 891 | { |
| 925 | struct cache_queue *cq; | 892 | struct cache_queue *cq; |
| @@ -1020,15 +987,21 @@ static void warn_no_listener(struct cache_detail *detail) | |||
| 1020 | if (detail->last_warn != detail->last_close) { | 987 | if (detail->last_warn != detail->last_close) { |
| 1021 | detail->last_warn = detail->last_close; | 988 | detail->last_warn = detail->last_close; |
| 1022 | if (detail->warn_no_listener) | 989 | if (detail->warn_no_listener) |
| 1023 | detail->warn_no_listener(detail); | 990 | detail->warn_no_listener(detail, detail->last_close != 0); |
| 1024 | } | 991 | } |
| 1025 | } | 992 | } |
| 1026 | 993 | ||
| 1027 | /* | 994 | /* |
| 1028 | * register an upcall request to user-space. | 995 | * register an upcall request to user-space and queue it up for read() by the |
| 996 | * upcall daemon. | ||
| 997 | * | ||
| 1029 | * Each request is at most one page long. | 998 | * Each request is at most one page long. |
| 1030 | */ | 999 | */ |
| 1031 | static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h) | 1000 | int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h, |
| 1001 | void (*cache_request)(struct cache_detail *, | ||
| 1002 | struct cache_head *, | ||
| 1003 | char **, | ||
| 1004 | int *)) | ||
| 1032 | { | 1005 | { |
| 1033 | 1006 | ||
| 1034 | char *buf; | 1007 | char *buf; |
| @@ -1036,9 +1009,6 @@ static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h) | |||
| 1036 | char *bp; | 1009 | char *bp; |
| 1037 | int len; | 1010 | int len; |
| 1038 | 1011 | ||
| 1039 | if (detail->cache_request == NULL) | ||
| 1040 | return -EINVAL; | ||
| 1041 | |||
| 1042 | if (atomic_read(&detail->readers) == 0 && | 1012 | if (atomic_read(&detail->readers) == 0 && |
| 1043 | detail->last_close < get_seconds() - 30) { | 1013 | detail->last_close < get_seconds() - 30) { |
| 1044 | warn_no_listener(detail); | 1014 | warn_no_listener(detail); |
| @@ -1057,7 +1027,7 @@ static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h) | |||
| 1057 | 1027 | ||
| 1058 | bp = buf; len = PAGE_SIZE; | 1028 | bp = buf; len = PAGE_SIZE; |
| 1059 | 1029 | ||
| 1060 | detail->cache_request(detail, h, &bp, &len); | 1030 | cache_request(detail, h, &bp, &len); |
| 1061 | 1031 | ||
| 1062 | if (len < 0) { | 1032 | if (len < 0) { |
| 1063 | kfree(buf); | 1033 | kfree(buf); |
| @@ -1075,6 +1045,7 @@ static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h) | |||
| 1075 | wake_up(&queue_wait); | 1045 | wake_up(&queue_wait); |
| 1076 | return 0; | 1046 | return 0; |
| 1077 | } | 1047 | } |
| 1048 | EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall); | ||
| 1078 | 1049 | ||
| 1079 | /* | 1050 | /* |
| 1080 | * parse a message from user-space and pass it | 1051 | * parse a message from user-space and pass it |
| @@ -1242,11 +1213,13 @@ static const struct seq_operations cache_content_op = { | |||
| 1242 | .show = c_show, | 1213 | .show = c_show, |
| 1243 | }; | 1214 | }; |
| 1244 | 1215 | ||
| 1245 | static int content_open(struct inode *inode, struct file *file) | 1216 | static int content_open(struct inode *inode, struct file *file, |
| 1217 | struct cache_detail *cd) | ||
| 1246 | { | 1218 | { |
| 1247 | struct handle *han; | 1219 | struct handle *han; |
| 1248 | struct cache_detail *cd = PDE(inode)->data; | ||
| 1249 | 1220 | ||
| 1221 | if (!cd || !try_module_get(cd->owner)) | ||
| 1222 | return -EACCES; | ||
| 1250 | han = __seq_open_private(file, &cache_content_op, sizeof(*han)); | 1223 | han = __seq_open_private(file, &cache_content_op, sizeof(*han)); |
| 1251 | if (han == NULL) | 1224 | if (han == NULL) |
| 1252 | return -ENOMEM; | 1225 | return -ENOMEM; |
| @@ -1255,17 +1228,33 @@ static int content_open(struct inode *inode, struct file *file) | |||
| 1255 | return 0; | 1228 | return 0; |
| 1256 | } | 1229 | } |
| 1257 | 1230 | ||
| 1258 | static const struct file_operations content_file_operations = { | 1231 | static int content_release(struct inode *inode, struct file *file, |
| 1259 | .open = content_open, | 1232 | struct cache_detail *cd) |
| 1260 | .read = seq_read, | 1233 | { |
| 1261 | .llseek = seq_lseek, | 1234 | int ret = seq_release_private(inode, file); |
| 1262 | .release = seq_release_private, | 1235 | module_put(cd->owner); |
| 1263 | }; | 1236 | return ret; |
| 1237 | } | ||
| 1238 | |||
| 1239 | static int open_flush(struct inode *inode, struct file *file, | ||
| 1240 | struct cache_detail *cd) | ||
| 1241 | { | ||
| 1242 | if (!cd || !try_module_get(cd->owner)) | ||
| 1243 | return -EACCES; | ||
| 1244 | return nonseekable_open(inode, file); | ||
| 1245 | } | ||
| 1246 | |||
| 1247 | static int release_flush(struct inode *inode, struct file *file, | ||
| 1248 | struct cache_detail *cd) | ||
| 1249 | { | ||
| 1250 | module_put(cd->owner); | ||
| 1251 | return 0; | ||
| 1252 | } | ||
| 1264 | 1253 | ||
| 1265 | static ssize_t read_flush(struct file *file, char __user *buf, | 1254 | static ssize_t read_flush(struct file *file, char __user *buf, |
| 1266 | size_t count, loff_t *ppos) | 1255 | size_t count, loff_t *ppos, |
| 1256 | struct cache_detail *cd) | ||
| 1267 | { | 1257 | { |
| 1268 | struct cache_detail *cd = PDE(file->f_path.dentry->d_inode)->data; | ||
| 1269 | char tbuf[20]; | 1258 | char tbuf[20]; |
| 1270 | unsigned long p = *ppos; | 1259 | unsigned long p = *ppos; |
| 1271 | size_t len; | 1260 | size_t len; |
| @@ -1283,10 +1272,10 @@ static ssize_t read_flush(struct file *file, char __user *buf, | |||
| 1283 | return len; | 1272 | return len; |
| 1284 | } | 1273 | } |
| 1285 | 1274 | ||
| 1286 | static ssize_t write_flush(struct file * file, const char __user * buf, | 1275 | static ssize_t write_flush(struct file *file, const char __user *buf, |
| 1287 | size_t count, loff_t *ppos) | 1276 | size_t count, loff_t *ppos, |
| 1277 | struct cache_detail *cd) | ||
| 1288 | { | 1278 | { |
| 1289 | struct cache_detail *cd = PDE(file->f_path.dentry->d_inode)->data; | ||
| 1290 | char tbuf[20]; | 1279 | char tbuf[20]; |
| 1291 | char *ep; | 1280 | char *ep; |
| 1292 | long flushtime; | 1281 | long flushtime; |
| @@ -1307,8 +1296,343 @@ static ssize_t write_flush(struct file * file, const char __user * buf, | |||
| 1307 | return count; | 1296 | return count; |
| 1308 | } | 1297 | } |
| 1309 | 1298 | ||
| 1310 | static const struct file_operations cache_flush_operations = { | 1299 | static ssize_t cache_read_procfs(struct file *filp, char __user *buf, |
| 1311 | .open = nonseekable_open, | 1300 | size_t count, loff_t *ppos) |
| 1312 | .read = read_flush, | 1301 | { |
| 1313 | .write = write_flush, | 1302 | struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data; |
| 1303 | |||
| 1304 | return cache_read(filp, buf, count, ppos, cd); | ||
| 1305 | } | ||
| 1306 | |||
| 1307 | static ssize_t cache_write_procfs(struct file *filp, const char __user *buf, | ||
| 1308 | size_t count, loff_t *ppos) | ||
| 1309 | { | ||
| 1310 | struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data; | ||
| 1311 | |||
| 1312 | return cache_write(filp, buf, count, ppos, cd); | ||
| 1313 | } | ||
| 1314 | |||
| 1315 | static unsigned int cache_poll_procfs(struct file *filp, poll_table *wait) | ||
| 1316 | { | ||
| 1317 | struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data; | ||
| 1318 | |||
| 1319 | return cache_poll(filp, wait, cd); | ||
| 1320 | } | ||
| 1321 | |||
| 1322 | static int cache_ioctl_procfs(struct inode *inode, struct file *filp, | ||
| 1323 | unsigned int cmd, unsigned long arg) | ||
| 1324 | { | ||
| 1325 | struct cache_detail *cd = PDE(inode)->data; | ||
| 1326 | |||
| 1327 | return cache_ioctl(inode, filp, cmd, arg, cd); | ||
| 1328 | } | ||
| 1329 | |||
| 1330 | static int cache_open_procfs(struct inode *inode, struct file *filp) | ||
| 1331 | { | ||
| 1332 | struct cache_detail *cd = PDE(inode)->data; | ||
| 1333 | |||
| 1334 | return cache_open(inode, filp, cd); | ||
| 1335 | } | ||
| 1336 | |||
| 1337 | static int cache_release_procfs(struct inode *inode, struct file *filp) | ||
| 1338 | { | ||
| 1339 | struct cache_detail *cd = PDE(inode)->data; | ||
| 1340 | |||
| 1341 | return cache_release(inode, filp, cd); | ||
| 1342 | } | ||
| 1343 | |||
| 1344 | static const struct file_operations cache_file_operations_procfs = { | ||
| 1345 | .owner = THIS_MODULE, | ||
| 1346 | .llseek = no_llseek, | ||
| 1347 | .read = cache_read_procfs, | ||
| 1348 | .write = cache_write_procfs, | ||
| 1349 | .poll = cache_poll_procfs, | ||
| 1350 | .ioctl = cache_ioctl_procfs, /* for FIONREAD */ | ||
| 1351 | .open = cache_open_procfs, | ||
| 1352 | .release = cache_release_procfs, | ||
| 1314 | }; | 1353 | }; |
| 1354 | |||
| 1355 | static int content_open_procfs(struct inode *inode, struct file *filp) | ||
| 1356 | { | ||
| 1357 | struct cache_detail *cd = PDE(inode)->data; | ||
| 1358 | |||
| 1359 | return content_open(inode, filp, cd); | ||
| 1360 | } | ||
| 1361 | |||
| 1362 | static int content_release_procfs(struct inode *inode, struct file *filp) | ||
| 1363 | { | ||
| 1364 | struct cache_detail *cd = PDE(inode)->data; | ||
| 1365 | |||
| 1366 | return content_release(inode, filp, cd); | ||
| 1367 | } | ||
| 1368 | |||
| 1369 | static const struct file_operations content_file_operations_procfs = { | ||
| 1370 | .open = content_open_procfs, | ||
| 1371 | .read = seq_read, | ||
| 1372 | .llseek = seq_lseek, | ||
| 1373 | .release = content_release_procfs, | ||
| 1374 | }; | ||
| 1375 | |||
| 1376 | static int open_flush_procfs(struct inode *inode, struct file *filp) | ||
| 1377 | { | ||
| 1378 | struct cache_detail *cd = PDE(inode)->data; | ||
| 1379 | |||
| 1380 | return open_flush(inode, filp, cd); | ||
| 1381 | } | ||
| 1382 | |||
| 1383 | static int release_flush_procfs(struct inode *inode, struct file *filp) | ||
| 1384 | { | ||
| 1385 | struct cache_detail *cd = PDE(inode)->data; | ||
| 1386 | |||
| 1387 | return release_flush(inode, filp, cd); | ||
| 1388 | } | ||
| 1389 | |||
| 1390 | static ssize_t read_flush_procfs(struct file *filp, char __user *buf, | ||
| 1391 | size_t count, loff_t *ppos) | ||
| 1392 | { | ||
| 1393 | struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data; | ||
| 1394 | |||
| 1395 | return read_flush(filp, buf, count, ppos, cd); | ||
| 1396 | } | ||
| 1397 | |||
| 1398 | static ssize_t write_flush_procfs(struct file *filp, | ||
| 1399 | const char __user *buf, | ||
| 1400 | size_t count, loff_t *ppos) | ||
| 1401 | { | ||
| 1402 | struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data; | ||
| 1403 | |||
| 1404 | return write_flush(filp, buf, count, ppos, cd); | ||
| 1405 | } | ||
| 1406 | |||
| 1407 | static const struct file_operations cache_flush_operations_procfs = { | ||
| 1408 | .open = open_flush_procfs, | ||
| 1409 | .read = read_flush_procfs, | ||
| 1410 | .write = write_flush_procfs, | ||
| 1411 | .release = release_flush_procfs, | ||
| 1412 | }; | ||
| 1413 | |||
| 1414 | static void remove_cache_proc_entries(struct cache_detail *cd) | ||
| 1415 | { | ||
| 1416 | if (cd->u.procfs.proc_ent == NULL) | ||
| 1417 | return; | ||
| 1418 | if (cd->u.procfs.flush_ent) | ||
| 1419 | remove_proc_entry("flush", cd->u.procfs.proc_ent); | ||
| 1420 | if (cd->u.procfs.channel_ent) | ||
| 1421 | remove_proc_entry("channel", cd->u.procfs.proc_ent); | ||
| 1422 | if (cd->u.procfs.content_ent) | ||
| 1423 | remove_proc_entry("content", cd->u.procfs.proc_ent); | ||
| 1424 | cd->u.procfs.proc_ent = NULL; | ||
| 1425 | remove_proc_entry(cd->name, proc_net_rpc); | ||
| 1426 | } | ||
| 1427 | |||
| 1428 | #ifdef CONFIG_PROC_FS | ||
| 1429 | static int create_cache_proc_entries(struct cache_detail *cd) | ||
| 1430 | { | ||
| 1431 | struct proc_dir_entry *p; | ||
| 1432 | |||
| 1433 | cd->u.procfs.proc_ent = proc_mkdir(cd->name, proc_net_rpc); | ||
| 1434 | if (cd->u.procfs.proc_ent == NULL) | ||
| 1435 | goto out_nomem; | ||
| 1436 | cd->u.procfs.channel_ent = NULL; | ||
| 1437 | cd->u.procfs.content_ent = NULL; | ||
| 1438 | |||
| 1439 | p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR, | ||
| 1440 | cd->u.procfs.proc_ent, | ||
| 1441 | &cache_flush_operations_procfs, cd); | ||
| 1442 | cd->u.procfs.flush_ent = p; | ||
| 1443 | if (p == NULL) | ||
| 1444 | goto out_nomem; | ||
| 1445 | |||
| 1446 | if (cd->cache_upcall || cd->cache_parse) { | ||
| 1447 | p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR, | ||
| 1448 | cd->u.procfs.proc_ent, | ||
| 1449 | &cache_file_operations_procfs, cd); | ||
| 1450 | cd->u.procfs.channel_ent = p; | ||
| 1451 | if (p == NULL) | ||
| 1452 | goto out_nomem; | ||
| 1453 | } | ||
| 1454 | if (cd->cache_show) { | ||
| 1455 | p = proc_create_data("content", S_IFREG|S_IRUSR|S_IWUSR, | ||
| 1456 | cd->u.procfs.proc_ent, | ||
| 1457 | &content_file_operations_procfs, cd); | ||
| 1458 | cd->u.procfs.content_ent = p; | ||
| 1459 | if (p == NULL) | ||
| 1460 | goto out_nomem; | ||
| 1461 | } | ||
| 1462 | return 0; | ||
| 1463 | out_nomem: | ||
| 1464 | remove_cache_proc_entries(cd); | ||
| 1465 | return -ENOMEM; | ||
| 1466 | } | ||
| 1467 | #else /* CONFIG_PROC_FS */ | ||
| 1468 | static int create_cache_proc_entries(struct cache_detail *cd) | ||
| 1469 | { | ||
| 1470 | return 0; | ||
| 1471 | } | ||
| 1472 | #endif | ||
| 1473 | |||
| 1474 | int cache_register(struct cache_detail *cd) | ||
| 1475 | { | ||
| 1476 | int ret; | ||
| 1477 | |||
| 1478 | sunrpc_init_cache_detail(cd); | ||
| 1479 | ret = create_cache_proc_entries(cd); | ||
| 1480 | if (ret) | ||
| 1481 | sunrpc_destroy_cache_detail(cd); | ||
| 1482 | return ret; | ||
| 1483 | } | ||
| 1484 | EXPORT_SYMBOL_GPL(cache_register); | ||
| 1485 | |||
| 1486 | void cache_unregister(struct cache_detail *cd) | ||
| 1487 | { | ||
| 1488 | remove_cache_proc_entries(cd); | ||
| 1489 | sunrpc_destroy_cache_detail(cd); | ||
| 1490 | } | ||
| 1491 | EXPORT_SYMBOL_GPL(cache_unregister); | ||
| 1492 | |||
| 1493 | static ssize_t cache_read_pipefs(struct file *filp, char __user *buf, | ||
| 1494 | size_t count, loff_t *ppos) | ||
| 1495 | { | ||
| 1496 | struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private; | ||
| 1497 | |||
| 1498 | return cache_read(filp, buf, count, ppos, cd); | ||
| 1499 | } | ||
| 1500 | |||
| 1501 | static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf, | ||
| 1502 | size_t count, loff_t *ppos) | ||
| 1503 | { | ||
| 1504 | struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private; | ||
| 1505 | |||
| 1506 | return cache_write(filp, buf, count, ppos, cd); | ||
| 1507 | } | ||
| 1508 | |||
| 1509 | static unsigned int cache_poll_pipefs(struct file *filp, poll_table *wait) | ||
| 1510 | { | ||
| 1511 | struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private; | ||
| 1512 | |||
| 1513 | return cache_poll(filp, wait, cd); | ||
| 1514 | } | ||
| 1515 | |||
| 1516 | static int cache_ioctl_pipefs(struct inode *inode, struct file *filp, | ||
| 1517 | unsigned int cmd, unsigned long arg) | ||
| 1518 | { | ||
| 1519 | struct cache_detail *cd = RPC_I(inode)->private; | ||
| 1520 | |||
| 1521 | return cache_ioctl(inode, filp, cmd, arg, cd); | ||
| 1522 | } | ||
| 1523 | |||
| 1524 | static int cache_open_pipefs(struct inode *inode, struct file *filp) | ||
| 1525 | { | ||
| 1526 | struct cache_detail *cd = RPC_I(inode)->private; | ||
| 1527 | |||
| 1528 | return cache_open(inode, filp, cd); | ||
| 1529 | } | ||
| 1530 | |||
| 1531 | static int cache_release_pipefs(struct inode *inode, struct file *filp) | ||
| 1532 | { | ||
| 1533 | struct cache_detail *cd = RPC_I(inode)->private; | ||
| 1534 | |||
| 1535 | return cache_release(inode, filp, cd); | ||
| 1536 | } | ||
| 1537 | |||
| 1538 | const struct file_operations cache_file_operations_pipefs = { | ||
| 1539 | .owner = THIS_MODULE, | ||
| 1540 | .llseek = no_llseek, | ||
| 1541 | .read = cache_read_pipefs, | ||
| 1542 | .write = cache_write_pipefs, | ||
| 1543 | .poll = cache_poll_pipefs, | ||
| 1544 | .ioctl = cache_ioctl_pipefs, /* for FIONREAD */ | ||
| 1545 | .open = cache_open_pipefs, | ||
| 1546 | .release = cache_release_pipefs, | ||
| 1547 | }; | ||
| 1548 | |||
| 1549 | static int content_open_pipefs(struct inode *inode, struct file *filp) | ||
| 1550 | { | ||
| 1551 | struct cache_detail *cd = RPC_I(inode)->private; | ||
| 1552 | |||
| 1553 | return content_open(inode, filp, cd); | ||
| 1554 | } | ||
| 1555 | |||
| 1556 | static int content_release_pipefs(struct inode *inode, struct file *filp) | ||
| 1557 | { | ||
| 1558 | struct cache_detail *cd = RPC_I(inode)->private; | ||
| 1559 | |||
| 1560 | return content_release(inode, filp, cd); | ||
| 1561 | } | ||
| 1562 | |||
| 1563 | const struct file_operations content_file_operations_pipefs = { | ||
| 1564 | .open = content_open_pipefs, | ||
| 1565 | .read = seq_read, | ||
| 1566 | .llseek = seq_lseek, | ||
| 1567 | .release = content_release_pipefs, | ||
| 1568 | }; | ||
| 1569 | |||
| 1570 | static int open_flush_pipefs(struct inode *inode, struct file *filp) | ||
| 1571 | { | ||
| 1572 | struct cache_detail *cd = RPC_I(inode)->private; | ||
| 1573 | |||
| 1574 | return open_flush(inode, filp, cd); | ||
| 1575 | } | ||
| 1576 | |||
| 1577 | static int release_flush_pipefs(struct inode *inode, struct file *filp) | ||
| 1578 | { | ||
| 1579 | struct cache_detail *cd = RPC_I(inode)->private; | ||
| 1580 | |||
| 1581 | return release_flush(inode, filp, cd); | ||
| 1582 | } | ||
| 1583 | |||
| 1584 | static ssize_t read_flush_pipefs(struct file *filp, char __user *buf, | ||
| 1585 | size_t count, loff_t *ppos) | ||
| 1586 | { | ||
| 1587 | struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private; | ||
| 1588 | |||
| 1589 | return read_flush(filp, buf, count, ppos, cd); | ||
| 1590 | } | ||
| 1591 | |||
| 1592 | static ssize_t write_flush_pipefs(struct file *filp, | ||
| 1593 | const char __user *buf, | ||
| 1594 | size_t count, loff_t *ppos) | ||
| 1595 | { | ||
| 1596 | struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private; | ||
| 1597 | |||
| 1598 | return write_flush(filp, buf, count, ppos, cd); | ||
| 1599 | } | ||
| 1600 | |||
| 1601 | const struct file_operations cache_flush_operations_pipefs = { | ||
| 1602 | .open = open_flush_pipefs, | ||
| 1603 | .read = read_flush_pipefs, | ||
| 1604 | .write = write_flush_pipefs, | ||
| 1605 | .release = release_flush_pipefs, | ||
| 1606 | }; | ||
| 1607 | |||
| 1608 | int sunrpc_cache_register_pipefs(struct dentry *parent, | ||
| 1609 | const char *name, mode_t umode, | ||
| 1610 | struct cache_detail *cd) | ||
| 1611 | { | ||
| 1612 | struct qstr q; | ||
| 1613 | struct dentry *dir; | ||
| 1614 | int ret = 0; | ||
| 1615 | |||
| 1616 | sunrpc_init_cache_detail(cd); | ||
| 1617 | q.name = name; | ||
| 1618 | q.len = strlen(name); | ||
| 1619 | q.hash = full_name_hash(q.name, q.len); | ||
| 1620 | dir = rpc_create_cache_dir(parent, &q, umode, cd); | ||
| 1621 | if (!IS_ERR(dir)) | ||
| 1622 | cd->u.pipefs.dir = dir; | ||
| 1623 | else { | ||
| 1624 | sunrpc_destroy_cache_detail(cd); | ||
| 1625 | ret = PTR_ERR(dir); | ||
| 1626 | } | ||
| 1627 | return ret; | ||
| 1628 | } | ||
| 1629 | EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs); | ||
| 1630 | |||
| 1631 | void sunrpc_cache_unregister_pipefs(struct cache_detail *cd) | ||
| 1632 | { | ||
| 1633 | rpc_remove_cache_dir(cd->u.pipefs.dir); | ||
| 1634 | cd->u.pipefs.dir = NULL; | ||
| 1635 | sunrpc_destroy_cache_detail(cd); | ||
| 1636 | } | ||
| 1637 | EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs); | ||
| 1638 | |||
