Trond Myklebust | e571cbf | 2009-08-19 18:12:27 -0400 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/nfs/cache_lib.c |
| 3 | * |
| 4 | * Helper routines for the NFS client caches |
| 5 | * |
| 6 | * Copyright (c) 2009 Trond Myklebust <Trond.Myklebust@netapp.com> |
| 7 | */ |
| 8 | #include <linux/kmod.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/moduleparam.h> |
| 11 | #include <linux/mount.h> |
| 12 | #include <linux/namei.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Trond Myklebust | e571cbf | 2009-08-19 18:12:27 -0400 | [diff] [blame] | 14 | #include <linux/sunrpc/cache.h> |
| 15 | #include <linux/sunrpc/rpc_pipe_fs.h> |
Stanislav Kinsbursky | 5c1cacb | 2011-11-25 17:12:56 +0300 | [diff] [blame] | 16 | #include <net/net_namespace.h> |
Trond Myklebust | e571cbf | 2009-08-19 18:12:27 -0400 | [diff] [blame] | 17 | |
| 18 | #include "cache_lib.h" |
| 19 | |
| 20 | #define NFS_CACHE_UPCALL_PATHLEN 256 |
| 21 | #define NFS_CACHE_UPCALL_TIMEOUT 15 |
| 22 | |
| 23 | static char nfs_cache_getent_prog[NFS_CACHE_UPCALL_PATHLEN] = |
| 24 | "/sbin/nfs_cache_getent"; |
| 25 | static unsigned long nfs_cache_getent_timeout = NFS_CACHE_UPCALL_TIMEOUT; |
| 26 | |
| 27 | module_param_string(cache_getent, nfs_cache_getent_prog, |
| 28 | sizeof(nfs_cache_getent_prog), 0600); |
| 29 | MODULE_PARM_DESC(cache_getent, "Path to the client cache upcall program"); |
| 30 | module_param_named(cache_getent_timeout, nfs_cache_getent_timeout, ulong, 0600); |
| 31 | MODULE_PARM_DESC(cache_getent_timeout, "Timeout (in seconds) after which " |
| 32 | "the cache upcall is assumed to have failed"); |
| 33 | |
| 34 | int nfs_cache_upcall(struct cache_detail *cd, char *entry_name) |
| 35 | { |
| 36 | static char *envp[] = { "HOME=/", |
| 37 | "TERM=linux", |
| 38 | "PATH=/sbin:/usr/sbin:/bin:/usr/bin", |
| 39 | NULL |
| 40 | }; |
| 41 | char *argv[] = { |
| 42 | nfs_cache_getent_prog, |
| 43 | cd->name, |
| 44 | entry_name, |
| 45 | NULL |
| 46 | }; |
| 47 | int ret = -EACCES; |
| 48 | |
| 49 | if (nfs_cache_getent_prog[0] == '\0') |
| 50 | goto out; |
| 51 | ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); |
| 52 | /* |
| 53 | * Disable the upcall mechanism if we're getting an ENOENT or |
| 54 | * EACCES error. The admin can re-enable it on the fly by using |
| 55 | * sysfs to set the 'cache_getent' parameter once the problem |
| 56 | * has been fixed. |
| 57 | */ |
| 58 | if (ret == -ENOENT || ret == -EACCES) |
| 59 | nfs_cache_getent_prog[0] = '\0'; |
| 60 | out: |
| 61 | return ret > 0 ? 0 : ret; |
| 62 | } |
| 63 | |
| 64 | /* |
| 65 | * Deferred request handling |
| 66 | */ |
| 67 | void nfs_cache_defer_req_put(struct nfs_cache_defer_req *dreq) |
| 68 | { |
| 69 | if (atomic_dec_and_test(&dreq->count)) |
| 70 | kfree(dreq); |
| 71 | } |
| 72 | |
| 73 | static void nfs_dns_cache_revisit(struct cache_deferred_req *d, int toomany) |
| 74 | { |
| 75 | struct nfs_cache_defer_req *dreq; |
| 76 | |
| 77 | dreq = container_of(d, struct nfs_cache_defer_req, deferred_req); |
| 78 | |
| 79 | complete_all(&dreq->completion); |
| 80 | nfs_cache_defer_req_put(dreq); |
| 81 | } |
| 82 | |
| 83 | static struct cache_deferred_req *nfs_dns_cache_defer(struct cache_req *req) |
| 84 | { |
| 85 | struct nfs_cache_defer_req *dreq; |
| 86 | |
| 87 | dreq = container_of(req, struct nfs_cache_defer_req, req); |
| 88 | dreq->deferred_req.revisit = nfs_dns_cache_revisit; |
| 89 | atomic_inc(&dreq->count); |
| 90 | |
| 91 | return &dreq->deferred_req; |
| 92 | } |
| 93 | |
| 94 | struct nfs_cache_defer_req *nfs_cache_defer_req_alloc(void) |
| 95 | { |
| 96 | struct nfs_cache_defer_req *dreq; |
| 97 | |
| 98 | dreq = kzalloc(sizeof(*dreq), GFP_KERNEL); |
| 99 | if (dreq) { |
| 100 | init_completion(&dreq->completion); |
| 101 | atomic_set(&dreq->count, 1); |
| 102 | dreq->req.defer = nfs_dns_cache_defer; |
| 103 | } |
| 104 | return dreq; |
| 105 | } |
| 106 | |
| 107 | int nfs_cache_wait_for_upcall(struct nfs_cache_defer_req *dreq) |
| 108 | { |
| 109 | if (wait_for_completion_timeout(&dreq->completion, |
| 110 | nfs_cache_getent_timeout * HZ) == 0) |
| 111 | return -ETIMEDOUT; |
| 112 | return 0; |
| 113 | } |
| 114 | |
Stanislav Kinsbursky | 9df69c8 | 2011-11-25 17:13:12 +0300 | [diff] [blame] | 115 | int nfs_cache_register_sb(struct super_block *sb, struct cache_detail *cd) |
Stanislav Kinsbursky | 5c1cacb | 2011-11-25 17:12:56 +0300 | [diff] [blame] | 116 | { |
| 117 | int ret; |
| 118 | struct dentry *dir; |
| 119 | |
| 120 | dir = rpc_d_lookup_sb(sb, "cache"); |
| 121 | BUG_ON(dir == NULL); |
| 122 | ret = sunrpc_cache_register_pipefs(dir, cd->name, 0600, cd); |
| 123 | dput(dir); |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | int nfs_cache_register_net(struct net *net, struct cache_detail *cd) |
Trond Myklebust | e571cbf | 2009-08-19 18:12:27 -0400 | [diff] [blame] | 128 | { |
Stanislav Kinsbursky | 5c1cacb | 2011-11-25 17:12:56 +0300 | [diff] [blame] | 129 | struct super_block *pipefs_sb; |
Stanislav Kinsbursky | 39cb67b | 2011-11-25 17:13:20 +0300 | [diff] [blame] | 130 | int ret = 0; |
Trond Myklebust | e571cbf | 2009-08-19 18:12:27 -0400 | [diff] [blame] | 131 | |
Stanislav Kinsbursky | 5c1cacb | 2011-11-25 17:12:56 +0300 | [diff] [blame] | 132 | pipefs_sb = rpc_get_sb_net(net); |
Stanislav Kinsbursky | 39cb67b | 2011-11-25 17:13:20 +0300 | [diff] [blame] | 133 | if (pipefs_sb) { |
| 134 | ret = nfs_cache_register_sb(pipefs_sb, cd); |
| 135 | rpc_put_sb_net(net); |
Stanislav Kinsbursky | 5c1cacb | 2011-11-25 17:12:56 +0300 | [diff] [blame] | 136 | } |
Trond Myklebust | e571cbf | 2009-08-19 18:12:27 -0400 | [diff] [blame] | 137 | return ret; |
| 138 | } |
| 139 | |
Stanislav Kinsbursky | 9df69c8 | 2011-11-25 17:13:12 +0300 | [diff] [blame] | 140 | void nfs_cache_unregister_sb(struct super_block *sb, struct cache_detail *cd) |
Trond Myklebust | e571cbf | 2009-08-19 18:12:27 -0400 | [diff] [blame] | 141 | { |
Stanislav Kinsbursky | 5c1cacb | 2011-11-25 17:12:56 +0300 | [diff] [blame] | 142 | if (cd->u.pipefs.dir) |
| 143 | sunrpc_cache_unregister_pipefs(cd); |
| 144 | } |
| 145 | |
| 146 | void nfs_cache_unregister_net(struct net *net, struct cache_detail *cd) |
| 147 | { |
| 148 | struct super_block *pipefs_sb; |
| 149 | |
| 150 | pipefs_sb = rpc_get_sb_net(net); |
| 151 | if (pipefs_sb) { |
| 152 | nfs_cache_unregister_sb(pipefs_sb, cd); |
| 153 | rpc_put_sb_net(net); |
| 154 | } |
Trond Myklebust | e571cbf | 2009-08-19 18:12:27 -0400 | [diff] [blame] | 155 | } |
| 156 | |
Stanislav Kinsbursky | 9222b95 | 2011-11-25 17:12:48 +0300 | [diff] [blame] | 157 | void nfs_cache_init(struct cache_detail *cd) |
| 158 | { |
| 159 | sunrpc_init_cache_detail(cd); |
| 160 | } |
| 161 | |
| 162 | void nfs_cache_destroy(struct cache_detail *cd) |
| 163 | { |
| 164 | sunrpc_destroy_cache_detail(cd); |
| 165 | } |