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