1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * linux/net/sunrpc/clnt.c 4 * 5 * This file contains the high-level RPC interface. 6 * It is modeled as a finite state machine to support both synchronous 7 * and asynchronous requests. 8 * 9 * - RPC header generation and argument serialization. 10 * - Credential refresh. 11 * - TCP connect handling. 12 * - Retry of operation when it is suspected the operation failed because 13 * of uid squashing on the server, or when the credentials were stale 14 * and need to be refreshed, or when a packet was damaged in transit. 15 * This may be have to be moved to the VFS layer. 16 * 17 * Copyright (C) 1992,1993 Rick Sladkey <jrs@world.std.com> 18 * Copyright (C) 1995,1996 Olaf Kirch <okir@monad.swb.de> 19 */ 20 21 22 #include <linux/module.h> 23 #include <linux/types.h> 24 #include <linux/kallsyms.h> 25 #include <linux/mm.h> 26 #include <linux/namei.h> 27 #include <linux/mount.h> 28 #include <linux/slab.h> 29 #include <linux/rcupdate.h> 30 #include <linux/utsname.h> 31 #include <linux/workqueue.h> 32 #include <linux/in.h> 33 #include <linux/in6.h> 34 #include <linux/un.h> 35 36 #include <linux/sunrpc/clnt.h> 37 #include <linux/sunrpc/addr.h> 38 #include <linux/sunrpc/rpc_pipe_fs.h> 39 #include <linux/sunrpc/metrics.h> 40 #include <linux/sunrpc/bc_xprt.h> 41 #include <trace/events/sunrpc.h> 42 43 #include "sunrpc.h" 44 #include "netns.h" 45 46 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 47 # define RPCDBG_FACILITY RPCDBG_CALL 48 #endif 49 50 #define dprint_status(t) \ 51 dprintk("RPC: %5u %s (status %d)\n", t->tk_pid, \ 52 __func__, t->tk_status) 53 54 /* 55 * All RPC clients are linked into this list 56 */ 57 58 static DECLARE_WAIT_QUEUE_HEAD(destroy_wait); 59 60 61 static void call_start(struct rpc_task *task); 62 static void call_reserve(struct rpc_task *task); 63 static void call_reserveresult(struct rpc_task *task); 64 static void call_allocate(struct rpc_task *task); 65 static void call_encode(struct rpc_task *task); 66 static void call_decode(struct rpc_task *task); 67 static void call_bind(struct rpc_task *task); 68 static void call_bind_status(struct rpc_task *task); 69 static void call_transmit(struct rpc_task *task); 70 static void call_status(struct rpc_task *task); 71 static void call_transmit_status(struct rpc_task *task); 72 static void call_refresh(struct rpc_task *task); 73 static void call_refreshresult(struct rpc_task *task); 74 static void call_connect(struct rpc_task *task); 75 static void call_connect_status(struct rpc_task *task); 76 77 static int rpc_encode_header(struct rpc_task *task, 78 struct xdr_stream *xdr); 79 static int rpc_decode_header(struct rpc_task *task, 80 struct xdr_stream *xdr); 81 static int rpc_ping(struct rpc_clnt *clnt); 82 static void rpc_check_timeout(struct rpc_task *task); 83 84 static void rpc_register_client(struct rpc_clnt *clnt) 85 { 86 struct net *net = rpc_net_ns(clnt); 87 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); 88 89 spin_lock(&sn->rpc_client_lock); 90 list_add(&clnt->cl_clients, &sn->all_clients); 91 spin_unlock(&sn->rpc_client_lock); 92 } 93 94 static void rpc_unregister_client(struct rpc_clnt *clnt) 95 { 96 struct net *net = rpc_net_ns(clnt); 97 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); 98 99 spin_lock(&sn->rpc_client_lock); 100 list_del(&clnt->cl_clients); 101 spin_unlock(&sn->rpc_client_lock); 102 } 103 104 static void __rpc_clnt_remove_pipedir(struct rpc_clnt *clnt) 105 { 106 rpc_remove_client_dir(clnt); 107 } 108 109 static void rpc_clnt_remove_pipedir(struct rpc_clnt *clnt) 110 { 111 struct net *net = rpc_net_ns(clnt); 112 struct super_block *pipefs_sb; 113 114 pipefs_sb = rpc_get_sb_net(net); 115 if (pipefs_sb) { 116 __rpc_clnt_remove_pipedir(clnt); 117 rpc_put_sb_net(net); 118 } 119 } 120 121 static struct dentry *rpc_setup_pipedir_sb(struct super_block *sb, 122 struct rpc_clnt *clnt) 123 { 124 static uint32_t clntid; 125 const char *dir_name = clnt->cl_program->pipe_dir_name; 126 char name[15]; 127 struct dentry *dir, *dentry; 128 129 dir = rpc_d_lookup_sb(sb, dir_name); 130 if (dir == NULL) { 131 pr_info("RPC: pipefs directory doesn't exist: %s\n", dir_name); 132 return dir; 133 } 134 for (;;) { 135 snprintf(name, sizeof(name), "clnt%x", (unsigned int)clntid++); 136 name[sizeof(name) - 1] = '\0'; 137 dentry = rpc_create_client_dir(dir, name, clnt); 138 if (!IS_ERR(dentry)) 139 break; 140 if (dentry == ERR_PTR(-EEXIST)) 141 continue; 142 printk(KERN_INFO "RPC: Couldn't create pipefs entry" 143 " %s/%s, error %ld\n", 144 dir_name, name, PTR_ERR(dentry)); 145 break; 146 } 147 dput(dir); 148 return dentry; 149 } 150 151 static int 152 rpc_setup_pipedir(struct super_block *pipefs_sb, struct rpc_clnt *clnt) 153 { 154 struct dentry *dentry; 155 156 if (clnt->cl_program->pipe_dir_name != NULL) { 157 dentry = rpc_setup_pipedir_sb(pipefs_sb, clnt); 158 if (IS_ERR(dentry)) 159 return PTR_ERR(dentry); 160 } 161 return 0; 162 } 163 164 static int rpc_clnt_skip_event(struct rpc_clnt *clnt, unsigned long event) 165 { 166 if (clnt->cl_program->pipe_dir_name == NULL) 167 return 1; 168 169 switch (event) { 170 case RPC_PIPEFS_MOUNT: 171 if (clnt->cl_pipedir_objects.pdh_dentry != NULL) 172 return 1; 173 if (atomic_read(&clnt->cl_count) == 0) 174 return 1; 175 break; 176 case RPC_PIPEFS_UMOUNT: 177 if (clnt->cl_pipedir_objects.pdh_dentry == NULL) 178 return 1; 179 break; 180 } 181 return 0; 182 } 183 184 static int __rpc_clnt_handle_event(struct rpc_clnt *clnt, unsigned long event, 185 struct super_block *sb) 186 { 187 struct dentry *dentry; 188 189 switch (event) { 190 case RPC_PIPEFS_MOUNT: 191 dentry = rpc_setup_pipedir_sb(sb, clnt); 192 if (!dentry) 193 return -ENOENT; 194 if (IS_ERR(dentry)) 195 return PTR_ERR(dentry); 196 break; 197 case RPC_PIPEFS_UMOUNT: 198 __rpc_clnt_remove_pipedir(clnt); 199 break; 200 default: 201 printk(KERN_ERR "%s: unknown event: %ld\n", __func__, event); 202 return -ENOTSUPP; 203 } 204 return 0; 205 } 206 207 static int __rpc_pipefs_event(struct rpc_clnt *clnt, unsigned long event, 208 struct super_block *sb) 209 { 210 int error = 0; 211 212 for (;; clnt = clnt->cl_parent) { 213 if (!rpc_clnt_skip_event(clnt, event)) 214 error = __rpc_clnt_handle_event(clnt, event, sb); 215 if (error || clnt == clnt->cl_parent) 216 break; 217 } 218 return error; 219 } 220 221 static struct rpc_clnt *rpc_get_client_for_event(struct net *net, int event) 222 { 223 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); 224 struct rpc_clnt *clnt; 225 226 spin_lock(&sn->rpc_client_lock); 227 list_for_each_entry(clnt, &sn->all_clients, cl_clients) { 228 if (rpc_clnt_skip_event(clnt, event)) 229 continue; 230 spin_unlock(&sn->rpc_client_lock); 231 return clnt; 232 } 233 spin_unlock(&sn->rpc_client_lock); 234 return NULL; 235 } 236 237 static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event, 238 void *ptr) 239 { 240 struct super_block *sb = ptr; 241 struct rpc_clnt *clnt; 242 int error = 0; 243 244 while ((clnt = rpc_get_client_for_event(sb->s_fs_info, event))) { 245 error = __rpc_pipefs_event(clnt, event, sb); 246 if (error) 247 break; 248 } 249 return error; 250 } 251 252 static struct notifier_block rpc_clients_block = { 253 .notifier_call = rpc_pipefs_event, 254 .priority = SUNRPC_PIPEFS_RPC_PRIO, 255 }; 256 257 int rpc_clients_notifier_register(void) 258 { 259 return rpc_pipefs_notifier_register(&rpc_clients_block); 260 } 261 262 void rpc_clients_notifier_unregister(void) 263 { 264 return rpc_pipefs_notifier_unregister(&rpc_clients_block); 265 } 266 267 static struct rpc_xprt *rpc_clnt_set_transport(struct rpc_clnt *clnt, 268 struct rpc_xprt *xprt, 269 const struct rpc_timeout *timeout) 270 { 271 struct rpc_xprt *old; 272 273 spin_lock(&clnt->cl_lock); 274 old = rcu_dereference_protected(clnt->cl_xprt, 275 lockdep_is_held(&clnt->cl_lock)); 276 277 if (!xprt_bound(xprt)) 278 clnt->cl_autobind = 1; 279 280 clnt->cl_timeout = timeout; 281 rcu_assign_pointer(clnt->cl_xprt, xprt); 282 spin_unlock(&clnt->cl_lock); 283 284 return old; 285 } 286 287 static void rpc_clnt_set_nodename(struct rpc_clnt *clnt, const char *nodename) 288 { 289 clnt->cl_nodelen = strlcpy(clnt->cl_nodename, 290 nodename, sizeof(clnt->cl_nodename)); 291 } 292 293 static int rpc_client_register(struct rpc_clnt *clnt, 294 rpc_authflavor_t pseudoflavor, 295 const char *client_name) 296 { 297 struct rpc_auth_create_args auth_args = { 298 .pseudoflavor = pseudoflavor, 299 .target_name = client_name, 300 }; 301 struct rpc_auth *auth; 302 struct net *net = rpc_net_ns(clnt); 303 struct super_block *pipefs_sb; 304 int err; 305 306 rpc_clnt_debugfs_register(clnt); 307 308 pipefs_sb = rpc_get_sb_net(net); 309 if (pipefs_sb) { 310 err = rpc_setup_pipedir(pipefs_sb, clnt); 311 if (err) 312 goto out; 313 } 314 315 rpc_register_client(clnt); 316 if (pipefs_sb) 317 rpc_put_sb_net(net); 318 319 auth = rpcauth_create(&auth_args, clnt); 320 if (IS_ERR(auth)) { 321 dprintk("RPC: Couldn't create auth handle (flavor %u)\n", 322 pseudoflavor); 323 err = PTR_ERR(auth); 324 goto err_auth; 325 } 326 return 0; 327 err_auth: 328 pipefs_sb = rpc_get_sb_net(net); 329 rpc_unregister_client(clnt); 330 __rpc_clnt_remove_pipedir(clnt); 331 out: 332 if (pipefs_sb) 333 rpc_put_sb_net(net); 334 rpc_clnt_debugfs_unregister(clnt); 335 return err; 336 } 337 338 static DEFINE_IDA(rpc_clids); 339 340 void rpc_cleanup_clids(void) 341 { 342 ida_destroy(&rpc_clids); 343 } 344 345 static int rpc_alloc_clid(struct rpc_clnt *clnt) 346 { 347 int clid; 348 349 clid = ida_simple_get(&rpc_clids, 0, 0, GFP_KERNEL); 350 if (clid < 0) 351 return clid; 352 clnt->cl_clid = clid; 353 return 0; 354 } 355 356 static void rpc_free_clid(struct rpc_clnt *clnt) 357 { 358 ida_simple_remove(&rpc_clids, clnt->cl_clid); 359 } 360 361 static struct rpc_clnt * rpc_new_client(const struct rpc_create_args *args, 362 struct rpc_xprt_switch *xps, 363 struct rpc_xprt *xprt, 364 struct rpc_clnt *parent) 365 { 366 const struct rpc_program *program = args->program; 367 const struct rpc_version *version; 368 struct rpc_clnt *clnt = NULL; 369 const struct rpc_timeout *timeout; 370 const char *nodename = args->nodename; 371 int err; 372 373 /* sanity check the name before trying to print it */ 374 dprintk("RPC: creating %s client for %s (xprt %p)\n", 375 program->name, args->servername, xprt); 376 377 err = rpciod_up(); 378 if (err) 379 goto out_no_rpciod; 380 381 err = -EINVAL; 382 if (args->version >= program->nrvers) 383 goto out_err; 384 version = program->version[args->version]; 385 if (version == NULL) 386 goto out_err; 387 388 err = -ENOMEM; 389 clnt = kzalloc(sizeof(*clnt), GFP_KERNEL); 390 if (!clnt) 391 goto out_err; 392 clnt->cl_parent = parent ? : clnt; 393 394 err = rpc_alloc_clid(clnt); 395 if (err) 396 goto out_no_clid; 397 398 clnt->cl_cred = get_cred(args->cred); 399 clnt->cl_procinfo = version->procs; 400 clnt->cl_maxproc = version->nrprocs; 401 clnt->cl_prog = args->prognumber ? : program->number; 402 clnt->cl_vers = version->number; 403 clnt->cl_stats = program->stats; 404 clnt->cl_metrics = rpc_alloc_iostats(clnt); 405 rpc_init_pipe_dir_head(&clnt->cl_pipedir_objects); 406 err = -ENOMEM; 407 if (clnt->cl_metrics == NULL) 408 goto out_no_stats; 409 clnt->cl_program = program; 410 INIT_LIST_HEAD(&clnt->cl_tasks); 411 spin_lock_init(&clnt->cl_lock); 412 413 timeout = xprt->timeout; 414 if (args->timeout != NULL) { 415 memcpy(&clnt->cl_timeout_default, args->timeout, 416 sizeof(clnt->cl_timeout_default)); 417 timeout = &clnt->cl_timeout_default; 418 } 419 420 rpc_clnt_set_transport(clnt, xprt, timeout); 421 xprt_iter_init(&clnt->cl_xpi, xps); 422 xprt_switch_put(xps); 423 424 clnt->cl_rtt = &clnt->cl_rtt_default; 425 rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval); 426 427 atomic_set(&clnt->cl_count, 1); 428 429 if (nodename == NULL) 430 nodename = utsname()->nodename; 431 /* save the nodename */ 432 rpc_clnt_set_nodename(clnt, nodename); 433 434 err = rpc_client_register(clnt, args->authflavor, args->client_name); 435 if (err) 436 goto out_no_path; 437 if (parent) 438 atomic_inc(&parent->cl_count); 439 return clnt; 440 441 out_no_path: 442 rpc_free_iostats(clnt->cl_metrics); 443 out_no_stats: 444 put_cred(clnt->cl_cred); 445 rpc_free_clid(clnt); 446 out_no_clid: 447 kfree(clnt); 448 out_err: 449 rpciod_down(); 450 out_no_rpciod: 451 xprt_switch_put(xps); 452 xprt_put(xprt); 453 return ERR_PTR(err); 454 } 455 456 static struct rpc_clnt *rpc_create_xprt(struct rpc_create_args *args, 457 struct rpc_xprt *xprt) 458 { 459 struct rpc_clnt *clnt = NULL; 460 struct rpc_xprt_switch *xps; 461 462 if (args->bc_xprt && args->bc_xprt->xpt_bc_xps) { 463 WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC)); 464 xps = args->bc_xprt->xpt_bc_xps; 465 xprt_switch_get(xps); 466 } else { 467 xps = xprt_switch_alloc(xprt, GFP_KERNEL); 468 if (xps == NULL) { 469 xprt_put(xprt); 470 return ERR_PTR(-ENOMEM); 471 } 472 if (xprt->bc_xprt) { 473 xprt_switch_get(xps); 474 xprt->bc_xprt->xpt_bc_xps = xps; 475 } 476 } 477 clnt = rpc_new_client(args, xps, xprt, NULL); 478 if (IS_ERR(clnt)) 479 return clnt; 480 481 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) { 482 int err = rpc_ping(clnt); 483 if (err != 0) { 484 rpc_shutdown_client(clnt); 485 return ERR_PTR(err); 486 } 487 } 488 489 clnt->cl_softrtry = 1; 490 if (args->flags & (RPC_CLNT_CREATE_HARDRTRY|RPC_CLNT_CREATE_SOFTERR)) { 491 clnt->cl_softrtry = 0; 492 if (args->flags & RPC_CLNT_CREATE_SOFTERR) 493 clnt->cl_softerr = 1; 494 } 495 496 if (args->flags & RPC_CLNT_CREATE_AUTOBIND) 497 clnt->cl_autobind = 1; 498 if (args->flags & RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT) 499 clnt->cl_noretranstimeo = 1; 500 if (args->flags & RPC_CLNT_CREATE_DISCRTRY) 501 clnt->cl_discrtry = 1; 502 if (!(args->flags & RPC_CLNT_CREATE_QUIET)) 503 clnt->cl_chatty = 1; 504 505 return clnt; 506 } 507 508 /** 509 * rpc_create - create an RPC client and transport with one call 510 * @args: rpc_clnt create argument structure 511 * 512 * Creates and initializes an RPC transport and an RPC client. 513 * 514 * It can ping the server in order to determine if it is up, and to see if 515 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables 516 * this behavior so asynchronous tasks can also use rpc_create. 517 */ 518 struct rpc_clnt *rpc_create(struct rpc_create_args *args) 519 { 520 struct rpc_xprt *xprt; 521 struct xprt_create xprtargs = { 522 .net = args->net, 523 .ident = args->protocol, 524 .srcaddr = args->saddress, 525 .dstaddr = args->address, 526 .addrlen = args->addrsize, 527 .servername = args->servername, 528 .bc_xprt = args->bc_xprt, 529 }; 530 char servername[48]; 531 532 if (args->bc_xprt) { 533 WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC)); 534 xprt = args->bc_xprt->xpt_bc_xprt; 535 if (xprt) { 536 xprt_get(xprt); 537 return rpc_create_xprt(args, xprt); 538 } 539 } 540 541 if (args->flags & RPC_CLNT_CREATE_INFINITE_SLOTS) 542 xprtargs.flags |= XPRT_CREATE_INFINITE_SLOTS; 543 if (args->flags & RPC_CLNT_CREATE_NO_IDLE_TIMEOUT) 544 xprtargs.flags |= XPRT_CREATE_NO_IDLE_TIMEOUT; 545 /* 546 * If the caller chooses not to specify a hostname, whip 547 * up a string representation of the passed-in address. 548 */ 549 if (xprtargs.servername == NULL) { 550 struct sockaddr_un *sun = 551 (struct sockaddr_un *)args->address; 552 struct sockaddr_in *sin = 553 (struct sockaddr_in *)args->address; 554 struct sockaddr_in6 *sin6 = 555 (struct sockaddr_in6 *)args->address; 556 557 servername[0] = '\0'; 558 switch (args->address->sa_family) { 559 case AF_LOCAL: 560 snprintf(servername, sizeof(servername), "%s", 561 sun->sun_path); 562 break; 563 case AF_INET: 564 snprintf(servername, sizeof(servername), "%pI4", 565 &sin->sin_addr.s_addr); 566 break; 567 case AF_INET6: 568 snprintf(servername, sizeof(servername), "%pI6", 569 &sin6->sin6_addr); 570 break; 571 default: 572 /* caller wants default server name, but 573 * address family isn't recognized. */ 574 return ERR_PTR(-EINVAL); 575 } 576 xprtargs.servername = servername; 577 } 578 579 xprt = xprt_create_transport(&xprtargs); 580 if (IS_ERR(xprt)) 581 return (struct rpc_clnt *)xprt; 582 583 /* 584 * By default, kernel RPC client connects from a reserved port. 585 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters, 586 * but it is always enabled for rpciod, which handles the connect 587 * operation. 588 */ 589 xprt->resvport = 1; 590 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT) 591 xprt->resvport = 0; 592 593 return rpc_create_xprt(args, xprt); 594 } 595 EXPORT_SYMBOL_GPL(rpc_create); 596 597 /* 598 * This function clones the RPC client structure. It allows us to share the 599 * same transport while varying parameters such as the authentication 600 * flavour. 601 */ 602 static struct rpc_clnt *__rpc_clone_client(struct rpc_create_args *args, 603 struct rpc_clnt *clnt) 604 { 605 struct rpc_xprt_switch *xps; 606 struct rpc_xprt *xprt; 607 struct rpc_clnt *new; 608 int err; 609 610 err = -ENOMEM; 611 rcu_read_lock(); 612 xprt = xprt_get(rcu_dereference(clnt->cl_xprt)); 613 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch)); 614 rcu_read_unlock(); 615 if (xprt == NULL || xps == NULL) { 616 xprt_put(xprt); 617 xprt_switch_put(xps); 618 goto out_err; 619 } 620 args->servername = xprt->servername; 621 args->nodename = clnt->cl_nodename; 622 623 new = rpc_new_client(args, xps, xprt, clnt); 624 if (IS_ERR(new)) { 625 err = PTR_ERR(new); 626 goto out_err; 627 } 628 629 /* Turn off autobind on clones */ 630 new->cl_autobind = 0; 631 new->cl_softrtry = clnt->cl_softrtry; 632 new->cl_softerr = clnt->cl_softerr; 633 new->cl_noretranstimeo = clnt->cl_noretranstimeo; 634 new->cl_discrtry = clnt->cl_discrtry; 635 new->cl_chatty = clnt->cl_chatty; 636 new->cl_principal = clnt->cl_principal; 637 return new; 638 639 out_err: 640 dprintk("RPC: %s: returned error %d\n", __func__, err); 641 return ERR_PTR(err); 642 } 643 644 /** 645 * rpc_clone_client - Clone an RPC client structure 646 * 647 * @clnt: RPC client whose parameters are copied 648 * 649 * Returns a fresh RPC client or an ERR_PTR. 650 */ 651 struct rpc_clnt *rpc_clone_client(struct rpc_clnt *clnt) 652 { 653 struct rpc_create_args args = { 654 .program = clnt->cl_program, 655 .prognumber = clnt->cl_prog, 656 .version = clnt->cl_vers, 657 .authflavor = clnt->cl_auth->au_flavor, 658 .cred = clnt->cl_cred, 659 }; 660 return __rpc_clone_client(&args, clnt); 661 } 662 EXPORT_SYMBOL_GPL(rpc_clone_client); 663 664 /** 665 * rpc_clone_client_set_auth - Clone an RPC client structure and set its auth 666 * 667 * @clnt: RPC client whose parameters are copied 668 * @flavor: security flavor for new client 669 * 670 * Returns a fresh RPC client or an ERR_PTR. 671 */ 672 struct rpc_clnt * 673 rpc_clone_client_set_auth(struct rpc_clnt *clnt, rpc_authflavor_t flavor) 674 { 675 struct rpc_create_args args = { 676 .program = clnt->cl_program, 677 .prognumber = clnt->cl_prog, 678 .version = clnt->cl_vers, 679 .authflavor = flavor, 680 .cred = clnt->cl_cred, 681 }; 682 return __rpc_clone_client(&args, clnt); 683 } 684 EXPORT_SYMBOL_GPL(rpc_clone_client_set_auth); 685 686 /** 687 * rpc_switch_client_transport: switch the RPC transport on the fly 688 * @clnt: pointer to a struct rpc_clnt 689 * @args: pointer to the new transport arguments 690 * @timeout: pointer to the new timeout parameters 691 * 692 * This function allows the caller to switch the RPC transport for the 693 * rpc_clnt structure 'clnt' to allow it to connect to a mirrored NFS 694 * server, for instance. It assumes that the caller has ensured that 695 * there are no active RPC tasks by using some form of locking. 696 * 697 * Returns zero if "clnt" is now using the new xprt. Otherwise a 698 * negative errno is returned, and "clnt" continues to use the old 699 * xprt. 700 */ 701 int rpc_switch_client_transport(struct rpc_clnt *clnt, 702 struct xprt_create *args, 703 const struct rpc_timeout *timeout) 704 { 705 const struct rpc_timeout *old_timeo; 706 rpc_authflavor_t pseudoflavor; 707 struct rpc_xprt_switch *xps, *oldxps; 708 struct rpc_xprt *xprt, *old; 709 struct rpc_clnt *parent; 710 int err; 711 712 xprt = xprt_create_transport(args); 713 if (IS_ERR(xprt)) { 714 dprintk("RPC: failed to create new xprt for clnt %p\n", 715 clnt); 716 return PTR_ERR(xprt); 717 } 718 719 xps = xprt_switch_alloc(xprt, GFP_KERNEL); 720 if (xps == NULL) { 721 xprt_put(xprt); 722 return -ENOMEM; 723 } 724 725 pseudoflavor = clnt->cl_auth->au_flavor; 726 727 old_timeo = clnt->cl_timeout; 728 old = rpc_clnt_set_transport(clnt, xprt, timeout); 729 oldxps = xprt_iter_xchg_switch(&clnt->cl_xpi, xps); 730 731 rpc_unregister_client(clnt); 732 __rpc_clnt_remove_pipedir(clnt); 733 rpc_clnt_debugfs_unregister(clnt); 734 735 /* 736 * A new transport was created. "clnt" therefore 737 * becomes the root of a new cl_parent tree. clnt's 738 * children, if it has any, still point to the old xprt. 739 */ 740 parent = clnt->cl_parent; 741 clnt->cl_parent = clnt; 742 743 /* 744 * The old rpc_auth cache cannot be re-used. GSS 745 * contexts in particular are between a single 746 * client and server. 747 */ 748 err = rpc_client_register(clnt, pseudoflavor, NULL); 749 if (err) 750 goto out_revert; 751 752 synchronize_rcu(); 753 if (parent != clnt) 754 rpc_release_client(parent); 755 xprt_switch_put(oldxps); 756 xprt_put(old); 757 dprintk("RPC: replaced xprt for clnt %p\n", clnt); 758 return 0; 759 760 out_revert: 761 xps = xprt_iter_xchg_switch(&clnt->cl_xpi, oldxps); 762 rpc_clnt_set_transport(clnt, old, old_timeo); 763 clnt->cl_parent = parent; 764 rpc_client_register(clnt, pseudoflavor, NULL); 765 xprt_switch_put(xps); 766 xprt_put(xprt); 767 dprintk("RPC: failed to switch xprt for clnt %p\n", clnt); 768 return err; 769 } 770 EXPORT_SYMBOL_GPL(rpc_switch_client_transport); 771 772 static 773 int rpc_clnt_xprt_iter_init(struct rpc_clnt *clnt, struct rpc_xprt_iter *xpi) 774 { 775 struct rpc_xprt_switch *xps; 776 777 rcu_read_lock(); 778 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch)); 779 rcu_read_unlock(); 780 if (xps == NULL) 781 return -EAGAIN; 782 xprt_iter_init_listall(xpi, xps); 783 xprt_switch_put(xps); 784 return 0; 785 } 786 787 /** 788 * rpc_clnt_iterate_for_each_xprt - Apply a function to all transports 789 * @clnt: pointer to client 790 * @fn: function to apply 791 * @data: void pointer to function data 792 * 793 * Iterates through the list of RPC transports currently attached to the 794 * client and applies the function fn(clnt, xprt, data). 795 * 796 * On error, the iteration stops, and the function returns the error value. 797 */ 798 int rpc_clnt_iterate_for_each_xprt(struct rpc_clnt *clnt, 799 int (*fn)(struct rpc_clnt *, struct rpc_xprt *, void *), 800 void *data) 801 { 802 struct rpc_xprt_iter xpi; 803 int ret; 804 805 ret = rpc_clnt_xprt_iter_init(clnt, &xpi); 806 if (ret) 807 return ret; 808 for (;;) { 809 struct rpc_xprt *xprt = xprt_iter_get_next(&xpi); 810 811 if (!xprt) 812 break; 813 ret = fn(clnt, xprt, data); 814 xprt_put(xprt); 815 if (ret < 0) 816 break; 817 } 818 xprt_iter_destroy(&xpi); 819 return ret; 820 } 821 EXPORT_SYMBOL_GPL(rpc_clnt_iterate_for_each_xprt); 822 823 /* 824 * Kill all tasks for the given client. 825 * XXX: kill their descendants as well? 826 */ 827 void rpc_killall_tasks(struct rpc_clnt *clnt) 828 { 829 struct rpc_task *rovr; 830 831 832 if (list_empty(&clnt->cl_tasks)) 833 return; 834 dprintk("RPC: killing all tasks for client %p\n", clnt); 835 /* 836 * Spin lock all_tasks to prevent changes... 837 */ 838 spin_lock(&clnt->cl_lock); 839 list_for_each_entry(rovr, &clnt->cl_tasks, tk_task) 840 rpc_signal_task(rovr); 841 spin_unlock(&clnt->cl_lock); 842 } 843 EXPORT_SYMBOL_GPL(rpc_killall_tasks); 844 845 /* 846 * Properly shut down an RPC client, terminating all outstanding 847 * requests. 848 */ 849 void rpc_shutdown_client(struct rpc_clnt *clnt) 850 { 851 might_sleep(); 852 853 dprintk_rcu("RPC: shutting down %s client for %s\n", 854 clnt->cl_program->name, 855 rcu_dereference(clnt->cl_xprt)->servername); 856 857 while (!list_empty(&clnt->cl_tasks)) { 858 rpc_killall_tasks(clnt); 859 wait_event_timeout(destroy_wait, 860 list_empty(&clnt->cl_tasks), 1*HZ); 861 } 862 863 rpc_release_client(clnt); 864 } 865 EXPORT_SYMBOL_GPL(rpc_shutdown_client); 866 867 /* 868 * Free an RPC client 869 */ 870 static struct rpc_clnt * 871 rpc_free_client(struct rpc_clnt *clnt) 872 { 873 struct rpc_clnt *parent = NULL; 874 875 dprintk_rcu("RPC: destroying %s client for %s\n", 876 clnt->cl_program->name, 877 rcu_dereference(clnt->cl_xprt)->servername); 878 if (clnt->cl_parent != clnt) 879 parent = clnt->cl_parent; 880 rpc_clnt_debugfs_unregister(clnt); 881 rpc_clnt_remove_pipedir(clnt); 882 rpc_unregister_client(clnt); 883 rpc_free_iostats(clnt->cl_metrics); 884 clnt->cl_metrics = NULL; 885 xprt_put(rcu_dereference_raw(clnt->cl_xprt)); 886 xprt_iter_destroy(&clnt->cl_xpi); 887 rpciod_down(); 888 put_cred(clnt->cl_cred); 889 rpc_free_clid(clnt); 890 kfree(clnt); 891 return parent; 892 } 893 894 /* 895 * Free an RPC client 896 */ 897 static struct rpc_clnt * 898 rpc_free_auth(struct rpc_clnt *clnt) 899 { 900 if (clnt->cl_auth == NULL) 901 return rpc_free_client(clnt); 902 903 /* 904 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to 905 * release remaining GSS contexts. This mechanism ensures 906 * that it can do so safely. 907 */ 908 atomic_inc(&clnt->cl_count); 909 rpcauth_release(clnt->cl_auth); 910 clnt->cl_auth = NULL; 911 if (atomic_dec_and_test(&clnt->cl_count)) 912 return rpc_free_client(clnt); 913 return NULL; 914 } 915 916 /* 917 * Release reference to the RPC client 918 */ 919 void 920 rpc_release_client(struct rpc_clnt *clnt) 921 { 922 dprintk("RPC: rpc_release_client(%p)\n", clnt); 923 924 do { 925 if (list_empty(&clnt->cl_tasks)) 926 wake_up(&destroy_wait); 927 if (!atomic_dec_and_test(&clnt->cl_count)) 928 break; 929 clnt = rpc_free_auth(clnt); 930 } while (clnt != NULL); 931 } 932 EXPORT_SYMBOL_GPL(rpc_release_client); 933 934 /** 935 * rpc_bind_new_program - bind a new RPC program to an existing client 936 * @old: old rpc_client 937 * @program: rpc program to set 938 * @vers: rpc program version 939 * 940 * Clones the rpc client and sets up a new RPC program. This is mainly 941 * of use for enabling different RPC programs to share the same transport. 942 * The Sun NFSv2/v3 ACL protocol can do this. 943 */ 944 struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old, 945 const struct rpc_program *program, 946 u32 vers) 947 { 948 struct rpc_create_args args = { 949 .program = program, 950 .prognumber = program->number, 951 .version = vers, 952 .authflavor = old->cl_auth->au_flavor, 953 .cred = old->cl_cred, 954 }; 955 struct rpc_clnt *clnt; 956 int err; 957 958 clnt = __rpc_clone_client(&args, old); 959 if (IS_ERR(clnt)) 960 goto out; 961 err = rpc_ping(clnt); 962 if (err != 0) { 963 rpc_shutdown_client(clnt); 964 clnt = ERR_PTR(err); 965 } 966 out: 967 return clnt; 968 } 969 EXPORT_SYMBOL_GPL(rpc_bind_new_program); 970 971 void rpc_task_release_transport(struct rpc_task *task) 972 { 973 struct rpc_xprt *xprt = task->tk_xprt; 974 975 if (xprt) { 976 task->tk_xprt = NULL; 977 xprt_put(xprt); 978 } 979 } 980 EXPORT_SYMBOL_GPL(rpc_task_release_transport); 981 982 void rpc_task_release_client(struct rpc_task *task) 983 { 984 struct rpc_clnt *clnt = task->tk_client; 985 986 if (clnt != NULL) { 987 /* Remove from client task list */ 988 spin_lock(&clnt->cl_lock); 989 list_del(&task->tk_task); 990 spin_unlock(&clnt->cl_lock); 991 task->tk_client = NULL; 992 993 rpc_release_client(clnt); 994 } 995 rpc_task_release_transport(task); 996 } 997 998 static 999 void rpc_task_set_transport(struct rpc_task *task, struct rpc_clnt *clnt) 1000 { 1001 if (!task->tk_xprt) 1002 task->tk_xprt = xprt_iter_get_next(&clnt->cl_xpi); 1003 } 1004 1005 static 1006 void rpc_task_set_client(struct rpc_task *task, struct rpc_clnt *clnt) 1007 { 1008 1009 if (clnt != NULL) { 1010 rpc_task_set_transport(task, clnt); 1011 task->tk_client = clnt; 1012 atomic_inc(&clnt->cl_count); 1013 if (clnt->cl_softrtry) 1014 task->tk_flags |= RPC_TASK_SOFT; 1015 if (clnt->cl_softerr) 1016 task->tk_flags |= RPC_TASK_TIMEOUT; 1017 if (clnt->cl_noretranstimeo) 1018 task->tk_flags |= RPC_TASK_NO_RETRANS_TIMEOUT; 1019 if (atomic_read(&clnt->cl_swapper)) 1020 task->tk_flags |= RPC_TASK_SWAPPER; 1021 /* Add to the client's list of all tasks */ 1022 spin_lock(&clnt->cl_lock); 1023 list_add_tail(&task->tk_task, &clnt->cl_tasks); 1024 spin_unlock(&clnt->cl_lock); 1025 } 1026 } 1027 1028 static void 1029 rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg) 1030 { 1031 if (msg != NULL) { 1032 task->tk_msg.rpc_proc = msg->rpc_proc; 1033 task->tk_msg.rpc_argp = msg->rpc_argp; 1034 task->tk_msg.rpc_resp = msg->rpc_resp; 1035 if (msg->rpc_cred != NULL) 1036 task->tk_msg.rpc_cred = get_cred(msg->rpc_cred); 1037 } 1038 } 1039 1040 /* 1041 * Default callback for async RPC calls 1042 */ 1043 static void 1044 rpc_default_callback(struct rpc_task *task, void *data) 1045 { 1046 } 1047 1048 static const struct rpc_call_ops rpc_default_ops = { 1049 .rpc_call_done = rpc_default_callback, 1050 }; 1051 1052 /** 1053 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it 1054 * @task_setup_data: pointer to task initialisation data 1055 */ 1056 struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data) 1057 { 1058 struct rpc_task *task; 1059 1060 task = rpc_new_task(task_setup_data); 1061 1062 rpc_task_set_client(task, task_setup_data->rpc_client); 1063 rpc_task_set_rpc_message(task, task_setup_data->rpc_message); 1064 1065 if (task->tk_action == NULL) 1066 rpc_call_start(task); 1067 1068 atomic_inc(&task->tk_count); 1069 rpc_execute(task); 1070 return task; 1071 } 1072 EXPORT_SYMBOL_GPL(rpc_run_task); 1073 1074 /** 1075 * rpc_call_sync - Perform a synchronous RPC call 1076 * @clnt: pointer to RPC client 1077 * @msg: RPC call parameters 1078 * @flags: RPC call flags 1079 */ 1080 int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags) 1081 { 1082 struct rpc_task *task; 1083 struct rpc_task_setup task_setup_data = { 1084 .rpc_client = clnt, 1085 .rpc_message = msg, 1086 .callback_ops = &rpc_default_ops, 1087 .flags = flags, 1088 }; 1089 int status; 1090 1091 WARN_ON_ONCE(flags & RPC_TASK_ASYNC); 1092 if (flags & RPC_TASK_ASYNC) { 1093 rpc_release_calldata(task_setup_data.callback_ops, 1094 task_setup_data.callback_data); 1095 return -EINVAL; 1096 } 1097 1098 task = rpc_run_task(&task_setup_data); 1099 if (IS_ERR(task)) 1100 return PTR_ERR(task); 1101 status = task->tk_status; 1102 rpc_put_task(task); 1103 return status; 1104 } 1105 EXPORT_SYMBOL_GPL(rpc_call_sync); 1106 1107 /** 1108 * rpc_call_async - Perform an asynchronous RPC call 1109 * @clnt: pointer to RPC client 1110 * @msg: RPC call parameters 1111 * @flags: RPC call flags 1112 * @tk_ops: RPC call ops 1113 * @data: user call data 1114 */ 1115 int 1116 rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags, 1117 const struct rpc_call_ops *tk_ops, void *data) 1118 { 1119 struct rpc_task *task; 1120 struct rpc_task_setup task_setup_data = { 1121 .rpc_client = clnt, 1122 .rpc_message = msg, 1123 .callback_ops = tk_ops, 1124 .callback_data = data, 1125 .flags = flags|RPC_TASK_ASYNC, 1126 }; 1127 1128 task = rpc_run_task(&task_setup_data); 1129 if (IS_ERR(task)) 1130 return PTR_ERR(task); 1131 rpc_put_task(task); 1132 return 0; 1133 } 1134 EXPORT_SYMBOL_GPL(rpc_call_async); 1135 1136 #if defined(CONFIG_SUNRPC_BACKCHANNEL) 1137 static void call_bc_encode(struct rpc_task *task); 1138 1139 /** 1140 * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run 1141 * rpc_execute against it 1142 * @req: RPC request 1143 */ 1144 struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req) 1145 { 1146 struct rpc_task *task; 1147 struct rpc_task_setup task_setup_data = { 1148 .callback_ops = &rpc_default_ops, 1149 .flags = RPC_TASK_SOFTCONN | 1150 RPC_TASK_NO_RETRANS_TIMEOUT, 1151 }; 1152 1153 dprintk("RPC: rpc_run_bc_task req= %p\n", req); 1154 /* 1155 * Create an rpc_task to send the data 1156 */ 1157 task = rpc_new_task(&task_setup_data); 1158 xprt_init_bc_request(req, task); 1159 1160 task->tk_action = call_bc_encode; 1161 atomic_inc(&task->tk_count); 1162 WARN_ON_ONCE(atomic_read(&task->tk_count) != 2); 1163 rpc_execute(task); 1164 1165 dprintk("RPC: rpc_run_bc_task: task= %p\n", task); 1166 return task; 1167 } 1168 #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 1169 1170 /** 1171 * rpc_prepare_reply_pages - Prepare to receive a reply data payload into pages 1172 * @req: RPC request to prepare 1173 * @pages: vector of struct page pointers 1174 * @base: offset in first page where receive should start, in bytes 1175 * @len: expected size of the upper layer data payload, in bytes 1176 * @hdrsize: expected size of upper layer reply header, in XDR words 1177 * 1178 */ 1179 void rpc_prepare_reply_pages(struct rpc_rqst *req, struct page **pages, 1180 unsigned int base, unsigned int len, 1181 unsigned int hdrsize) 1182 { 1183 /* Subtract one to force an extra word of buffer space for the 1184 * payload's XDR pad to fall into the rcv_buf's tail iovec. 1185 */ 1186 hdrsize += RPC_REPHDRSIZE + req->rq_cred->cr_auth->au_ralign - 1; 1187 1188 xdr_inline_pages(&req->rq_rcv_buf, hdrsize << 2, pages, base, len); 1189 trace_rpc_reply_pages(req); 1190 } 1191 EXPORT_SYMBOL_GPL(rpc_prepare_reply_pages); 1192 1193 void 1194 rpc_call_start(struct rpc_task *task) 1195 { 1196 task->tk_action = call_start; 1197 } 1198 EXPORT_SYMBOL_GPL(rpc_call_start); 1199 1200 /** 1201 * rpc_peeraddr - extract remote peer address from clnt's xprt 1202 * @clnt: RPC client structure 1203 * @buf: target buffer 1204 * @bufsize: length of target buffer 1205 * 1206 * Returns the number of bytes that are actually in the stored address. 1207 */ 1208 size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize) 1209 { 1210 size_t bytes; 1211 struct rpc_xprt *xprt; 1212 1213 rcu_read_lock(); 1214 xprt = rcu_dereference(clnt->cl_xprt); 1215 1216 bytes = xprt->addrlen; 1217 if (bytes > bufsize) 1218 bytes = bufsize; 1219 memcpy(buf, &xprt->addr, bytes); 1220 rcu_read_unlock(); 1221 1222 return bytes; 1223 } 1224 EXPORT_SYMBOL_GPL(rpc_peeraddr); 1225 1226 /** 1227 * rpc_peeraddr2str - return remote peer address in printable format 1228 * @clnt: RPC client structure 1229 * @format: address format 1230 * 1231 * NB: the lifetime of the memory referenced by the returned pointer is 1232 * the same as the rpc_xprt itself. As long as the caller uses this 1233 * pointer, it must hold the RCU read lock. 1234 */ 1235 const char *rpc_peeraddr2str(struct rpc_clnt *clnt, 1236 enum rpc_display_format_t format) 1237 { 1238 struct rpc_xprt *xprt; 1239 1240 xprt = rcu_dereference(clnt->cl_xprt); 1241 1242 if (xprt->address_strings[format] != NULL) 1243 return xprt->address_strings[format]; 1244 else 1245 return "unprintable"; 1246 } 1247 EXPORT_SYMBOL_GPL(rpc_peeraddr2str); 1248 1249 static const struct sockaddr_in rpc_inaddr_loopback = { 1250 .sin_family = AF_INET, 1251 .sin_addr.s_addr = htonl(INADDR_ANY), 1252 }; 1253 1254 static const struct sockaddr_in6 rpc_in6addr_loopback = { 1255 .sin6_family = AF_INET6, 1256 .sin6_addr = IN6ADDR_ANY_INIT, 1257 }; 1258 1259 /* 1260 * Try a getsockname() on a connected datagram socket. Using a 1261 * connected datagram socket prevents leaving a socket in TIME_WAIT. 1262 * This conserves the ephemeral port number space. 1263 * 1264 * Returns zero and fills in "buf" if successful; otherwise, a 1265 * negative errno is returned. 1266 */ 1267 static int rpc_sockname(struct net *net, struct sockaddr *sap, size_t salen, 1268 struct sockaddr *buf) 1269 { 1270 struct socket *sock; 1271 int err; 1272 1273 err = __sock_create(net, sap->sa_family, 1274 SOCK_DGRAM, IPPROTO_UDP, &sock, 1); 1275 if (err < 0) { 1276 dprintk("RPC: can't create UDP socket (%d)\n", err); 1277 goto out; 1278 } 1279 1280 switch (sap->sa_family) { 1281 case AF_INET: 1282 err = kernel_bind(sock, 1283 (struct sockaddr *)&rpc_inaddr_loopback, 1284 sizeof(rpc_inaddr_loopback)); 1285 break; 1286 case AF_INET6: 1287 err = kernel_bind(sock, 1288 (struct sockaddr *)&rpc_in6addr_loopback, 1289 sizeof(rpc_in6addr_loopback)); 1290 break; 1291 default: 1292 err = -EAFNOSUPPORT; 1293 goto out; 1294 } 1295 if (err < 0) { 1296 dprintk("RPC: can't bind UDP socket (%d)\n", err); 1297 goto out_release; 1298 } 1299 1300 err = kernel_connect(sock, sap, salen, 0); 1301 if (err < 0) { 1302 dprintk("RPC: can't connect UDP socket (%d)\n", err); 1303 goto out_release; 1304 } 1305 1306 err = kernel_getsockname(sock, buf); 1307 if (err < 0) { 1308 dprintk("RPC: getsockname failed (%d)\n", err); 1309 goto out_release; 1310 } 1311 1312 err = 0; 1313 if (buf->sa_family == AF_INET6) { 1314 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf; 1315 sin6->sin6_scope_id = 0; 1316 } 1317 dprintk("RPC: %s succeeded\n", __func__); 1318 1319 out_release: 1320 sock_release(sock); 1321 out: 1322 return err; 1323 } 1324 1325 /* 1326 * Scraping a connected socket failed, so we don't have a useable 1327 * local address. Fallback: generate an address that will prevent 1328 * the server from calling us back. 1329 * 1330 * Returns zero and fills in "buf" if successful; otherwise, a 1331 * negative errno is returned. 1332 */ 1333 static int rpc_anyaddr(int family, struct sockaddr *buf, size_t buflen) 1334 { 1335 switch (family) { 1336 case AF_INET: 1337 if (buflen < sizeof(rpc_inaddr_loopback)) 1338 return -EINVAL; 1339 memcpy(buf, &rpc_inaddr_loopback, 1340 sizeof(rpc_inaddr_loopback)); 1341 break; 1342 case AF_INET6: 1343 if (buflen < sizeof(rpc_in6addr_loopback)) 1344 return -EINVAL; 1345 memcpy(buf, &rpc_in6addr_loopback, 1346 sizeof(rpc_in6addr_loopback)); 1347 break; 1348 default: 1349 dprintk("RPC: %s: address family not supported\n", 1350 __func__); 1351 return -EAFNOSUPPORT; 1352 } 1353 dprintk("RPC: %s: succeeded\n", __func__); 1354 return 0; 1355 } 1356 1357 /** 1358 * rpc_localaddr - discover local endpoint address for an RPC client 1359 * @clnt: RPC client structure 1360 * @buf: target buffer 1361 * @buflen: size of target buffer, in bytes 1362 * 1363 * Returns zero and fills in "buf" and "buflen" if successful; 1364 * otherwise, a negative errno is returned. 1365 * 1366 * This works even if the underlying transport is not currently connected, 1367 * or if the upper layer never previously provided a source address. 1368 * 1369 * The result of this function call is transient: multiple calls in 1370 * succession may give different results, depending on how local 1371 * networking configuration changes over time. 1372 */ 1373 int rpc_localaddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t buflen) 1374 { 1375 struct sockaddr_storage address; 1376 struct sockaddr *sap = (struct sockaddr *)&address; 1377 struct rpc_xprt *xprt; 1378 struct net *net; 1379 size_t salen; 1380 int err; 1381 1382 rcu_read_lock(); 1383 xprt = rcu_dereference(clnt->cl_xprt); 1384 salen = xprt->addrlen; 1385 memcpy(sap, &xprt->addr, salen); 1386 net = get_net(xprt->xprt_net); 1387 rcu_read_unlock(); 1388 1389 rpc_set_port(sap, 0); 1390 err = rpc_sockname(net, sap, salen, buf); 1391 put_net(net); 1392 if (err != 0) 1393 /* Couldn't discover local address, return ANYADDR */ 1394 return rpc_anyaddr(sap->sa_family, buf, buflen); 1395 return 0; 1396 } 1397 EXPORT_SYMBOL_GPL(rpc_localaddr); 1398 1399 void 1400 rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize) 1401 { 1402 struct rpc_xprt *xprt; 1403 1404 rcu_read_lock(); 1405 xprt = rcu_dereference(clnt->cl_xprt); 1406 if (xprt->ops->set_buffer_size) 1407 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize); 1408 rcu_read_unlock(); 1409 } 1410 EXPORT_SYMBOL_GPL(rpc_setbufsize); 1411 1412 /** 1413 * rpc_net_ns - Get the network namespace for this RPC client 1414 * @clnt: RPC client to query 1415 * 1416 */ 1417 struct net *rpc_net_ns(struct rpc_clnt *clnt) 1418 { 1419 struct net *ret; 1420 1421 rcu_read_lock(); 1422 ret = rcu_dereference(clnt->cl_xprt)->xprt_net; 1423 rcu_read_unlock(); 1424 return ret; 1425 } 1426 EXPORT_SYMBOL_GPL(rpc_net_ns); 1427 1428 /** 1429 * rpc_max_payload - Get maximum payload size for a transport, in bytes 1430 * @clnt: RPC client to query 1431 * 1432 * For stream transports, this is one RPC record fragment (see RFC 1433 * 1831), as we don't support multi-record requests yet. For datagram 1434 * transports, this is the size of an IP packet minus the IP, UDP, and 1435 * RPC header sizes. 1436 */ 1437 size_t rpc_max_payload(struct rpc_clnt *clnt) 1438 { 1439 size_t ret; 1440 1441 rcu_read_lock(); 1442 ret = rcu_dereference(clnt->cl_xprt)->max_payload; 1443 rcu_read_unlock(); 1444 return ret; 1445 } 1446 EXPORT_SYMBOL_GPL(rpc_max_payload); 1447 1448 /** 1449 * rpc_max_bc_payload - Get maximum backchannel payload size, in bytes 1450 * @clnt: RPC client to query 1451 */ 1452 size_t rpc_max_bc_payload(struct rpc_clnt *clnt) 1453 { 1454 struct rpc_xprt *xprt; 1455 size_t ret; 1456 1457 rcu_read_lock(); 1458 xprt = rcu_dereference(clnt->cl_xprt); 1459 ret = xprt->ops->bc_maxpayload(xprt); 1460 rcu_read_unlock(); 1461 return ret; 1462 } 1463 EXPORT_SYMBOL_GPL(rpc_max_bc_payload); 1464 1465 /** 1466 * rpc_force_rebind - force transport to check that remote port is unchanged 1467 * @clnt: client to rebind 1468 * 1469 */ 1470 void rpc_force_rebind(struct rpc_clnt *clnt) 1471 { 1472 if (clnt->cl_autobind) { 1473 rcu_read_lock(); 1474 xprt_clear_bound(rcu_dereference(clnt->cl_xprt)); 1475 rcu_read_unlock(); 1476 } 1477 } 1478 EXPORT_SYMBOL_GPL(rpc_force_rebind); 1479 1480 static int 1481 __rpc_restart_call(struct rpc_task *task, void (*action)(struct rpc_task *)) 1482 { 1483 task->tk_status = 0; 1484 task->tk_rpc_status = 0; 1485 task->tk_action = action; 1486 return 1; 1487 } 1488 1489 /* 1490 * Restart an (async) RPC call. Usually called from within the 1491 * exit handler. 1492 */ 1493 int 1494 rpc_restart_call(struct rpc_task *task) 1495 { 1496 return __rpc_restart_call(task, call_start); 1497 } 1498 EXPORT_SYMBOL_GPL(rpc_restart_call); 1499 1500 /* 1501 * Restart an (async) RPC call from the call_prepare state. 1502 * Usually called from within the exit handler. 1503 */ 1504 int 1505 rpc_restart_call_prepare(struct rpc_task *task) 1506 { 1507 if (task->tk_ops->rpc_call_prepare != NULL) 1508 return __rpc_restart_call(task, rpc_prepare_task); 1509 return rpc_restart_call(task); 1510 } 1511 EXPORT_SYMBOL_GPL(rpc_restart_call_prepare); 1512 1513 const char 1514 *rpc_proc_name(const struct rpc_task *task) 1515 { 1516 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc; 1517 1518 if (proc) { 1519 if (proc->p_name) 1520 return proc->p_name; 1521 else 1522 return "NULL"; 1523 } else 1524 return "no proc"; 1525 } 1526 1527 static void 1528 __rpc_call_rpcerror(struct rpc_task *task, int tk_status, int rpc_status) 1529 { 1530 task->tk_rpc_status = rpc_status; 1531 rpc_exit(task, tk_status); 1532 } 1533 1534 static void 1535 rpc_call_rpcerror(struct rpc_task *task, int status) 1536 { 1537 __rpc_call_rpcerror(task, status, status); 1538 } 1539 1540 /* 1541 * 0. Initial state 1542 * 1543 * Other FSM states can be visited zero or more times, but 1544 * this state is visited exactly once for each RPC. 1545 */ 1546 static void 1547 call_start(struct rpc_task *task) 1548 { 1549 struct rpc_clnt *clnt = task->tk_client; 1550 int idx = task->tk_msg.rpc_proc->p_statidx; 1551 1552 trace_rpc_request(task); 1553 dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task->tk_pid, 1554 clnt->cl_program->name, clnt->cl_vers, 1555 rpc_proc_name(task), 1556 (RPC_IS_ASYNC(task) ? "async" : "sync")); 1557 1558 /* Increment call count (version might not be valid for ping) */ 1559 if (clnt->cl_program->version[clnt->cl_vers]) 1560 clnt->cl_program->version[clnt->cl_vers]->counts[idx]++; 1561 clnt->cl_stats->rpccnt++; 1562 task->tk_action = call_reserve; 1563 rpc_task_set_transport(task, clnt); 1564 } 1565 1566 /* 1567 * 1. Reserve an RPC call slot 1568 */ 1569 static void 1570 call_reserve(struct rpc_task *task) 1571 { 1572 dprint_status(task); 1573 1574 task->tk_status = 0; 1575 task->tk_action = call_reserveresult; 1576 xprt_reserve(task); 1577 } 1578 1579 static void call_retry_reserve(struct rpc_task *task); 1580 1581 /* 1582 * 1b. Grok the result of xprt_reserve() 1583 */ 1584 static void 1585 call_reserveresult(struct rpc_task *task) 1586 { 1587 int status = task->tk_status; 1588 1589 dprint_status(task); 1590 1591 /* 1592 * After a call to xprt_reserve(), we must have either 1593 * a request slot or else an error status. 1594 */ 1595 task->tk_status = 0; 1596 if (status >= 0) { 1597 if (task->tk_rqstp) { 1598 task->tk_action = call_refresh; 1599 return; 1600 } 1601 1602 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n", 1603 __func__, status); 1604 rpc_call_rpcerror(task, -EIO); 1605 return; 1606 } 1607 1608 /* 1609 * Even though there was an error, we may have acquired 1610 * a request slot somehow. Make sure not to leak it. 1611 */ 1612 if (task->tk_rqstp) { 1613 printk(KERN_ERR "%s: status=%d, request allocated anyway\n", 1614 __func__, status); 1615 xprt_release(task); 1616 } 1617 1618 switch (status) { 1619 case -ENOMEM: 1620 rpc_delay(task, HZ >> 2); 1621 /* fall through */ 1622 case -EAGAIN: /* woken up; retry */ 1623 task->tk_action = call_retry_reserve; 1624 return; 1625 case -EIO: /* probably a shutdown */ 1626 break; 1627 default: 1628 printk(KERN_ERR "%s: unrecognized error %d, exiting\n", 1629 __func__, status); 1630 break; 1631 } 1632 rpc_call_rpcerror(task, status); 1633 } 1634 1635 /* 1636 * 1c. Retry reserving an RPC call slot 1637 */ 1638 static void 1639 call_retry_reserve(struct rpc_task *task) 1640 { 1641 dprint_status(task); 1642 1643 task->tk_status = 0; 1644 task->tk_action = call_reserveresult; 1645 xprt_retry_reserve(task); 1646 } 1647 1648 /* 1649 * 2. Bind and/or refresh the credentials 1650 */ 1651 static void 1652 call_refresh(struct rpc_task *task) 1653 { 1654 dprint_status(task); 1655 1656 task->tk_action = call_refreshresult; 1657 task->tk_status = 0; 1658 task->tk_client->cl_stats->rpcauthrefresh++; 1659 rpcauth_refreshcred(task); 1660 } 1661 1662 /* 1663 * 2a. Process the results of a credential refresh 1664 */ 1665 static void 1666 call_refreshresult(struct rpc_task *task) 1667 { 1668 int status = task->tk_status; 1669 1670 dprint_status(task); 1671 1672 task->tk_status = 0; 1673 task->tk_action = call_refresh; 1674 switch (status) { 1675 case 0: 1676 if (rpcauth_uptodatecred(task)) { 1677 task->tk_action = call_allocate; 1678 return; 1679 } 1680 /* Use rate-limiting and a max number of retries if refresh 1681 * had status 0 but failed to update the cred. 1682 */ 1683 /* fall through */ 1684 case -ETIMEDOUT: 1685 rpc_delay(task, 3*HZ); 1686 /* fall through */ 1687 case -EAGAIN: 1688 status = -EACCES; 1689 /* fall through */ 1690 case -EKEYEXPIRED: 1691 if (!task->tk_cred_retry) 1692 break; 1693 task->tk_cred_retry--; 1694 dprintk("RPC: %5u %s: retry refresh creds\n", 1695 task->tk_pid, __func__); 1696 return; 1697 } 1698 dprintk("RPC: %5u %s: refresh creds failed with error %d\n", 1699 task->tk_pid, __func__, status); 1700 rpc_call_rpcerror(task, status); 1701 } 1702 1703 /* 1704 * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc. 1705 * (Note: buffer memory is freed in xprt_release). 1706 */ 1707 static void 1708 call_allocate(struct rpc_task *task) 1709 { 1710 const struct rpc_auth *auth = task->tk_rqstp->rq_cred->cr_auth; 1711 struct rpc_rqst *req = task->tk_rqstp; 1712 struct rpc_xprt *xprt = req->rq_xprt; 1713 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc; 1714 int status; 1715 1716 dprint_status(task); 1717 1718 task->tk_status = 0; 1719 task->tk_action = call_encode; 1720 1721 if (req->rq_buffer) 1722 return; 1723 1724 if (proc->p_proc != 0) { 1725 BUG_ON(proc->p_arglen == 0); 1726 if (proc->p_decode != NULL) 1727 BUG_ON(proc->p_replen == 0); 1728 } 1729 1730 /* 1731 * Calculate the size (in quads) of the RPC call 1732 * and reply headers, and convert both values 1733 * to byte sizes. 1734 */ 1735 req->rq_callsize = RPC_CALLHDRSIZE + (auth->au_cslack << 1) + 1736 proc->p_arglen; 1737 req->rq_callsize <<= 2; 1738 /* 1739 * Note: the reply buffer must at minimum allocate enough space 1740 * for the 'struct accepted_reply' from RFC5531. 1741 */ 1742 req->rq_rcvsize = RPC_REPHDRSIZE + auth->au_rslack + \ 1743 max_t(size_t, proc->p_replen, 2); 1744 req->rq_rcvsize <<= 2; 1745 1746 status = xprt->ops->buf_alloc(task); 1747 xprt_inject_disconnect(xprt); 1748 if (status == 0) 1749 return; 1750 if (status != -ENOMEM) { 1751 rpc_call_rpcerror(task, status); 1752 return; 1753 } 1754 1755 dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid); 1756 1757 if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) { 1758 task->tk_action = call_allocate; 1759 rpc_delay(task, HZ>>4); 1760 return; 1761 } 1762 1763 rpc_exit(task, -ERESTARTSYS); 1764 } 1765 1766 static int 1767 rpc_task_need_encode(struct rpc_task *task) 1768 { 1769 return test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) == 0 && 1770 (!(task->tk_flags & RPC_TASK_SENT) || 1771 !(task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) || 1772 xprt_request_need_retransmit(task)); 1773 } 1774 1775 static void 1776 rpc_xdr_encode(struct rpc_task *task) 1777 { 1778 struct rpc_rqst *req = task->tk_rqstp; 1779 struct xdr_stream xdr; 1780 1781 xdr_buf_init(&req->rq_snd_buf, 1782 req->rq_buffer, 1783 req->rq_callsize); 1784 xdr_buf_init(&req->rq_rcv_buf, 1785 req->rq_rbuffer, 1786 req->rq_rcvsize); 1787 1788 req->rq_snd_buf.head[0].iov_len = 0; 1789 xdr_init_encode(&xdr, &req->rq_snd_buf, 1790 req->rq_snd_buf.head[0].iov_base, req); 1791 if (rpc_encode_header(task, &xdr)) 1792 return; 1793 1794 task->tk_status = rpcauth_wrap_req(task, &xdr); 1795 } 1796 1797 /* 1798 * 3. Encode arguments of an RPC call 1799 */ 1800 static void 1801 call_encode(struct rpc_task *task) 1802 { 1803 if (!rpc_task_need_encode(task)) 1804 goto out; 1805 dprint_status(task); 1806 /* Encode here so that rpcsec_gss can use correct sequence number. */ 1807 rpc_xdr_encode(task); 1808 /* Did the encode result in an error condition? */ 1809 if (task->tk_status != 0) { 1810 /* Was the error nonfatal? */ 1811 switch (task->tk_status) { 1812 case -EAGAIN: 1813 case -ENOMEM: 1814 rpc_delay(task, HZ >> 4); 1815 break; 1816 case -EKEYEXPIRED: 1817 if (!task->tk_cred_retry) { 1818 rpc_exit(task, task->tk_status); 1819 } else { 1820 task->tk_action = call_refresh; 1821 task->tk_cred_retry--; 1822 dprintk("RPC: %5u %s: retry refresh creds\n", 1823 task->tk_pid, __func__); 1824 } 1825 break; 1826 default: 1827 rpc_call_rpcerror(task, task->tk_status); 1828 } 1829 return; 1830 } else { 1831 xprt_request_prepare(task->tk_rqstp); 1832 } 1833 1834 /* Add task to reply queue before transmission to avoid races */ 1835 if (rpc_reply_expected(task)) 1836 xprt_request_enqueue_receive(task); 1837 xprt_request_enqueue_transmit(task); 1838 out: 1839 task->tk_action = call_transmit; 1840 /* Check that the connection is OK */ 1841 if (!xprt_bound(task->tk_xprt)) 1842 task->tk_action = call_bind; 1843 else if (!xprt_connected(task->tk_xprt)) 1844 task->tk_action = call_connect; 1845 } 1846 1847 /* 1848 * Helpers to check if the task was already transmitted, and 1849 * to take action when that is the case. 1850 */ 1851 static bool 1852 rpc_task_transmitted(struct rpc_task *task) 1853 { 1854 return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate); 1855 } 1856 1857 static void 1858 rpc_task_handle_transmitted(struct rpc_task *task) 1859 { 1860 xprt_end_transmit(task); 1861 task->tk_action = call_transmit_status; 1862 } 1863 1864 /* 1865 * 4. Get the server port number if not yet set 1866 */ 1867 static void 1868 call_bind(struct rpc_task *task) 1869 { 1870 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt; 1871 1872 if (rpc_task_transmitted(task)) { 1873 rpc_task_handle_transmitted(task); 1874 return; 1875 } 1876 1877 if (xprt_bound(xprt)) { 1878 task->tk_action = call_connect; 1879 return; 1880 } 1881 1882 dprint_status(task); 1883 1884 task->tk_action = call_bind_status; 1885 if (!xprt_prepare_transmit(task)) 1886 return; 1887 1888 xprt->ops->rpcbind(task); 1889 } 1890 1891 /* 1892 * 4a. Sort out bind result 1893 */ 1894 static void 1895 call_bind_status(struct rpc_task *task) 1896 { 1897 int status = -EIO; 1898 1899 if (rpc_task_transmitted(task)) { 1900 rpc_task_handle_transmitted(task); 1901 return; 1902 } 1903 1904 if (task->tk_status >= 0) { 1905 dprint_status(task); 1906 task->tk_status = 0; 1907 task->tk_action = call_connect; 1908 return; 1909 } 1910 1911 trace_rpc_bind_status(task); 1912 switch (task->tk_status) { 1913 case -ENOMEM: 1914 dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid); 1915 rpc_delay(task, HZ >> 2); 1916 goto retry_timeout; 1917 case -EACCES: 1918 dprintk("RPC: %5u remote rpcbind: RPC program/version " 1919 "unavailable\n", task->tk_pid); 1920 /* fail immediately if this is an RPC ping */ 1921 if (task->tk_msg.rpc_proc->p_proc == 0) { 1922 status = -EOPNOTSUPP; 1923 break; 1924 } 1925 if (task->tk_rebind_retry == 0) 1926 break; 1927 task->tk_rebind_retry--; 1928 rpc_delay(task, 3*HZ); 1929 goto retry_timeout; 1930 case -EAGAIN: 1931 goto retry_timeout; 1932 case -ETIMEDOUT: 1933 dprintk("RPC: %5u rpcbind request timed out\n", 1934 task->tk_pid); 1935 goto retry_timeout; 1936 case -EPFNOSUPPORT: 1937 /* server doesn't support any rpcbind version we know of */ 1938 dprintk("RPC: %5u unrecognized remote rpcbind service\n", 1939 task->tk_pid); 1940 break; 1941 case -EPROTONOSUPPORT: 1942 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n", 1943 task->tk_pid); 1944 goto retry_timeout; 1945 case -ECONNREFUSED: /* connection problems */ 1946 case -ECONNRESET: 1947 case -ECONNABORTED: 1948 case -ENOTCONN: 1949 case -EHOSTDOWN: 1950 case -ENETDOWN: 1951 case -EHOSTUNREACH: 1952 case -ENETUNREACH: 1953 case -ENOBUFS: 1954 case -EPIPE: 1955 dprintk("RPC: %5u remote rpcbind unreachable: %d\n", 1956 task->tk_pid, task->tk_status); 1957 if (!RPC_IS_SOFTCONN(task)) { 1958 rpc_delay(task, 5*HZ); 1959 goto retry_timeout; 1960 } 1961 status = task->tk_status; 1962 break; 1963 default: 1964 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n", 1965 task->tk_pid, -task->tk_status); 1966 } 1967 1968 rpc_call_rpcerror(task, status); 1969 return; 1970 1971 retry_timeout: 1972 task->tk_status = 0; 1973 task->tk_action = call_bind; 1974 rpc_check_timeout(task); 1975 } 1976 1977 /* 1978 * 4b. Connect to the RPC server 1979 */ 1980 static void 1981 call_connect(struct rpc_task *task) 1982 { 1983 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt; 1984 1985 if (rpc_task_transmitted(task)) { 1986 rpc_task_handle_transmitted(task); 1987 return; 1988 } 1989 1990 if (xprt_connected(xprt)) { 1991 task->tk_action = call_transmit; 1992 return; 1993 } 1994 1995 dprintk("RPC: %5u call_connect xprt %p %s connected\n", 1996 task->tk_pid, xprt, 1997 (xprt_connected(xprt) ? "is" : "is not")); 1998 1999 task->tk_action = call_connect_status; 2000 if (task->tk_status < 0) 2001 return; 2002 if (task->tk_flags & RPC_TASK_NOCONNECT) { 2003 rpc_call_rpcerror(task, -ENOTCONN); 2004 return; 2005 } 2006 if (!xprt_prepare_transmit(task)) 2007 return; 2008 xprt_connect(task); 2009 } 2010 2011 /* 2012 * 4c. Sort out connect result 2013 */ 2014 static void 2015 call_connect_status(struct rpc_task *task) 2016 { 2017 struct rpc_clnt *clnt = task->tk_client; 2018 int status = task->tk_status; 2019 2020 if (rpc_task_transmitted(task)) { 2021 rpc_task_handle_transmitted(task); 2022 return; 2023 } 2024 2025 dprint_status(task); 2026 2027 trace_rpc_connect_status(task); 2028 task->tk_status = 0; 2029 switch (status) { 2030 case -ECONNREFUSED: 2031 /* A positive refusal suggests a rebind is needed. */ 2032 if (RPC_IS_SOFTCONN(task)) 2033 break; 2034 if (clnt->cl_autobind) { 2035 rpc_force_rebind(clnt); 2036 goto out_retry; 2037 } 2038 /* fall through */ 2039 case -ECONNRESET: 2040 case -ECONNABORTED: 2041 case -ENETDOWN: 2042 case -ENETUNREACH: 2043 case -EHOSTUNREACH: 2044 case -EADDRINUSE: 2045 case -ENOBUFS: 2046 case -EPIPE: 2047 xprt_conditional_disconnect(task->tk_rqstp->rq_xprt, 2048 task->tk_rqstp->rq_connect_cookie); 2049 if (RPC_IS_SOFTCONN(task)) 2050 break; 2051 /* retry with existing socket, after a delay */ 2052 rpc_delay(task, 3*HZ); 2053 /* fall through */ 2054 case -ENOTCONN: 2055 case -EAGAIN: 2056 case -ETIMEDOUT: 2057 goto out_retry; 2058 case 0: 2059 clnt->cl_stats->netreconn++; 2060 task->tk_action = call_transmit; 2061 return; 2062 } 2063 rpc_call_rpcerror(task, status); 2064 return; 2065 out_retry: 2066 /* Check for timeouts before looping back to call_bind */ 2067 task->tk_action = call_bind; 2068 rpc_check_timeout(task); 2069 } 2070 2071 /* 2072 * 5. Transmit the RPC request, and wait for reply 2073 */ 2074 static void 2075 call_transmit(struct rpc_task *task) 2076 { 2077 if (rpc_task_transmitted(task)) { 2078 rpc_task_handle_transmitted(task); 2079 return; 2080 } 2081 2082 dprint_status(task); 2083 2084 task->tk_action = call_transmit_status; 2085 if (!xprt_prepare_transmit(task)) 2086 return; 2087 task->tk_status = 0; 2088 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) { 2089 if (!xprt_connected(task->tk_xprt)) { 2090 task->tk_status = -ENOTCONN; 2091 return; 2092 } 2093 xprt_transmit(task); 2094 } 2095 xprt_end_transmit(task); 2096 } 2097 2098 /* 2099 * 5a. Handle cleanup after a transmission 2100 */ 2101 static void 2102 call_transmit_status(struct rpc_task *task) 2103 { 2104 task->tk_action = call_status; 2105 2106 /* 2107 * Common case: success. Force the compiler to put this 2108 * test first. 2109 */ 2110 if (rpc_task_transmitted(task)) { 2111 task->tk_status = 0; 2112 xprt_request_wait_receive(task); 2113 return; 2114 } 2115 2116 switch (task->tk_status) { 2117 default: 2118 dprint_status(task); 2119 break; 2120 case -EBADMSG: 2121 task->tk_status = 0; 2122 task->tk_action = call_encode; 2123 break; 2124 /* 2125 * Special cases: if we've been waiting on the 2126 * socket's write_space() callback, or if the 2127 * socket just returned a connection error, 2128 * then hold onto the transport lock. 2129 */ 2130 case -ENOBUFS: 2131 rpc_delay(task, HZ>>2); 2132 /* fall through */ 2133 case -EBADSLT: 2134 case -EAGAIN: 2135 task->tk_action = call_transmit; 2136 task->tk_status = 0; 2137 break; 2138 case -ECONNREFUSED: 2139 case -EHOSTDOWN: 2140 case -ENETDOWN: 2141 case -EHOSTUNREACH: 2142 case -ENETUNREACH: 2143 case -EPERM: 2144 if (RPC_IS_SOFTCONN(task)) { 2145 if (!task->tk_msg.rpc_proc->p_proc) 2146 trace_xprt_ping(task->tk_xprt, 2147 task->tk_status); 2148 rpc_call_rpcerror(task, task->tk_status); 2149 return; 2150 } 2151 /* fall through */ 2152 case -ECONNRESET: 2153 case -ECONNABORTED: 2154 case -EADDRINUSE: 2155 case -ENOTCONN: 2156 case -EPIPE: 2157 task->tk_action = call_bind; 2158 task->tk_status = 0; 2159 break; 2160 } 2161 rpc_check_timeout(task); 2162 } 2163 2164 #if defined(CONFIG_SUNRPC_BACKCHANNEL) 2165 static void call_bc_transmit(struct rpc_task *task); 2166 static void call_bc_transmit_status(struct rpc_task *task); 2167 2168 static void 2169 call_bc_encode(struct rpc_task *task) 2170 { 2171 xprt_request_enqueue_transmit(task); 2172 task->tk_action = call_bc_transmit; 2173 } 2174 2175 /* 2176 * 5b. Send the backchannel RPC reply. On error, drop the reply. In 2177 * addition, disconnect on connectivity errors. 2178 */ 2179 static void 2180 call_bc_transmit(struct rpc_task *task) 2181 { 2182 task->tk_action = call_bc_transmit_status; 2183 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) { 2184 if (!xprt_prepare_transmit(task)) 2185 return; 2186 task->tk_status = 0; 2187 xprt_transmit(task); 2188 } 2189 xprt_end_transmit(task); 2190 } 2191 2192 static void 2193 call_bc_transmit_status(struct rpc_task *task) 2194 { 2195 struct rpc_rqst *req = task->tk_rqstp; 2196 2197 if (rpc_task_transmitted(task)) 2198 task->tk_status = 0; 2199 2200 dprint_status(task); 2201 2202 switch (task->tk_status) { 2203 case 0: 2204 /* Success */ 2205 case -ENETDOWN: 2206 case -EHOSTDOWN: 2207 case -EHOSTUNREACH: 2208 case -ENETUNREACH: 2209 case -ECONNRESET: 2210 case -ECONNREFUSED: 2211 case -EADDRINUSE: 2212 case -ENOTCONN: 2213 case -EPIPE: 2214 break; 2215 case -ENOBUFS: 2216 rpc_delay(task, HZ>>2); 2217 /* fall through */ 2218 case -EBADSLT: 2219 case -EAGAIN: 2220 task->tk_status = 0; 2221 task->tk_action = call_bc_transmit; 2222 return; 2223 case -ETIMEDOUT: 2224 /* 2225 * Problem reaching the server. Disconnect and let the 2226 * forechannel reestablish the connection. The server will 2227 * have to retransmit the backchannel request and we'll 2228 * reprocess it. Since these ops are idempotent, there's no 2229 * need to cache our reply at this time. 2230 */ 2231 printk(KERN_NOTICE "RPC: Could not send backchannel reply " 2232 "error: %d\n", task->tk_status); 2233 xprt_conditional_disconnect(req->rq_xprt, 2234 req->rq_connect_cookie); 2235 break; 2236 default: 2237 /* 2238 * We were unable to reply and will have to drop the 2239 * request. The server should reconnect and retransmit. 2240 */ 2241 printk(KERN_NOTICE "RPC: Could not send backchannel reply " 2242 "error: %d\n", task->tk_status); 2243 break; 2244 } 2245 task->tk_action = rpc_exit_task; 2246 } 2247 #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 2248 2249 /* 2250 * 6. Sort out the RPC call status 2251 */ 2252 static void 2253 call_status(struct rpc_task *task) 2254 { 2255 struct rpc_clnt *clnt = task->tk_client; 2256 int status; 2257 2258 if (!task->tk_msg.rpc_proc->p_proc) 2259 trace_xprt_ping(task->tk_xprt, task->tk_status); 2260 2261 dprint_status(task); 2262 2263 status = task->tk_status; 2264 if (status >= 0) { 2265 task->tk_action = call_decode; 2266 return; 2267 } 2268 2269 trace_rpc_call_status(task); 2270 task->tk_status = 0; 2271 switch(status) { 2272 case -EHOSTDOWN: 2273 case -ENETDOWN: 2274 case -EHOSTUNREACH: 2275 case -ENETUNREACH: 2276 case -EPERM: 2277 if (RPC_IS_SOFTCONN(task)) 2278 goto out_exit; 2279 /* 2280 * Delay any retries for 3 seconds, then handle as if it 2281 * were a timeout. 2282 */ 2283 rpc_delay(task, 3*HZ); 2284 /* fall through */ 2285 case -ETIMEDOUT: 2286 break; 2287 case -ECONNREFUSED: 2288 case -ECONNRESET: 2289 case -ECONNABORTED: 2290 case -ENOTCONN: 2291 rpc_force_rebind(clnt); 2292 /* fall through */ 2293 case -EADDRINUSE: 2294 rpc_delay(task, 3*HZ); 2295 /* fall through */ 2296 case -EPIPE: 2297 case -EAGAIN: 2298 break; 2299 case -EIO: 2300 /* shutdown or soft timeout */ 2301 goto out_exit; 2302 default: 2303 if (clnt->cl_chatty) 2304 printk("%s: RPC call returned error %d\n", 2305 clnt->cl_program->name, -status); 2306 goto out_exit; 2307 } 2308 task->tk_action = call_encode; 2309 rpc_check_timeout(task); 2310 return; 2311 out_exit: 2312 rpc_call_rpcerror(task, status); 2313 } 2314 2315 static bool 2316 rpc_check_connected(const struct rpc_rqst *req) 2317 { 2318 /* No allocated request or transport? return true */ 2319 if (!req || !req->rq_xprt) 2320 return true; 2321 return xprt_connected(req->rq_xprt); 2322 } 2323 2324 static void 2325 rpc_check_timeout(struct rpc_task *task) 2326 { 2327 struct rpc_clnt *clnt = task->tk_client; 2328 2329 if (xprt_adjust_timeout(task->tk_rqstp) == 0) 2330 return; 2331 2332 dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid); 2333 task->tk_timeouts++; 2334 2335 if (RPC_IS_SOFTCONN(task) && !rpc_check_connected(task->tk_rqstp)) { 2336 rpc_call_rpcerror(task, -ETIMEDOUT); 2337 return; 2338 } 2339 2340 if (RPC_IS_SOFT(task)) { 2341 /* 2342 * Once a "no retrans timeout" soft tasks (a.k.a NFSv4) has 2343 * been sent, it should time out only if the transport 2344 * connection gets terminally broken. 2345 */ 2346 if ((task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) && 2347 rpc_check_connected(task->tk_rqstp)) 2348 return; 2349 2350 if (clnt->cl_chatty) { 2351 pr_notice_ratelimited( 2352 "%s: server %s not responding, timed out\n", 2353 clnt->cl_program->name, 2354 task->tk_xprt->servername); 2355 } 2356 if (task->tk_flags & RPC_TASK_TIMEOUT) 2357 rpc_call_rpcerror(task, -ETIMEDOUT); 2358 else 2359 __rpc_call_rpcerror(task, -EIO, -ETIMEDOUT); 2360 return; 2361 } 2362 2363 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) { 2364 task->tk_flags |= RPC_CALL_MAJORSEEN; 2365 if (clnt->cl_chatty) { 2366 pr_notice_ratelimited( 2367 "%s: server %s not responding, still trying\n", 2368 clnt->cl_program->name, 2369 task->tk_xprt->servername); 2370 } 2371 } 2372 rpc_force_rebind(clnt); 2373 /* 2374 * Did our request time out due to an RPCSEC_GSS out-of-sequence 2375 * event? RFC2203 requires the server to drop all such requests. 2376 */ 2377 rpcauth_invalcred(task); 2378 } 2379 2380 /* 2381 * 7. Decode the RPC reply 2382 */ 2383 static void 2384 call_decode(struct rpc_task *task) 2385 { 2386 struct rpc_clnt *clnt = task->tk_client; 2387 struct rpc_rqst *req = task->tk_rqstp; 2388 struct xdr_stream xdr; 2389 2390 dprint_status(task); 2391 2392 if (!task->tk_msg.rpc_proc->p_decode) { 2393 task->tk_action = rpc_exit_task; 2394 return; 2395 } 2396 2397 if (task->tk_flags & RPC_CALL_MAJORSEEN) { 2398 if (clnt->cl_chatty) { 2399 pr_notice_ratelimited("%s: server %s OK\n", 2400 clnt->cl_program->name, 2401 task->tk_xprt->servername); 2402 } 2403 task->tk_flags &= ~RPC_CALL_MAJORSEEN; 2404 } 2405 2406 /* 2407 * Ensure that we see all writes made by xprt_complete_rqst() 2408 * before it changed req->rq_reply_bytes_recvd. 2409 */ 2410 smp_rmb(); 2411 req->rq_rcv_buf.len = req->rq_private_buf.len; 2412 2413 /* Check that the softirq receive buffer is valid */ 2414 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf, 2415 sizeof(req->rq_rcv_buf)) != 0); 2416 2417 xdr_init_decode(&xdr, &req->rq_rcv_buf, 2418 req->rq_rcv_buf.head[0].iov_base, req); 2419 switch (rpc_decode_header(task, &xdr)) { 2420 case 0: 2421 task->tk_action = rpc_exit_task; 2422 task->tk_status = rpcauth_unwrap_resp(task, &xdr); 2423 dprintk("RPC: %5u %s result %d\n", 2424 task->tk_pid, __func__, task->tk_status); 2425 return; 2426 case -EAGAIN: 2427 task->tk_status = 0; 2428 xdr_free_bvec(&req->rq_rcv_buf); 2429 req->rq_reply_bytes_recvd = 0; 2430 req->rq_rcv_buf.len = 0; 2431 if (task->tk_client->cl_discrtry) 2432 xprt_conditional_disconnect(req->rq_xprt, 2433 req->rq_connect_cookie); 2434 task->tk_action = call_encode; 2435 rpc_check_timeout(task); 2436 break; 2437 case -EKEYREJECTED: 2438 task->tk_action = call_reserve; 2439 rpc_check_timeout(task); 2440 rpcauth_invalcred(task); 2441 /* Ensure we obtain a new XID if we retry! */ 2442 xprt_release(task); 2443 } 2444 } 2445 2446 static int 2447 rpc_encode_header(struct rpc_task *task, struct xdr_stream *xdr) 2448 { 2449 struct rpc_clnt *clnt = task->tk_client; 2450 struct rpc_rqst *req = task->tk_rqstp; 2451 __be32 *p; 2452 int error; 2453 2454 error = -EMSGSIZE; 2455 p = xdr_reserve_space(xdr, RPC_CALLHDRSIZE << 2); 2456 if (!p) 2457 goto out_fail; 2458 *p++ = req->rq_xid; 2459 *p++ = rpc_call; 2460 *p++ = cpu_to_be32(RPC_VERSION); 2461 *p++ = cpu_to_be32(clnt->cl_prog); 2462 *p++ = cpu_to_be32(clnt->cl_vers); 2463 *p = cpu_to_be32(task->tk_msg.rpc_proc->p_proc); 2464 2465 error = rpcauth_marshcred(task, xdr); 2466 if (error < 0) 2467 goto out_fail; 2468 return 0; 2469 out_fail: 2470 trace_rpc_bad_callhdr(task); 2471 rpc_exit(task, error); 2472 return error; 2473 } 2474 2475 static noinline int 2476 rpc_decode_header(struct rpc_task *task, struct xdr_stream *xdr) 2477 { 2478 struct rpc_clnt *clnt = task->tk_client; 2479 int error; 2480 __be32 *p; 2481 2482 /* RFC-1014 says that the representation of XDR data must be a 2483 * multiple of four bytes 2484 * - if it isn't pointer subtraction in the NFS client may give 2485 * undefined results 2486 */ 2487 if (task->tk_rqstp->rq_rcv_buf.len & 3) 2488 goto out_unparsable; 2489 2490 p = xdr_inline_decode(xdr, 3 * sizeof(*p)); 2491 if (!p) 2492 goto out_unparsable; 2493 p++; /* skip XID */ 2494 if (*p++ != rpc_reply) 2495 goto out_unparsable; 2496 if (*p++ != rpc_msg_accepted) 2497 goto out_msg_denied; 2498 2499 error = rpcauth_checkverf(task, xdr); 2500 if (error) 2501 goto out_verifier; 2502 2503 p = xdr_inline_decode(xdr, sizeof(*p)); 2504 if (!p) 2505 goto out_unparsable; 2506 switch (*p) { 2507 case rpc_success: 2508 return 0; 2509 case rpc_prog_unavail: 2510 trace_rpc__prog_unavail(task); 2511 error = -EPFNOSUPPORT; 2512 goto out_err; 2513 case rpc_prog_mismatch: 2514 trace_rpc__prog_mismatch(task); 2515 error = -EPROTONOSUPPORT; 2516 goto out_err; 2517 case rpc_proc_unavail: 2518 trace_rpc__proc_unavail(task); 2519 error = -EOPNOTSUPP; 2520 goto out_err; 2521 case rpc_garbage_args: 2522 case rpc_system_err: 2523 trace_rpc__garbage_args(task); 2524 error = -EIO; 2525 break; 2526 default: 2527 goto out_unparsable; 2528 } 2529 2530 out_garbage: 2531 clnt->cl_stats->rpcgarbage++; 2532 if (task->tk_garb_retry) { 2533 task->tk_garb_retry--; 2534 task->tk_action = call_encode; 2535 return -EAGAIN; 2536 } 2537 out_err: 2538 rpc_exit(task, error); 2539 return error; 2540 2541 out_unparsable: 2542 trace_rpc__unparsable(task); 2543 error = -EIO; 2544 goto out_garbage; 2545 2546 out_verifier: 2547 trace_rpc_bad_verifier(task); 2548 goto out_garbage; 2549 2550 out_msg_denied: 2551 error = -EACCES; 2552 p = xdr_inline_decode(xdr, sizeof(*p)); 2553 if (!p) 2554 goto out_unparsable; 2555 switch (*p++) { 2556 case rpc_auth_error: 2557 break; 2558 case rpc_mismatch: 2559 trace_rpc__mismatch(task); 2560 error = -EPROTONOSUPPORT; 2561 goto out_err; 2562 default: 2563 goto out_unparsable; 2564 } 2565 2566 p = xdr_inline_decode(xdr, sizeof(*p)); 2567 if (!p) 2568 goto out_unparsable; 2569 switch (*p++) { 2570 case rpc_autherr_rejectedcred: 2571 case rpc_autherr_rejectedverf: 2572 case rpcsec_gsserr_credproblem: 2573 case rpcsec_gsserr_ctxproblem: 2574 if (!task->tk_cred_retry) 2575 break; 2576 task->tk_cred_retry--; 2577 trace_rpc__stale_creds(task); 2578 return -EKEYREJECTED; 2579 case rpc_autherr_badcred: 2580 case rpc_autherr_badverf: 2581 /* possibly garbled cred/verf? */ 2582 if (!task->tk_garb_retry) 2583 break; 2584 task->tk_garb_retry--; 2585 trace_rpc__bad_creds(task); 2586 task->tk_action = call_encode; 2587 return -EAGAIN; 2588 case rpc_autherr_tooweak: 2589 trace_rpc__auth_tooweak(task); 2590 pr_warn("RPC: server %s requires stronger authentication.\n", 2591 task->tk_xprt->servername); 2592 break; 2593 default: 2594 goto out_unparsable; 2595 } 2596 goto out_err; 2597 } 2598 2599 static void rpcproc_encode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr, 2600 const void *obj) 2601 { 2602 } 2603 2604 static int rpcproc_decode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr, 2605 void *obj) 2606 { 2607 return 0; 2608 } 2609 2610 static const struct rpc_procinfo rpcproc_null = { 2611 .p_encode = rpcproc_encode_null, 2612 .p_decode = rpcproc_decode_null, 2613 }; 2614 2615 static int rpc_ping(struct rpc_clnt *clnt) 2616 { 2617 struct rpc_message msg = { 2618 .rpc_proc = &rpcproc_null, 2619 }; 2620 int err; 2621 err = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN | 2622 RPC_TASK_NULLCREDS); 2623 return err; 2624 } 2625 2626 static 2627 struct rpc_task *rpc_call_null_helper(struct rpc_clnt *clnt, 2628 struct rpc_xprt *xprt, struct rpc_cred *cred, int flags, 2629 const struct rpc_call_ops *ops, void *data) 2630 { 2631 struct rpc_message msg = { 2632 .rpc_proc = &rpcproc_null, 2633 }; 2634 struct rpc_task_setup task_setup_data = { 2635 .rpc_client = clnt, 2636 .rpc_xprt = xprt, 2637 .rpc_message = &msg, 2638 .rpc_op_cred = cred, 2639 .callback_ops = (ops != NULL) ? ops : &rpc_default_ops, 2640 .callback_data = data, 2641 .flags = flags | RPC_TASK_NULLCREDS, 2642 }; 2643 2644 return rpc_run_task(&task_setup_data); 2645 } 2646 2647 struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags) 2648 { 2649 return rpc_call_null_helper(clnt, NULL, cred, flags, NULL, NULL); 2650 } 2651 EXPORT_SYMBOL_GPL(rpc_call_null); 2652 2653 struct rpc_cb_add_xprt_calldata { 2654 struct rpc_xprt_switch *xps; 2655 struct rpc_xprt *xprt; 2656 }; 2657 2658 static void rpc_cb_add_xprt_done(struct rpc_task *task, void *calldata) 2659 { 2660 struct rpc_cb_add_xprt_calldata *data = calldata; 2661 2662 if (task->tk_status == 0) 2663 rpc_xprt_switch_add_xprt(data->xps, data->xprt); 2664 } 2665 2666 static void rpc_cb_add_xprt_release(void *calldata) 2667 { 2668 struct rpc_cb_add_xprt_calldata *data = calldata; 2669 2670 xprt_put(data->xprt); 2671 xprt_switch_put(data->xps); 2672 kfree(data); 2673 } 2674 2675 static const struct rpc_call_ops rpc_cb_add_xprt_call_ops = { 2676 .rpc_call_done = rpc_cb_add_xprt_done, 2677 .rpc_release = rpc_cb_add_xprt_release, 2678 }; 2679 2680 /** 2681 * rpc_clnt_test_and_add_xprt - Test and add a new transport to a rpc_clnt 2682 * @clnt: pointer to struct rpc_clnt 2683 * @xps: pointer to struct rpc_xprt_switch, 2684 * @xprt: pointer struct rpc_xprt 2685 * @dummy: unused 2686 */ 2687 int rpc_clnt_test_and_add_xprt(struct rpc_clnt *clnt, 2688 struct rpc_xprt_switch *xps, struct rpc_xprt *xprt, 2689 void *dummy) 2690 { 2691 struct rpc_cb_add_xprt_calldata *data; 2692 struct rpc_task *task; 2693 2694 data = kmalloc(sizeof(*data), GFP_NOFS); 2695 if (!data) 2696 return -ENOMEM; 2697 data->xps = xprt_switch_get(xps); 2698 data->xprt = xprt_get(xprt); 2699 2700 task = rpc_call_null_helper(clnt, xprt, NULL, 2701 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC|RPC_TASK_NULLCREDS, 2702 &rpc_cb_add_xprt_call_ops, data); 2703 if (IS_ERR(task)) 2704 return PTR_ERR(task); 2705 rpc_put_task(task); 2706 return 1; 2707 } 2708 EXPORT_SYMBOL_GPL(rpc_clnt_test_and_add_xprt); 2709 2710 /** 2711 * rpc_clnt_setup_test_and_add_xprt() 2712 * 2713 * This is an rpc_clnt_add_xprt setup() function which returns 1 so: 2714 * 1) caller of the test function must dereference the rpc_xprt_switch 2715 * and the rpc_xprt. 2716 * 2) test function must call rpc_xprt_switch_add_xprt, usually in 2717 * the rpc_call_done routine. 2718 * 2719 * Upon success (return of 1), the test function adds the new 2720 * transport to the rpc_clnt xprt switch 2721 * 2722 * @clnt: struct rpc_clnt to get the new transport 2723 * @xps: the rpc_xprt_switch to hold the new transport 2724 * @xprt: the rpc_xprt to test 2725 * @data: a struct rpc_add_xprt_test pointer that holds the test function 2726 * and test function call data 2727 */ 2728 int rpc_clnt_setup_test_and_add_xprt(struct rpc_clnt *clnt, 2729 struct rpc_xprt_switch *xps, 2730 struct rpc_xprt *xprt, 2731 void *data) 2732 { 2733 struct rpc_task *task; 2734 struct rpc_add_xprt_test *xtest = (struct rpc_add_xprt_test *)data; 2735 int status = -EADDRINUSE; 2736 2737 xprt = xprt_get(xprt); 2738 xprt_switch_get(xps); 2739 2740 if (rpc_xprt_switch_has_addr(xps, (struct sockaddr *)&xprt->addr)) 2741 goto out_err; 2742 2743 /* Test the connection */ 2744 task = rpc_call_null_helper(clnt, xprt, NULL, 2745 RPC_TASK_SOFT | RPC_TASK_SOFTCONN | RPC_TASK_NULLCREDS, 2746 NULL, NULL); 2747 if (IS_ERR(task)) { 2748 status = PTR_ERR(task); 2749 goto out_err; 2750 } 2751 status = task->tk_status; 2752 rpc_put_task(task); 2753 2754 if (status < 0) 2755 goto out_err; 2756 2757 /* rpc_xprt_switch and rpc_xprt are deferrenced by add_xprt_test() */ 2758 xtest->add_xprt_test(clnt, xprt, xtest->data); 2759 2760 xprt_put(xprt); 2761 xprt_switch_put(xps); 2762 2763 /* so that rpc_clnt_add_xprt does not call rpc_xprt_switch_add_xprt */ 2764 return 1; 2765 out_err: 2766 xprt_put(xprt); 2767 xprt_switch_put(xps); 2768 pr_info("RPC: rpc_clnt_test_xprt failed: %d addr %s not added\n", 2769 status, xprt->address_strings[RPC_DISPLAY_ADDR]); 2770 return status; 2771 } 2772 EXPORT_SYMBOL_GPL(rpc_clnt_setup_test_and_add_xprt); 2773 2774 /** 2775 * rpc_clnt_add_xprt - Add a new transport to a rpc_clnt 2776 * @clnt: pointer to struct rpc_clnt 2777 * @xprtargs: pointer to struct xprt_create 2778 * @setup: callback to test and/or set up the connection 2779 * @data: pointer to setup function data 2780 * 2781 * Creates a new transport using the parameters set in args and 2782 * adds it to clnt. 2783 * If ping is set, then test that connectivity succeeds before 2784 * adding the new transport. 2785 * 2786 */ 2787 int rpc_clnt_add_xprt(struct rpc_clnt *clnt, 2788 struct xprt_create *xprtargs, 2789 int (*setup)(struct rpc_clnt *, 2790 struct rpc_xprt_switch *, 2791 struct rpc_xprt *, 2792 void *), 2793 void *data) 2794 { 2795 struct rpc_xprt_switch *xps; 2796 struct rpc_xprt *xprt; 2797 unsigned long connect_timeout; 2798 unsigned long reconnect_timeout; 2799 unsigned char resvport; 2800 int ret = 0; 2801 2802 rcu_read_lock(); 2803 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch)); 2804 xprt = xprt_iter_xprt(&clnt->cl_xpi); 2805 if (xps == NULL || xprt == NULL) { 2806 rcu_read_unlock(); 2807 xprt_switch_put(xps); 2808 return -EAGAIN; 2809 } 2810 resvport = xprt->resvport; 2811 connect_timeout = xprt->connect_timeout; 2812 reconnect_timeout = xprt->max_reconnect_timeout; 2813 rcu_read_unlock(); 2814 2815 xprt = xprt_create_transport(xprtargs); 2816 if (IS_ERR(xprt)) { 2817 ret = PTR_ERR(xprt); 2818 goto out_put_switch; 2819 } 2820 xprt->resvport = resvport; 2821 if (xprt->ops->set_connect_timeout != NULL) 2822 xprt->ops->set_connect_timeout(xprt, 2823 connect_timeout, 2824 reconnect_timeout); 2825 2826 rpc_xprt_switch_set_roundrobin(xps); 2827 if (setup) { 2828 ret = setup(clnt, xps, xprt, data); 2829 if (ret != 0) 2830 goto out_put_xprt; 2831 } 2832 rpc_xprt_switch_add_xprt(xps, xprt); 2833 out_put_xprt: 2834 xprt_put(xprt); 2835 out_put_switch: 2836 xprt_switch_put(xps); 2837 return ret; 2838 } 2839 EXPORT_SYMBOL_GPL(rpc_clnt_add_xprt); 2840 2841 struct connect_timeout_data { 2842 unsigned long connect_timeout; 2843 unsigned long reconnect_timeout; 2844 }; 2845 2846 static int 2847 rpc_xprt_set_connect_timeout(struct rpc_clnt *clnt, 2848 struct rpc_xprt *xprt, 2849 void *data) 2850 { 2851 struct connect_timeout_data *timeo = data; 2852 2853 if (xprt->ops->set_connect_timeout) 2854 xprt->ops->set_connect_timeout(xprt, 2855 timeo->connect_timeout, 2856 timeo->reconnect_timeout); 2857 return 0; 2858 } 2859 2860 void 2861 rpc_set_connect_timeout(struct rpc_clnt *clnt, 2862 unsigned long connect_timeout, 2863 unsigned long reconnect_timeout) 2864 { 2865 struct connect_timeout_data timeout = { 2866 .connect_timeout = connect_timeout, 2867 .reconnect_timeout = reconnect_timeout, 2868 }; 2869 rpc_clnt_iterate_for_each_xprt(clnt, 2870 rpc_xprt_set_connect_timeout, 2871 &timeout); 2872 } 2873 EXPORT_SYMBOL_GPL(rpc_set_connect_timeout); 2874 2875 void rpc_clnt_xprt_switch_put(struct rpc_clnt *clnt) 2876 { 2877 rcu_read_lock(); 2878 xprt_switch_put(rcu_dereference(clnt->cl_xpi.xpi_xpswitch)); 2879 rcu_read_unlock(); 2880 } 2881 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_put); 2882 2883 void rpc_clnt_xprt_switch_add_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt) 2884 { 2885 rcu_read_lock(); 2886 rpc_xprt_switch_add_xprt(rcu_dereference(clnt->cl_xpi.xpi_xpswitch), 2887 xprt); 2888 rcu_read_unlock(); 2889 } 2890 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_add_xprt); 2891 2892 bool rpc_clnt_xprt_switch_has_addr(struct rpc_clnt *clnt, 2893 const struct sockaddr *sap) 2894 { 2895 struct rpc_xprt_switch *xps; 2896 bool ret; 2897 2898 rcu_read_lock(); 2899 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch); 2900 ret = rpc_xprt_switch_has_addr(xps, sap); 2901 rcu_read_unlock(); 2902 return ret; 2903 } 2904 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_has_addr); 2905 2906 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 2907 static void rpc_show_header(void) 2908 { 2909 printk(KERN_INFO "-pid- flgs status -client- --rqstp- " 2910 "-timeout ---ops--\n"); 2911 } 2912 2913 static void rpc_show_task(const struct rpc_clnt *clnt, 2914 const struct rpc_task *task) 2915 { 2916 const char *rpc_waitq = "none"; 2917 2918 if (RPC_IS_QUEUED(task)) 2919 rpc_waitq = rpc_qname(task->tk_waitqueue); 2920 2921 printk(KERN_INFO "%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%ps q:%s\n", 2922 task->tk_pid, task->tk_flags, task->tk_status, 2923 clnt, task->tk_rqstp, rpc_task_timeout(task), task->tk_ops, 2924 clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task), 2925 task->tk_action, rpc_waitq); 2926 } 2927 2928 void rpc_show_tasks(struct net *net) 2929 { 2930 struct rpc_clnt *clnt; 2931 struct rpc_task *task; 2932 int header = 0; 2933 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); 2934 2935 spin_lock(&sn->rpc_client_lock); 2936 list_for_each_entry(clnt, &sn->all_clients, cl_clients) { 2937 spin_lock(&clnt->cl_lock); 2938 list_for_each_entry(task, &clnt->cl_tasks, tk_task) { 2939 if (!header) { 2940 rpc_show_header(); 2941 header++; 2942 } 2943 rpc_show_task(clnt, task); 2944 } 2945 spin_unlock(&clnt->cl_lock); 2946 } 2947 spin_unlock(&sn->rpc_client_lock); 2948 } 2949 #endif 2950 2951 #if IS_ENABLED(CONFIG_SUNRPC_SWAP) 2952 static int 2953 rpc_clnt_swap_activate_callback(struct rpc_clnt *clnt, 2954 struct rpc_xprt *xprt, 2955 void *dummy) 2956 { 2957 return xprt_enable_swap(xprt); 2958 } 2959 2960 int 2961 rpc_clnt_swap_activate(struct rpc_clnt *clnt) 2962 { 2963 if (atomic_inc_return(&clnt->cl_swapper) == 1) 2964 return rpc_clnt_iterate_for_each_xprt(clnt, 2965 rpc_clnt_swap_activate_callback, NULL); 2966 return 0; 2967 } 2968 EXPORT_SYMBOL_GPL(rpc_clnt_swap_activate); 2969 2970 static int 2971 rpc_clnt_swap_deactivate_callback(struct rpc_clnt *clnt, 2972 struct rpc_xprt *xprt, 2973 void *dummy) 2974 { 2975 xprt_disable_swap(xprt); 2976 return 0; 2977 } 2978 2979 void 2980 rpc_clnt_swap_deactivate(struct rpc_clnt *clnt) 2981 { 2982 if (atomic_dec_if_positive(&clnt->cl_swapper) == 0) 2983 rpc_clnt_iterate_for_each_xprt(clnt, 2984 rpc_clnt_swap_deactivate_callback, NULL); 2985 } 2986 EXPORT_SYMBOL_GPL(rpc_clnt_swap_deactivate); 2987 #endif /* CONFIG_SUNRPC_SWAP */ 2988