Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Chuck Lever | 55aa4f5 | 2005-08-11 16:25:47 -0400 | [diff] [blame] | 2 | * linux/net/sunrpc/clnt.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * This file contains the high-level RPC interface. |
| 5 | * It is modeled as a finite state machine to support both synchronous |
| 6 | * and asynchronous requests. |
| 7 | * |
| 8 | * - RPC header generation and argument serialization. |
| 9 | * - Credential refresh. |
| 10 | * - TCP connect handling. |
| 11 | * - Retry of operation when it is suspected the operation failed because |
| 12 | * of uid squashing on the server, or when the credentials were stale |
| 13 | * and need to be refreshed, or when a packet was damaged in transit. |
| 14 | * This may be have to be moved to the VFS layer. |
| 15 | * |
| 16 | * NB: BSD uses a more intelligent approach to guessing when a request |
| 17 | * or reply has been lost by keeping the RTO estimate for each procedure. |
| 18 | * We currently make do with a constant timeout value. |
| 19 | * |
| 20 | * Copyright (C) 1992,1993 Rick Sladkey <jrs@world.std.com> |
| 21 | * Copyright (C) 1995,1996 Olaf Kirch <okir@monad.swb.de> |
| 22 | */ |
| 23 | |
| 24 | #include <asm/system.h> |
| 25 | |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/types.h> |
| 28 | #include <linux/mm.h> |
| 29 | #include <linux/slab.h> |
Trond Myklebust | 6d5fcb5 | 2006-10-18 16:01:06 -0400 | [diff] [blame] | 30 | #include <linux/smp_lock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/utsname.h> |
Chuck Lever | 11c556b | 2006-03-20 13:44:22 -0500 | [diff] [blame] | 32 | #include <linux/workqueue.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | #include <linux/sunrpc/clnt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/sunrpc/rpc_pipe_fs.h> |
Chuck Lever | 11c556b | 2006-03-20 13:44:22 -0500 | [diff] [blame] | 36 | #include <linux/sunrpc/metrics.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #ifdef RPC_DEBUG |
| 40 | # define RPCDBG_FACILITY RPCDBG_CALL |
| 41 | #endif |
| 42 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 43 | #define dprint_status(t) \ |
| 44 | dprintk("RPC: %5u %s (status %d)\n", t->tk_pid, \ |
| 45 | __FUNCTION__, t->tk_status) |
| 46 | |
Trond Myklebust | 188fef1 | 2007-06-16 14:18:40 -0400 | [diff] [blame] | 47 | /* |
| 48 | * All RPC clients are linked into this list |
| 49 | */ |
| 50 | static LIST_HEAD(all_clients); |
| 51 | static DEFINE_SPINLOCK(rpc_client_lock); |
| 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | static DECLARE_WAIT_QUEUE_HEAD(destroy_wait); |
| 54 | |
| 55 | |
| 56 | static void call_start(struct rpc_task *task); |
| 57 | static void call_reserve(struct rpc_task *task); |
| 58 | static void call_reserveresult(struct rpc_task *task); |
| 59 | static void call_allocate(struct rpc_task *task); |
| 60 | static void call_encode(struct rpc_task *task); |
| 61 | static void call_decode(struct rpc_task *task); |
| 62 | static void call_bind(struct rpc_task *task); |
Chuck Lever | da35187 | 2005-08-11 16:25:11 -0400 | [diff] [blame] | 63 | static void call_bind_status(struct rpc_task *task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | static void call_transmit(struct rpc_task *task); |
| 65 | static void call_status(struct rpc_task *task); |
Trond Myklebust | 940e331 | 2005-11-09 21:45:24 -0500 | [diff] [blame] | 66 | static void call_transmit_status(struct rpc_task *task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | static void call_refresh(struct rpc_task *task); |
| 68 | static void call_refreshresult(struct rpc_task *task); |
| 69 | static void call_timeout(struct rpc_task *task); |
| 70 | static void call_connect(struct rpc_task *task); |
| 71 | static void call_connect_status(struct rpc_task *task); |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 72 | static __be32 * call_header(struct rpc_task *task); |
| 73 | static __be32 * call_verify(struct rpc_task *task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
Trond Myklebust | 64c91a1 | 2007-06-23 10:17:16 -0400 | [diff] [blame] | 75 | static int rpc_ping(struct rpc_clnt *clnt, int flags); |
| 76 | |
Trond Myklebust | 188fef1 | 2007-06-16 14:18:40 -0400 | [diff] [blame] | 77 | static void rpc_register_client(struct rpc_clnt *clnt) |
| 78 | { |
| 79 | spin_lock(&rpc_client_lock); |
| 80 | list_add(&clnt->cl_clients, &all_clients); |
| 81 | spin_unlock(&rpc_client_lock); |
| 82 | } |
| 83 | |
| 84 | static void rpc_unregister_client(struct rpc_clnt *clnt) |
| 85 | { |
| 86 | spin_lock(&rpc_client_lock); |
| 87 | list_del(&clnt->cl_clients); |
| 88 | spin_unlock(&rpc_client_lock); |
| 89 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
| 91 | static int |
| 92 | rpc_setup_pipedir(struct rpc_clnt *clnt, char *dir_name) |
| 93 | { |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 94 | static uint32_t clntid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | int error; |
| 96 | |
Trond Myklebust | 5428154 | 2006-03-20 13:44:49 -0500 | [diff] [blame] | 97 | clnt->cl_vfsmnt = ERR_PTR(-ENOENT); |
| 98 | clnt->cl_dentry = ERR_PTR(-ENOENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | if (dir_name == NULL) |
| 100 | return 0; |
Trond Myklebust | 5428154 | 2006-03-20 13:44:49 -0500 | [diff] [blame] | 101 | |
| 102 | clnt->cl_vfsmnt = rpc_get_mount(); |
| 103 | if (IS_ERR(clnt->cl_vfsmnt)) |
| 104 | return PTR_ERR(clnt->cl_vfsmnt); |
| 105 | |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 106 | for (;;) { |
| 107 | snprintf(clnt->cl_pathname, sizeof(clnt->cl_pathname), |
| 108 | "%s/clnt%x", dir_name, |
| 109 | (unsigned int)clntid++); |
| 110 | clnt->cl_pathname[sizeof(clnt->cl_pathname) - 1] = '\0'; |
| 111 | clnt->cl_dentry = rpc_mkdir(clnt->cl_pathname, clnt); |
| 112 | if (!IS_ERR(clnt->cl_dentry)) |
| 113 | return 0; |
Christoph Hellwig | 278c995 | 2005-07-24 23:53:01 +0100 | [diff] [blame] | 114 | error = PTR_ERR(clnt->cl_dentry); |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 115 | if (error != -EEXIST) { |
| 116 | printk(KERN_INFO "RPC: Couldn't create pipefs entry %s, error %d\n", |
| 117 | clnt->cl_pathname, error); |
Trond Myklebust | 5428154 | 2006-03-20 13:44:49 -0500 | [diff] [blame] | 118 | rpc_put_mount(); |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 119 | return error; |
| 120 | } |
Christoph Hellwig | 278c995 | 2005-07-24 23:53:01 +0100 | [diff] [blame] | 121 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Chuck Lever | ff9aa5e | 2006-08-22 20:06:21 -0400 | [diff] [blame] | 124 | static struct rpc_clnt * rpc_new_client(struct rpc_xprt *xprt, char *servname, struct rpc_program *program, u32 vers, rpc_authflavor_t flavor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
| 126 | struct rpc_version *version; |
| 127 | struct rpc_clnt *clnt = NULL; |
J. Bruce Fields | 6a19275 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 128 | struct rpc_auth *auth; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | int err; |
Chuck Lever | 06b8d25 | 2007-09-11 18:00:20 -0400 | [diff] [blame] | 130 | size_t len; |
| 131 | |
| 132 | /* sanity check the name before trying to print it */ |
| 133 | err = -EINVAL; |
| 134 | len = strlen(servname); |
| 135 | if (len > RPC_MAXNETNAMELEN) |
| 136 | goto out_no_rpciod; |
| 137 | len++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 139 | dprintk("RPC: creating %s client for %s (xprt %p)\n", |
| 140 | program->name, servname, xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Trond Myklebust | 4ada539 | 2007-06-14 17:26:17 -0400 | [diff] [blame] | 142 | err = rpciod_up(); |
| 143 | if (err) |
| 144 | goto out_no_rpciod; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | err = -EINVAL; |
| 146 | if (!xprt) |
Adrian Bunk | 712917d | 2006-03-13 21:20:47 -0800 | [diff] [blame] | 147 | goto out_no_xprt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | if (vers >= program->nrvers || !(version = program->version[vers])) |
| 149 | goto out_err; |
| 150 | |
| 151 | err = -ENOMEM; |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 152 | clnt = kzalloc(sizeof(*clnt), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | if (!clnt) |
| 154 | goto out_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | clnt->cl_parent = clnt; |
| 156 | |
| 157 | clnt->cl_server = clnt->cl_inline_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | if (len > sizeof(clnt->cl_inline_name)) { |
| 159 | char *buf = kmalloc(len, GFP_KERNEL); |
| 160 | if (buf != 0) |
| 161 | clnt->cl_server = buf; |
| 162 | else |
| 163 | len = sizeof(clnt->cl_inline_name); |
| 164 | } |
| 165 | strlcpy(clnt->cl_server, servname, len); |
| 166 | |
| 167 | clnt->cl_xprt = xprt; |
| 168 | clnt->cl_procinfo = version->procs; |
| 169 | clnt->cl_maxproc = version->nrprocs; |
| 170 | clnt->cl_protname = program->name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | clnt->cl_prog = program->number; |
| 172 | clnt->cl_vers = version->number; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | clnt->cl_stats = program->stats; |
Chuck Lever | 11c556b | 2006-03-20 13:44:22 -0500 | [diff] [blame] | 174 | clnt->cl_metrics = rpc_alloc_iostats(clnt); |
Trond Myklebust | 23bf85b | 2006-11-21 10:40:23 -0500 | [diff] [blame] | 175 | err = -ENOMEM; |
| 176 | if (clnt->cl_metrics == NULL) |
| 177 | goto out_no_stats; |
Trond Myklebust | 3e32a5d | 2006-11-16 11:37:27 -0500 | [diff] [blame] | 178 | clnt->cl_program = program; |
Trond Myklebust | 6529eba | 2007-06-14 16:40:14 -0400 | [diff] [blame] | 179 | INIT_LIST_HEAD(&clnt->cl_tasks); |
Trond Myklebust | 4bef61f | 2007-06-16 14:17:01 -0400 | [diff] [blame] | 180 | spin_lock_init(&clnt->cl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
Chuck Lever | ec739ef | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 182 | if (!xprt_bound(clnt->cl_xprt)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | clnt->cl_autobind = 1; |
| 184 | |
| 185 | clnt->cl_rtt = &clnt->cl_rtt_default; |
| 186 | rpc_init_rtt(&clnt->cl_rtt_default, xprt->timeout.to_initval); |
| 187 | |
Trond Myklebust | 34f52e3 | 2007-06-14 16:40:31 -0400 | [diff] [blame] | 188 | kref_init(&clnt->cl_kref); |
| 189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | err = rpc_setup_pipedir(clnt, program->pipe_dir_name); |
| 191 | if (err < 0) |
| 192 | goto out_no_path; |
| 193 | |
J. Bruce Fields | 6a19275 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 194 | auth = rpcauth_create(flavor, clnt); |
| 195 | if (IS_ERR(auth)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %u)\n", |
| 197 | flavor); |
J. Bruce Fields | 6a19275 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 198 | err = PTR_ERR(auth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | goto out_no_auth; |
| 200 | } |
| 201 | |
| 202 | /* save the nodename */ |
Serge E. Hallyn | e9ff399 | 2006-10-02 02:18:11 -0700 | [diff] [blame] | 203 | clnt->cl_nodelen = strlen(utsname()->nodename); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | if (clnt->cl_nodelen > UNX_MAXNODENAME) |
| 205 | clnt->cl_nodelen = UNX_MAXNODENAME; |
Serge E. Hallyn | e9ff399 | 2006-10-02 02:18:11 -0700 | [diff] [blame] | 206 | memcpy(clnt->cl_nodename, utsname()->nodename, clnt->cl_nodelen); |
Trond Myklebust | 6529eba | 2007-06-14 16:40:14 -0400 | [diff] [blame] | 207 | rpc_register_client(clnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | return clnt; |
| 209 | |
| 210 | out_no_auth: |
Trond Myklebust | 5428154 | 2006-03-20 13:44:49 -0500 | [diff] [blame] | 211 | if (!IS_ERR(clnt->cl_dentry)) { |
Trond Myklebust | dff02cc | 2006-07-31 14:17:18 -0700 | [diff] [blame] | 212 | rpc_rmdir(clnt->cl_dentry); |
Trond Myklebust | 5428154 | 2006-03-20 13:44:49 -0500 | [diff] [blame] | 213 | rpc_put_mount(); |
| 214 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | out_no_path: |
Trond Myklebust | 23bf85b | 2006-11-21 10:40:23 -0500 | [diff] [blame] | 216 | rpc_free_iostats(clnt->cl_metrics); |
| 217 | out_no_stats: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | if (clnt->cl_server != clnt->cl_inline_name) |
| 219 | kfree(clnt->cl_server); |
| 220 | kfree(clnt); |
| 221 | out_err: |
Trond Myklebust | 6b6ca86 | 2006-09-05 12:55:57 -0400 | [diff] [blame] | 222 | xprt_put(xprt); |
Adrian Bunk | 712917d | 2006-03-13 21:20:47 -0800 | [diff] [blame] | 223 | out_no_xprt: |
Trond Myklebust | 4ada539 | 2007-06-14 17:26:17 -0400 | [diff] [blame] | 224 | rpciod_down(); |
| 225 | out_no_rpciod: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | return ERR_PTR(err); |
| 227 | } |
| 228 | |
Chuck Lever | c286676 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 229 | /* |
| 230 | * rpc_create - create an RPC client and transport with one call |
| 231 | * @args: rpc_clnt create argument structure |
| 232 | * |
| 233 | * Creates and initializes an RPC transport and an RPC client. |
| 234 | * |
| 235 | * It can ping the server in order to determine if it is up, and to see if |
| 236 | * it supports this program and version. RPC_CLNT_CREATE_NOPING disables |
| 237 | * this behavior so asynchronous tasks can also use rpc_create. |
| 238 | */ |
| 239 | struct rpc_clnt *rpc_create(struct rpc_create_args *args) |
| 240 | { |
| 241 | struct rpc_xprt *xprt; |
| 242 | struct rpc_clnt *clnt; |
\"Talpey, Thomas\ | 3c341b0b | 2007-09-10 13:47:07 -0400 | [diff] [blame] | 243 | struct xprt_create xprtargs = { |
\"Talpey, Thomas\ | 4fa016e | 2007-09-10 13:47:57 -0400 | [diff] [blame] | 244 | .ident = args->protocol, |
Frank van Maarseveen | d3bc9a1 | 2007-07-09 22:23:35 +0200 | [diff] [blame] | 245 | .srcaddr = args->saddress, |
Frank van Maarseveen | 96802a0 | 2007-07-08 13:08:54 +0200 | [diff] [blame] | 246 | .dstaddr = args->address, |
| 247 | .addrlen = args->addrsize, |
| 248 | .timeout = args->timeout |
| 249 | }; |
Chuck Lever | 43780b8 | 2007-07-01 12:13:22 -0400 | [diff] [blame] | 250 | char servername[20]; |
Chuck Lever | c286676 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 251 | |
Frank van Maarseveen | 96802a0 | 2007-07-08 13:08:54 +0200 | [diff] [blame] | 252 | xprt = xprt_create_transport(&xprtargs); |
Chuck Lever | c286676 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 253 | if (IS_ERR(xprt)) |
| 254 | return (struct rpc_clnt *)xprt; |
| 255 | |
| 256 | /* |
Chuck Lever | 43780b8 | 2007-07-01 12:13:22 -0400 | [diff] [blame] | 257 | * If the caller chooses not to specify a hostname, whip |
| 258 | * up a string representation of the passed-in address. |
| 259 | */ |
| 260 | if (args->servername == NULL) { |
| 261 | struct sockaddr_in *addr = |
J. Bruce Fields | afde94f | 2007-09-26 14:38:08 -0400 | [diff] [blame] | 262 | (struct sockaddr_in *) args->address; |
Chuck Lever | 43780b8 | 2007-07-01 12:13:22 -0400 | [diff] [blame] | 263 | snprintf(servername, sizeof(servername), NIPQUAD_FMT, |
| 264 | NIPQUAD(addr->sin_addr.s_addr)); |
| 265 | args->servername = servername; |
| 266 | } |
| 267 | |
| 268 | /* |
Chuck Lever | c286676 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 269 | * By default, kernel RPC client connects from a reserved port. |
| 270 | * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters, |
| 271 | * but it is always enabled for rpciod, which handles the connect |
| 272 | * operation. |
| 273 | */ |
| 274 | xprt->resvport = 1; |
| 275 | if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT) |
| 276 | xprt->resvport = 0; |
| 277 | |
Chuck Lever | c286676 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 278 | clnt = rpc_new_client(xprt, args->servername, args->program, |
| 279 | args->version, args->authflavor); |
| 280 | if (IS_ERR(clnt)) |
| 281 | return clnt; |
| 282 | |
| 283 | if (!(args->flags & RPC_CLNT_CREATE_NOPING)) { |
Matthew Wilcox | 150030b | 2007-12-06 16:24:39 -0500 | [diff] [blame^] | 284 | int err = rpc_ping(clnt, RPC_TASK_SOFT); |
Chuck Lever | c286676 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 285 | if (err != 0) { |
| 286 | rpc_shutdown_client(clnt); |
| 287 | return ERR_PTR(err); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | clnt->cl_softrtry = 1; |
| 292 | if (args->flags & RPC_CLNT_CREATE_HARDRTRY) |
| 293 | clnt->cl_softrtry = 0; |
| 294 | |
Chuck Lever | c286676 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 295 | if (args->flags & RPC_CLNT_CREATE_AUTOBIND) |
| 296 | clnt->cl_autobind = 1; |
Chuck Lever | 43d78ef | 2007-02-06 18:26:11 -0500 | [diff] [blame] | 297 | if (args->flags & RPC_CLNT_CREATE_DISCRTRY) |
| 298 | clnt->cl_discrtry = 1; |
Chuck Lever | c286676 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 299 | |
| 300 | return clnt; |
| 301 | } |
Chuck Lever | b86acd5 | 2006-08-22 20:06:22 -0400 | [diff] [blame] | 302 | EXPORT_SYMBOL_GPL(rpc_create); |
Chuck Lever | c286676 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | /* |
| 305 | * This function clones the RPC client structure. It allows us to share the |
| 306 | * same transport while varying parameters such as the authentication |
| 307 | * flavour. |
| 308 | */ |
| 309 | struct rpc_clnt * |
| 310 | rpc_clone_client(struct rpc_clnt *clnt) |
| 311 | { |
| 312 | struct rpc_clnt *new; |
Trond Myklebust | 3e32a5d | 2006-11-16 11:37:27 -0500 | [diff] [blame] | 313 | int err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | |
Arnaldo Carvalho de Melo | e69062b | 2006-11-21 01:21:34 -0200 | [diff] [blame] | 315 | new = kmemdup(clnt, sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | if (!new) |
| 317 | goto out_no_clnt; |
Trond Myklebust | d431a555 | 2007-06-17 17:07:54 -0400 | [diff] [blame] | 318 | new->cl_parent = clnt; |
| 319 | /* Turn off autobind on clones */ |
| 320 | new->cl_autobind = 0; |
| 321 | INIT_LIST_HEAD(&new->cl_tasks); |
| 322 | spin_lock_init(&new->cl_lock); |
| 323 | rpc_init_rtt(&new->cl_rtt_default, clnt->cl_xprt->timeout.to_initval); |
Trond Myklebust | 23bf85b | 2006-11-21 10:40:23 -0500 | [diff] [blame] | 324 | new->cl_metrics = rpc_alloc_iostats(clnt); |
| 325 | if (new->cl_metrics == NULL) |
| 326 | goto out_no_stats; |
Trond Myklebust | 34f52e3 | 2007-06-14 16:40:31 -0400 | [diff] [blame] | 327 | kref_init(&new->cl_kref); |
Trond Myklebust | 3e32a5d | 2006-11-16 11:37:27 -0500 | [diff] [blame] | 328 | err = rpc_setup_pipedir(new, clnt->cl_program->pipe_dir_name); |
| 329 | if (err != 0) |
| 330 | goto out_no_path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | if (new->cl_auth) |
| 332 | atomic_inc(&new->cl_auth->au_count); |
Trond Myklebust | d431a555 | 2007-06-17 17:07:54 -0400 | [diff] [blame] | 333 | xprt_get(clnt->cl_xprt); |
| 334 | kref_get(&clnt->cl_kref); |
Trond Myklebust | 6529eba | 2007-06-14 16:40:14 -0400 | [diff] [blame] | 335 | rpc_register_client(new); |
Trond Myklebust | 4ada539 | 2007-06-14 17:26:17 -0400 | [diff] [blame] | 336 | rpciod_up(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | return new; |
Trond Myklebust | 3e32a5d | 2006-11-16 11:37:27 -0500 | [diff] [blame] | 338 | out_no_path: |
| 339 | rpc_free_iostats(new->cl_metrics); |
Trond Myklebust | 23bf85b | 2006-11-21 10:40:23 -0500 | [diff] [blame] | 340 | out_no_stats: |
| 341 | kfree(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | out_no_clnt: |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 343 | dprintk("RPC: %s: returned error %d\n", __FUNCTION__, err); |
Trond Myklebust | 3e32a5d | 2006-11-16 11:37:27 -0500 | [diff] [blame] | 344 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | /* |
| 348 | * Properly shut down an RPC client, terminating all outstanding |
Trond Myklebust | 90c5755 | 2007-06-09 19:49:36 -0400 | [diff] [blame] | 349 | * requests. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | */ |
Trond Myklebust | 4c402b4 | 2007-06-14 16:40:32 -0400 | [diff] [blame] | 351 | void rpc_shutdown_client(struct rpc_clnt *clnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | { |
Trond Myklebust | 34f52e3 | 2007-06-14 16:40:31 -0400 | [diff] [blame] | 353 | dprintk("RPC: shutting down %s client for %s\n", |
| 354 | clnt->cl_protname, clnt->cl_server); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | |
Trond Myklebust | 34f52e3 | 2007-06-14 16:40:31 -0400 | [diff] [blame] | 356 | while (!list_empty(&clnt->cl_tasks)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | rpc_killall_tasks(clnt); |
Ingo Molnar | 532347e | 2006-01-09 20:52:53 -0800 | [diff] [blame] | 358 | wait_event_timeout(destroy_wait, |
Trond Myklebust | 34f52e3 | 2007-06-14 16:40:31 -0400 | [diff] [blame] | 359 | list_empty(&clnt->cl_tasks), 1*HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Trond Myklebust | 4c402b4 | 2007-06-14 16:40:32 -0400 | [diff] [blame] | 362 | rpc_release_client(clnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | /* |
Trond Myklebust | 34f52e3 | 2007-06-14 16:40:31 -0400 | [diff] [blame] | 366 | * Free an RPC client |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | */ |
Trond Myklebust | 34f52e3 | 2007-06-14 16:40:31 -0400 | [diff] [blame] | 368 | static void |
| 369 | rpc_free_client(struct kref *kref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | { |
Trond Myklebust | 34f52e3 | 2007-06-14 16:40:31 -0400 | [diff] [blame] | 371 | struct rpc_clnt *clnt = container_of(kref, struct rpc_clnt, cl_kref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 373 | dprintk("RPC: destroying %s client for %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | clnt->cl_protname, clnt->cl_server); |
Trond Myklebust | 8f8e7a5 | 2006-08-14 13:11:15 -0400 | [diff] [blame] | 375 | if (!IS_ERR(clnt->cl_dentry)) { |
Trond Myklebust | dff02cc | 2006-07-31 14:17:18 -0700 | [diff] [blame] | 376 | rpc_rmdir(clnt->cl_dentry); |
Trond Myklebust | 8f8e7a5 | 2006-08-14 13:11:15 -0400 | [diff] [blame] | 377 | rpc_put_mount(); |
| 378 | } |
Trond Myklebust | 3e32a5d | 2006-11-16 11:37:27 -0500 | [diff] [blame] | 379 | if (clnt->cl_parent != clnt) { |
Trond Myklebust | 8ad7c89 | 2007-06-14 16:40:32 -0400 | [diff] [blame] | 380 | rpc_release_client(clnt->cl_parent); |
Trond Myklebust | 3e32a5d | 2006-11-16 11:37:27 -0500 | [diff] [blame] | 381 | goto out_free; |
| 382 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | if (clnt->cl_server != clnt->cl_inline_name) |
| 384 | kfree(clnt->cl_server); |
| 385 | out_free: |
Trond Myklebust | 6529eba | 2007-06-14 16:40:14 -0400 | [diff] [blame] | 386 | rpc_unregister_client(clnt); |
Chuck Lever | 11c556b | 2006-03-20 13:44:22 -0500 | [diff] [blame] | 387 | rpc_free_iostats(clnt->cl_metrics); |
| 388 | clnt->cl_metrics = NULL; |
Trond Myklebust | 6b6ca86 | 2006-09-05 12:55:57 -0400 | [diff] [blame] | 389 | xprt_put(clnt->cl_xprt); |
Trond Myklebust | 4ada539 | 2007-06-14 17:26:17 -0400 | [diff] [blame] | 390 | rpciod_down(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | kfree(clnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | /* |
Trond Myklebust | 1dd17ec | 2007-06-26 16:57:41 -0400 | [diff] [blame] | 395 | * Free an RPC client |
| 396 | */ |
| 397 | static void |
| 398 | rpc_free_auth(struct kref *kref) |
| 399 | { |
| 400 | struct rpc_clnt *clnt = container_of(kref, struct rpc_clnt, cl_kref); |
| 401 | |
| 402 | if (clnt->cl_auth == NULL) { |
| 403 | rpc_free_client(kref); |
| 404 | return; |
| 405 | } |
| 406 | |
| 407 | /* |
| 408 | * Note: RPCSEC_GSS may need to send NULL RPC calls in order to |
| 409 | * release remaining GSS contexts. This mechanism ensures |
| 410 | * that it can do so safely. |
| 411 | */ |
| 412 | kref_init(kref); |
| 413 | rpcauth_release(clnt->cl_auth); |
| 414 | clnt->cl_auth = NULL; |
| 415 | kref_put(kref, rpc_free_client); |
| 416 | } |
| 417 | |
| 418 | /* |
Trond Myklebust | 34f52e3 | 2007-06-14 16:40:31 -0400 | [diff] [blame] | 419 | * Release reference to the RPC client |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | */ |
| 421 | void |
| 422 | rpc_release_client(struct rpc_clnt *clnt) |
| 423 | { |
Trond Myklebust | 34f52e3 | 2007-06-14 16:40:31 -0400 | [diff] [blame] | 424 | dprintk("RPC: rpc_release_client(%p)\n", clnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
Trond Myklebust | 34f52e3 | 2007-06-14 16:40:31 -0400 | [diff] [blame] | 426 | if (list_empty(&clnt->cl_tasks)) |
| 427 | wake_up(&destroy_wait); |
Trond Myklebust | 1dd17ec | 2007-06-26 16:57:41 -0400 | [diff] [blame] | 428 | kref_put(&clnt->cl_kref, rpc_free_auth); |
Trond Myklebust | 34f52e3 | 2007-06-14 16:40:31 -0400 | [diff] [blame] | 429 | } |
| 430 | |
Andreas Gruenbacher | 007e251 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 431 | /** |
| 432 | * rpc_bind_new_program - bind a new RPC program to an existing client |
| 433 | * @old - old rpc_client |
| 434 | * @program - rpc program to set |
| 435 | * @vers - rpc program version |
| 436 | * |
| 437 | * Clones the rpc client and sets up a new RPC program. This is mainly |
| 438 | * of use for enabling different RPC programs to share the same transport. |
| 439 | * The Sun NFSv2/v3 ACL protocol can do this. |
| 440 | */ |
| 441 | struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old, |
| 442 | struct rpc_program *program, |
Chuck Lever | 89eb21c | 2007-09-11 18:00:09 -0400 | [diff] [blame] | 443 | u32 vers) |
Andreas Gruenbacher | 007e251 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 444 | { |
| 445 | struct rpc_clnt *clnt; |
| 446 | struct rpc_version *version; |
| 447 | int err; |
| 448 | |
| 449 | BUG_ON(vers >= program->nrvers || !program->version[vers]); |
| 450 | version = program->version[vers]; |
| 451 | clnt = rpc_clone_client(old); |
| 452 | if (IS_ERR(clnt)) |
| 453 | goto out; |
| 454 | clnt->cl_procinfo = version->procs; |
| 455 | clnt->cl_maxproc = version->nrprocs; |
| 456 | clnt->cl_protname = program->name; |
| 457 | clnt->cl_prog = program->number; |
| 458 | clnt->cl_vers = version->number; |
| 459 | clnt->cl_stats = program->stats; |
Matthew Wilcox | 150030b | 2007-12-06 16:24:39 -0500 | [diff] [blame^] | 460 | err = rpc_ping(clnt, RPC_TASK_SOFT); |
Andreas Gruenbacher | 007e251 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 461 | if (err != 0) { |
| 462 | rpc_shutdown_client(clnt); |
| 463 | clnt = ERR_PTR(err); |
| 464 | } |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 465 | out: |
Andreas Gruenbacher | 007e251 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 466 | return clnt; |
| 467 | } |
| 468 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | /* |
| 470 | * Default callback for async RPC calls |
| 471 | */ |
| 472 | static void |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 473 | rpc_default_callback(struct rpc_task *task, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | { |
| 475 | } |
| 476 | |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 477 | static const struct rpc_call_ops rpc_default_ops = { |
| 478 | .rpc_call_done = rpc_default_callback, |
| 479 | }; |
| 480 | |
Trond Myklebust | 6e5b70e | 2007-06-12 10:02:37 -0400 | [diff] [blame] | 481 | static |
| 482 | struct rpc_task *rpc_do_run_task(struct rpc_clnt *clnt, |
| 483 | struct rpc_message *msg, |
| 484 | int flags, |
| 485 | const struct rpc_call_ops *ops, |
| 486 | void *data) |
| 487 | { |
| 488 | struct rpc_task *task, *ret; |
Trond Myklebust | 6e5b70e | 2007-06-12 10:02:37 -0400 | [diff] [blame] | 489 | |
| 490 | task = rpc_new_task(clnt, flags, ops, data); |
| 491 | if (task == NULL) { |
| 492 | rpc_release_calldata(ops, data); |
| 493 | return ERR_PTR(-ENOMEM); |
| 494 | } |
| 495 | |
| 496 | /* Mask signals on synchronous RPC calls and RPCSEC_GSS upcalls */ |
Trond Myklebust | 6e5b70e | 2007-06-12 10:02:37 -0400 | [diff] [blame] | 497 | if (msg != NULL) { |
| 498 | rpc_call_setup(task, msg, 0); |
| 499 | if (task->tk_status != 0) { |
| 500 | ret = ERR_PTR(task->tk_status); |
| 501 | rpc_put_task(task); |
| 502 | goto out; |
| 503 | } |
| 504 | } |
| 505 | atomic_inc(&task->tk_count); |
| 506 | rpc_execute(task); |
| 507 | ret = task; |
| 508 | out: |
Trond Myklebust | 6e5b70e | 2007-06-12 10:02:37 -0400 | [diff] [blame] | 509 | return ret; |
| 510 | } |
| 511 | |
| 512 | /** |
| 513 | * rpc_call_sync - Perform a synchronous RPC call |
| 514 | * @clnt: pointer to RPC client |
| 515 | * @msg: RPC call parameters |
| 516 | * @flags: RPC call flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | */ |
| 518 | int rpc_call_sync(struct rpc_clnt *clnt, struct rpc_message *msg, int flags) |
| 519 | { |
| 520 | struct rpc_task *task; |
Trond Myklebust | 6e5b70e | 2007-06-12 10:02:37 -0400 | [diff] [blame] | 521 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | BUG_ON(flags & RPC_TASK_ASYNC); |
| 524 | |
Trond Myklebust | 6e5b70e | 2007-06-12 10:02:37 -0400 | [diff] [blame] | 525 | task = rpc_do_run_task(clnt, msg, flags, &rpc_default_ops, NULL); |
| 526 | if (IS_ERR(task)) |
| 527 | return PTR_ERR(task); |
Trond Myklebust | e60859a | 2006-01-03 09:55:10 +0100 | [diff] [blame] | 528 | status = task->tk_status; |
Trond Myklebust | bde8f00 | 2007-01-24 11:54:53 -0800 | [diff] [blame] | 529 | rpc_put_task(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | return status; |
| 531 | } |
| 532 | |
Trond Myklebust | 6e5b70e | 2007-06-12 10:02:37 -0400 | [diff] [blame] | 533 | /** |
| 534 | * rpc_call_async - Perform an asynchronous RPC call |
| 535 | * @clnt: pointer to RPC client |
| 536 | * @msg: RPC call parameters |
| 537 | * @flags: RPC call flags |
| 538 | * @ops: RPC call ops |
| 539 | * @data: user call data |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | */ |
| 541 | int |
| 542 | rpc_call_async(struct rpc_clnt *clnt, struct rpc_message *msg, int flags, |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 543 | const struct rpc_call_ops *tk_ops, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | { |
| 545 | struct rpc_task *task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | |
Trond Myklebust | 6e5b70e | 2007-06-12 10:02:37 -0400 | [diff] [blame] | 547 | task = rpc_do_run_task(clnt, msg, flags|RPC_TASK_ASYNC, tk_ops, data); |
| 548 | if (IS_ERR(task)) |
| 549 | return PTR_ERR(task); |
| 550 | rpc_put_task(task); |
| 551 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Trond Myklebust | 6e5b70e | 2007-06-12 10:02:37 -0400 | [diff] [blame] | 554 | /** |
| 555 | * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it |
| 556 | * @clnt: pointer to RPC client |
| 557 | * @flags: RPC flags |
| 558 | * @ops: RPC call ops |
| 559 | * @data: user call data |
| 560 | */ |
| 561 | struct rpc_task *rpc_run_task(struct rpc_clnt *clnt, int flags, |
| 562 | const struct rpc_call_ops *tk_ops, |
| 563 | void *data) |
| 564 | { |
| 565 | return rpc_do_run_task(clnt, NULL, flags, tk_ops, data); |
| 566 | } |
| 567 | EXPORT_SYMBOL(rpc_run_task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | |
| 569 | void |
| 570 | rpc_call_setup(struct rpc_task *task, struct rpc_message *msg, int flags) |
| 571 | { |
| 572 | task->tk_msg = *msg; |
| 573 | task->tk_flags |= flags; |
| 574 | /* Bind the user cred */ |
| 575 | if (task->tk_msg.rpc_cred != NULL) |
| 576 | rpcauth_holdcred(task); |
| 577 | else |
| 578 | rpcauth_bindcred(task); |
| 579 | |
| 580 | if (task->tk_status == 0) |
| 581 | task->tk_action = call_start; |
| 582 | else |
Trond Myklebust | abbcf28 | 2006-01-03 09:55:03 +0100 | [diff] [blame] | 583 | task->tk_action = rpc_exit_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | } |
| 585 | |
Chuck Lever | ed39440 | 2006-08-22 20:06:17 -0400 | [diff] [blame] | 586 | /** |
| 587 | * rpc_peeraddr - extract remote peer address from clnt's xprt |
| 588 | * @clnt: RPC client structure |
| 589 | * @buf: target buffer |
| 590 | * @size: length of target buffer |
| 591 | * |
| 592 | * Returns the number of bytes that are actually in the stored address. |
| 593 | */ |
| 594 | size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize) |
| 595 | { |
| 596 | size_t bytes; |
| 597 | struct rpc_xprt *xprt = clnt->cl_xprt; |
| 598 | |
| 599 | bytes = sizeof(xprt->addr); |
| 600 | if (bytes > bufsize) |
| 601 | bytes = bufsize; |
| 602 | memcpy(buf, &clnt->cl_xprt->addr, bytes); |
Chuck Lever | c4efcb1 | 2006-08-22 20:06:19 -0400 | [diff] [blame] | 603 | return xprt->addrlen; |
Chuck Lever | ed39440 | 2006-08-22 20:06:17 -0400 | [diff] [blame] | 604 | } |
Chuck Lever | b86acd5 | 2006-08-22 20:06:22 -0400 | [diff] [blame] | 605 | EXPORT_SYMBOL_GPL(rpc_peeraddr); |
Chuck Lever | ed39440 | 2006-08-22 20:06:17 -0400 | [diff] [blame] | 606 | |
Chuck Lever | f425eba | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 607 | /** |
| 608 | * rpc_peeraddr2str - return remote peer address in printable format |
| 609 | * @clnt: RPC client structure |
| 610 | * @format: address format |
| 611 | * |
| 612 | */ |
| 613 | char *rpc_peeraddr2str(struct rpc_clnt *clnt, enum rpc_display_format_t format) |
| 614 | { |
| 615 | struct rpc_xprt *xprt = clnt->cl_xprt; |
Chuck Lever | 7559c7a | 2006-12-05 16:35:37 -0500 | [diff] [blame] | 616 | |
| 617 | if (xprt->address_strings[format] != NULL) |
| 618 | return xprt->address_strings[format]; |
| 619 | else |
| 620 | return "unprintable"; |
Chuck Lever | f425eba | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 621 | } |
Chuck Lever | b86acd5 | 2006-08-22 20:06:22 -0400 | [diff] [blame] | 622 | EXPORT_SYMBOL_GPL(rpc_peeraddr2str); |
Chuck Lever | f425eba | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 623 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | void |
| 625 | rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize) |
| 626 | { |
| 627 | struct rpc_xprt *xprt = clnt->cl_xprt; |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 628 | if (xprt->ops->set_buffer_size) |
| 629 | xprt->ops->set_buffer_size(xprt, sndsize, rcvsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | /* |
| 633 | * Return size of largest payload RPC client can support, in bytes |
| 634 | * |
| 635 | * For stream transports, this is one RPC record fragment (see RFC |
| 636 | * 1831), as we don't support multi-record requests yet. For datagram |
| 637 | * transports, this is the size of an IP packet minus the IP, UDP, and |
| 638 | * RPC header sizes. |
| 639 | */ |
| 640 | size_t rpc_max_payload(struct rpc_clnt *clnt) |
| 641 | { |
| 642 | return clnt->cl_xprt->max_payload; |
| 643 | } |
Chuck Lever | b86acd5 | 2006-08-22 20:06:22 -0400 | [diff] [blame] | 644 | EXPORT_SYMBOL_GPL(rpc_max_payload); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | |
Chuck Lever | 35f5a42 | 2006-01-03 09:55:50 +0100 | [diff] [blame] | 646 | /** |
| 647 | * rpc_force_rebind - force transport to check that remote port is unchanged |
| 648 | * @clnt: client to rebind |
| 649 | * |
| 650 | */ |
| 651 | void rpc_force_rebind(struct rpc_clnt *clnt) |
| 652 | { |
| 653 | if (clnt->cl_autobind) |
Chuck Lever | ec739ef | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 654 | xprt_clear_bound(clnt->cl_xprt); |
Chuck Lever | 35f5a42 | 2006-01-03 09:55:50 +0100 | [diff] [blame] | 655 | } |
Chuck Lever | b86acd5 | 2006-08-22 20:06:22 -0400 | [diff] [blame] | 656 | EXPORT_SYMBOL_GPL(rpc_force_rebind); |
Chuck Lever | 35f5a42 | 2006-01-03 09:55:50 +0100 | [diff] [blame] | 657 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | /* |
| 659 | * Restart an (async) RPC call. Usually called from within the |
| 660 | * exit handler. |
| 661 | */ |
| 662 | void |
| 663 | rpc_restart_call(struct rpc_task *task) |
| 664 | { |
| 665 | if (RPC_ASSASSINATED(task)) |
| 666 | return; |
| 667 | |
| 668 | task->tk_action = call_start; |
| 669 | } |
| 670 | |
| 671 | /* |
| 672 | * 0. Initial state |
| 673 | * |
| 674 | * Other FSM states can be visited zero or more times, but |
| 675 | * this state is visited exactly once for each RPC. |
| 676 | */ |
| 677 | static void |
| 678 | call_start(struct rpc_task *task) |
| 679 | { |
| 680 | struct rpc_clnt *clnt = task->tk_client; |
| 681 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 682 | dprintk("RPC: %5u call_start %s%d proc %d (%s)\n", task->tk_pid, |
| 683 | clnt->cl_protname, clnt->cl_vers, |
| 684 | task->tk_msg.rpc_proc->p_proc, |
| 685 | (RPC_IS_ASYNC(task) ? "async" : "sync")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | |
| 687 | /* Increment call count */ |
| 688 | task->tk_msg.rpc_proc->p_count++; |
| 689 | clnt->cl_stats->rpccnt++; |
| 690 | task->tk_action = call_reserve; |
| 691 | } |
| 692 | |
| 693 | /* |
| 694 | * 1. Reserve an RPC call slot |
| 695 | */ |
| 696 | static void |
| 697 | call_reserve(struct rpc_task *task) |
| 698 | { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 699 | dprint_status(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | |
| 701 | if (!rpcauth_uptodatecred(task)) { |
| 702 | task->tk_action = call_refresh; |
| 703 | return; |
| 704 | } |
| 705 | |
| 706 | task->tk_status = 0; |
| 707 | task->tk_action = call_reserveresult; |
| 708 | xprt_reserve(task); |
| 709 | } |
| 710 | |
| 711 | /* |
| 712 | * 1b. Grok the result of xprt_reserve() |
| 713 | */ |
| 714 | static void |
| 715 | call_reserveresult(struct rpc_task *task) |
| 716 | { |
| 717 | int status = task->tk_status; |
| 718 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 719 | dprint_status(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | |
| 721 | /* |
| 722 | * After a call to xprt_reserve(), we must have either |
| 723 | * a request slot or else an error status. |
| 724 | */ |
| 725 | task->tk_status = 0; |
| 726 | if (status >= 0) { |
| 727 | if (task->tk_rqstp) { |
| 728 | task->tk_action = call_allocate; |
| 729 | return; |
| 730 | } |
| 731 | |
| 732 | printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n", |
| 733 | __FUNCTION__, status); |
| 734 | rpc_exit(task, -EIO); |
| 735 | return; |
| 736 | } |
| 737 | |
| 738 | /* |
| 739 | * Even though there was an error, we may have acquired |
| 740 | * a request slot somehow. Make sure not to leak it. |
| 741 | */ |
| 742 | if (task->tk_rqstp) { |
| 743 | printk(KERN_ERR "%s: status=%d, request allocated anyway\n", |
| 744 | __FUNCTION__, status); |
| 745 | xprt_release(task); |
| 746 | } |
| 747 | |
| 748 | switch (status) { |
| 749 | case -EAGAIN: /* woken up; retry */ |
| 750 | task->tk_action = call_reserve; |
| 751 | return; |
| 752 | case -EIO: /* probably a shutdown */ |
| 753 | break; |
| 754 | default: |
| 755 | printk(KERN_ERR "%s: unrecognized error %d, exiting\n", |
| 756 | __FUNCTION__, status); |
| 757 | break; |
| 758 | } |
| 759 | rpc_exit(task, status); |
| 760 | } |
| 761 | |
| 762 | /* |
| 763 | * 2. Allocate the buffer. For details, see sched.c:rpc_malloc. |
Chuck Lever | 0210714 | 2006-01-03 09:55:49 +0100 | [diff] [blame] | 764 | * (Note: buffer memory is freed in xprt_release). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | */ |
| 766 | static void |
| 767 | call_allocate(struct rpc_task *task) |
| 768 | { |
Trond Myklebust | 1be27f3 | 2007-06-27 14:29:04 -0400 | [diff] [blame] | 769 | unsigned int slack = task->tk_msg.rpc_cred->cr_auth->au_cslack; |
Chuck Lever | 0210714 | 2006-01-03 09:55:49 +0100 | [diff] [blame] | 770 | struct rpc_rqst *req = task->tk_rqstp; |
| 771 | struct rpc_xprt *xprt = task->tk_xprt; |
Chuck Lever | 2bea90d | 2007-03-29 16:47:53 -0400 | [diff] [blame] | 772 | struct rpc_procinfo *proc = task->tk_msg.rpc_proc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 774 | dprint_status(task); |
| 775 | |
Chuck Lever | 2bea90d | 2007-03-29 16:47:53 -0400 | [diff] [blame] | 776 | task->tk_status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | task->tk_action = call_bind; |
Chuck Lever | 2bea90d | 2007-03-29 16:47:53 -0400 | [diff] [blame] | 778 | |
Chuck Lever | 0210714 | 2006-01-03 09:55:49 +0100 | [diff] [blame] | 779 | if (req->rq_buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | return; |
| 781 | |
Chuck Lever | 2bea90d | 2007-03-29 16:47:53 -0400 | [diff] [blame] | 782 | if (proc->p_proc != 0) { |
| 783 | BUG_ON(proc->p_arglen == 0); |
| 784 | if (proc->p_decode != NULL) |
| 785 | BUG_ON(proc->p_replen == 0); |
| 786 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | |
Chuck Lever | 2bea90d | 2007-03-29 16:47:53 -0400 | [diff] [blame] | 788 | /* |
| 789 | * Calculate the size (in quads) of the RPC call |
| 790 | * and reply headers, and convert both values |
| 791 | * to byte sizes. |
| 792 | */ |
| 793 | req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen; |
| 794 | req->rq_callsize <<= 2; |
| 795 | req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen; |
| 796 | req->rq_rcvsize <<= 2; |
| 797 | |
Chuck Lever | c5a4dd8 | 2007-03-29 16:47:58 -0400 | [diff] [blame] | 798 | req->rq_buffer = xprt->ops->buf_alloc(task, |
| 799 | req->rq_callsize + req->rq_rcvsize); |
Chuck Lever | 2bea90d | 2007-03-29 16:47:53 -0400 | [diff] [blame] | 800 | if (req->rq_buffer != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | return; |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 802 | |
| 803 | dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | |
Trond Myklebust | 14b218a | 2005-06-22 17:16:28 +0000 | [diff] [blame] | 805 | if (RPC_IS_ASYNC(task) || !signalled()) { |
Trond Myklebust | b6e9c71 | 2007-10-01 12:06:44 -0400 | [diff] [blame] | 806 | task->tk_action = call_allocate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | rpc_delay(task, HZ>>4); |
| 808 | return; |
| 809 | } |
| 810 | |
| 811 | rpc_exit(task, -ERESTARTSYS); |
| 812 | } |
| 813 | |
Trond Myklebust | 940e331 | 2005-11-09 21:45:24 -0500 | [diff] [blame] | 814 | static inline int |
| 815 | rpc_task_need_encode(struct rpc_task *task) |
| 816 | { |
| 817 | return task->tk_rqstp->rq_snd_buf.len == 0; |
| 818 | } |
| 819 | |
| 820 | static inline void |
| 821 | rpc_task_force_reencode(struct rpc_task *task) |
| 822 | { |
| 823 | task->tk_rqstp->rq_snd_buf.len = 0; |
| 824 | } |
| 825 | |
Chuck Lever | 2bea90d | 2007-03-29 16:47:53 -0400 | [diff] [blame] | 826 | static inline void |
| 827 | rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len) |
| 828 | { |
| 829 | buf->head[0].iov_base = start; |
| 830 | buf->head[0].iov_len = len; |
| 831 | buf->tail[0].iov_len = 0; |
| 832 | buf->page_len = 0; |
\"Talpey, Thomas\ | 4f22ccc | 2007-09-10 13:44:58 -0400 | [diff] [blame] | 833 | buf->flags = 0; |
Chuck Lever | 2bea90d | 2007-03-29 16:47:53 -0400 | [diff] [blame] | 834 | buf->len = 0; |
| 835 | buf->buflen = len; |
| 836 | } |
| 837 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | /* |
| 839 | * 3. Encode arguments of an RPC call |
| 840 | */ |
| 841 | static void |
| 842 | call_encode(struct rpc_task *task) |
| 843 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | struct rpc_rqst *req = task->tk_rqstp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | kxdrproc_t encode; |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 846 | __be32 *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 848 | dprint_status(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | |
Chuck Lever | 2bea90d | 2007-03-29 16:47:53 -0400 | [diff] [blame] | 850 | rpc_xdr_buf_init(&req->rq_snd_buf, |
| 851 | req->rq_buffer, |
| 852 | req->rq_callsize); |
| 853 | rpc_xdr_buf_init(&req->rq_rcv_buf, |
| 854 | (char *)req->rq_buffer + req->rq_callsize, |
| 855 | req->rq_rcvsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | |
| 857 | /* Encode header and provided arguments */ |
| 858 | encode = task->tk_msg.rpc_proc->p_encode; |
| 859 | if (!(p = call_header(task))) { |
| 860 | printk(KERN_INFO "RPC: call_header failed, exit EIO\n"); |
| 861 | rpc_exit(task, -EIO); |
| 862 | return; |
| 863 | } |
J. Bruce Fields | f368031 | 2005-10-13 16:54:48 -0400 | [diff] [blame] | 864 | if (encode == NULL) |
| 865 | return; |
| 866 | |
| 867 | task->tk_status = rpcauth_wrap_req(task, encode, req, p, |
| 868 | task->tk_msg.rpc_argp); |
| 869 | if (task->tk_status == -ENOMEM) { |
| 870 | /* XXX: Is this sane? */ |
| 871 | rpc_delay(task, 3*HZ); |
| 872 | task->tk_status = -EAGAIN; |
| 873 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | /* |
| 877 | * 4. Get the server port number if not yet set |
| 878 | */ |
| 879 | static void |
| 880 | call_bind(struct rpc_task *task) |
| 881 | { |
Chuck Lever | ec739ef | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 882 | struct rpc_xprt *xprt = task->tk_xprt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 884 | dprint_status(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | |
Chuck Lever | da35187 | 2005-08-11 16:25:11 -0400 | [diff] [blame] | 886 | task->tk_action = call_connect; |
Chuck Lever | ec739ef | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 887 | if (!xprt_bound(xprt)) { |
Chuck Lever | da35187 | 2005-08-11 16:25:11 -0400 | [diff] [blame] | 888 | task->tk_action = call_bind_status; |
Chuck Lever | ec739ef | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 889 | task->tk_timeout = xprt->bind_timeout; |
Chuck Lever | bbf7c1d | 2006-08-22 20:06:16 -0400 | [diff] [blame] | 890 | xprt->ops->rpcbind(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | } |
| 892 | } |
| 893 | |
| 894 | /* |
Chuck Lever | da35187 | 2005-08-11 16:25:11 -0400 | [diff] [blame] | 895 | * 4a. Sort out bind result |
| 896 | */ |
| 897 | static void |
| 898 | call_bind_status(struct rpc_task *task) |
| 899 | { |
Chuck Lever | 906462a | 2007-09-11 18:00:47 -0400 | [diff] [blame] | 900 | int status = -EIO; |
Chuck Lever | da35187 | 2005-08-11 16:25:11 -0400 | [diff] [blame] | 901 | |
| 902 | if (task->tk_status >= 0) { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 903 | dprint_status(task); |
Chuck Lever | da35187 | 2005-08-11 16:25:11 -0400 | [diff] [blame] | 904 | task->tk_status = 0; |
| 905 | task->tk_action = call_connect; |
| 906 | return; |
| 907 | } |
| 908 | |
| 909 | switch (task->tk_status) { |
Chuck Lever | 2429cbf | 2007-09-11 18:00:41 -0400 | [diff] [blame] | 910 | case -EAGAIN: |
| 911 | dprintk("RPC: %5u rpcbind waiting for another request " |
| 912 | "to finish\n", task->tk_pid); |
| 913 | /* avoid busy-waiting here -- could be a network outage. */ |
| 914 | rpc_delay(task, 5*HZ); |
| 915 | goto retry_timeout; |
Chuck Lever | da35187 | 2005-08-11 16:25:11 -0400 | [diff] [blame] | 916 | case -EACCES: |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 917 | dprintk("RPC: %5u remote rpcbind: RPC program/version " |
| 918 | "unavailable\n", task->tk_pid); |
Chuck Lever | b79dc8c | 2007-09-11 18:00:52 -0400 | [diff] [blame] | 919 | /* fail immediately if this is an RPC ping */ |
| 920 | if (task->tk_msg.rpc_proc->p_proc == 0) { |
| 921 | status = -EOPNOTSUPP; |
| 922 | break; |
| 923 | } |
Chuck Lever | ea635a5 | 2005-10-06 23:12:58 -0400 | [diff] [blame] | 924 | rpc_delay(task, 3*HZ); |
Trond Myklebust | da45828e | 2006-08-31 15:44:52 -0400 | [diff] [blame] | 925 | goto retry_timeout; |
Chuck Lever | da35187 | 2005-08-11 16:25:11 -0400 | [diff] [blame] | 926 | case -ETIMEDOUT: |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 927 | dprintk("RPC: %5u rpcbind request timed out\n", |
Chuck Lever | da35187 | 2005-08-11 16:25:11 -0400 | [diff] [blame] | 928 | task->tk_pid); |
Trond Myklebust | da45828e | 2006-08-31 15:44:52 -0400 | [diff] [blame] | 929 | goto retry_timeout; |
Chuck Lever | da35187 | 2005-08-11 16:25:11 -0400 | [diff] [blame] | 930 | case -EPFNOSUPPORT: |
Chuck Lever | 906462a | 2007-09-11 18:00:47 -0400 | [diff] [blame] | 931 | /* server doesn't support any rpcbind version we know of */ |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 932 | dprintk("RPC: %5u remote rpcbind service unavailable\n", |
Chuck Lever | da35187 | 2005-08-11 16:25:11 -0400 | [diff] [blame] | 933 | task->tk_pid); |
| 934 | break; |
| 935 | case -EPROTONOSUPPORT: |
Chuck Lever | 00a6e7b | 2007-03-29 16:48:33 -0400 | [diff] [blame] | 936 | dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n", |
Chuck Lever | da35187 | 2005-08-11 16:25:11 -0400 | [diff] [blame] | 937 | task->tk_pid); |
Chuck Lever | 00a6e7b | 2007-03-29 16:48:33 -0400 | [diff] [blame] | 938 | task->tk_status = 0; |
| 939 | task->tk_action = call_bind; |
| 940 | return; |
Chuck Lever | da35187 | 2005-08-11 16:25:11 -0400 | [diff] [blame] | 941 | default: |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 942 | dprintk("RPC: %5u unrecognized rpcbind error (%d)\n", |
Chuck Lever | da35187 | 2005-08-11 16:25:11 -0400 | [diff] [blame] | 943 | task->tk_pid, -task->tk_status); |
Chuck Lever | da35187 | 2005-08-11 16:25:11 -0400 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | rpc_exit(task, status); |
| 947 | return; |
| 948 | |
Trond Myklebust | da45828e | 2006-08-31 15:44:52 -0400 | [diff] [blame] | 949 | retry_timeout: |
| 950 | task->tk_action = call_timeout; |
Chuck Lever | da35187 | 2005-08-11 16:25:11 -0400 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | /* |
| 954 | * 4b. Connect to the RPC server |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | */ |
| 956 | static void |
| 957 | call_connect(struct rpc_task *task) |
| 958 | { |
Chuck Lever | da35187 | 2005-08-11 16:25:11 -0400 | [diff] [blame] | 959 | struct rpc_xprt *xprt = task->tk_xprt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 961 | dprintk("RPC: %5u call_connect xprt %p %s connected\n", |
Chuck Lever | da35187 | 2005-08-11 16:25:11 -0400 | [diff] [blame] | 962 | task->tk_pid, xprt, |
| 963 | (xprt_connected(xprt) ? "is" : "is not")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | |
Chuck Lever | da35187 | 2005-08-11 16:25:11 -0400 | [diff] [blame] | 965 | task->tk_action = call_transmit; |
| 966 | if (!xprt_connected(xprt)) { |
| 967 | task->tk_action = call_connect_status; |
| 968 | if (task->tk_status < 0) |
| 969 | return; |
| 970 | xprt_connect(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | /* |
Chuck Lever | da35187 | 2005-08-11 16:25:11 -0400 | [diff] [blame] | 975 | * 4c. Sort out connect result |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | */ |
| 977 | static void |
| 978 | call_connect_status(struct rpc_task *task) |
| 979 | { |
| 980 | struct rpc_clnt *clnt = task->tk_client; |
| 981 | int status = task->tk_status; |
| 982 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 983 | dprint_status(task); |
Chuck Lever | da35187 | 2005-08-11 16:25:11 -0400 | [diff] [blame] | 984 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | task->tk_status = 0; |
| 986 | if (status >= 0) { |
| 987 | clnt->cl_stats->netreconn++; |
| 988 | task->tk_action = call_transmit; |
| 989 | return; |
| 990 | } |
| 991 | |
Chuck Lever | da35187 | 2005-08-11 16:25:11 -0400 | [diff] [blame] | 992 | /* Something failed: remote service port may have changed */ |
Chuck Lever | 35f5a42 | 2006-01-03 09:55:50 +0100 | [diff] [blame] | 993 | rpc_force_rebind(clnt); |
Chuck Lever | da35187 | 2005-08-11 16:25:11 -0400 | [diff] [blame] | 994 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | switch (status) { |
| 996 | case -ENOTCONN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | case -EAGAIN: |
Chuck Lever | da35187 | 2005-08-11 16:25:11 -0400 | [diff] [blame] | 998 | task->tk_action = call_bind; |
Trond Myklebust | da45828e | 2006-08-31 15:44:52 -0400 | [diff] [blame] | 999 | if (!RPC_IS_SOFT(task)) |
| 1000 | return; |
| 1001 | /* if soft mounted, test if we've timed out */ |
| 1002 | case -ETIMEDOUT: |
| 1003 | task->tk_action = call_timeout; |
| 1004 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | } |
Trond Myklebust | da45828e | 2006-08-31 15:44:52 -0400 | [diff] [blame] | 1006 | rpc_exit(task, -EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | /* |
| 1010 | * 5. Transmit the RPC request, and wait for reply |
| 1011 | */ |
| 1012 | static void |
| 1013 | call_transmit(struct rpc_task *task) |
| 1014 | { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1015 | dprint_status(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | |
| 1017 | task->tk_action = call_status; |
| 1018 | if (task->tk_status < 0) |
| 1019 | return; |
| 1020 | task->tk_status = xprt_prepare_transmit(task); |
| 1021 | if (task->tk_status != 0) |
| 1022 | return; |
Trond Myklebust | e0ab53d | 2006-07-27 17:22:50 -0400 | [diff] [blame] | 1023 | task->tk_action = call_transmit_status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | /* Encode here so that rpcsec_gss can use correct sequence number. */ |
Trond Myklebust | 940e331 | 2005-11-09 21:45:24 -0500 | [diff] [blame] | 1025 | if (rpc_task_need_encode(task)) { |
Trond Myklebust | e0ab53d | 2006-07-27 17:22:50 -0400 | [diff] [blame] | 1026 | BUG_ON(task->tk_rqstp->rq_bytes_sent != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | call_encode(task); |
Trond Myklebust | 5e5ce5b | 2005-10-18 14:20:11 -0700 | [diff] [blame] | 1028 | /* Did the encode result in an error condition? */ |
| 1029 | if (task->tk_status != 0) |
Trond Myklebust | e0ab53d | 2006-07-27 17:22:50 -0400 | [diff] [blame] | 1030 | return; |
Trond Myklebust | 5e5ce5b | 2005-10-18 14:20:11 -0700 | [diff] [blame] | 1031 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | xprt_transmit(task); |
| 1033 | if (task->tk_status < 0) |
| 1034 | return; |
Trond Myklebust | e0ab53d | 2006-07-27 17:22:50 -0400 | [diff] [blame] | 1035 | /* |
| 1036 | * On success, ensure that we call xprt_end_transmit() before sleeping |
| 1037 | * in order to allow access to the socket to other RPC requests. |
| 1038 | */ |
| 1039 | call_transmit_status(task); |
| 1040 | if (task->tk_msg.rpc_proc->p_decode != NULL) |
| 1041 | return; |
| 1042 | task->tk_action = rpc_exit_task; |
| 1043 | rpc_wake_up_task(task); |
| 1044 | } |
| 1045 | |
| 1046 | /* |
| 1047 | * 5a. Handle cleanup after a transmission |
| 1048 | */ |
| 1049 | static void |
| 1050 | call_transmit_status(struct rpc_task *task) |
| 1051 | { |
| 1052 | task->tk_action = call_status; |
| 1053 | /* |
| 1054 | * Special case: if we've been waiting on the socket's write_space() |
| 1055 | * callback, then don't call xprt_end_transmit(). |
| 1056 | */ |
| 1057 | if (task->tk_status == -EAGAIN) |
| 1058 | return; |
| 1059 | xprt_end_transmit(task); |
Trond Myklebust | 940e331 | 2005-11-09 21:45:24 -0500 | [diff] [blame] | 1060 | rpc_task_force_reencode(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | /* |
| 1064 | * 6. Sort out the RPC call status |
| 1065 | */ |
| 1066 | static void |
| 1067 | call_status(struct rpc_task *task) |
| 1068 | { |
| 1069 | struct rpc_clnt *clnt = task->tk_client; |
| 1070 | struct rpc_rqst *req = task->tk_rqstp; |
| 1071 | int status; |
| 1072 | |
| 1073 | if (req->rq_received > 0 && !req->rq_bytes_sent) |
| 1074 | task->tk_status = req->rq_received; |
| 1075 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1076 | dprint_status(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | |
| 1078 | status = task->tk_status; |
| 1079 | if (status >= 0) { |
| 1080 | task->tk_action = call_decode; |
| 1081 | return; |
| 1082 | } |
| 1083 | |
| 1084 | task->tk_status = 0; |
| 1085 | switch(status) { |
Trond Myklebust | 7630399 | 2006-08-30 14:32:49 -0400 | [diff] [blame] | 1086 | case -EHOSTDOWN: |
| 1087 | case -EHOSTUNREACH: |
| 1088 | case -ENETUNREACH: |
| 1089 | /* |
| 1090 | * Delay any retries for 3 seconds, then handle as if it |
| 1091 | * were a timeout. |
| 1092 | */ |
| 1093 | rpc_delay(task, 3*HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | case -ETIMEDOUT: |
| 1095 | task->tk_action = call_timeout; |
Trond Myklebust | 241c39b | 2007-04-20 16:12:55 -0400 | [diff] [blame] | 1096 | if (task->tk_client->cl_discrtry) |
| 1097 | xprt_disconnect(task->tk_xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | break; |
| 1099 | case -ECONNREFUSED: |
| 1100 | case -ENOTCONN: |
Chuck Lever | 35f5a42 | 2006-01-03 09:55:50 +0100 | [diff] [blame] | 1101 | rpc_force_rebind(clnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | task->tk_action = call_bind; |
| 1103 | break; |
| 1104 | case -EAGAIN: |
| 1105 | task->tk_action = call_transmit; |
| 1106 | break; |
| 1107 | case -EIO: |
| 1108 | /* shutdown or soft timeout */ |
| 1109 | rpc_exit(task, status); |
| 1110 | break; |
| 1111 | default: |
Chuck Lever | f518e35a | 2006-01-03 09:55:52 +0100 | [diff] [blame] | 1112 | printk("%s: RPC call returned error %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | clnt->cl_protname, -status); |
| 1114 | rpc_exit(task, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | /* |
Trond Myklebust | e0ab53d | 2006-07-27 17:22:50 -0400 | [diff] [blame] | 1119 | * 6a. Handle RPC timeout |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | * We do not release the request slot, so we keep using the |
| 1121 | * same XID for all retransmits. |
| 1122 | */ |
| 1123 | static void |
| 1124 | call_timeout(struct rpc_task *task) |
| 1125 | { |
| 1126 | struct rpc_clnt *clnt = task->tk_client; |
| 1127 | |
| 1128 | if (xprt_adjust_timeout(task->tk_rqstp) == 0) { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1129 | dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | goto retry; |
| 1131 | } |
| 1132 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1133 | dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid); |
Chuck Lever | ef759a2 | 2006-03-20 13:44:17 -0500 | [diff] [blame] | 1134 | task->tk_timeouts++; |
| 1135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | if (RPC_IS_SOFT(task)) { |
Chuck Lever | f518e35a | 2006-01-03 09:55:52 +0100 | [diff] [blame] | 1137 | printk(KERN_NOTICE "%s: server %s not responding, timed out\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | clnt->cl_protname, clnt->cl_server); |
| 1139 | rpc_exit(task, -EIO); |
| 1140 | return; |
| 1141 | } |
| 1142 | |
Chuck Lever | f518e35a | 2006-01-03 09:55:52 +0100 | [diff] [blame] | 1143 | if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | task->tk_flags |= RPC_CALL_MAJORSEEN; |
| 1145 | printk(KERN_NOTICE "%s: server %s not responding, still trying\n", |
| 1146 | clnt->cl_protname, clnt->cl_server); |
| 1147 | } |
Chuck Lever | 35f5a42 | 2006-01-03 09:55:50 +0100 | [diff] [blame] | 1148 | rpc_force_rebind(clnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | |
| 1150 | retry: |
| 1151 | clnt->cl_stats->rpcretrans++; |
| 1152 | task->tk_action = call_bind; |
| 1153 | task->tk_status = 0; |
| 1154 | } |
| 1155 | |
| 1156 | /* |
| 1157 | * 7. Decode the RPC reply |
| 1158 | */ |
| 1159 | static void |
| 1160 | call_decode(struct rpc_task *task) |
| 1161 | { |
| 1162 | struct rpc_clnt *clnt = task->tk_client; |
| 1163 | struct rpc_rqst *req = task->tk_rqstp; |
| 1164 | kxdrproc_t decode = task->tk_msg.rpc_proc->p_decode; |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 1165 | __be32 *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1167 | dprintk("RPC: %5u call_decode (status %d)\n", |
| 1168 | task->tk_pid, task->tk_status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | |
Chuck Lever | f518e35a | 2006-01-03 09:55:52 +0100 | [diff] [blame] | 1170 | if (task->tk_flags & RPC_CALL_MAJORSEEN) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | printk(KERN_NOTICE "%s: server %s OK\n", |
| 1172 | clnt->cl_protname, clnt->cl_server); |
| 1173 | task->tk_flags &= ~RPC_CALL_MAJORSEEN; |
| 1174 | } |
| 1175 | |
| 1176 | if (task->tk_status < 12) { |
| 1177 | if (!RPC_IS_SOFT(task)) { |
| 1178 | task->tk_action = call_bind; |
| 1179 | clnt->cl_stats->rpcretrans++; |
| 1180 | goto out_retry; |
| 1181 | } |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1182 | dprintk("RPC: %s: too small RPC reply size (%d bytes)\n", |
| 1183 | clnt->cl_protname, task->tk_status); |
Trond Myklebust | da45828e | 2006-08-31 15:44:52 -0400 | [diff] [blame] | 1184 | task->tk_action = call_timeout; |
| 1185 | goto out_retry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | } |
| 1187 | |
Trond Myklebust | 43ac3f2 | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1188 | /* |
| 1189 | * Ensure that we see all writes made by xprt_complete_rqst() |
| 1190 | * before it changed req->rq_received. |
| 1191 | */ |
| 1192 | smp_rmb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | req->rq_rcv_buf.len = req->rq_private_buf.len; |
| 1194 | |
| 1195 | /* Check that the softirq receive buffer is valid */ |
| 1196 | WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf, |
| 1197 | sizeof(req->rq_rcv_buf)) != 0); |
| 1198 | |
| 1199 | /* Verify the RPC header */ |
Trond Myklebust | abbcf28 | 2006-01-03 09:55:03 +0100 | [diff] [blame] | 1200 | p = call_verify(task); |
| 1201 | if (IS_ERR(p)) { |
| 1202 | if (p == ERR_PTR(-EAGAIN)) |
| 1203 | goto out_retry; |
| 1204 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | } |
| 1206 | |
Trond Myklebust | abbcf28 | 2006-01-03 09:55:03 +0100 | [diff] [blame] | 1207 | task->tk_action = rpc_exit_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | |
Trond Myklebust | 6d5fcb5 | 2006-10-18 16:01:06 -0400 | [diff] [blame] | 1209 | if (decode) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | task->tk_status = rpcauth_unwrap_resp(task, decode, req, p, |
| 1211 | task->tk_msg.rpc_resp); |
Trond Myklebust | 6d5fcb5 | 2006-10-18 16:01:06 -0400 | [diff] [blame] | 1212 | } |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1213 | dprintk("RPC: %5u call_decode result %d\n", task->tk_pid, |
| 1214 | task->tk_status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | return; |
| 1216 | out_retry: |
| 1217 | req->rq_received = req->rq_private_buf.len = 0; |
| 1218 | task->tk_status = 0; |
Trond Myklebust | 241c39b | 2007-04-20 16:12:55 -0400 | [diff] [blame] | 1219 | if (task->tk_client->cl_discrtry) |
| 1220 | xprt_disconnect(task->tk_xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | /* |
| 1224 | * 8. Refresh the credentials if rejected by the server |
| 1225 | */ |
| 1226 | static void |
| 1227 | call_refresh(struct rpc_task *task) |
| 1228 | { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1229 | dprint_status(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | task->tk_action = call_refreshresult; |
| 1232 | task->tk_status = 0; |
| 1233 | task->tk_client->cl_stats->rpcauthrefresh++; |
| 1234 | rpcauth_refreshcred(task); |
| 1235 | } |
| 1236 | |
| 1237 | /* |
| 1238 | * 8a. Process the results of a credential refresh |
| 1239 | */ |
| 1240 | static void |
| 1241 | call_refreshresult(struct rpc_task *task) |
| 1242 | { |
| 1243 | int status = task->tk_status; |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1244 | |
| 1245 | dprint_status(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | |
| 1247 | task->tk_status = 0; |
| 1248 | task->tk_action = call_reserve; |
| 1249 | if (status >= 0 && rpcauth_uptodatecred(task)) |
| 1250 | return; |
| 1251 | if (status == -EACCES) { |
| 1252 | rpc_exit(task, -EACCES); |
| 1253 | return; |
| 1254 | } |
| 1255 | task->tk_action = call_refresh; |
| 1256 | if (status != -ETIMEDOUT) |
| 1257 | rpc_delay(task, 3*HZ); |
| 1258 | return; |
| 1259 | } |
| 1260 | |
| 1261 | /* |
| 1262 | * Call header serialization |
| 1263 | */ |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 1264 | static __be32 * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | call_header(struct rpc_task *task) |
| 1266 | { |
| 1267 | struct rpc_clnt *clnt = task->tk_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | struct rpc_rqst *req = task->tk_rqstp; |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 1269 | __be32 *p = req->rq_svec[0].iov_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | |
| 1271 | /* FIXME: check buffer size? */ |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1272 | |
| 1273 | p = xprt_skip_transport_header(task->tk_xprt, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | *p++ = req->rq_xid; /* XID */ |
| 1275 | *p++ = htonl(RPC_CALL); /* CALL */ |
| 1276 | *p++ = htonl(RPC_VERSION); /* RPC version */ |
| 1277 | *p++ = htonl(clnt->cl_prog); /* program number */ |
| 1278 | *p++ = htonl(clnt->cl_vers); /* program version */ |
| 1279 | *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */ |
Trond Myklebust | 334ccfd | 2005-06-22 17:16:19 +0000 | [diff] [blame] | 1280 | p = rpcauth_marshcred(task, p); |
| 1281 | req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p); |
| 1282 | return p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | /* |
| 1286 | * Reply header verification |
| 1287 | */ |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 1288 | static __be32 * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | call_verify(struct rpc_task *task) |
| 1290 | { |
| 1291 | struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0]; |
| 1292 | int len = task->tk_rqstp->rq_rcv_buf.len >> 2; |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 1293 | __be32 *p = iov->iov_base; |
| 1294 | u32 n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | int error = -EACCES; |
| 1296 | |
David Howells | e889649 | 2006-08-24 15:44:19 -0400 | [diff] [blame] | 1297 | if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) { |
| 1298 | /* RFC-1014 says that the representation of XDR data must be a |
| 1299 | * multiple of four bytes |
| 1300 | * - if it isn't pointer subtraction in the NFS client may give |
| 1301 | * undefined results |
| 1302 | */ |
Trond Myklebust | 8a702bb | 2007-06-27 18:30:26 -0400 | [diff] [blame] | 1303 | dprintk("RPC: %5u %s: XDR representation not a multiple of" |
| 1304 | " 4 bytes: 0x%x\n", task->tk_pid, __FUNCTION__, |
| 1305 | task->tk_rqstp->rq_rcv_buf.len); |
David Howells | e889649 | 2006-08-24 15:44:19 -0400 | [diff] [blame] | 1306 | goto out_eio; |
| 1307 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | if ((len -= 3) < 0) |
| 1309 | goto out_overflow; |
| 1310 | p += 1; /* skip XID */ |
| 1311 | |
| 1312 | if ((n = ntohl(*p++)) != RPC_REPLY) { |
Trond Myklebust | 8a702bb | 2007-06-27 18:30:26 -0400 | [diff] [blame] | 1313 | dprintk("RPC: %5u %s: not an RPC reply: %x\n", |
| 1314 | task->tk_pid, __FUNCTION__, n); |
Trond Myklebust | abbcf28 | 2006-01-03 09:55:03 +0100 | [diff] [blame] | 1315 | goto out_garbage; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | } |
| 1317 | if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) { |
| 1318 | if (--len < 0) |
| 1319 | goto out_overflow; |
| 1320 | switch ((n = ntohl(*p++))) { |
| 1321 | case RPC_AUTH_ERROR: |
| 1322 | break; |
| 1323 | case RPC_MISMATCH: |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1324 | dprintk("RPC: %5u %s: RPC call version " |
| 1325 | "mismatch!\n", |
| 1326 | task->tk_pid, __FUNCTION__); |
Andreas Gruenbacher | cdf4770 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 1327 | error = -EPROTONOSUPPORT; |
| 1328 | goto out_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | default: |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1330 | dprintk("RPC: %5u %s: RPC call rejected, " |
| 1331 | "unknown error: %x\n", |
| 1332 | task->tk_pid, __FUNCTION__, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | goto out_eio; |
| 1334 | } |
| 1335 | if (--len < 0) |
| 1336 | goto out_overflow; |
| 1337 | switch ((n = ntohl(*p++))) { |
| 1338 | case RPC_AUTH_REJECTEDCRED: |
| 1339 | case RPC_AUTH_REJECTEDVERF: |
| 1340 | case RPCSEC_GSS_CREDPROBLEM: |
| 1341 | case RPCSEC_GSS_CTXPROBLEM: |
| 1342 | if (!task->tk_cred_retry) |
| 1343 | break; |
| 1344 | task->tk_cred_retry--; |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1345 | dprintk("RPC: %5u %s: retry stale creds\n", |
| 1346 | task->tk_pid, __FUNCTION__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | rpcauth_invalcred(task); |
Trond Myklebust | 220bcc2 | 2007-10-01 12:06:48 -0400 | [diff] [blame] | 1348 | /* Ensure we obtain a new XID! */ |
| 1349 | xprt_release(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | task->tk_action = call_refresh; |
Trond Myklebust | abbcf28 | 2006-01-03 09:55:03 +0100 | [diff] [blame] | 1351 | goto out_retry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | case RPC_AUTH_BADCRED: |
| 1353 | case RPC_AUTH_BADVERF: |
| 1354 | /* possibly garbled cred/verf? */ |
| 1355 | if (!task->tk_garb_retry) |
| 1356 | break; |
| 1357 | task->tk_garb_retry--; |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1358 | dprintk("RPC: %5u %s: retry garbled creds\n", |
| 1359 | task->tk_pid, __FUNCTION__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | task->tk_action = call_bind; |
Trond Myklebust | abbcf28 | 2006-01-03 09:55:03 +0100 | [diff] [blame] | 1361 | goto out_retry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | case RPC_AUTH_TOOWEAK: |
Levent Serinol | 1356b8c | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 1363 | printk(KERN_NOTICE "call_verify: server %s requires stronger " |
| 1364 | "authentication.\n", task->tk_client->cl_server); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | break; |
| 1366 | default: |
Trond Myklebust | 8a702bb | 2007-06-27 18:30:26 -0400 | [diff] [blame] | 1367 | dprintk("RPC: %5u %s: unknown auth error: %x\n", |
| 1368 | task->tk_pid, __FUNCTION__, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | error = -EIO; |
| 1370 | } |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1371 | dprintk("RPC: %5u %s: call rejected %d\n", |
| 1372 | task->tk_pid, __FUNCTION__, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | goto out_err; |
| 1374 | } |
| 1375 | if (!(p = rpcauth_checkverf(task, p))) { |
Trond Myklebust | 8a702bb | 2007-06-27 18:30:26 -0400 | [diff] [blame] | 1376 | dprintk("RPC: %5u %s: auth check failed\n", |
| 1377 | task->tk_pid, __FUNCTION__); |
Trond Myklebust | abbcf28 | 2006-01-03 09:55:03 +0100 | [diff] [blame] | 1378 | goto out_garbage; /* bad verifier, retry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | } |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 1380 | len = p - (__be32 *)iov->iov_base - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | if (len < 0) |
| 1382 | goto out_overflow; |
| 1383 | switch ((n = ntohl(*p++))) { |
| 1384 | case RPC_SUCCESS: |
| 1385 | return p; |
| 1386 | case RPC_PROG_UNAVAIL: |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1387 | dprintk("RPC: %5u %s: program %u is unsupported by server %s\n", |
| 1388 | task->tk_pid, __FUNCTION__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | (unsigned int)task->tk_client->cl_prog, |
| 1390 | task->tk_client->cl_server); |
Andreas Gruenbacher | cdf4770 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 1391 | error = -EPFNOSUPPORT; |
| 1392 | goto out_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | case RPC_PROG_MISMATCH: |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1394 | dprintk("RPC: %5u %s: program %u, version %u unsupported by " |
| 1395 | "server %s\n", task->tk_pid, __FUNCTION__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | (unsigned int)task->tk_client->cl_prog, |
| 1397 | (unsigned int)task->tk_client->cl_vers, |
| 1398 | task->tk_client->cl_server); |
Andreas Gruenbacher | cdf4770 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 1399 | error = -EPROTONOSUPPORT; |
| 1400 | goto out_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | case RPC_PROC_UNAVAIL: |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1402 | dprintk("RPC: %5u %s: proc %p unsupported by program %u, " |
| 1403 | "version %u on server %s\n", |
| 1404 | task->tk_pid, __FUNCTION__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | task->tk_msg.rpc_proc, |
| 1406 | task->tk_client->cl_prog, |
| 1407 | task->tk_client->cl_vers, |
| 1408 | task->tk_client->cl_server); |
Andreas Gruenbacher | cdf4770 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 1409 | error = -EOPNOTSUPP; |
| 1410 | goto out_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | case RPC_GARBAGE_ARGS: |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1412 | dprintk("RPC: %5u %s: server saw garbage\n", |
| 1413 | task->tk_pid, __FUNCTION__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | break; /* retry */ |
| 1415 | default: |
Trond Myklebust | 8a702bb | 2007-06-27 18:30:26 -0400 | [diff] [blame] | 1416 | dprintk("RPC: %5u %s: server accept status: %x\n", |
| 1417 | task->tk_pid, __FUNCTION__, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | /* Also retry */ |
| 1419 | } |
| 1420 | |
Trond Myklebust | abbcf28 | 2006-01-03 09:55:03 +0100 | [diff] [blame] | 1421 | out_garbage: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | task->tk_client->cl_stats->rpcgarbage++; |
| 1423 | if (task->tk_garb_retry) { |
| 1424 | task->tk_garb_retry--; |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1425 | dprintk("RPC: %5u %s: retrying\n", |
| 1426 | task->tk_pid, __FUNCTION__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | task->tk_action = call_bind; |
Trond Myklebust | abbcf28 | 2006-01-03 09:55:03 +0100 | [diff] [blame] | 1428 | out_retry: |
| 1429 | return ERR_PTR(-EAGAIN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | out_eio: |
| 1432 | error = -EIO; |
| 1433 | out_err: |
| 1434 | rpc_exit(task, error); |
Trond Myklebust | 8a702bb | 2007-06-27 18:30:26 -0400 | [diff] [blame] | 1435 | dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid, |
| 1436 | __FUNCTION__, error); |
Trond Myklebust | abbcf28 | 2006-01-03 09:55:03 +0100 | [diff] [blame] | 1437 | return ERR_PTR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | out_overflow: |
Trond Myklebust | 8a702bb | 2007-06-27 18:30:26 -0400 | [diff] [blame] | 1439 | dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid, |
| 1440 | __FUNCTION__); |
Trond Myklebust | abbcf28 | 2006-01-03 09:55:03 +0100 | [diff] [blame] | 1441 | goto out_garbage; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | } |
Trond Myklebust | 5ee0ed7 | 2005-06-22 17:16:20 +0000 | [diff] [blame] | 1443 | |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 1444 | static int rpcproc_encode_null(void *rqstp, __be32 *data, void *obj) |
Trond Myklebust | 5ee0ed7 | 2005-06-22 17:16:20 +0000 | [diff] [blame] | 1445 | { |
| 1446 | return 0; |
| 1447 | } |
| 1448 | |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 1449 | static int rpcproc_decode_null(void *rqstp, __be32 *data, void *obj) |
Trond Myklebust | 5ee0ed7 | 2005-06-22 17:16:20 +0000 | [diff] [blame] | 1450 | { |
| 1451 | return 0; |
| 1452 | } |
| 1453 | |
| 1454 | static struct rpc_procinfo rpcproc_null = { |
| 1455 | .p_encode = rpcproc_encode_null, |
| 1456 | .p_decode = rpcproc_decode_null, |
| 1457 | }; |
| 1458 | |
Trond Myklebust | 64c91a1 | 2007-06-23 10:17:16 -0400 | [diff] [blame] | 1459 | static int rpc_ping(struct rpc_clnt *clnt, int flags) |
Trond Myklebust | 5ee0ed7 | 2005-06-22 17:16:20 +0000 | [diff] [blame] | 1460 | { |
| 1461 | struct rpc_message msg = { |
| 1462 | .rpc_proc = &rpcproc_null, |
| 1463 | }; |
| 1464 | int err; |
| 1465 | msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0); |
| 1466 | err = rpc_call_sync(clnt, &msg, flags); |
| 1467 | put_rpccred(msg.rpc_cred); |
| 1468 | return err; |
| 1469 | } |
Trond Myklebust | 188fef1 | 2007-06-16 14:18:40 -0400 | [diff] [blame] | 1470 | |
Trond Myklebust | 5e1550d | 2007-06-23 10:17:16 -0400 | [diff] [blame] | 1471 | struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags) |
| 1472 | { |
| 1473 | struct rpc_message msg = { |
| 1474 | .rpc_proc = &rpcproc_null, |
| 1475 | .rpc_cred = cred, |
| 1476 | }; |
| 1477 | return rpc_do_run_task(clnt, &msg, flags, &rpc_default_ops, NULL); |
| 1478 | } |
| 1479 | EXPORT_SYMBOL(rpc_call_null); |
| 1480 | |
Trond Myklebust | 188fef1 | 2007-06-16 14:18:40 -0400 | [diff] [blame] | 1481 | #ifdef RPC_DEBUG |
| 1482 | void rpc_show_tasks(void) |
| 1483 | { |
| 1484 | struct rpc_clnt *clnt; |
| 1485 | struct rpc_task *t; |
| 1486 | |
| 1487 | spin_lock(&rpc_client_lock); |
| 1488 | if (list_empty(&all_clients)) |
| 1489 | goto out; |
| 1490 | printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout " |
| 1491 | "-rpcwait -action- ---ops--\n"); |
| 1492 | list_for_each_entry(clnt, &all_clients, cl_clients) { |
| 1493 | if (list_empty(&clnt->cl_tasks)) |
| 1494 | continue; |
| 1495 | spin_lock(&clnt->cl_lock); |
| 1496 | list_for_each_entry(t, &clnt->cl_tasks, tk_task) { |
| 1497 | const char *rpc_waitq = "none"; |
Chuck Lever | d66968f | 2007-09-11 18:00:25 -0400 | [diff] [blame] | 1498 | int proc; |
| 1499 | |
| 1500 | if (t->tk_msg.rpc_proc) |
| 1501 | proc = t->tk_msg.rpc_proc->p_proc; |
| 1502 | else |
| 1503 | proc = -1; |
Trond Myklebust | 188fef1 | 2007-06-16 14:18:40 -0400 | [diff] [blame] | 1504 | |
| 1505 | if (RPC_IS_QUEUED(t)) |
| 1506 | rpc_waitq = rpc_qname(t->u.tk_wait.rpc_waitq); |
| 1507 | |
| 1508 | printk("%5u %04d %04x %6d %8p %6d %8p %8ld %8s %8p %8p\n", |
Chuck Lever | d66968f | 2007-09-11 18:00:25 -0400 | [diff] [blame] | 1509 | t->tk_pid, proc, |
Trond Myklebust | 188fef1 | 2007-06-16 14:18:40 -0400 | [diff] [blame] | 1510 | t->tk_flags, t->tk_status, |
| 1511 | t->tk_client, |
| 1512 | (t->tk_client ? t->tk_client->cl_prog : 0), |
| 1513 | t->tk_rqstp, t->tk_timeout, |
| 1514 | rpc_waitq, |
| 1515 | t->tk_action, t->tk_ops); |
| 1516 | } |
| 1517 | spin_unlock(&clnt->cl_lock); |
| 1518 | } |
| 1519 | out: |
| 1520 | spin_unlock(&rpc_client_lock); |
| 1521 | } |
| 1522 | #endif |