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