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