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