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