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