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