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