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 new->cl_cred = get_cred(clnt->cl_cred); 638 return new; 639 640 out_err: 641 dprintk("RPC: %s: returned error %d\n", __func__, err); 642 return ERR_PTR(err); 643 } 644 645 /** 646 * rpc_clone_client - Clone an RPC client structure 647 * 648 * @clnt: RPC client whose parameters are copied 649 * 650 * Returns a fresh RPC client or an ERR_PTR. 651 */ 652 struct rpc_clnt *rpc_clone_client(struct rpc_clnt *clnt) 653 { 654 struct rpc_create_args args = { 655 .program = clnt->cl_program, 656 .prognumber = clnt->cl_prog, 657 .version = clnt->cl_vers, 658 .authflavor = clnt->cl_auth->au_flavor, 659 .cred = clnt->cl_cred, 660 }; 661 return __rpc_clone_client(&args, clnt); 662 } 663 EXPORT_SYMBOL_GPL(rpc_clone_client); 664 665 /** 666 * rpc_clone_client_set_auth - Clone an RPC client structure and set its auth 667 * 668 * @clnt: RPC client whose parameters are copied 669 * @flavor: security flavor for new client 670 * 671 * Returns a fresh RPC client or an ERR_PTR. 672 */ 673 struct rpc_clnt * 674 rpc_clone_client_set_auth(struct rpc_clnt *clnt, rpc_authflavor_t flavor) 675 { 676 struct rpc_create_args args = { 677 .program = clnt->cl_program, 678 .prognumber = clnt->cl_prog, 679 .version = clnt->cl_vers, 680 .authflavor = flavor, 681 .cred = clnt->cl_cred, 682 }; 683 return __rpc_clone_client(&args, clnt); 684 } 685 EXPORT_SYMBOL_GPL(rpc_clone_client_set_auth); 686 687 /** 688 * rpc_switch_client_transport: switch the RPC transport on the fly 689 * @clnt: pointer to a struct rpc_clnt 690 * @args: pointer to the new transport arguments 691 * @timeout: pointer to the new timeout parameters 692 * 693 * This function allows the caller to switch the RPC transport for the 694 * rpc_clnt structure 'clnt' to allow it to connect to a mirrored NFS 695 * server, for instance. It assumes that the caller has ensured that 696 * there are no active RPC tasks by using some form of locking. 697 * 698 * Returns zero if "clnt" is now using the new xprt. Otherwise a 699 * negative errno is returned, and "clnt" continues to use the old 700 * xprt. 701 */ 702 int rpc_switch_client_transport(struct rpc_clnt *clnt, 703 struct xprt_create *args, 704 const struct rpc_timeout *timeout) 705 { 706 const struct rpc_timeout *old_timeo; 707 rpc_authflavor_t pseudoflavor; 708 struct rpc_xprt_switch *xps, *oldxps; 709 struct rpc_xprt *xprt, *old; 710 struct rpc_clnt *parent; 711 int err; 712 713 xprt = xprt_create_transport(args); 714 if (IS_ERR(xprt)) { 715 dprintk("RPC: failed to create new xprt for clnt %p\n", 716 clnt); 717 return PTR_ERR(xprt); 718 } 719 720 xps = xprt_switch_alloc(xprt, GFP_KERNEL); 721 if (xps == NULL) { 722 xprt_put(xprt); 723 return -ENOMEM; 724 } 725 726 pseudoflavor = clnt->cl_auth->au_flavor; 727 728 old_timeo = clnt->cl_timeout; 729 old = rpc_clnt_set_transport(clnt, xprt, timeout); 730 oldxps = xprt_iter_xchg_switch(&clnt->cl_xpi, xps); 731 732 rpc_unregister_client(clnt); 733 __rpc_clnt_remove_pipedir(clnt); 734 rpc_clnt_debugfs_unregister(clnt); 735 736 /* 737 * A new transport was created. "clnt" therefore 738 * becomes the root of a new cl_parent tree. clnt's 739 * children, if it has any, still point to the old xprt. 740 */ 741 parent = clnt->cl_parent; 742 clnt->cl_parent = clnt; 743 744 /* 745 * The old rpc_auth cache cannot be re-used. GSS 746 * contexts in particular are between a single 747 * client and server. 748 */ 749 err = rpc_client_register(clnt, pseudoflavor, NULL); 750 if (err) 751 goto out_revert; 752 753 synchronize_rcu(); 754 if (parent != clnt) 755 rpc_release_client(parent); 756 xprt_switch_put(oldxps); 757 xprt_put(old); 758 dprintk("RPC: replaced xprt for clnt %p\n", clnt); 759 return 0; 760 761 out_revert: 762 xps = xprt_iter_xchg_switch(&clnt->cl_xpi, oldxps); 763 rpc_clnt_set_transport(clnt, old, old_timeo); 764 clnt->cl_parent = parent; 765 rpc_client_register(clnt, pseudoflavor, NULL); 766 xprt_switch_put(xps); 767 xprt_put(xprt); 768 dprintk("RPC: failed to switch xprt for clnt %p\n", clnt); 769 return err; 770 } 771 EXPORT_SYMBOL_GPL(rpc_switch_client_transport); 772 773 static 774 int rpc_clnt_xprt_iter_init(struct rpc_clnt *clnt, struct rpc_xprt_iter *xpi) 775 { 776 struct rpc_xprt_switch *xps; 777 778 rcu_read_lock(); 779 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch)); 780 rcu_read_unlock(); 781 if (xps == NULL) 782 return -EAGAIN; 783 xprt_iter_init_listall(xpi, xps); 784 xprt_switch_put(xps); 785 return 0; 786 } 787 788 /** 789 * rpc_clnt_iterate_for_each_xprt - Apply a function to all transports 790 * @clnt: pointer to client 791 * @fn: function to apply 792 * @data: void pointer to function data 793 * 794 * Iterates through the list of RPC transports currently attached to the 795 * client and applies the function fn(clnt, xprt, data). 796 * 797 * On error, the iteration stops, and the function returns the error value. 798 */ 799 int rpc_clnt_iterate_for_each_xprt(struct rpc_clnt *clnt, 800 int (*fn)(struct rpc_clnt *, struct rpc_xprt *, void *), 801 void *data) 802 { 803 struct rpc_xprt_iter xpi; 804 int ret; 805 806 ret = rpc_clnt_xprt_iter_init(clnt, &xpi); 807 if (ret) 808 return ret; 809 for (;;) { 810 struct rpc_xprt *xprt = xprt_iter_get_next(&xpi); 811 812 if (!xprt) 813 break; 814 ret = fn(clnt, xprt, data); 815 xprt_put(xprt); 816 if (ret < 0) 817 break; 818 } 819 xprt_iter_destroy(&xpi); 820 return ret; 821 } 822 EXPORT_SYMBOL_GPL(rpc_clnt_iterate_for_each_xprt); 823 824 /* 825 * Kill all tasks for the given client. 826 * XXX: kill their descendants as well? 827 */ 828 void rpc_killall_tasks(struct rpc_clnt *clnt) 829 { 830 struct rpc_task *rovr; 831 832 833 if (list_empty(&clnt->cl_tasks)) 834 return; 835 dprintk("RPC: killing all tasks for client %p\n", clnt); 836 /* 837 * Spin lock all_tasks to prevent changes... 838 */ 839 spin_lock(&clnt->cl_lock); 840 list_for_each_entry(rovr, &clnt->cl_tasks, tk_task) 841 rpc_signal_task(rovr); 842 spin_unlock(&clnt->cl_lock); 843 } 844 EXPORT_SYMBOL_GPL(rpc_killall_tasks); 845 846 /* 847 * Properly shut down an RPC client, terminating all outstanding 848 * requests. 849 */ 850 void rpc_shutdown_client(struct rpc_clnt *clnt) 851 { 852 might_sleep(); 853 854 dprintk_rcu("RPC: shutting down %s client for %s\n", 855 clnt->cl_program->name, 856 rcu_dereference(clnt->cl_xprt)->servername); 857 858 while (!list_empty(&clnt->cl_tasks)) { 859 rpc_killall_tasks(clnt); 860 wait_event_timeout(destroy_wait, 861 list_empty(&clnt->cl_tasks), 1*HZ); 862 } 863 864 rpc_release_client(clnt); 865 } 866 EXPORT_SYMBOL_GPL(rpc_shutdown_client); 867 868 /* 869 * Free an RPC client 870 */ 871 static struct rpc_clnt * 872 rpc_free_client(struct rpc_clnt *clnt) 873 { 874 struct rpc_clnt *parent = NULL; 875 876 dprintk_rcu("RPC: destroying %s client for %s\n", 877 clnt->cl_program->name, 878 rcu_dereference(clnt->cl_xprt)->servername); 879 if (clnt->cl_parent != clnt) 880 parent = clnt->cl_parent; 881 rpc_clnt_debugfs_unregister(clnt); 882 rpc_clnt_remove_pipedir(clnt); 883 rpc_unregister_client(clnt); 884 rpc_free_iostats(clnt->cl_metrics); 885 clnt->cl_metrics = NULL; 886 xprt_put(rcu_dereference_raw(clnt->cl_xprt)); 887 xprt_iter_destroy(&clnt->cl_xpi); 888 rpciod_down(); 889 put_cred(clnt->cl_cred); 890 rpc_free_clid(clnt); 891 kfree(clnt); 892 return parent; 893 } 894 895 /* 896 * Free an RPC client 897 */ 898 static struct rpc_clnt * 899 rpc_free_auth(struct rpc_clnt *clnt) 900 { 901 if (clnt->cl_auth == NULL) 902 return rpc_free_client(clnt); 903 904 /* 905 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to 906 * release remaining GSS contexts. This mechanism ensures 907 * that it can do so safely. 908 */ 909 atomic_inc(&clnt->cl_count); 910 rpcauth_release(clnt->cl_auth); 911 clnt->cl_auth = NULL; 912 if (atomic_dec_and_test(&clnt->cl_count)) 913 return rpc_free_client(clnt); 914 return NULL; 915 } 916 917 /* 918 * Release reference to the RPC client 919 */ 920 void 921 rpc_release_client(struct rpc_clnt *clnt) 922 { 923 dprintk("RPC: rpc_release_client(%p)\n", clnt); 924 925 do { 926 if (list_empty(&clnt->cl_tasks)) 927 wake_up(&destroy_wait); 928 if (!atomic_dec_and_test(&clnt->cl_count)) 929 break; 930 clnt = rpc_free_auth(clnt); 931 } while (clnt != NULL); 932 } 933 EXPORT_SYMBOL_GPL(rpc_release_client); 934 935 /** 936 * rpc_bind_new_program - bind a new RPC program to an existing client 937 * @old: old rpc_client 938 * @program: rpc program to set 939 * @vers: rpc program version 940 * 941 * Clones the rpc client and sets up a new RPC program. This is mainly 942 * of use for enabling different RPC programs to share the same transport. 943 * The Sun NFSv2/v3 ACL protocol can do this. 944 */ 945 struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old, 946 const struct rpc_program *program, 947 u32 vers) 948 { 949 struct rpc_create_args args = { 950 .program = program, 951 .prognumber = program->number, 952 .version = vers, 953 .authflavor = old->cl_auth->au_flavor, 954 .cred = old->cl_cred, 955 }; 956 struct rpc_clnt *clnt; 957 int err; 958 959 clnt = __rpc_clone_client(&args, old); 960 if (IS_ERR(clnt)) 961 goto out; 962 err = rpc_ping(clnt); 963 if (err != 0) { 964 rpc_shutdown_client(clnt); 965 clnt = ERR_PTR(err); 966 } 967 out: 968 return clnt; 969 } 970 EXPORT_SYMBOL_GPL(rpc_bind_new_program); 971 972 void rpc_task_release_transport(struct rpc_task *task) 973 { 974 struct rpc_xprt *xprt = task->tk_xprt; 975 976 if (xprt) { 977 task->tk_xprt = NULL; 978 xprt_put(xprt); 979 } 980 } 981 EXPORT_SYMBOL_GPL(rpc_task_release_transport); 982 983 void rpc_task_release_client(struct rpc_task *task) 984 { 985 struct rpc_clnt *clnt = task->tk_client; 986 987 if (clnt != NULL) { 988 /* Remove from client task list */ 989 spin_lock(&clnt->cl_lock); 990 list_del(&task->tk_task); 991 spin_unlock(&clnt->cl_lock); 992 task->tk_client = NULL; 993 994 rpc_release_client(clnt); 995 } 996 rpc_task_release_transport(task); 997 } 998 999 static 1000 void rpc_task_set_transport(struct rpc_task *task, struct rpc_clnt *clnt) 1001 { 1002 if (!task->tk_xprt) 1003 task->tk_xprt = xprt_iter_get_next(&clnt->cl_xpi); 1004 } 1005 1006 static 1007 void rpc_task_set_client(struct rpc_task *task, struct rpc_clnt *clnt) 1008 { 1009 1010 if (clnt != NULL) { 1011 rpc_task_set_transport(task, clnt); 1012 task->tk_client = clnt; 1013 atomic_inc(&clnt->cl_count); 1014 if (clnt->cl_softrtry) 1015 task->tk_flags |= RPC_TASK_SOFT; 1016 if (clnt->cl_softerr) 1017 task->tk_flags |= RPC_TASK_TIMEOUT; 1018 if (clnt->cl_noretranstimeo) 1019 task->tk_flags |= RPC_TASK_NO_RETRANS_TIMEOUT; 1020 if (atomic_read(&clnt->cl_swapper)) 1021 task->tk_flags |= RPC_TASK_SWAPPER; 1022 /* Add to the client's list of all tasks */ 1023 spin_lock(&clnt->cl_lock); 1024 list_add_tail(&task->tk_task, &clnt->cl_tasks); 1025 spin_unlock(&clnt->cl_lock); 1026 } 1027 } 1028 1029 static void 1030 rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg) 1031 { 1032 if (msg != NULL) { 1033 task->tk_msg.rpc_proc = msg->rpc_proc; 1034 task->tk_msg.rpc_argp = msg->rpc_argp; 1035 task->tk_msg.rpc_resp = msg->rpc_resp; 1036 if (msg->rpc_cred != NULL) 1037 task->tk_msg.rpc_cred = get_cred(msg->rpc_cred); 1038 } 1039 } 1040 1041 /* 1042 * Default callback for async RPC calls 1043 */ 1044 static void 1045 rpc_default_callback(struct rpc_task *task, void *data) 1046 { 1047 } 1048 1049 static const struct rpc_call_ops rpc_default_ops = { 1050 .rpc_call_done = rpc_default_callback, 1051 }; 1052 1053 /** 1054 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it 1055 * @task_setup_data: pointer to task initialisation data 1056 */ 1057 struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data) 1058 { 1059 struct rpc_task *task; 1060 1061 task = rpc_new_task(task_setup_data); 1062 1063 rpc_task_set_client(task, task_setup_data->rpc_client); 1064 rpc_task_set_rpc_message(task, task_setup_data->rpc_message); 1065 1066 if (task->tk_action == NULL) 1067 rpc_call_start(task); 1068 1069 atomic_inc(&task->tk_count); 1070 rpc_execute(task); 1071 return task; 1072 } 1073 EXPORT_SYMBOL_GPL(rpc_run_task); 1074 1075 /** 1076 * rpc_call_sync - Perform a synchronous RPC call 1077 * @clnt: pointer to RPC client 1078 * @msg: RPC call parameters 1079 * @flags: RPC call flags 1080 */ 1081 int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags) 1082 { 1083 struct rpc_task *task; 1084 struct rpc_task_setup task_setup_data = { 1085 .rpc_client = clnt, 1086 .rpc_message = msg, 1087 .callback_ops = &rpc_default_ops, 1088 .flags = flags, 1089 }; 1090 int status; 1091 1092 WARN_ON_ONCE(flags & RPC_TASK_ASYNC); 1093 if (flags & RPC_TASK_ASYNC) { 1094 rpc_release_calldata(task_setup_data.callback_ops, 1095 task_setup_data.callback_data); 1096 return -EINVAL; 1097 } 1098 1099 task = rpc_run_task(&task_setup_data); 1100 if (IS_ERR(task)) 1101 return PTR_ERR(task); 1102 status = task->tk_status; 1103 rpc_put_task(task); 1104 return status; 1105 } 1106 EXPORT_SYMBOL_GPL(rpc_call_sync); 1107 1108 /** 1109 * rpc_call_async - Perform an asynchronous RPC call 1110 * @clnt: pointer to RPC client 1111 * @msg: RPC call parameters 1112 * @flags: RPC call flags 1113 * @tk_ops: RPC call ops 1114 * @data: user call data 1115 */ 1116 int 1117 rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags, 1118 const struct rpc_call_ops *tk_ops, void *data) 1119 { 1120 struct rpc_task *task; 1121 struct rpc_task_setup task_setup_data = { 1122 .rpc_client = clnt, 1123 .rpc_message = msg, 1124 .callback_ops = tk_ops, 1125 .callback_data = data, 1126 .flags = flags|RPC_TASK_ASYNC, 1127 }; 1128 1129 task = rpc_run_task(&task_setup_data); 1130 if (IS_ERR(task)) 1131 return PTR_ERR(task); 1132 rpc_put_task(task); 1133 return 0; 1134 } 1135 EXPORT_SYMBOL_GPL(rpc_call_async); 1136 1137 #if defined(CONFIG_SUNRPC_BACKCHANNEL) 1138 static void call_bc_encode(struct rpc_task *task); 1139 1140 /** 1141 * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run 1142 * rpc_execute against it 1143 * @req: RPC request 1144 */ 1145 struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req) 1146 { 1147 struct rpc_task *task; 1148 struct rpc_task_setup task_setup_data = { 1149 .callback_ops = &rpc_default_ops, 1150 .flags = RPC_TASK_SOFTCONN | 1151 RPC_TASK_NO_RETRANS_TIMEOUT, 1152 }; 1153 1154 dprintk("RPC: rpc_run_bc_task req= %p\n", req); 1155 /* 1156 * Create an rpc_task to send the data 1157 */ 1158 task = rpc_new_task(&task_setup_data); 1159 xprt_init_bc_request(req, task); 1160 1161 task->tk_action = call_bc_encode; 1162 atomic_inc(&task->tk_count); 1163 WARN_ON_ONCE(atomic_read(&task->tk_count) != 2); 1164 rpc_execute(task); 1165 1166 dprintk("RPC: rpc_run_bc_task: task= %p\n", task); 1167 return task; 1168 } 1169 #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 1170 1171 /** 1172 * rpc_prepare_reply_pages - Prepare to receive a reply data payload into pages 1173 * @req: RPC request to prepare 1174 * @pages: vector of struct page pointers 1175 * @base: offset in first page where receive should start, in bytes 1176 * @len: expected size of the upper layer data payload, in bytes 1177 * @hdrsize: expected size of upper layer reply header, in XDR words 1178 * 1179 */ 1180 void rpc_prepare_reply_pages(struct rpc_rqst *req, struct page **pages, 1181 unsigned int base, unsigned int len, 1182 unsigned int hdrsize) 1183 { 1184 /* Subtract one to force an extra word of buffer space for the 1185 * payload's XDR pad to fall into the rcv_buf's tail iovec. 1186 */ 1187 hdrsize += RPC_REPHDRSIZE + req->rq_cred->cr_auth->au_ralign - 1; 1188 1189 xdr_inline_pages(&req->rq_rcv_buf, hdrsize << 2, pages, base, len); 1190 trace_rpc_reply_pages(req); 1191 } 1192 EXPORT_SYMBOL_GPL(rpc_prepare_reply_pages); 1193 1194 void 1195 rpc_call_start(struct rpc_task *task) 1196 { 1197 task->tk_action = call_start; 1198 } 1199 EXPORT_SYMBOL_GPL(rpc_call_start); 1200 1201 /** 1202 * rpc_peeraddr - extract remote peer address from clnt's xprt 1203 * @clnt: RPC client structure 1204 * @buf: target buffer 1205 * @bufsize: length of target buffer 1206 * 1207 * Returns the number of bytes that are actually in the stored address. 1208 */ 1209 size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize) 1210 { 1211 size_t bytes; 1212 struct rpc_xprt *xprt; 1213 1214 rcu_read_lock(); 1215 xprt = rcu_dereference(clnt->cl_xprt); 1216 1217 bytes = xprt->addrlen; 1218 if (bytes > bufsize) 1219 bytes = bufsize; 1220 memcpy(buf, &xprt->addr, bytes); 1221 rcu_read_unlock(); 1222 1223 return bytes; 1224 } 1225 EXPORT_SYMBOL_GPL(rpc_peeraddr); 1226 1227 /** 1228 * rpc_peeraddr2str - return remote peer address in printable format 1229 * @clnt: RPC client structure 1230 * @format: address format 1231 * 1232 * NB: the lifetime of the memory referenced by the returned pointer is 1233 * the same as the rpc_xprt itself. As long as the caller uses this 1234 * pointer, it must hold the RCU read lock. 1235 */ 1236 const char *rpc_peeraddr2str(struct rpc_clnt *clnt, 1237 enum rpc_display_format_t format) 1238 { 1239 struct rpc_xprt *xprt; 1240 1241 xprt = rcu_dereference(clnt->cl_xprt); 1242 1243 if (xprt->address_strings[format] != NULL) 1244 return xprt->address_strings[format]; 1245 else 1246 return "unprintable"; 1247 } 1248 EXPORT_SYMBOL_GPL(rpc_peeraddr2str); 1249 1250 static const struct sockaddr_in rpc_inaddr_loopback = { 1251 .sin_family = AF_INET, 1252 .sin_addr.s_addr = htonl(INADDR_ANY), 1253 }; 1254 1255 static const struct sockaddr_in6 rpc_in6addr_loopback = { 1256 .sin6_family = AF_INET6, 1257 .sin6_addr = IN6ADDR_ANY_INIT, 1258 }; 1259 1260 /* 1261 * Try a getsockname() on a connected datagram socket. Using a 1262 * connected datagram socket prevents leaving a socket in TIME_WAIT. 1263 * This conserves the ephemeral port number space. 1264 * 1265 * Returns zero and fills in "buf" if successful; otherwise, a 1266 * negative errno is returned. 1267 */ 1268 static int rpc_sockname(struct net *net, struct sockaddr *sap, size_t salen, 1269 struct sockaddr *buf) 1270 { 1271 struct socket *sock; 1272 int err; 1273 1274 err = __sock_create(net, sap->sa_family, 1275 SOCK_DGRAM, IPPROTO_UDP, &sock, 1); 1276 if (err < 0) { 1277 dprintk("RPC: can't create UDP socket (%d)\n", err); 1278 goto out; 1279 } 1280 1281 switch (sap->sa_family) { 1282 case AF_INET: 1283 err = kernel_bind(sock, 1284 (struct sockaddr *)&rpc_inaddr_loopback, 1285 sizeof(rpc_inaddr_loopback)); 1286 break; 1287 case AF_INET6: 1288 err = kernel_bind(sock, 1289 (struct sockaddr *)&rpc_in6addr_loopback, 1290 sizeof(rpc_in6addr_loopback)); 1291 break; 1292 default: 1293 err = -EAFNOSUPPORT; 1294 goto out; 1295 } 1296 if (err < 0) { 1297 dprintk("RPC: can't bind UDP socket (%d)\n", err); 1298 goto out_release; 1299 } 1300 1301 err = kernel_connect(sock, sap, salen, 0); 1302 if (err < 0) { 1303 dprintk("RPC: can't connect UDP socket (%d)\n", err); 1304 goto out_release; 1305 } 1306 1307 err = kernel_getsockname(sock, buf); 1308 if (err < 0) { 1309 dprintk("RPC: getsockname failed (%d)\n", err); 1310 goto out_release; 1311 } 1312 1313 err = 0; 1314 if (buf->sa_family == AF_INET6) { 1315 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf; 1316 sin6->sin6_scope_id = 0; 1317 } 1318 dprintk("RPC: %s succeeded\n", __func__); 1319 1320 out_release: 1321 sock_release(sock); 1322 out: 1323 return err; 1324 } 1325 1326 /* 1327 * Scraping a connected socket failed, so we don't have a useable 1328 * local address. Fallback: generate an address that will prevent 1329 * the server from calling us back. 1330 * 1331 * Returns zero and fills in "buf" if successful; otherwise, a 1332 * negative errno is returned. 1333 */ 1334 static int rpc_anyaddr(int family, struct sockaddr *buf, size_t buflen) 1335 { 1336 switch (family) { 1337 case AF_INET: 1338 if (buflen < sizeof(rpc_inaddr_loopback)) 1339 return -EINVAL; 1340 memcpy(buf, &rpc_inaddr_loopback, 1341 sizeof(rpc_inaddr_loopback)); 1342 break; 1343 case AF_INET6: 1344 if (buflen < sizeof(rpc_in6addr_loopback)) 1345 return -EINVAL; 1346 memcpy(buf, &rpc_in6addr_loopback, 1347 sizeof(rpc_in6addr_loopback)); 1348 break; 1349 default: 1350 dprintk("RPC: %s: address family not supported\n", 1351 __func__); 1352 return -EAFNOSUPPORT; 1353 } 1354 dprintk("RPC: %s: succeeded\n", __func__); 1355 return 0; 1356 } 1357 1358 /** 1359 * rpc_localaddr - discover local endpoint address for an RPC client 1360 * @clnt: RPC client structure 1361 * @buf: target buffer 1362 * @buflen: size of target buffer, in bytes 1363 * 1364 * Returns zero and fills in "buf" and "buflen" if successful; 1365 * otherwise, a negative errno is returned. 1366 * 1367 * This works even if the underlying transport is not currently connected, 1368 * or if the upper layer never previously provided a source address. 1369 * 1370 * The result of this function call is transient: multiple calls in 1371 * succession may give different results, depending on how local 1372 * networking configuration changes over time. 1373 */ 1374 int rpc_localaddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t buflen) 1375 { 1376 struct sockaddr_storage address; 1377 struct sockaddr *sap = (struct sockaddr *)&address; 1378 struct rpc_xprt *xprt; 1379 struct net *net; 1380 size_t salen; 1381 int err; 1382 1383 rcu_read_lock(); 1384 xprt = rcu_dereference(clnt->cl_xprt); 1385 salen = xprt->addrlen; 1386 memcpy(sap, &xprt->addr, salen); 1387 net = get_net(xprt->xprt_net); 1388 rcu_read_unlock(); 1389 1390 rpc_set_port(sap, 0); 1391 err = rpc_sockname(net, sap, salen, buf); 1392 put_net(net); 1393 if (err != 0) 1394 /* Couldn't discover local address, return ANYADDR */ 1395 return rpc_anyaddr(sap->sa_family, buf, buflen); 1396 return 0; 1397 } 1398 EXPORT_SYMBOL_GPL(rpc_localaddr); 1399 1400 void 1401 rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize) 1402 { 1403 struct rpc_xprt *xprt; 1404 1405 rcu_read_lock(); 1406 xprt = rcu_dereference(clnt->cl_xprt); 1407 if (xprt->ops->set_buffer_size) 1408 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize); 1409 rcu_read_unlock(); 1410 } 1411 EXPORT_SYMBOL_GPL(rpc_setbufsize); 1412 1413 /** 1414 * rpc_net_ns - Get the network namespace for this RPC client 1415 * @clnt: RPC client to query 1416 * 1417 */ 1418 struct net *rpc_net_ns(struct rpc_clnt *clnt) 1419 { 1420 struct net *ret; 1421 1422 rcu_read_lock(); 1423 ret = rcu_dereference(clnt->cl_xprt)->xprt_net; 1424 rcu_read_unlock(); 1425 return ret; 1426 } 1427 EXPORT_SYMBOL_GPL(rpc_net_ns); 1428 1429 /** 1430 * rpc_max_payload - Get maximum payload size for a transport, in bytes 1431 * @clnt: RPC client to query 1432 * 1433 * For stream transports, this is one RPC record fragment (see RFC 1434 * 1831), as we don't support multi-record requests yet. For datagram 1435 * transports, this is the size of an IP packet minus the IP, UDP, and 1436 * RPC header sizes. 1437 */ 1438 size_t rpc_max_payload(struct rpc_clnt *clnt) 1439 { 1440 size_t ret; 1441 1442 rcu_read_lock(); 1443 ret = rcu_dereference(clnt->cl_xprt)->max_payload; 1444 rcu_read_unlock(); 1445 return ret; 1446 } 1447 EXPORT_SYMBOL_GPL(rpc_max_payload); 1448 1449 /** 1450 * rpc_max_bc_payload - Get maximum backchannel payload size, in bytes 1451 * @clnt: RPC client to query 1452 */ 1453 size_t rpc_max_bc_payload(struct rpc_clnt *clnt) 1454 { 1455 struct rpc_xprt *xprt; 1456 size_t ret; 1457 1458 rcu_read_lock(); 1459 xprt = rcu_dereference(clnt->cl_xprt); 1460 ret = xprt->ops->bc_maxpayload(xprt); 1461 rcu_read_unlock(); 1462 return ret; 1463 } 1464 EXPORT_SYMBOL_GPL(rpc_max_bc_payload); 1465 1466 /** 1467 * rpc_force_rebind - force transport to check that remote port is unchanged 1468 * @clnt: client to rebind 1469 * 1470 */ 1471 void rpc_force_rebind(struct rpc_clnt *clnt) 1472 { 1473 if (clnt->cl_autobind) { 1474 rcu_read_lock(); 1475 xprt_clear_bound(rcu_dereference(clnt->cl_xprt)); 1476 rcu_read_unlock(); 1477 } 1478 } 1479 EXPORT_SYMBOL_GPL(rpc_force_rebind); 1480 1481 static int 1482 __rpc_restart_call(struct rpc_task *task, void (*action)(struct rpc_task *)) 1483 { 1484 task->tk_status = 0; 1485 task->tk_rpc_status = 0; 1486 task->tk_action = action; 1487 return 1; 1488 } 1489 1490 /* 1491 * Restart an (async) RPC call. Usually called from within the 1492 * exit handler. 1493 */ 1494 int 1495 rpc_restart_call(struct rpc_task *task) 1496 { 1497 return __rpc_restart_call(task, call_start); 1498 } 1499 EXPORT_SYMBOL_GPL(rpc_restart_call); 1500 1501 /* 1502 * Restart an (async) RPC call from the call_prepare state. 1503 * Usually called from within the exit handler. 1504 */ 1505 int 1506 rpc_restart_call_prepare(struct rpc_task *task) 1507 { 1508 if (task->tk_ops->rpc_call_prepare != NULL) 1509 return __rpc_restart_call(task, rpc_prepare_task); 1510 return rpc_restart_call(task); 1511 } 1512 EXPORT_SYMBOL_GPL(rpc_restart_call_prepare); 1513 1514 const char 1515 *rpc_proc_name(const struct rpc_task *task) 1516 { 1517 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc; 1518 1519 if (proc) { 1520 if (proc->p_name) 1521 return proc->p_name; 1522 else 1523 return "NULL"; 1524 } else 1525 return "no proc"; 1526 } 1527 1528 static void 1529 __rpc_call_rpcerror(struct rpc_task *task, int tk_status, int rpc_status) 1530 { 1531 task->tk_rpc_status = rpc_status; 1532 rpc_exit(task, tk_status); 1533 } 1534 1535 static void 1536 rpc_call_rpcerror(struct rpc_task *task, int status) 1537 { 1538 __rpc_call_rpcerror(task, status, status); 1539 } 1540 1541 /* 1542 * 0. Initial state 1543 * 1544 * Other FSM states can be visited zero or more times, but 1545 * this state is visited exactly once for each RPC. 1546 */ 1547 static void 1548 call_start(struct rpc_task *task) 1549 { 1550 struct rpc_clnt *clnt = task->tk_client; 1551 int idx = task->tk_msg.rpc_proc->p_statidx; 1552 1553 trace_rpc_request(task); 1554 dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task->tk_pid, 1555 clnt->cl_program->name, clnt->cl_vers, 1556 rpc_proc_name(task), 1557 (RPC_IS_ASYNC(task) ? "async" : "sync")); 1558 1559 /* Increment call count (version might not be valid for ping) */ 1560 if (clnt->cl_program->version[clnt->cl_vers]) 1561 clnt->cl_program->version[clnt->cl_vers]->counts[idx]++; 1562 clnt->cl_stats->rpccnt++; 1563 task->tk_action = call_reserve; 1564 rpc_task_set_transport(task, clnt); 1565 } 1566 1567 /* 1568 * 1. Reserve an RPC call slot 1569 */ 1570 static void 1571 call_reserve(struct rpc_task *task) 1572 { 1573 dprint_status(task); 1574 1575 task->tk_status = 0; 1576 task->tk_action = call_reserveresult; 1577 xprt_reserve(task); 1578 } 1579 1580 static void call_retry_reserve(struct rpc_task *task); 1581 1582 /* 1583 * 1b. Grok the result of xprt_reserve() 1584 */ 1585 static void 1586 call_reserveresult(struct rpc_task *task) 1587 { 1588 int status = task->tk_status; 1589 1590 dprint_status(task); 1591 1592 /* 1593 * After a call to xprt_reserve(), we must have either 1594 * a request slot or else an error status. 1595 */ 1596 task->tk_status = 0; 1597 if (status >= 0) { 1598 if (task->tk_rqstp) { 1599 task->tk_action = call_refresh; 1600 return; 1601 } 1602 1603 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n", 1604 __func__, status); 1605 rpc_call_rpcerror(task, -EIO); 1606 return; 1607 } 1608 1609 /* 1610 * Even though there was an error, we may have acquired 1611 * a request slot somehow. Make sure not to leak it. 1612 */ 1613 if (task->tk_rqstp) { 1614 printk(KERN_ERR "%s: status=%d, request allocated anyway\n", 1615 __func__, status); 1616 xprt_release(task); 1617 } 1618 1619 switch (status) { 1620 case -ENOMEM: 1621 rpc_delay(task, HZ >> 2); 1622 /* fall through */ 1623 case -EAGAIN: /* woken up; retry */ 1624 task->tk_action = call_retry_reserve; 1625 return; 1626 case -EIO: /* probably a shutdown */ 1627 break; 1628 default: 1629 printk(KERN_ERR "%s: unrecognized error %d, exiting\n", 1630 __func__, status); 1631 break; 1632 } 1633 rpc_call_rpcerror(task, status); 1634 } 1635 1636 /* 1637 * 1c. Retry reserving an RPC call slot 1638 */ 1639 static void 1640 call_retry_reserve(struct rpc_task *task) 1641 { 1642 dprint_status(task); 1643 1644 task->tk_status = 0; 1645 task->tk_action = call_reserveresult; 1646 xprt_retry_reserve(task); 1647 } 1648 1649 /* 1650 * 2. Bind and/or refresh the credentials 1651 */ 1652 static void 1653 call_refresh(struct rpc_task *task) 1654 { 1655 dprint_status(task); 1656 1657 task->tk_action = call_refreshresult; 1658 task->tk_status = 0; 1659 task->tk_client->cl_stats->rpcauthrefresh++; 1660 rpcauth_refreshcred(task); 1661 } 1662 1663 /* 1664 * 2a. Process the results of a credential refresh 1665 */ 1666 static void 1667 call_refreshresult(struct rpc_task *task) 1668 { 1669 int status = task->tk_status; 1670 1671 dprint_status(task); 1672 1673 task->tk_status = 0; 1674 task->tk_action = call_refresh; 1675 switch (status) { 1676 case 0: 1677 if (rpcauth_uptodatecred(task)) { 1678 task->tk_action = call_allocate; 1679 return; 1680 } 1681 /* Use rate-limiting and a max number of retries if refresh 1682 * had status 0 but failed to update the cred. 1683 */ 1684 /* fall through */ 1685 case -ETIMEDOUT: 1686 rpc_delay(task, 3*HZ); 1687 /* fall through */ 1688 case -EAGAIN: 1689 status = -EACCES; 1690 /* fall through */ 1691 case -EKEYEXPIRED: 1692 if (!task->tk_cred_retry) 1693 break; 1694 task->tk_cred_retry--; 1695 dprintk("RPC: %5u %s: retry refresh creds\n", 1696 task->tk_pid, __func__); 1697 return; 1698 } 1699 dprintk("RPC: %5u %s: refresh creds failed with error %d\n", 1700 task->tk_pid, __func__, status); 1701 rpc_call_rpcerror(task, status); 1702 } 1703 1704 /* 1705 * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc. 1706 * (Note: buffer memory is freed in xprt_release). 1707 */ 1708 static void 1709 call_allocate(struct rpc_task *task) 1710 { 1711 const struct rpc_auth *auth = task->tk_rqstp->rq_cred->cr_auth; 1712 struct rpc_rqst *req = task->tk_rqstp; 1713 struct rpc_xprt *xprt = req->rq_xprt; 1714 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc; 1715 int status; 1716 1717 dprint_status(task); 1718 1719 task->tk_status = 0; 1720 task->tk_action = call_encode; 1721 1722 if (req->rq_buffer) 1723 return; 1724 1725 if (proc->p_proc != 0) { 1726 BUG_ON(proc->p_arglen == 0); 1727 if (proc->p_decode != NULL) 1728 BUG_ON(proc->p_replen == 0); 1729 } 1730 1731 /* 1732 * Calculate the size (in quads) of the RPC call 1733 * and reply headers, and convert both values 1734 * to byte sizes. 1735 */ 1736 req->rq_callsize = RPC_CALLHDRSIZE + (auth->au_cslack << 1) + 1737 proc->p_arglen; 1738 req->rq_callsize <<= 2; 1739 /* 1740 * Note: the reply buffer must at minimum allocate enough space 1741 * for the 'struct accepted_reply' from RFC5531. 1742 */ 1743 req->rq_rcvsize = RPC_REPHDRSIZE + auth->au_rslack + \ 1744 max_t(size_t, proc->p_replen, 2); 1745 req->rq_rcvsize <<= 2; 1746 1747 status = xprt->ops->buf_alloc(task); 1748 xprt_inject_disconnect(xprt); 1749 if (status == 0) 1750 return; 1751 if (status != -ENOMEM) { 1752 rpc_call_rpcerror(task, status); 1753 return; 1754 } 1755 1756 dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid); 1757 1758 if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) { 1759 task->tk_action = call_allocate; 1760 rpc_delay(task, HZ>>4); 1761 return; 1762 } 1763 1764 rpc_exit(task, -ERESTARTSYS); 1765 } 1766 1767 static int 1768 rpc_task_need_encode(struct rpc_task *task) 1769 { 1770 return test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) == 0 && 1771 (!(task->tk_flags & RPC_TASK_SENT) || 1772 !(task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) || 1773 xprt_request_need_retransmit(task)); 1774 } 1775 1776 static void 1777 rpc_xdr_encode(struct rpc_task *task) 1778 { 1779 struct rpc_rqst *req = task->tk_rqstp; 1780 struct xdr_stream xdr; 1781 1782 xdr_buf_init(&req->rq_snd_buf, 1783 req->rq_buffer, 1784 req->rq_callsize); 1785 xdr_buf_init(&req->rq_rcv_buf, 1786 req->rq_rbuffer, 1787 req->rq_rcvsize); 1788 1789 req->rq_snd_buf.head[0].iov_len = 0; 1790 xdr_init_encode(&xdr, &req->rq_snd_buf, 1791 req->rq_snd_buf.head[0].iov_base, req); 1792 if (rpc_encode_header(task, &xdr)) 1793 return; 1794 1795 task->tk_status = rpcauth_wrap_req(task, &xdr); 1796 } 1797 1798 /* 1799 * 3. Encode arguments of an RPC call 1800 */ 1801 static void 1802 call_encode(struct rpc_task *task) 1803 { 1804 if (!rpc_task_need_encode(task)) 1805 goto out; 1806 dprint_status(task); 1807 /* Encode here so that rpcsec_gss can use correct sequence number. */ 1808 rpc_xdr_encode(task); 1809 /* Did the encode result in an error condition? */ 1810 if (task->tk_status != 0) { 1811 /* Was the error nonfatal? */ 1812 switch (task->tk_status) { 1813 case -EAGAIN: 1814 case -ENOMEM: 1815 rpc_delay(task, HZ >> 4); 1816 break; 1817 case -EKEYEXPIRED: 1818 if (!task->tk_cred_retry) { 1819 rpc_exit(task, task->tk_status); 1820 } else { 1821 task->tk_action = call_refresh; 1822 task->tk_cred_retry--; 1823 dprintk("RPC: %5u %s: retry refresh creds\n", 1824 task->tk_pid, __func__); 1825 } 1826 break; 1827 default: 1828 rpc_call_rpcerror(task, task->tk_status); 1829 } 1830 return; 1831 } else { 1832 xprt_request_prepare(task->tk_rqstp); 1833 } 1834 1835 /* Add task to reply queue before transmission to avoid races */ 1836 if (rpc_reply_expected(task)) 1837 xprt_request_enqueue_receive(task); 1838 xprt_request_enqueue_transmit(task); 1839 out: 1840 task->tk_action = call_transmit; 1841 /* Check that the connection is OK */ 1842 if (!xprt_bound(task->tk_xprt)) 1843 task->tk_action = call_bind; 1844 else if (!xprt_connected(task->tk_xprt)) 1845 task->tk_action = call_connect; 1846 } 1847 1848 /* 1849 * Helpers to check if the task was already transmitted, and 1850 * to take action when that is the case. 1851 */ 1852 static bool 1853 rpc_task_transmitted(struct rpc_task *task) 1854 { 1855 return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate); 1856 } 1857 1858 static void 1859 rpc_task_handle_transmitted(struct rpc_task *task) 1860 { 1861 xprt_end_transmit(task); 1862 task->tk_action = call_transmit_status; 1863 } 1864 1865 /* 1866 * 4. Get the server port number if not yet set 1867 */ 1868 static void 1869 call_bind(struct rpc_task *task) 1870 { 1871 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt; 1872 1873 if (rpc_task_transmitted(task)) { 1874 rpc_task_handle_transmitted(task); 1875 return; 1876 } 1877 1878 if (xprt_bound(xprt)) { 1879 task->tk_action = call_connect; 1880 return; 1881 } 1882 1883 dprint_status(task); 1884 1885 task->tk_action = call_bind_status; 1886 if (!xprt_prepare_transmit(task)) 1887 return; 1888 1889 xprt->ops->rpcbind(task); 1890 } 1891 1892 /* 1893 * 4a. Sort out bind result 1894 */ 1895 static void 1896 call_bind_status(struct rpc_task *task) 1897 { 1898 int status = -EIO; 1899 1900 if (rpc_task_transmitted(task)) { 1901 rpc_task_handle_transmitted(task); 1902 return; 1903 } 1904 1905 if (task->tk_status >= 0) { 1906 dprint_status(task); 1907 task->tk_status = 0; 1908 task->tk_action = call_connect; 1909 return; 1910 } 1911 1912 trace_rpc_bind_status(task); 1913 switch (task->tk_status) { 1914 case -ENOMEM: 1915 dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid); 1916 rpc_delay(task, HZ >> 2); 1917 goto retry_timeout; 1918 case -EACCES: 1919 dprintk("RPC: %5u remote rpcbind: RPC program/version " 1920 "unavailable\n", task->tk_pid); 1921 /* fail immediately if this is an RPC ping */ 1922 if (task->tk_msg.rpc_proc->p_proc == 0) { 1923 status = -EOPNOTSUPP; 1924 break; 1925 } 1926 if (task->tk_rebind_retry == 0) 1927 break; 1928 task->tk_rebind_retry--; 1929 rpc_delay(task, 3*HZ); 1930 goto retry_timeout; 1931 case -EAGAIN: 1932 goto retry_timeout; 1933 case -ETIMEDOUT: 1934 dprintk("RPC: %5u rpcbind request timed out\n", 1935 task->tk_pid); 1936 goto retry_timeout; 1937 case -EPFNOSUPPORT: 1938 /* server doesn't support any rpcbind version we know of */ 1939 dprintk("RPC: %5u unrecognized remote rpcbind service\n", 1940 task->tk_pid); 1941 break; 1942 case -EPROTONOSUPPORT: 1943 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n", 1944 task->tk_pid); 1945 goto retry_timeout; 1946 case -ECONNREFUSED: /* connection problems */ 1947 case -ECONNRESET: 1948 case -ECONNABORTED: 1949 case -ENOTCONN: 1950 case -EHOSTDOWN: 1951 case -ENETDOWN: 1952 case -EHOSTUNREACH: 1953 case -ENETUNREACH: 1954 case -ENOBUFS: 1955 case -EPIPE: 1956 dprintk("RPC: %5u remote rpcbind unreachable: %d\n", 1957 task->tk_pid, task->tk_status); 1958 if (!RPC_IS_SOFTCONN(task)) { 1959 rpc_delay(task, 5*HZ); 1960 goto retry_timeout; 1961 } 1962 status = task->tk_status; 1963 break; 1964 default: 1965 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n", 1966 task->tk_pid, -task->tk_status); 1967 } 1968 1969 rpc_call_rpcerror(task, status); 1970 return; 1971 1972 retry_timeout: 1973 task->tk_status = 0; 1974 task->tk_action = call_bind; 1975 rpc_check_timeout(task); 1976 } 1977 1978 /* 1979 * 4b. Connect to the RPC server 1980 */ 1981 static void 1982 call_connect(struct rpc_task *task) 1983 { 1984 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt; 1985 1986 if (rpc_task_transmitted(task)) { 1987 rpc_task_handle_transmitted(task); 1988 return; 1989 } 1990 1991 if (xprt_connected(xprt)) { 1992 task->tk_action = call_transmit; 1993 return; 1994 } 1995 1996 dprintk("RPC: %5u call_connect xprt %p %s connected\n", 1997 task->tk_pid, xprt, 1998 (xprt_connected(xprt) ? "is" : "is not")); 1999 2000 task->tk_action = call_connect_status; 2001 if (task->tk_status < 0) 2002 return; 2003 if (task->tk_flags & RPC_TASK_NOCONNECT) { 2004 rpc_call_rpcerror(task, -ENOTCONN); 2005 return; 2006 } 2007 if (!xprt_prepare_transmit(task)) 2008 return; 2009 xprt_connect(task); 2010 } 2011 2012 /* 2013 * 4c. Sort out connect result 2014 */ 2015 static void 2016 call_connect_status(struct rpc_task *task) 2017 { 2018 struct rpc_clnt *clnt = task->tk_client; 2019 int status = task->tk_status; 2020 2021 if (rpc_task_transmitted(task)) { 2022 rpc_task_handle_transmitted(task); 2023 return; 2024 } 2025 2026 dprint_status(task); 2027 2028 trace_rpc_connect_status(task); 2029 task->tk_status = 0; 2030 switch (status) { 2031 case -ECONNREFUSED: 2032 /* A positive refusal suggests a rebind is needed. */ 2033 if (RPC_IS_SOFTCONN(task)) 2034 break; 2035 if (clnt->cl_autobind) { 2036 rpc_force_rebind(clnt); 2037 goto out_retry; 2038 } 2039 /* fall through */ 2040 case -ECONNRESET: 2041 case -ECONNABORTED: 2042 case -ENETDOWN: 2043 case -ENETUNREACH: 2044 case -EHOSTUNREACH: 2045 case -EADDRINUSE: 2046 case -ENOBUFS: 2047 case -EPIPE: 2048 xprt_conditional_disconnect(task->tk_rqstp->rq_xprt, 2049 task->tk_rqstp->rq_connect_cookie); 2050 if (RPC_IS_SOFTCONN(task)) 2051 break; 2052 /* retry with existing socket, after a delay */ 2053 rpc_delay(task, 3*HZ); 2054 /* fall through */ 2055 case -ENOTCONN: 2056 case -EAGAIN: 2057 case -ETIMEDOUT: 2058 goto out_retry; 2059 case 0: 2060 clnt->cl_stats->netreconn++; 2061 task->tk_action = call_transmit; 2062 return; 2063 } 2064 rpc_call_rpcerror(task, status); 2065 return; 2066 out_retry: 2067 /* Check for timeouts before looping back to call_bind */ 2068 task->tk_action = call_bind; 2069 rpc_check_timeout(task); 2070 } 2071 2072 /* 2073 * 5. Transmit the RPC request, and wait for reply 2074 */ 2075 static void 2076 call_transmit(struct rpc_task *task) 2077 { 2078 if (rpc_task_transmitted(task)) { 2079 rpc_task_handle_transmitted(task); 2080 return; 2081 } 2082 2083 dprint_status(task); 2084 2085 task->tk_action = call_transmit_status; 2086 if (!xprt_prepare_transmit(task)) 2087 return; 2088 task->tk_status = 0; 2089 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) { 2090 if (!xprt_connected(task->tk_xprt)) { 2091 task->tk_status = -ENOTCONN; 2092 return; 2093 } 2094 xprt_transmit(task); 2095 } 2096 xprt_end_transmit(task); 2097 } 2098 2099 /* 2100 * 5a. Handle cleanup after a transmission 2101 */ 2102 static void 2103 call_transmit_status(struct rpc_task *task) 2104 { 2105 task->tk_action = call_status; 2106 2107 /* 2108 * Common case: success. Force the compiler to put this 2109 * test first. 2110 */ 2111 if (rpc_task_transmitted(task)) { 2112 task->tk_status = 0; 2113 xprt_request_wait_receive(task); 2114 return; 2115 } 2116 2117 switch (task->tk_status) { 2118 default: 2119 dprint_status(task); 2120 break; 2121 case -EBADMSG: 2122 task->tk_status = 0; 2123 task->tk_action = call_encode; 2124 break; 2125 /* 2126 * Special cases: if we've been waiting on the 2127 * socket's write_space() callback, or if the 2128 * socket just returned a connection error, 2129 * then hold onto the transport lock. 2130 */ 2131 case -ENOBUFS: 2132 rpc_delay(task, HZ>>2); 2133 /* fall through */ 2134 case -EBADSLT: 2135 case -EAGAIN: 2136 task->tk_action = call_transmit; 2137 task->tk_status = 0; 2138 break; 2139 case -ECONNREFUSED: 2140 case -EHOSTDOWN: 2141 case -ENETDOWN: 2142 case -EHOSTUNREACH: 2143 case -ENETUNREACH: 2144 case -EPERM: 2145 if (RPC_IS_SOFTCONN(task)) { 2146 if (!task->tk_msg.rpc_proc->p_proc) 2147 trace_xprt_ping(task->tk_xprt, 2148 task->tk_status); 2149 rpc_call_rpcerror(task, task->tk_status); 2150 return; 2151 } 2152 /* fall through */ 2153 case -ECONNRESET: 2154 case -ECONNABORTED: 2155 case -EADDRINUSE: 2156 case -ENOTCONN: 2157 case -EPIPE: 2158 task->tk_action = call_bind; 2159 task->tk_status = 0; 2160 break; 2161 } 2162 rpc_check_timeout(task); 2163 } 2164 2165 #if defined(CONFIG_SUNRPC_BACKCHANNEL) 2166 static void call_bc_transmit(struct rpc_task *task); 2167 static void call_bc_transmit_status(struct rpc_task *task); 2168 2169 static void 2170 call_bc_encode(struct rpc_task *task) 2171 { 2172 xprt_request_enqueue_transmit(task); 2173 task->tk_action = call_bc_transmit; 2174 } 2175 2176 /* 2177 * 5b. Send the backchannel RPC reply. On error, drop the reply. In 2178 * addition, disconnect on connectivity errors. 2179 */ 2180 static void 2181 call_bc_transmit(struct rpc_task *task) 2182 { 2183 task->tk_action = call_bc_transmit_status; 2184 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) { 2185 if (!xprt_prepare_transmit(task)) 2186 return; 2187 task->tk_status = 0; 2188 xprt_transmit(task); 2189 } 2190 xprt_end_transmit(task); 2191 } 2192 2193 static void 2194 call_bc_transmit_status(struct rpc_task *task) 2195 { 2196 struct rpc_rqst *req = task->tk_rqstp; 2197 2198 if (rpc_task_transmitted(task)) 2199 task->tk_status = 0; 2200 2201 dprint_status(task); 2202 2203 switch (task->tk_status) { 2204 case 0: 2205 /* Success */ 2206 case -ENETDOWN: 2207 case -EHOSTDOWN: 2208 case -EHOSTUNREACH: 2209 case -ENETUNREACH: 2210 case -ECONNRESET: 2211 case -ECONNREFUSED: 2212 case -EADDRINUSE: 2213 case -ENOTCONN: 2214 case -EPIPE: 2215 break; 2216 case -ENOBUFS: 2217 rpc_delay(task, HZ>>2); 2218 /* fall through */ 2219 case -EBADSLT: 2220 case -EAGAIN: 2221 task->tk_status = 0; 2222 task->tk_action = call_bc_transmit; 2223 return; 2224 case -ETIMEDOUT: 2225 /* 2226 * Problem reaching the server. Disconnect and let the 2227 * forechannel reestablish the connection. The server will 2228 * have to retransmit the backchannel request and we'll 2229 * reprocess it. Since these ops are idempotent, there's no 2230 * need to cache our reply at this time. 2231 */ 2232 printk(KERN_NOTICE "RPC: Could not send backchannel reply " 2233 "error: %d\n", task->tk_status); 2234 xprt_conditional_disconnect(req->rq_xprt, 2235 req->rq_connect_cookie); 2236 break; 2237 default: 2238 /* 2239 * We were unable to reply and will have to drop the 2240 * request. The server should reconnect and retransmit. 2241 */ 2242 printk(KERN_NOTICE "RPC: Could not send backchannel reply " 2243 "error: %d\n", task->tk_status); 2244 break; 2245 } 2246 task->tk_action = rpc_exit_task; 2247 } 2248 #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 2249 2250 /* 2251 * 6. Sort out the RPC call status 2252 */ 2253 static void 2254 call_status(struct rpc_task *task) 2255 { 2256 struct rpc_clnt *clnt = task->tk_client; 2257 int status; 2258 2259 if (!task->tk_msg.rpc_proc->p_proc) 2260 trace_xprt_ping(task->tk_xprt, task->tk_status); 2261 2262 dprint_status(task); 2263 2264 status = task->tk_status; 2265 if (status >= 0) { 2266 task->tk_action = call_decode; 2267 return; 2268 } 2269 2270 trace_rpc_call_status(task); 2271 task->tk_status = 0; 2272 switch(status) { 2273 case -EHOSTDOWN: 2274 case -ENETDOWN: 2275 case -EHOSTUNREACH: 2276 case -ENETUNREACH: 2277 case -EPERM: 2278 if (RPC_IS_SOFTCONN(task)) 2279 goto out_exit; 2280 /* 2281 * Delay any retries for 3 seconds, then handle as if it 2282 * were a timeout. 2283 */ 2284 rpc_delay(task, 3*HZ); 2285 /* fall through */ 2286 case -ETIMEDOUT: 2287 break; 2288 case -ECONNREFUSED: 2289 case -ECONNRESET: 2290 case -ECONNABORTED: 2291 case -ENOTCONN: 2292 rpc_force_rebind(clnt); 2293 /* fall through */ 2294 case -EADDRINUSE: 2295 rpc_delay(task, 3*HZ); 2296 /* fall through */ 2297 case -EPIPE: 2298 case -EAGAIN: 2299 break; 2300 case -EIO: 2301 /* shutdown or soft timeout */ 2302 goto out_exit; 2303 default: 2304 if (clnt->cl_chatty) 2305 printk("%s: RPC call returned error %d\n", 2306 clnt->cl_program->name, -status); 2307 goto out_exit; 2308 } 2309 task->tk_action = call_encode; 2310 rpc_check_timeout(task); 2311 return; 2312 out_exit: 2313 rpc_call_rpcerror(task, status); 2314 } 2315 2316 static bool 2317 rpc_check_connected(const struct rpc_rqst *req) 2318 { 2319 /* No allocated request or transport? return true */ 2320 if (!req || !req->rq_xprt) 2321 return true; 2322 return xprt_connected(req->rq_xprt); 2323 } 2324 2325 static void 2326 rpc_check_timeout(struct rpc_task *task) 2327 { 2328 struct rpc_clnt *clnt = task->tk_client; 2329 2330 if (xprt_adjust_timeout(task->tk_rqstp) == 0) 2331 return; 2332 2333 dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid); 2334 task->tk_timeouts++; 2335 2336 if (RPC_IS_SOFTCONN(task) && !rpc_check_connected(task->tk_rqstp)) { 2337 rpc_call_rpcerror(task, -ETIMEDOUT); 2338 return; 2339 } 2340 2341 if (RPC_IS_SOFT(task)) { 2342 /* 2343 * Once a "no retrans timeout" soft tasks (a.k.a NFSv4) has 2344 * been sent, it should time out only if the transport 2345 * connection gets terminally broken. 2346 */ 2347 if ((task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) && 2348 rpc_check_connected(task->tk_rqstp)) 2349 return; 2350 2351 if (clnt->cl_chatty) { 2352 pr_notice_ratelimited( 2353 "%s: server %s not responding, timed out\n", 2354 clnt->cl_program->name, 2355 task->tk_xprt->servername); 2356 } 2357 if (task->tk_flags & RPC_TASK_TIMEOUT) 2358 rpc_call_rpcerror(task, -ETIMEDOUT); 2359 else 2360 __rpc_call_rpcerror(task, -EIO, -ETIMEDOUT); 2361 return; 2362 } 2363 2364 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) { 2365 task->tk_flags |= RPC_CALL_MAJORSEEN; 2366 if (clnt->cl_chatty) { 2367 pr_notice_ratelimited( 2368 "%s: server %s not responding, still trying\n", 2369 clnt->cl_program->name, 2370 task->tk_xprt->servername); 2371 } 2372 } 2373 rpc_force_rebind(clnt); 2374 /* 2375 * Did our request time out due to an RPCSEC_GSS out-of-sequence 2376 * event? RFC2203 requires the server to drop all such requests. 2377 */ 2378 rpcauth_invalcred(task); 2379 } 2380 2381 /* 2382 * 7. Decode the RPC reply 2383 */ 2384 static void 2385 call_decode(struct rpc_task *task) 2386 { 2387 struct rpc_clnt *clnt = task->tk_client; 2388 struct rpc_rqst *req = task->tk_rqstp; 2389 struct xdr_stream xdr; 2390 2391 dprint_status(task); 2392 2393 if (!task->tk_msg.rpc_proc->p_decode) { 2394 task->tk_action = rpc_exit_task; 2395 return; 2396 } 2397 2398 if (task->tk_flags & RPC_CALL_MAJORSEEN) { 2399 if (clnt->cl_chatty) { 2400 pr_notice_ratelimited("%s: server %s OK\n", 2401 clnt->cl_program->name, 2402 task->tk_xprt->servername); 2403 } 2404 task->tk_flags &= ~RPC_CALL_MAJORSEEN; 2405 } 2406 2407 /* 2408 * Ensure that we see all writes made by xprt_complete_rqst() 2409 * before it changed req->rq_reply_bytes_recvd. 2410 */ 2411 smp_rmb(); 2412 req->rq_rcv_buf.len = req->rq_private_buf.len; 2413 2414 /* Check that the softirq receive buffer is valid */ 2415 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf, 2416 sizeof(req->rq_rcv_buf)) != 0); 2417 2418 xdr_init_decode(&xdr, &req->rq_rcv_buf, 2419 req->rq_rcv_buf.head[0].iov_base, req); 2420 switch (rpc_decode_header(task, &xdr)) { 2421 case 0: 2422 task->tk_action = rpc_exit_task; 2423 task->tk_status = rpcauth_unwrap_resp(task, &xdr); 2424 dprintk("RPC: %5u %s result %d\n", 2425 task->tk_pid, __func__, task->tk_status); 2426 return; 2427 case -EAGAIN: 2428 task->tk_status = 0; 2429 xdr_free_bvec(&req->rq_rcv_buf); 2430 req->rq_reply_bytes_recvd = 0; 2431 req->rq_rcv_buf.len = 0; 2432 if (task->tk_client->cl_discrtry) 2433 xprt_conditional_disconnect(req->rq_xprt, 2434 req->rq_connect_cookie); 2435 task->tk_action = call_encode; 2436 rpc_check_timeout(task); 2437 break; 2438 case -EKEYREJECTED: 2439 task->tk_action = call_reserve; 2440 rpc_check_timeout(task); 2441 rpcauth_invalcred(task); 2442 /* Ensure we obtain a new XID if we retry! */ 2443 xprt_release(task); 2444 } 2445 } 2446 2447 static int 2448 rpc_encode_header(struct rpc_task *task, struct xdr_stream *xdr) 2449 { 2450 struct rpc_clnt *clnt = task->tk_client; 2451 struct rpc_rqst *req = task->tk_rqstp; 2452 __be32 *p; 2453 int error; 2454 2455 error = -EMSGSIZE; 2456 p = xdr_reserve_space(xdr, RPC_CALLHDRSIZE << 2); 2457 if (!p) 2458 goto out_fail; 2459 *p++ = req->rq_xid; 2460 *p++ = rpc_call; 2461 *p++ = cpu_to_be32(RPC_VERSION); 2462 *p++ = cpu_to_be32(clnt->cl_prog); 2463 *p++ = cpu_to_be32(clnt->cl_vers); 2464 *p = cpu_to_be32(task->tk_msg.rpc_proc->p_proc); 2465 2466 error = rpcauth_marshcred(task, xdr); 2467 if (error < 0) 2468 goto out_fail; 2469 return 0; 2470 out_fail: 2471 trace_rpc_bad_callhdr(task); 2472 rpc_exit(task, error); 2473 return error; 2474 } 2475 2476 static noinline int 2477 rpc_decode_header(struct rpc_task *task, struct xdr_stream *xdr) 2478 { 2479 struct rpc_clnt *clnt = task->tk_client; 2480 int error; 2481 __be32 *p; 2482 2483 /* RFC-1014 says that the representation of XDR data must be a 2484 * multiple of four bytes 2485 * - if it isn't pointer subtraction in the NFS client may give 2486 * undefined results 2487 */ 2488 if (task->tk_rqstp->rq_rcv_buf.len & 3) 2489 goto out_unparsable; 2490 2491 p = xdr_inline_decode(xdr, 3 * sizeof(*p)); 2492 if (!p) 2493 goto out_unparsable; 2494 p++; /* skip XID */ 2495 if (*p++ != rpc_reply) 2496 goto out_unparsable; 2497 if (*p++ != rpc_msg_accepted) 2498 goto out_msg_denied; 2499 2500 error = rpcauth_checkverf(task, xdr); 2501 if (error) 2502 goto out_verifier; 2503 2504 p = xdr_inline_decode(xdr, sizeof(*p)); 2505 if (!p) 2506 goto out_unparsable; 2507 switch (*p) { 2508 case rpc_success: 2509 return 0; 2510 case rpc_prog_unavail: 2511 trace_rpc__prog_unavail(task); 2512 error = -EPFNOSUPPORT; 2513 goto out_err; 2514 case rpc_prog_mismatch: 2515 trace_rpc__prog_mismatch(task); 2516 error = -EPROTONOSUPPORT; 2517 goto out_err; 2518 case rpc_proc_unavail: 2519 trace_rpc__proc_unavail(task); 2520 error = -EOPNOTSUPP; 2521 goto out_err; 2522 case rpc_garbage_args: 2523 case rpc_system_err: 2524 trace_rpc__garbage_args(task); 2525 error = -EIO; 2526 break; 2527 default: 2528 goto out_unparsable; 2529 } 2530 2531 out_garbage: 2532 clnt->cl_stats->rpcgarbage++; 2533 if (task->tk_garb_retry) { 2534 task->tk_garb_retry--; 2535 task->tk_action = call_encode; 2536 return -EAGAIN; 2537 } 2538 out_err: 2539 rpc_exit(task, error); 2540 return error; 2541 2542 out_unparsable: 2543 trace_rpc__unparsable(task); 2544 error = -EIO; 2545 goto out_garbage; 2546 2547 out_verifier: 2548 trace_rpc_bad_verifier(task); 2549 goto out_garbage; 2550 2551 out_msg_denied: 2552 error = -EACCES; 2553 p = xdr_inline_decode(xdr, sizeof(*p)); 2554 if (!p) 2555 goto out_unparsable; 2556 switch (*p++) { 2557 case rpc_auth_error: 2558 break; 2559 case rpc_mismatch: 2560 trace_rpc__mismatch(task); 2561 error = -EPROTONOSUPPORT; 2562 goto out_err; 2563 default: 2564 goto out_unparsable; 2565 } 2566 2567 p = xdr_inline_decode(xdr, sizeof(*p)); 2568 if (!p) 2569 goto out_unparsable; 2570 switch (*p++) { 2571 case rpc_autherr_rejectedcred: 2572 case rpc_autherr_rejectedverf: 2573 case rpcsec_gsserr_credproblem: 2574 case rpcsec_gsserr_ctxproblem: 2575 if (!task->tk_cred_retry) 2576 break; 2577 task->tk_cred_retry--; 2578 trace_rpc__stale_creds(task); 2579 return -EKEYREJECTED; 2580 case rpc_autherr_badcred: 2581 case rpc_autherr_badverf: 2582 /* possibly garbled cred/verf? */ 2583 if (!task->tk_garb_retry) 2584 break; 2585 task->tk_garb_retry--; 2586 trace_rpc__bad_creds(task); 2587 task->tk_action = call_encode; 2588 return -EAGAIN; 2589 case rpc_autherr_tooweak: 2590 trace_rpc__auth_tooweak(task); 2591 pr_warn("RPC: server %s requires stronger authentication.\n", 2592 task->tk_xprt->servername); 2593 break; 2594 default: 2595 goto out_unparsable; 2596 } 2597 goto out_err; 2598 } 2599 2600 static void rpcproc_encode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr, 2601 const void *obj) 2602 { 2603 } 2604 2605 static int rpcproc_decode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr, 2606 void *obj) 2607 { 2608 return 0; 2609 } 2610 2611 static const struct rpc_procinfo rpcproc_null = { 2612 .p_encode = rpcproc_encode_null, 2613 .p_decode = rpcproc_decode_null, 2614 }; 2615 2616 static int rpc_ping(struct rpc_clnt *clnt) 2617 { 2618 struct rpc_message msg = { 2619 .rpc_proc = &rpcproc_null, 2620 }; 2621 int err; 2622 err = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN | 2623 RPC_TASK_NULLCREDS); 2624 return err; 2625 } 2626 2627 static 2628 struct rpc_task *rpc_call_null_helper(struct rpc_clnt *clnt, 2629 struct rpc_xprt *xprt, struct rpc_cred *cred, int flags, 2630 const struct rpc_call_ops *ops, void *data) 2631 { 2632 struct rpc_message msg = { 2633 .rpc_proc = &rpcproc_null, 2634 }; 2635 struct rpc_task_setup task_setup_data = { 2636 .rpc_client = clnt, 2637 .rpc_xprt = xprt, 2638 .rpc_message = &msg, 2639 .rpc_op_cred = cred, 2640 .callback_ops = (ops != NULL) ? ops : &rpc_default_ops, 2641 .callback_data = data, 2642 .flags = flags | RPC_TASK_NULLCREDS, 2643 }; 2644 2645 return rpc_run_task(&task_setup_data); 2646 } 2647 2648 struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags) 2649 { 2650 return rpc_call_null_helper(clnt, NULL, cred, flags, NULL, NULL); 2651 } 2652 EXPORT_SYMBOL_GPL(rpc_call_null); 2653 2654 struct rpc_cb_add_xprt_calldata { 2655 struct rpc_xprt_switch *xps; 2656 struct rpc_xprt *xprt; 2657 }; 2658 2659 static void rpc_cb_add_xprt_done(struct rpc_task *task, void *calldata) 2660 { 2661 struct rpc_cb_add_xprt_calldata *data = calldata; 2662 2663 if (task->tk_status == 0) 2664 rpc_xprt_switch_add_xprt(data->xps, data->xprt); 2665 } 2666 2667 static void rpc_cb_add_xprt_release(void *calldata) 2668 { 2669 struct rpc_cb_add_xprt_calldata *data = calldata; 2670 2671 xprt_put(data->xprt); 2672 xprt_switch_put(data->xps); 2673 kfree(data); 2674 } 2675 2676 static const struct rpc_call_ops rpc_cb_add_xprt_call_ops = { 2677 .rpc_call_done = rpc_cb_add_xprt_done, 2678 .rpc_release = rpc_cb_add_xprt_release, 2679 }; 2680 2681 /** 2682 * rpc_clnt_test_and_add_xprt - Test and add a new transport to a rpc_clnt 2683 * @clnt: pointer to struct rpc_clnt 2684 * @xps: pointer to struct rpc_xprt_switch, 2685 * @xprt: pointer struct rpc_xprt 2686 * @dummy: unused 2687 */ 2688 int rpc_clnt_test_and_add_xprt(struct rpc_clnt *clnt, 2689 struct rpc_xprt_switch *xps, struct rpc_xprt *xprt, 2690 void *dummy) 2691 { 2692 struct rpc_cb_add_xprt_calldata *data; 2693 struct rpc_task *task; 2694 2695 data = kmalloc(sizeof(*data), GFP_NOFS); 2696 if (!data) 2697 return -ENOMEM; 2698 data->xps = xprt_switch_get(xps); 2699 data->xprt = xprt_get(xprt); 2700 2701 task = rpc_call_null_helper(clnt, xprt, NULL, 2702 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC|RPC_TASK_NULLCREDS, 2703 &rpc_cb_add_xprt_call_ops, data); 2704 if (IS_ERR(task)) 2705 return PTR_ERR(task); 2706 rpc_put_task(task); 2707 return 1; 2708 } 2709 EXPORT_SYMBOL_GPL(rpc_clnt_test_and_add_xprt); 2710 2711 /** 2712 * rpc_clnt_setup_test_and_add_xprt() 2713 * 2714 * This is an rpc_clnt_add_xprt setup() function which returns 1 so: 2715 * 1) caller of the test function must dereference the rpc_xprt_switch 2716 * and the rpc_xprt. 2717 * 2) test function must call rpc_xprt_switch_add_xprt, usually in 2718 * the rpc_call_done routine. 2719 * 2720 * Upon success (return of 1), the test function adds the new 2721 * transport to the rpc_clnt xprt switch 2722 * 2723 * @clnt: struct rpc_clnt to get the new transport 2724 * @xps: the rpc_xprt_switch to hold the new transport 2725 * @xprt: the rpc_xprt to test 2726 * @data: a struct rpc_add_xprt_test pointer that holds the test function 2727 * and test function call data 2728 */ 2729 int rpc_clnt_setup_test_and_add_xprt(struct rpc_clnt *clnt, 2730 struct rpc_xprt_switch *xps, 2731 struct rpc_xprt *xprt, 2732 void *data) 2733 { 2734 struct rpc_task *task; 2735 struct rpc_add_xprt_test *xtest = (struct rpc_add_xprt_test *)data; 2736 int status = -EADDRINUSE; 2737 2738 xprt = xprt_get(xprt); 2739 xprt_switch_get(xps); 2740 2741 if (rpc_xprt_switch_has_addr(xps, (struct sockaddr *)&xprt->addr)) 2742 goto out_err; 2743 2744 /* Test the connection */ 2745 task = rpc_call_null_helper(clnt, xprt, NULL, 2746 RPC_TASK_SOFT | RPC_TASK_SOFTCONN | RPC_TASK_NULLCREDS, 2747 NULL, NULL); 2748 if (IS_ERR(task)) { 2749 status = PTR_ERR(task); 2750 goto out_err; 2751 } 2752 status = task->tk_status; 2753 rpc_put_task(task); 2754 2755 if (status < 0) 2756 goto out_err; 2757 2758 /* rpc_xprt_switch and rpc_xprt are deferrenced by add_xprt_test() */ 2759 xtest->add_xprt_test(clnt, xprt, xtest->data); 2760 2761 xprt_put(xprt); 2762 xprt_switch_put(xps); 2763 2764 /* so that rpc_clnt_add_xprt does not call rpc_xprt_switch_add_xprt */ 2765 return 1; 2766 out_err: 2767 xprt_put(xprt); 2768 xprt_switch_put(xps); 2769 pr_info("RPC: rpc_clnt_test_xprt failed: %d addr %s not added\n", 2770 status, xprt->address_strings[RPC_DISPLAY_ADDR]); 2771 return status; 2772 } 2773 EXPORT_SYMBOL_GPL(rpc_clnt_setup_test_and_add_xprt); 2774 2775 /** 2776 * rpc_clnt_add_xprt - Add a new transport to a rpc_clnt 2777 * @clnt: pointer to struct rpc_clnt 2778 * @xprtargs: pointer to struct xprt_create 2779 * @setup: callback to test and/or set up the connection 2780 * @data: pointer to setup function data 2781 * 2782 * Creates a new transport using the parameters set in args and 2783 * adds it to clnt. 2784 * If ping is set, then test that connectivity succeeds before 2785 * adding the new transport. 2786 * 2787 */ 2788 int rpc_clnt_add_xprt(struct rpc_clnt *clnt, 2789 struct xprt_create *xprtargs, 2790 int (*setup)(struct rpc_clnt *, 2791 struct rpc_xprt_switch *, 2792 struct rpc_xprt *, 2793 void *), 2794 void *data) 2795 { 2796 struct rpc_xprt_switch *xps; 2797 struct rpc_xprt *xprt; 2798 unsigned long connect_timeout; 2799 unsigned long reconnect_timeout; 2800 unsigned char resvport; 2801 int ret = 0; 2802 2803 rcu_read_lock(); 2804 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch)); 2805 xprt = xprt_iter_xprt(&clnt->cl_xpi); 2806 if (xps == NULL || xprt == NULL) { 2807 rcu_read_unlock(); 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