SUNRPC: Modify synopsis of rpc_client_register()
[deliverable/linux.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/utsname.h>
29 #include <linux/workqueue.h>
30 #include <linux/in.h>
31 #include <linux/in6.h>
32 #include <linux/un.h>
33 #include <linux/rcupdate.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 #ifdef RPC_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 int err = 0;
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 err;
205 }
206
207 static int __rpc_pipefs_event(struct rpc_clnt *clnt, unsigned long event,
208 struct super_block *sb)
209 {
210 int error = 0;
211
212 for (;; clnt = clnt->cl_parent) {
213 if (!rpc_clnt_skip_event(clnt, event))
214 error = __rpc_clnt_handle_event(clnt, event, sb);
215 if (error || clnt == clnt->cl_parent)
216 break;
217 }
218 return error;
219 }
220
221 static struct rpc_clnt *rpc_get_client_for_event(struct net *net, int event)
222 {
223 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
224 struct rpc_clnt *clnt;
225
226 spin_lock(&sn->rpc_client_lock);
227 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
228 if (rpc_clnt_skip_event(clnt, event))
229 continue;
230 spin_unlock(&sn->rpc_client_lock);
231 return clnt;
232 }
233 spin_unlock(&sn->rpc_client_lock);
234 return NULL;
235 }
236
237 static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
238 void *ptr)
239 {
240 struct super_block *sb = ptr;
241 struct rpc_clnt *clnt;
242 int error = 0;
243
244 while ((clnt = rpc_get_client_for_event(sb->s_fs_info, event))) {
245 error = __rpc_pipefs_event(clnt, event, sb);
246 if (error)
247 break;
248 }
249 return error;
250 }
251
252 static struct notifier_block rpc_clients_block = {
253 .notifier_call = rpc_pipefs_event,
254 .priority = SUNRPC_PIPEFS_RPC_PRIO,
255 };
256
257 int rpc_clients_notifier_register(void)
258 {
259 return rpc_pipefs_notifier_register(&rpc_clients_block);
260 }
261
262 void rpc_clients_notifier_unregister(void)
263 {
264 return rpc_pipefs_notifier_unregister(&rpc_clients_block);
265 }
266
267 static void rpc_clnt_set_nodename(struct rpc_clnt *clnt, const char *nodename)
268 {
269 clnt->cl_nodelen = strlen(nodename);
270 if (clnt->cl_nodelen > UNX_MAXNODENAME)
271 clnt->cl_nodelen = UNX_MAXNODENAME;
272 memcpy(clnt->cl_nodename, nodename, clnt->cl_nodelen);
273 }
274
275 static int rpc_client_register(struct rpc_clnt *clnt,
276 rpc_authflavor_t pseudoflavor,
277 const char *client_name)
278 {
279 struct rpc_auth_create_args auth_args = {
280 .pseudoflavor = pseudoflavor,
281 .target_name = client_name,
282 };
283 struct rpc_auth *auth;
284 struct net *net = rpc_net_ns(clnt);
285 struct super_block *pipefs_sb;
286 int err;
287
288 pipefs_sb = rpc_get_sb_net(net);
289 if (pipefs_sb) {
290 err = rpc_setup_pipedir(pipefs_sb, clnt);
291 if (err)
292 goto out;
293 }
294
295 rpc_register_client(clnt);
296 if (pipefs_sb)
297 rpc_put_sb_net(net);
298
299 auth = rpcauth_create(&auth_args, clnt);
300 if (IS_ERR(auth)) {
301 dprintk("RPC: Couldn't create auth handle (flavor %u)\n",
302 pseudoflavor);
303 err = PTR_ERR(auth);
304 goto err_auth;
305 }
306 return 0;
307 err_auth:
308 pipefs_sb = rpc_get_sb_net(net);
309 rpc_unregister_client(clnt);
310 __rpc_clnt_remove_pipedir(clnt);
311 out:
312 if (pipefs_sb)
313 rpc_put_sb_net(net);
314 return err;
315 }
316
317 static DEFINE_IDA(rpc_clids);
318
319 static int rpc_alloc_clid(struct rpc_clnt *clnt)
320 {
321 int clid;
322
323 clid = ida_simple_get(&rpc_clids, 0, 0, GFP_KERNEL);
324 if (clid < 0)
325 return clid;
326 clnt->cl_clid = clid;
327 return 0;
328 }
329
330 static void rpc_free_clid(struct rpc_clnt *clnt)
331 {
332 ida_simple_remove(&rpc_clids, clnt->cl_clid);
333 }
334
335 static struct rpc_clnt * rpc_new_client(const struct rpc_create_args *args,
336 struct rpc_xprt *xprt,
337 struct rpc_clnt *parent)
338 {
339 const struct rpc_program *program = args->program;
340 const struct rpc_version *version;
341 struct rpc_clnt *clnt = NULL;
342 int err;
343
344 /* sanity check the name before trying to print it */
345 dprintk("RPC: creating %s client for %s (xprt %p)\n",
346 program->name, args->servername, xprt);
347
348 err = rpciod_up();
349 if (err)
350 goto out_no_rpciod;
351
352 err = -EINVAL;
353 if (args->version >= program->nrvers)
354 goto out_err;
355 version = program->version[args->version];
356 if (version == NULL)
357 goto out_err;
358
359 err = -ENOMEM;
360 clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
361 if (!clnt)
362 goto out_err;
363 clnt->cl_parent = parent ? : clnt;
364
365 err = rpc_alloc_clid(clnt);
366 if (err)
367 goto out_no_clid;
368
369 rcu_assign_pointer(clnt->cl_xprt, xprt);
370 clnt->cl_procinfo = version->procs;
371 clnt->cl_maxproc = version->nrprocs;
372 clnt->cl_prog = args->prognumber ? : program->number;
373 clnt->cl_vers = version->number;
374 clnt->cl_stats = program->stats;
375 clnt->cl_metrics = rpc_alloc_iostats(clnt);
376 rpc_init_pipe_dir_head(&clnt->cl_pipedir_objects);
377 err = -ENOMEM;
378 if (clnt->cl_metrics == NULL)
379 goto out_no_stats;
380 clnt->cl_program = program;
381 INIT_LIST_HEAD(&clnt->cl_tasks);
382 spin_lock_init(&clnt->cl_lock);
383
384 if (!xprt_bound(xprt))
385 clnt->cl_autobind = 1;
386
387 clnt->cl_timeout = xprt->timeout;
388 if (args->timeout != NULL) {
389 memcpy(&clnt->cl_timeout_default, args->timeout,
390 sizeof(clnt->cl_timeout_default));
391 clnt->cl_timeout = &clnt->cl_timeout_default;
392 }
393
394 clnt->cl_rtt = &clnt->cl_rtt_default;
395 rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval);
396
397 atomic_set(&clnt->cl_count, 1);
398
399 /* save the nodename */
400 rpc_clnt_set_nodename(clnt, utsname()->nodename);
401
402 err = rpc_client_register(clnt, args->authflavor, args->client_name);
403 if (err)
404 goto out_no_path;
405 if (parent)
406 atomic_inc(&parent->cl_count);
407 return clnt;
408
409 out_no_path:
410 rpc_free_iostats(clnt->cl_metrics);
411 out_no_stats:
412 rpc_free_clid(clnt);
413 out_no_clid:
414 kfree(clnt);
415 out_err:
416 rpciod_down();
417 out_no_rpciod:
418 xprt_put(xprt);
419 return ERR_PTR(err);
420 }
421
422 /**
423 * rpc_create - create an RPC client and transport with one call
424 * @args: rpc_clnt create argument structure
425 *
426 * Creates and initializes an RPC transport and an RPC client.
427 *
428 * It can ping the server in order to determine if it is up, and to see if
429 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
430 * this behavior so asynchronous tasks can also use rpc_create.
431 */
432 struct rpc_clnt *rpc_create(struct rpc_create_args *args)
433 {
434 struct rpc_xprt *xprt;
435 struct rpc_clnt *clnt;
436 struct xprt_create xprtargs = {
437 .net = args->net,
438 .ident = args->protocol,
439 .srcaddr = args->saddress,
440 .dstaddr = args->address,
441 .addrlen = args->addrsize,
442 .servername = args->servername,
443 .bc_xprt = args->bc_xprt,
444 };
445 char servername[48];
446
447 if (args->flags & RPC_CLNT_CREATE_INFINITE_SLOTS)
448 xprtargs.flags |= XPRT_CREATE_INFINITE_SLOTS;
449 if (args->flags & RPC_CLNT_CREATE_NO_IDLE_TIMEOUT)
450 xprtargs.flags |= XPRT_CREATE_NO_IDLE_TIMEOUT;
451 /*
452 * If the caller chooses not to specify a hostname, whip
453 * up a string representation of the passed-in address.
454 */
455 if (xprtargs.servername == NULL) {
456 struct sockaddr_un *sun =
457 (struct sockaddr_un *)args->address;
458 struct sockaddr_in *sin =
459 (struct sockaddr_in *)args->address;
460 struct sockaddr_in6 *sin6 =
461 (struct sockaddr_in6 *)args->address;
462
463 servername[0] = '\0';
464 switch (args->address->sa_family) {
465 case AF_LOCAL:
466 snprintf(servername, sizeof(servername), "%s",
467 sun->sun_path);
468 break;
469 case AF_INET:
470 snprintf(servername, sizeof(servername), "%pI4",
471 &sin->sin_addr.s_addr);
472 break;
473 case AF_INET6:
474 snprintf(servername, sizeof(servername), "%pI6",
475 &sin6->sin6_addr);
476 break;
477 default:
478 /* caller wants default server name, but
479 * address family isn't recognized. */
480 return ERR_PTR(-EINVAL);
481 }
482 xprtargs.servername = servername;
483 }
484
485 xprt = xprt_create_transport(&xprtargs);
486 if (IS_ERR(xprt))
487 return (struct rpc_clnt *)xprt;
488
489 /*
490 * By default, kernel RPC client connects from a reserved port.
491 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
492 * but it is always enabled for rpciod, which handles the connect
493 * operation.
494 */
495 xprt->resvport = 1;
496 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
497 xprt->resvport = 0;
498
499 clnt = rpc_new_client(args, xprt, NULL);
500 if (IS_ERR(clnt))
501 return clnt;
502
503 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
504 int err = rpc_ping(clnt);
505 if (err != 0) {
506 rpc_shutdown_client(clnt);
507 return ERR_PTR(err);
508 }
509 }
510
511 clnt->cl_softrtry = 1;
512 if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
513 clnt->cl_softrtry = 0;
514
515 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
516 clnt->cl_autobind = 1;
517 if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
518 clnt->cl_discrtry = 1;
519 if (!(args->flags & RPC_CLNT_CREATE_QUIET))
520 clnt->cl_chatty = 1;
521
522 return clnt;
523 }
524 EXPORT_SYMBOL_GPL(rpc_create);
525
526 /*
527 * This function clones the RPC client structure. It allows us to share the
528 * same transport while varying parameters such as the authentication
529 * flavour.
530 */
531 static struct rpc_clnt *__rpc_clone_client(struct rpc_create_args *args,
532 struct rpc_clnt *clnt)
533 {
534 struct rpc_xprt *xprt;
535 struct rpc_clnt *new;
536 int err;
537
538 err = -ENOMEM;
539 rcu_read_lock();
540 xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
541 rcu_read_unlock();
542 if (xprt == NULL)
543 goto out_err;
544 args->servername = xprt->servername;
545
546 new = rpc_new_client(args, xprt, clnt);
547 if (IS_ERR(new)) {
548 err = PTR_ERR(new);
549 goto out_err;
550 }
551
552 /* Turn off autobind on clones */
553 new->cl_autobind = 0;
554 new->cl_softrtry = clnt->cl_softrtry;
555 new->cl_discrtry = clnt->cl_discrtry;
556 new->cl_chatty = clnt->cl_chatty;
557 return new;
558
559 out_err:
560 dprintk("RPC: %s: returned error %d\n", __func__, err);
561 return ERR_PTR(err);
562 }
563
564 /**
565 * rpc_clone_client - Clone an RPC client structure
566 *
567 * @clnt: RPC client whose parameters are copied
568 *
569 * Returns a fresh RPC client or an ERR_PTR.
570 */
571 struct rpc_clnt *rpc_clone_client(struct rpc_clnt *clnt)
572 {
573 struct rpc_create_args args = {
574 .program = clnt->cl_program,
575 .prognumber = clnt->cl_prog,
576 .version = clnt->cl_vers,
577 .authflavor = clnt->cl_auth->au_flavor,
578 };
579 return __rpc_clone_client(&args, clnt);
580 }
581 EXPORT_SYMBOL_GPL(rpc_clone_client);
582
583 /**
584 * rpc_clone_client_set_auth - Clone an RPC client structure and set its auth
585 *
586 * @clnt: RPC client whose parameters are copied
587 * @flavor: security flavor for new client
588 *
589 * Returns a fresh RPC client or an ERR_PTR.
590 */
591 struct rpc_clnt *
592 rpc_clone_client_set_auth(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
593 {
594 struct rpc_create_args args = {
595 .program = clnt->cl_program,
596 .prognumber = clnt->cl_prog,
597 .version = clnt->cl_vers,
598 .authflavor = flavor,
599 };
600 return __rpc_clone_client(&args, clnt);
601 }
602 EXPORT_SYMBOL_GPL(rpc_clone_client_set_auth);
603
604 /*
605 * Kill all tasks for the given client.
606 * XXX: kill their descendants as well?
607 */
608 void rpc_killall_tasks(struct rpc_clnt *clnt)
609 {
610 struct rpc_task *rovr;
611
612
613 if (list_empty(&clnt->cl_tasks))
614 return;
615 dprintk("RPC: killing all tasks for client %p\n", clnt);
616 /*
617 * Spin lock all_tasks to prevent changes...
618 */
619 spin_lock(&clnt->cl_lock);
620 list_for_each_entry(rovr, &clnt->cl_tasks, tk_task) {
621 if (!RPC_IS_ACTIVATED(rovr))
622 continue;
623 if (!(rovr->tk_flags & RPC_TASK_KILLED)) {
624 rovr->tk_flags |= RPC_TASK_KILLED;
625 rpc_exit(rovr, -EIO);
626 if (RPC_IS_QUEUED(rovr))
627 rpc_wake_up_queued_task(rovr->tk_waitqueue,
628 rovr);
629 }
630 }
631 spin_unlock(&clnt->cl_lock);
632 }
633 EXPORT_SYMBOL_GPL(rpc_killall_tasks);
634
635 /*
636 * Properly shut down an RPC client, terminating all outstanding
637 * requests.
638 */
639 void rpc_shutdown_client(struct rpc_clnt *clnt)
640 {
641 might_sleep();
642
643 dprintk_rcu("RPC: shutting down %s client for %s\n",
644 clnt->cl_program->name,
645 rcu_dereference(clnt->cl_xprt)->servername);
646
647 while (!list_empty(&clnt->cl_tasks)) {
648 rpc_killall_tasks(clnt);
649 wait_event_timeout(destroy_wait,
650 list_empty(&clnt->cl_tasks), 1*HZ);
651 }
652
653 rpc_release_client(clnt);
654 }
655 EXPORT_SYMBOL_GPL(rpc_shutdown_client);
656
657 /*
658 * Free an RPC client
659 */
660 static void
661 rpc_free_client(struct rpc_clnt *clnt)
662 {
663 dprintk_rcu("RPC: destroying %s client for %s\n",
664 clnt->cl_program->name,
665 rcu_dereference(clnt->cl_xprt)->servername);
666 if (clnt->cl_parent != clnt)
667 rpc_release_client(clnt->cl_parent);
668 rpc_clnt_remove_pipedir(clnt);
669 rpc_unregister_client(clnt);
670 rpc_free_iostats(clnt->cl_metrics);
671 clnt->cl_metrics = NULL;
672 xprt_put(rcu_dereference_raw(clnt->cl_xprt));
673 rpciod_down();
674 rpc_free_clid(clnt);
675 kfree(clnt);
676 }
677
678 /*
679 * Free an RPC client
680 */
681 static void
682 rpc_free_auth(struct rpc_clnt *clnt)
683 {
684 if (clnt->cl_auth == NULL) {
685 rpc_free_client(clnt);
686 return;
687 }
688
689 /*
690 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
691 * release remaining GSS contexts. This mechanism ensures
692 * that it can do so safely.
693 */
694 atomic_inc(&clnt->cl_count);
695 rpcauth_release(clnt->cl_auth);
696 clnt->cl_auth = NULL;
697 if (atomic_dec_and_test(&clnt->cl_count))
698 rpc_free_client(clnt);
699 }
700
701 /*
702 * Release reference to the RPC client
703 */
704 void
705 rpc_release_client(struct rpc_clnt *clnt)
706 {
707 dprintk("RPC: rpc_release_client(%p)\n", clnt);
708
709 if (list_empty(&clnt->cl_tasks))
710 wake_up(&destroy_wait);
711 if (atomic_dec_and_test(&clnt->cl_count))
712 rpc_free_auth(clnt);
713 }
714 EXPORT_SYMBOL_GPL(rpc_release_client);
715
716 /**
717 * rpc_bind_new_program - bind a new RPC program to an existing client
718 * @old: old rpc_client
719 * @program: rpc program to set
720 * @vers: rpc program version
721 *
722 * Clones the rpc client and sets up a new RPC program. This is mainly
723 * of use for enabling different RPC programs to share the same transport.
724 * The Sun NFSv2/v3 ACL protocol can do this.
725 */
726 struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
727 const struct rpc_program *program,
728 u32 vers)
729 {
730 struct rpc_create_args args = {
731 .program = program,
732 .prognumber = program->number,
733 .version = vers,
734 .authflavor = old->cl_auth->au_flavor,
735 };
736 struct rpc_clnt *clnt;
737 int err;
738
739 clnt = __rpc_clone_client(&args, old);
740 if (IS_ERR(clnt))
741 goto out;
742 err = rpc_ping(clnt);
743 if (err != 0) {
744 rpc_shutdown_client(clnt);
745 clnt = ERR_PTR(err);
746 }
747 out:
748 return clnt;
749 }
750 EXPORT_SYMBOL_GPL(rpc_bind_new_program);
751
752 void rpc_task_release_client(struct rpc_task *task)
753 {
754 struct rpc_clnt *clnt = task->tk_client;
755
756 if (clnt != NULL) {
757 /* Remove from client task list */
758 spin_lock(&clnt->cl_lock);
759 list_del(&task->tk_task);
760 spin_unlock(&clnt->cl_lock);
761 task->tk_client = NULL;
762
763 rpc_release_client(clnt);
764 }
765 }
766
767 static
768 void rpc_task_set_client(struct rpc_task *task, struct rpc_clnt *clnt)
769 {
770 if (clnt != NULL) {
771 rpc_task_release_client(task);
772 task->tk_client = clnt;
773 atomic_inc(&clnt->cl_count);
774 if (clnt->cl_softrtry)
775 task->tk_flags |= RPC_TASK_SOFT;
776 if (clnt->cl_noretranstimeo)
777 task->tk_flags |= RPC_TASK_NO_RETRANS_TIMEOUT;
778 if (sk_memalloc_socks()) {
779 struct rpc_xprt *xprt;
780
781 rcu_read_lock();
782 xprt = rcu_dereference(clnt->cl_xprt);
783 if (xprt->swapper)
784 task->tk_flags |= RPC_TASK_SWAPPER;
785 rcu_read_unlock();
786 }
787 /* Add to the client's list of all tasks */
788 spin_lock(&clnt->cl_lock);
789 list_add_tail(&task->tk_task, &clnt->cl_tasks);
790 spin_unlock(&clnt->cl_lock);
791 }
792 }
793
794 void rpc_task_reset_client(struct rpc_task *task, struct rpc_clnt *clnt)
795 {
796 rpc_task_release_client(task);
797 rpc_task_set_client(task, clnt);
798 }
799 EXPORT_SYMBOL_GPL(rpc_task_reset_client);
800
801
802 static void
803 rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg)
804 {
805 if (msg != NULL) {
806 task->tk_msg.rpc_proc = msg->rpc_proc;
807 task->tk_msg.rpc_argp = msg->rpc_argp;
808 task->tk_msg.rpc_resp = msg->rpc_resp;
809 if (msg->rpc_cred != NULL)
810 task->tk_msg.rpc_cred = get_rpccred(msg->rpc_cred);
811 }
812 }
813
814 /*
815 * Default callback for async RPC calls
816 */
817 static void
818 rpc_default_callback(struct rpc_task *task, void *data)
819 {
820 }
821
822 static const struct rpc_call_ops rpc_default_ops = {
823 .rpc_call_done = rpc_default_callback,
824 };
825
826 /**
827 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
828 * @task_setup_data: pointer to task initialisation data
829 */
830 struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
831 {
832 struct rpc_task *task;
833
834 task = rpc_new_task(task_setup_data);
835 if (IS_ERR(task))
836 goto out;
837
838 rpc_task_set_client(task, task_setup_data->rpc_client);
839 rpc_task_set_rpc_message(task, task_setup_data->rpc_message);
840
841 if (task->tk_action == NULL)
842 rpc_call_start(task);
843
844 atomic_inc(&task->tk_count);
845 rpc_execute(task);
846 out:
847 return task;
848 }
849 EXPORT_SYMBOL_GPL(rpc_run_task);
850
851 /**
852 * rpc_call_sync - Perform a synchronous RPC call
853 * @clnt: pointer to RPC client
854 * @msg: RPC call parameters
855 * @flags: RPC call flags
856 */
857 int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
858 {
859 struct rpc_task *task;
860 struct rpc_task_setup task_setup_data = {
861 .rpc_client = clnt,
862 .rpc_message = msg,
863 .callback_ops = &rpc_default_ops,
864 .flags = flags,
865 };
866 int status;
867
868 WARN_ON_ONCE(flags & RPC_TASK_ASYNC);
869 if (flags & RPC_TASK_ASYNC) {
870 rpc_release_calldata(task_setup_data.callback_ops,
871 task_setup_data.callback_data);
872 return -EINVAL;
873 }
874
875 task = rpc_run_task(&task_setup_data);
876 if (IS_ERR(task))
877 return PTR_ERR(task);
878 status = task->tk_status;
879 rpc_put_task(task);
880 return status;
881 }
882 EXPORT_SYMBOL_GPL(rpc_call_sync);
883
884 /**
885 * rpc_call_async - Perform an asynchronous RPC call
886 * @clnt: pointer to RPC client
887 * @msg: RPC call parameters
888 * @flags: RPC call flags
889 * @tk_ops: RPC call ops
890 * @data: user call data
891 */
892 int
893 rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
894 const struct rpc_call_ops *tk_ops, void *data)
895 {
896 struct rpc_task *task;
897 struct rpc_task_setup task_setup_data = {
898 .rpc_client = clnt,
899 .rpc_message = msg,
900 .callback_ops = tk_ops,
901 .callback_data = data,
902 .flags = flags|RPC_TASK_ASYNC,
903 };
904
905 task = rpc_run_task(&task_setup_data);
906 if (IS_ERR(task))
907 return PTR_ERR(task);
908 rpc_put_task(task);
909 return 0;
910 }
911 EXPORT_SYMBOL_GPL(rpc_call_async);
912
913 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
914 /**
915 * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
916 * rpc_execute against it
917 * @req: RPC request
918 * @tk_ops: RPC call ops
919 */
920 struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req,
921 const struct rpc_call_ops *tk_ops)
922 {
923 struct rpc_task *task;
924 struct xdr_buf *xbufp = &req->rq_snd_buf;
925 struct rpc_task_setup task_setup_data = {
926 .callback_ops = tk_ops,
927 };
928
929 dprintk("RPC: rpc_run_bc_task req= %p\n", req);
930 /*
931 * Create an rpc_task to send the data
932 */
933 task = rpc_new_task(&task_setup_data);
934 if (IS_ERR(task)) {
935 xprt_free_bc_request(req);
936 goto out;
937 }
938 task->tk_rqstp = req;
939
940 /*
941 * Set up the xdr_buf length.
942 * This also indicates that the buffer is XDR encoded already.
943 */
944 xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
945 xbufp->tail[0].iov_len;
946
947 task->tk_action = call_bc_transmit;
948 atomic_inc(&task->tk_count);
949 WARN_ON_ONCE(atomic_read(&task->tk_count) != 2);
950 rpc_execute(task);
951
952 out:
953 dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
954 return task;
955 }
956 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
957
958 void
959 rpc_call_start(struct rpc_task *task)
960 {
961 task->tk_action = call_start;
962 }
963 EXPORT_SYMBOL_GPL(rpc_call_start);
964
965 /**
966 * rpc_peeraddr - extract remote peer address from clnt's xprt
967 * @clnt: RPC client structure
968 * @buf: target buffer
969 * @bufsize: length of target buffer
970 *
971 * Returns the number of bytes that are actually in the stored address.
972 */
973 size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
974 {
975 size_t bytes;
976 struct rpc_xprt *xprt;
977
978 rcu_read_lock();
979 xprt = rcu_dereference(clnt->cl_xprt);
980
981 bytes = xprt->addrlen;
982 if (bytes > bufsize)
983 bytes = bufsize;
984 memcpy(buf, &xprt->addr, bytes);
985 rcu_read_unlock();
986
987 return bytes;
988 }
989 EXPORT_SYMBOL_GPL(rpc_peeraddr);
990
991 /**
992 * rpc_peeraddr2str - return remote peer address in printable format
993 * @clnt: RPC client structure
994 * @format: address format
995 *
996 * NB: the lifetime of the memory referenced by the returned pointer is
997 * the same as the rpc_xprt itself. As long as the caller uses this
998 * pointer, it must hold the RCU read lock.
999 */
1000 const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
1001 enum rpc_display_format_t format)
1002 {
1003 struct rpc_xprt *xprt;
1004
1005 xprt = rcu_dereference(clnt->cl_xprt);
1006
1007 if (xprt->address_strings[format] != NULL)
1008 return xprt->address_strings[format];
1009 else
1010 return "unprintable";
1011 }
1012 EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
1013
1014 static const struct sockaddr_in rpc_inaddr_loopback = {
1015 .sin_family = AF_INET,
1016 .sin_addr.s_addr = htonl(INADDR_ANY),
1017 };
1018
1019 static const struct sockaddr_in6 rpc_in6addr_loopback = {
1020 .sin6_family = AF_INET6,
1021 .sin6_addr = IN6ADDR_ANY_INIT,
1022 };
1023
1024 /*
1025 * Try a getsockname() on a connected datagram socket. Using a
1026 * connected datagram socket prevents leaving a socket in TIME_WAIT.
1027 * This conserves the ephemeral port number space.
1028 *
1029 * Returns zero and fills in "buf" if successful; otherwise, a
1030 * negative errno is returned.
1031 */
1032 static int rpc_sockname(struct net *net, struct sockaddr *sap, size_t salen,
1033 struct sockaddr *buf, int buflen)
1034 {
1035 struct socket *sock;
1036 int err;
1037
1038 err = __sock_create(net, sap->sa_family,
1039 SOCK_DGRAM, IPPROTO_UDP, &sock, 1);
1040 if (err < 0) {
1041 dprintk("RPC: can't create UDP socket (%d)\n", err);
1042 goto out;
1043 }
1044
1045 switch (sap->sa_family) {
1046 case AF_INET:
1047 err = kernel_bind(sock,
1048 (struct sockaddr *)&rpc_inaddr_loopback,
1049 sizeof(rpc_inaddr_loopback));
1050 break;
1051 case AF_INET6:
1052 err = kernel_bind(sock,
1053 (struct sockaddr *)&rpc_in6addr_loopback,
1054 sizeof(rpc_in6addr_loopback));
1055 break;
1056 default:
1057 err = -EAFNOSUPPORT;
1058 goto out;
1059 }
1060 if (err < 0) {
1061 dprintk("RPC: can't bind UDP socket (%d)\n", err);
1062 goto out_release;
1063 }
1064
1065 err = kernel_connect(sock, sap, salen, 0);
1066 if (err < 0) {
1067 dprintk("RPC: can't connect UDP socket (%d)\n", err);
1068 goto out_release;
1069 }
1070
1071 err = kernel_getsockname(sock, buf, &buflen);
1072 if (err < 0) {
1073 dprintk("RPC: getsockname failed (%d)\n", err);
1074 goto out_release;
1075 }
1076
1077 err = 0;
1078 if (buf->sa_family == AF_INET6) {
1079 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1080 sin6->sin6_scope_id = 0;
1081 }
1082 dprintk("RPC: %s succeeded\n", __func__);
1083
1084 out_release:
1085 sock_release(sock);
1086 out:
1087 return err;
1088 }
1089
1090 /*
1091 * Scraping a connected socket failed, so we don't have a useable
1092 * local address. Fallback: generate an address that will prevent
1093 * the server from calling us back.
1094 *
1095 * Returns zero and fills in "buf" if successful; otherwise, a
1096 * negative errno is returned.
1097 */
1098 static int rpc_anyaddr(int family, struct sockaddr *buf, size_t buflen)
1099 {
1100 switch (family) {
1101 case AF_INET:
1102 if (buflen < sizeof(rpc_inaddr_loopback))
1103 return -EINVAL;
1104 memcpy(buf, &rpc_inaddr_loopback,
1105 sizeof(rpc_inaddr_loopback));
1106 break;
1107 case AF_INET6:
1108 if (buflen < sizeof(rpc_in6addr_loopback))
1109 return -EINVAL;
1110 memcpy(buf, &rpc_in6addr_loopback,
1111 sizeof(rpc_in6addr_loopback));
1112 default:
1113 dprintk("RPC: %s: address family not supported\n",
1114 __func__);
1115 return -EAFNOSUPPORT;
1116 }
1117 dprintk("RPC: %s: succeeded\n", __func__);
1118 return 0;
1119 }
1120
1121 /**
1122 * rpc_localaddr - discover local endpoint address for an RPC client
1123 * @clnt: RPC client structure
1124 * @buf: target buffer
1125 * @buflen: size of target buffer, in bytes
1126 *
1127 * Returns zero and fills in "buf" and "buflen" if successful;
1128 * otherwise, a negative errno is returned.
1129 *
1130 * This works even if the underlying transport is not currently connected,
1131 * or if the upper layer never previously provided a source address.
1132 *
1133 * The result of this function call is transient: multiple calls in
1134 * succession may give different results, depending on how local
1135 * networking configuration changes over time.
1136 */
1137 int rpc_localaddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t buflen)
1138 {
1139 struct sockaddr_storage address;
1140 struct sockaddr *sap = (struct sockaddr *)&address;
1141 struct rpc_xprt *xprt;
1142 struct net *net;
1143 size_t salen;
1144 int err;
1145
1146 rcu_read_lock();
1147 xprt = rcu_dereference(clnt->cl_xprt);
1148 salen = xprt->addrlen;
1149 memcpy(sap, &xprt->addr, salen);
1150 net = get_net(xprt->xprt_net);
1151 rcu_read_unlock();
1152
1153 rpc_set_port(sap, 0);
1154 err = rpc_sockname(net, sap, salen, buf, buflen);
1155 put_net(net);
1156 if (err != 0)
1157 /* Couldn't discover local address, return ANYADDR */
1158 return rpc_anyaddr(sap->sa_family, buf, buflen);
1159 return 0;
1160 }
1161 EXPORT_SYMBOL_GPL(rpc_localaddr);
1162
1163 void
1164 rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
1165 {
1166 struct rpc_xprt *xprt;
1167
1168 rcu_read_lock();
1169 xprt = rcu_dereference(clnt->cl_xprt);
1170 if (xprt->ops->set_buffer_size)
1171 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
1172 rcu_read_unlock();
1173 }
1174 EXPORT_SYMBOL_GPL(rpc_setbufsize);
1175
1176 /**
1177 * rpc_protocol - Get transport protocol number for an RPC client
1178 * @clnt: RPC client to query
1179 *
1180 */
1181 int rpc_protocol(struct rpc_clnt *clnt)
1182 {
1183 int protocol;
1184
1185 rcu_read_lock();
1186 protocol = rcu_dereference(clnt->cl_xprt)->prot;
1187 rcu_read_unlock();
1188 return protocol;
1189 }
1190 EXPORT_SYMBOL_GPL(rpc_protocol);
1191
1192 /**
1193 * rpc_net_ns - Get the network namespace for this RPC client
1194 * @clnt: RPC client to query
1195 *
1196 */
1197 struct net *rpc_net_ns(struct rpc_clnt *clnt)
1198 {
1199 struct net *ret;
1200
1201 rcu_read_lock();
1202 ret = rcu_dereference(clnt->cl_xprt)->xprt_net;
1203 rcu_read_unlock();
1204 return ret;
1205 }
1206 EXPORT_SYMBOL_GPL(rpc_net_ns);
1207
1208 /**
1209 * rpc_max_payload - Get maximum payload size for a transport, in bytes
1210 * @clnt: RPC client to query
1211 *
1212 * For stream transports, this is one RPC record fragment (see RFC
1213 * 1831), as we don't support multi-record requests yet. For datagram
1214 * transports, this is the size of an IP packet minus the IP, UDP, and
1215 * RPC header sizes.
1216 */
1217 size_t rpc_max_payload(struct rpc_clnt *clnt)
1218 {
1219 size_t ret;
1220
1221 rcu_read_lock();
1222 ret = rcu_dereference(clnt->cl_xprt)->max_payload;
1223 rcu_read_unlock();
1224 return ret;
1225 }
1226 EXPORT_SYMBOL_GPL(rpc_max_payload);
1227
1228 /**
1229 * rpc_get_timeout - Get timeout for transport in units of HZ
1230 * @clnt: RPC client to query
1231 */
1232 unsigned long rpc_get_timeout(struct rpc_clnt *clnt)
1233 {
1234 unsigned long ret;
1235
1236 rcu_read_lock();
1237 ret = rcu_dereference(clnt->cl_xprt)->timeout->to_initval;
1238 rcu_read_unlock();
1239 return ret;
1240 }
1241 EXPORT_SYMBOL_GPL(rpc_get_timeout);
1242
1243 /**
1244 * rpc_force_rebind - force transport to check that remote port is unchanged
1245 * @clnt: client to rebind
1246 *
1247 */
1248 void rpc_force_rebind(struct rpc_clnt *clnt)
1249 {
1250 if (clnt->cl_autobind) {
1251 rcu_read_lock();
1252 xprt_clear_bound(rcu_dereference(clnt->cl_xprt));
1253 rcu_read_unlock();
1254 }
1255 }
1256 EXPORT_SYMBOL_GPL(rpc_force_rebind);
1257
1258 /*
1259 * Restart an (async) RPC call from the call_prepare state.
1260 * Usually called from within the exit handler.
1261 */
1262 int
1263 rpc_restart_call_prepare(struct rpc_task *task)
1264 {
1265 if (RPC_ASSASSINATED(task))
1266 return 0;
1267 task->tk_action = call_start;
1268 if (task->tk_ops->rpc_call_prepare != NULL)
1269 task->tk_action = rpc_prepare_task;
1270 return 1;
1271 }
1272 EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
1273
1274 /*
1275 * Restart an (async) RPC call. Usually called from within the
1276 * exit handler.
1277 */
1278 int
1279 rpc_restart_call(struct rpc_task *task)
1280 {
1281 if (RPC_ASSASSINATED(task))
1282 return 0;
1283 task->tk_action = call_start;
1284 return 1;
1285 }
1286 EXPORT_SYMBOL_GPL(rpc_restart_call);
1287
1288 #ifdef RPC_DEBUG
1289 static const char *rpc_proc_name(const struct rpc_task *task)
1290 {
1291 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1292
1293 if (proc) {
1294 if (proc->p_name)
1295 return proc->p_name;
1296 else
1297 return "NULL";
1298 } else
1299 return "no proc";
1300 }
1301 #endif
1302
1303 /*
1304 * 0. Initial state
1305 *
1306 * Other FSM states can be visited zero or more times, but
1307 * this state is visited exactly once for each RPC.
1308 */
1309 static void
1310 call_start(struct rpc_task *task)
1311 {
1312 struct rpc_clnt *clnt = task->tk_client;
1313
1314 dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task->tk_pid,
1315 clnt->cl_program->name, clnt->cl_vers,
1316 rpc_proc_name(task),
1317 (RPC_IS_ASYNC(task) ? "async" : "sync"));
1318
1319 /* Increment call count */
1320 task->tk_msg.rpc_proc->p_count++;
1321 clnt->cl_stats->rpccnt++;
1322 task->tk_action = call_reserve;
1323 }
1324
1325 /*
1326 * 1. Reserve an RPC call slot
1327 */
1328 static void
1329 call_reserve(struct rpc_task *task)
1330 {
1331 dprint_status(task);
1332
1333 task->tk_status = 0;
1334 task->tk_action = call_reserveresult;
1335 xprt_reserve(task);
1336 }
1337
1338 static void call_retry_reserve(struct rpc_task *task);
1339
1340 /*
1341 * 1b. Grok the result of xprt_reserve()
1342 */
1343 static void
1344 call_reserveresult(struct rpc_task *task)
1345 {
1346 int status = task->tk_status;
1347
1348 dprint_status(task);
1349
1350 /*
1351 * After a call to xprt_reserve(), we must have either
1352 * a request slot or else an error status.
1353 */
1354 task->tk_status = 0;
1355 if (status >= 0) {
1356 if (task->tk_rqstp) {
1357 task->tk_action = call_refresh;
1358 return;
1359 }
1360
1361 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
1362 __func__, status);
1363 rpc_exit(task, -EIO);
1364 return;
1365 }
1366
1367 /*
1368 * Even though there was an error, we may have acquired
1369 * a request slot somehow. Make sure not to leak it.
1370 */
1371 if (task->tk_rqstp) {
1372 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
1373 __func__, status);
1374 xprt_release(task);
1375 }
1376
1377 switch (status) {
1378 case -ENOMEM:
1379 rpc_delay(task, HZ >> 2);
1380 case -EAGAIN: /* woken up; retry */
1381 task->tk_action = call_retry_reserve;
1382 return;
1383 case -EIO: /* probably a shutdown */
1384 break;
1385 default:
1386 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
1387 __func__, status);
1388 break;
1389 }
1390 rpc_exit(task, status);
1391 }
1392
1393 /*
1394 * 1c. Retry reserving an RPC call slot
1395 */
1396 static void
1397 call_retry_reserve(struct rpc_task *task)
1398 {
1399 dprint_status(task);
1400
1401 task->tk_status = 0;
1402 task->tk_action = call_reserveresult;
1403 xprt_retry_reserve(task);
1404 }
1405
1406 /*
1407 * 2. Bind and/or refresh the credentials
1408 */
1409 static void
1410 call_refresh(struct rpc_task *task)
1411 {
1412 dprint_status(task);
1413
1414 task->tk_action = call_refreshresult;
1415 task->tk_status = 0;
1416 task->tk_client->cl_stats->rpcauthrefresh++;
1417 rpcauth_refreshcred(task);
1418 }
1419
1420 /*
1421 * 2a. Process the results of a credential refresh
1422 */
1423 static void
1424 call_refreshresult(struct rpc_task *task)
1425 {
1426 int status = task->tk_status;
1427
1428 dprint_status(task);
1429
1430 task->tk_status = 0;
1431 task->tk_action = call_refresh;
1432 switch (status) {
1433 case 0:
1434 if (rpcauth_uptodatecred(task))
1435 task->tk_action = call_allocate;
1436 return;
1437 case -ETIMEDOUT:
1438 rpc_delay(task, 3*HZ);
1439 case -EAGAIN:
1440 status = -EACCES;
1441 case -EKEYEXPIRED:
1442 if (!task->tk_cred_retry)
1443 break;
1444 task->tk_cred_retry--;
1445 dprintk("RPC: %5u %s: retry refresh creds\n",
1446 task->tk_pid, __func__);
1447 return;
1448 }
1449 dprintk("RPC: %5u %s: refresh creds failed with error %d\n",
1450 task->tk_pid, __func__, status);
1451 rpc_exit(task, status);
1452 }
1453
1454 /*
1455 * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
1456 * (Note: buffer memory is freed in xprt_release).
1457 */
1458 static void
1459 call_allocate(struct rpc_task *task)
1460 {
1461 unsigned int slack = task->tk_rqstp->rq_cred->cr_auth->au_cslack;
1462 struct rpc_rqst *req = task->tk_rqstp;
1463 struct rpc_xprt *xprt = req->rq_xprt;
1464 struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1465
1466 dprint_status(task);
1467
1468 task->tk_status = 0;
1469 task->tk_action = call_bind;
1470
1471 if (req->rq_buffer)
1472 return;
1473
1474 if (proc->p_proc != 0) {
1475 BUG_ON(proc->p_arglen == 0);
1476 if (proc->p_decode != NULL)
1477 BUG_ON(proc->p_replen == 0);
1478 }
1479
1480 /*
1481 * Calculate the size (in quads) of the RPC call
1482 * and reply headers, and convert both values
1483 * to byte sizes.
1484 */
1485 req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
1486 req->rq_callsize <<= 2;
1487 req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
1488 req->rq_rcvsize <<= 2;
1489
1490 req->rq_buffer = xprt->ops->buf_alloc(task,
1491 req->rq_callsize + req->rq_rcvsize);
1492 if (req->rq_buffer != NULL)
1493 return;
1494
1495 dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
1496
1497 if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) {
1498 task->tk_action = call_allocate;
1499 rpc_delay(task, HZ>>4);
1500 return;
1501 }
1502
1503 rpc_exit(task, -ERESTARTSYS);
1504 }
1505
1506 static inline int
1507 rpc_task_need_encode(struct rpc_task *task)
1508 {
1509 return task->tk_rqstp->rq_snd_buf.len == 0;
1510 }
1511
1512 static inline void
1513 rpc_task_force_reencode(struct rpc_task *task)
1514 {
1515 task->tk_rqstp->rq_snd_buf.len = 0;
1516 task->tk_rqstp->rq_bytes_sent = 0;
1517 }
1518
1519 static inline void
1520 rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
1521 {
1522 buf->head[0].iov_base = start;
1523 buf->head[0].iov_len = len;
1524 buf->tail[0].iov_len = 0;
1525 buf->page_len = 0;
1526 buf->flags = 0;
1527 buf->len = 0;
1528 buf->buflen = len;
1529 }
1530
1531 /*
1532 * 3. Encode arguments of an RPC call
1533 */
1534 static void
1535 rpc_xdr_encode(struct rpc_task *task)
1536 {
1537 struct rpc_rqst *req = task->tk_rqstp;
1538 kxdreproc_t encode;
1539 __be32 *p;
1540
1541 dprint_status(task);
1542
1543 rpc_xdr_buf_init(&req->rq_snd_buf,
1544 req->rq_buffer,
1545 req->rq_callsize);
1546 rpc_xdr_buf_init(&req->rq_rcv_buf,
1547 (char *)req->rq_buffer + req->rq_callsize,
1548 req->rq_rcvsize);
1549
1550 p = rpc_encode_header(task);
1551 if (p == NULL) {
1552 printk(KERN_INFO "RPC: couldn't encode RPC header, exit EIO\n");
1553 rpc_exit(task, -EIO);
1554 return;
1555 }
1556
1557 encode = task->tk_msg.rpc_proc->p_encode;
1558 if (encode == NULL)
1559 return;
1560
1561 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
1562 task->tk_msg.rpc_argp);
1563 }
1564
1565 /*
1566 * 4. Get the server port number if not yet set
1567 */
1568 static void
1569 call_bind(struct rpc_task *task)
1570 {
1571 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
1572
1573 dprint_status(task);
1574
1575 task->tk_action = call_connect;
1576 if (!xprt_bound(xprt)) {
1577 task->tk_action = call_bind_status;
1578 task->tk_timeout = xprt->bind_timeout;
1579 xprt->ops->rpcbind(task);
1580 }
1581 }
1582
1583 /*
1584 * 4a. Sort out bind result
1585 */
1586 static void
1587 call_bind_status(struct rpc_task *task)
1588 {
1589 int status = -EIO;
1590
1591 if (task->tk_status >= 0) {
1592 dprint_status(task);
1593 task->tk_status = 0;
1594 task->tk_action = call_connect;
1595 return;
1596 }
1597
1598 trace_rpc_bind_status(task);
1599 switch (task->tk_status) {
1600 case -ENOMEM:
1601 dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid);
1602 rpc_delay(task, HZ >> 2);
1603 goto retry_timeout;
1604 case -EACCES:
1605 dprintk("RPC: %5u remote rpcbind: RPC program/version "
1606 "unavailable\n", task->tk_pid);
1607 /* fail immediately if this is an RPC ping */
1608 if (task->tk_msg.rpc_proc->p_proc == 0) {
1609 status = -EOPNOTSUPP;
1610 break;
1611 }
1612 if (task->tk_rebind_retry == 0)
1613 break;
1614 task->tk_rebind_retry--;
1615 rpc_delay(task, 3*HZ);
1616 goto retry_timeout;
1617 case -ETIMEDOUT:
1618 dprintk("RPC: %5u rpcbind request timed out\n",
1619 task->tk_pid);
1620 goto retry_timeout;
1621 case -EPFNOSUPPORT:
1622 /* server doesn't support any rpcbind version we know of */
1623 dprintk("RPC: %5u unrecognized remote rpcbind service\n",
1624 task->tk_pid);
1625 break;
1626 case -EPROTONOSUPPORT:
1627 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
1628 task->tk_pid);
1629 task->tk_status = 0;
1630 task->tk_action = call_bind;
1631 return;
1632 case -ECONNREFUSED: /* connection problems */
1633 case -ECONNRESET:
1634 case -ENOTCONN:
1635 case -EHOSTDOWN:
1636 case -EHOSTUNREACH:
1637 case -ENETUNREACH:
1638 case -EPIPE:
1639 dprintk("RPC: %5u remote rpcbind unreachable: %d\n",
1640 task->tk_pid, task->tk_status);
1641 if (!RPC_IS_SOFTCONN(task)) {
1642 rpc_delay(task, 5*HZ);
1643 goto retry_timeout;
1644 }
1645 status = task->tk_status;
1646 break;
1647 default:
1648 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
1649 task->tk_pid, -task->tk_status);
1650 }
1651
1652 rpc_exit(task, status);
1653 return;
1654
1655 retry_timeout:
1656 task->tk_action = call_timeout;
1657 }
1658
1659 /*
1660 * 4b. Connect to the RPC server
1661 */
1662 static void
1663 call_connect(struct rpc_task *task)
1664 {
1665 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
1666
1667 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
1668 task->tk_pid, xprt,
1669 (xprt_connected(xprt) ? "is" : "is not"));
1670
1671 task->tk_action = call_transmit;
1672 if (!xprt_connected(xprt)) {
1673 task->tk_action = call_connect_status;
1674 if (task->tk_status < 0)
1675 return;
1676 if (task->tk_flags & RPC_TASK_NOCONNECT) {
1677 rpc_exit(task, -ENOTCONN);
1678 return;
1679 }
1680 xprt_connect(task);
1681 }
1682 }
1683
1684 /*
1685 * 4c. Sort out connect result
1686 */
1687 static void
1688 call_connect_status(struct rpc_task *task)
1689 {
1690 struct rpc_clnt *clnt = task->tk_client;
1691 int status = task->tk_status;
1692
1693 dprint_status(task);
1694
1695 trace_rpc_connect_status(task, status);
1696 task->tk_status = 0;
1697 switch (status) {
1698 /* if soft mounted, test if we've timed out */
1699 case -ETIMEDOUT:
1700 task->tk_action = call_timeout;
1701 return;
1702 case -ECONNREFUSED:
1703 case -ECONNRESET:
1704 case -ENETUNREACH:
1705 /* retry with existing socket, after a delay */
1706 rpc_delay(task, 3*HZ);
1707 if (RPC_IS_SOFTCONN(task))
1708 break;
1709 case -EAGAIN:
1710 task->tk_action = call_bind;
1711 return;
1712 case 0:
1713 clnt->cl_stats->netreconn++;
1714 task->tk_action = call_transmit;
1715 return;
1716 }
1717 rpc_exit(task, status);
1718 }
1719
1720 /*
1721 * 5. Transmit the RPC request, and wait for reply
1722 */
1723 static void
1724 call_transmit(struct rpc_task *task)
1725 {
1726 int is_retrans = RPC_WAS_SENT(task);
1727
1728 dprint_status(task);
1729
1730 task->tk_action = call_status;
1731 if (task->tk_status < 0)
1732 return;
1733 if (!xprt_prepare_transmit(task))
1734 return;
1735 task->tk_action = call_transmit_status;
1736 /* Encode here so that rpcsec_gss can use correct sequence number. */
1737 if (rpc_task_need_encode(task)) {
1738 rpc_xdr_encode(task);
1739 /* Did the encode result in an error condition? */
1740 if (task->tk_status != 0) {
1741 /* Was the error nonfatal? */
1742 if (task->tk_status == -EAGAIN)
1743 rpc_delay(task, HZ >> 4);
1744 else
1745 rpc_exit(task, task->tk_status);
1746 return;
1747 }
1748 }
1749 xprt_transmit(task);
1750 if (task->tk_status < 0)
1751 return;
1752 if (is_retrans)
1753 task->tk_client->cl_stats->rpcretrans++;
1754 /*
1755 * On success, ensure that we call xprt_end_transmit() before sleeping
1756 * in order to allow access to the socket to other RPC requests.
1757 */
1758 call_transmit_status(task);
1759 if (rpc_reply_expected(task))
1760 return;
1761 task->tk_action = rpc_exit_task;
1762 rpc_wake_up_queued_task(&task->tk_rqstp->rq_xprt->pending, task);
1763 }
1764
1765 /*
1766 * 5a. Handle cleanup after a transmission
1767 */
1768 static void
1769 call_transmit_status(struct rpc_task *task)
1770 {
1771 task->tk_action = call_status;
1772
1773 /*
1774 * Common case: success. Force the compiler to put this
1775 * test first.
1776 */
1777 if (task->tk_status == 0) {
1778 xprt_end_transmit(task);
1779 rpc_task_force_reencode(task);
1780 return;
1781 }
1782
1783 switch (task->tk_status) {
1784 case -EAGAIN:
1785 break;
1786 default:
1787 dprint_status(task);
1788 xprt_end_transmit(task);
1789 rpc_task_force_reencode(task);
1790 break;
1791 /*
1792 * Special cases: if we've been waiting on the
1793 * socket's write_space() callback, or if the
1794 * socket just returned a connection error,
1795 * then hold onto the transport lock.
1796 */
1797 case -ECONNREFUSED:
1798 case -EHOSTDOWN:
1799 case -EHOSTUNREACH:
1800 case -ENETUNREACH:
1801 if (RPC_IS_SOFTCONN(task)) {
1802 xprt_end_transmit(task);
1803 rpc_exit(task, task->tk_status);
1804 break;
1805 }
1806 case -ECONNRESET:
1807 case -ENOTCONN:
1808 case -EPIPE:
1809 rpc_task_force_reencode(task);
1810 }
1811 }
1812
1813 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1814 /*
1815 * 5b. Send the backchannel RPC reply. On error, drop the reply. In
1816 * addition, disconnect on connectivity errors.
1817 */
1818 static void
1819 call_bc_transmit(struct rpc_task *task)
1820 {
1821 struct rpc_rqst *req = task->tk_rqstp;
1822
1823 if (!xprt_prepare_transmit(task)) {
1824 /*
1825 * Could not reserve the transport. Try again after the
1826 * transport is released.
1827 */
1828 task->tk_status = 0;
1829 task->tk_action = call_bc_transmit;
1830 return;
1831 }
1832
1833 task->tk_action = rpc_exit_task;
1834 if (task->tk_status < 0) {
1835 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
1836 "error: %d\n", task->tk_status);
1837 return;
1838 }
1839
1840 xprt_transmit(task);
1841 xprt_end_transmit(task);
1842 dprint_status(task);
1843 switch (task->tk_status) {
1844 case 0:
1845 /* Success */
1846 break;
1847 case -EHOSTDOWN:
1848 case -EHOSTUNREACH:
1849 case -ENETUNREACH:
1850 case -ETIMEDOUT:
1851 /*
1852 * Problem reaching the server. Disconnect and let the
1853 * forechannel reestablish the connection. The server will
1854 * have to retransmit the backchannel request and we'll
1855 * reprocess it. Since these ops are idempotent, there's no
1856 * need to cache our reply at this time.
1857 */
1858 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
1859 "error: %d\n", task->tk_status);
1860 xprt_conditional_disconnect(req->rq_xprt,
1861 req->rq_connect_cookie);
1862 break;
1863 default:
1864 /*
1865 * We were unable to reply and will have to drop the
1866 * request. The server should reconnect and retransmit.
1867 */
1868 WARN_ON_ONCE(task->tk_status == -EAGAIN);
1869 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
1870 "error: %d\n", task->tk_status);
1871 break;
1872 }
1873 rpc_wake_up_queued_task(&req->rq_xprt->pending, task);
1874 }
1875 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1876
1877 /*
1878 * 6. Sort out the RPC call status
1879 */
1880 static void
1881 call_status(struct rpc_task *task)
1882 {
1883 struct rpc_clnt *clnt = task->tk_client;
1884 struct rpc_rqst *req = task->tk_rqstp;
1885 int status;
1886
1887 if (req->rq_reply_bytes_recvd > 0 && !req->rq_bytes_sent)
1888 task->tk_status = req->rq_reply_bytes_recvd;
1889
1890 dprint_status(task);
1891
1892 status = task->tk_status;
1893 if (status >= 0) {
1894 task->tk_action = call_decode;
1895 return;
1896 }
1897
1898 trace_rpc_call_status(task);
1899 task->tk_status = 0;
1900 switch(status) {
1901 case -EHOSTDOWN:
1902 case -EHOSTUNREACH:
1903 case -ENETUNREACH:
1904 /*
1905 * Delay any retries for 3 seconds, then handle as if it
1906 * were a timeout.
1907 */
1908 rpc_delay(task, 3*HZ);
1909 case -ETIMEDOUT:
1910 task->tk_action = call_timeout;
1911 if (!(task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT)
1912 && task->tk_client->cl_discrtry)
1913 xprt_conditional_disconnect(req->rq_xprt,
1914 req->rq_connect_cookie);
1915 break;
1916 case -ECONNRESET:
1917 case -ECONNREFUSED:
1918 rpc_force_rebind(clnt);
1919 rpc_delay(task, 3*HZ);
1920 case -EPIPE:
1921 case -ENOTCONN:
1922 task->tk_action = call_bind;
1923 break;
1924 case -EAGAIN:
1925 task->tk_action = call_transmit;
1926 break;
1927 case -EIO:
1928 /* shutdown or soft timeout */
1929 rpc_exit(task, status);
1930 break;
1931 default:
1932 if (clnt->cl_chatty)
1933 printk("%s: RPC call returned error %d\n",
1934 clnt->cl_program->name, -status);
1935 rpc_exit(task, status);
1936 }
1937 }
1938
1939 /*
1940 * 6a. Handle RPC timeout
1941 * We do not release the request slot, so we keep using the
1942 * same XID for all retransmits.
1943 */
1944 static void
1945 call_timeout(struct rpc_task *task)
1946 {
1947 struct rpc_clnt *clnt = task->tk_client;
1948
1949 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
1950 dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
1951 goto retry;
1952 }
1953
1954 dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
1955 task->tk_timeouts++;
1956
1957 if (RPC_IS_SOFTCONN(task)) {
1958 rpc_exit(task, -ETIMEDOUT);
1959 return;
1960 }
1961 if (RPC_IS_SOFT(task)) {
1962 if (clnt->cl_chatty) {
1963 rcu_read_lock();
1964 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
1965 clnt->cl_program->name,
1966 rcu_dereference(clnt->cl_xprt)->servername);
1967 rcu_read_unlock();
1968 }
1969 if (task->tk_flags & RPC_TASK_TIMEOUT)
1970 rpc_exit(task, -ETIMEDOUT);
1971 else
1972 rpc_exit(task, -EIO);
1973 return;
1974 }
1975
1976 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
1977 task->tk_flags |= RPC_CALL_MAJORSEEN;
1978 if (clnt->cl_chatty) {
1979 rcu_read_lock();
1980 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
1981 clnt->cl_program->name,
1982 rcu_dereference(clnt->cl_xprt)->servername);
1983 rcu_read_unlock();
1984 }
1985 }
1986 rpc_force_rebind(clnt);
1987 /*
1988 * Did our request time out due to an RPCSEC_GSS out-of-sequence
1989 * event? RFC2203 requires the server to drop all such requests.
1990 */
1991 rpcauth_invalcred(task);
1992
1993 retry:
1994 task->tk_action = call_bind;
1995 task->tk_status = 0;
1996 }
1997
1998 /*
1999 * 7. Decode the RPC reply
2000 */
2001 static void
2002 call_decode(struct rpc_task *task)
2003 {
2004 struct rpc_clnt *clnt = task->tk_client;
2005 struct rpc_rqst *req = task->tk_rqstp;
2006 kxdrdproc_t decode = task->tk_msg.rpc_proc->p_decode;
2007 __be32 *p;
2008
2009 dprint_status(task);
2010
2011 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
2012 if (clnt->cl_chatty) {
2013 rcu_read_lock();
2014 printk(KERN_NOTICE "%s: server %s OK\n",
2015 clnt->cl_program->name,
2016 rcu_dereference(clnt->cl_xprt)->servername);
2017 rcu_read_unlock();
2018 }
2019 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
2020 }
2021
2022 /*
2023 * Ensure that we see all writes made by xprt_complete_rqst()
2024 * before it changed req->rq_reply_bytes_recvd.
2025 */
2026 smp_rmb();
2027 req->rq_rcv_buf.len = req->rq_private_buf.len;
2028
2029 /* Check that the softirq receive buffer is valid */
2030 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
2031 sizeof(req->rq_rcv_buf)) != 0);
2032
2033 if (req->rq_rcv_buf.len < 12) {
2034 if (!RPC_IS_SOFT(task)) {
2035 task->tk_action = call_bind;
2036 goto out_retry;
2037 }
2038 dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
2039 clnt->cl_program->name, task->tk_status);
2040 task->tk_action = call_timeout;
2041 goto out_retry;
2042 }
2043
2044 p = rpc_verify_header(task);
2045 if (IS_ERR(p)) {
2046 if (p == ERR_PTR(-EAGAIN))
2047 goto out_retry;
2048 return;
2049 }
2050
2051 task->tk_action = rpc_exit_task;
2052
2053 if (decode) {
2054 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
2055 task->tk_msg.rpc_resp);
2056 }
2057 dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
2058 task->tk_status);
2059 return;
2060 out_retry:
2061 task->tk_status = 0;
2062 /* Note: rpc_verify_header() may have freed the RPC slot */
2063 if (task->tk_rqstp == req) {
2064 req->rq_reply_bytes_recvd = req->rq_rcv_buf.len = 0;
2065 if (task->tk_client->cl_discrtry)
2066 xprt_conditional_disconnect(req->rq_xprt,
2067 req->rq_connect_cookie);
2068 }
2069 }
2070
2071 static __be32 *
2072 rpc_encode_header(struct rpc_task *task)
2073 {
2074 struct rpc_clnt *clnt = task->tk_client;
2075 struct rpc_rqst *req = task->tk_rqstp;
2076 __be32 *p = req->rq_svec[0].iov_base;
2077
2078 /* FIXME: check buffer size? */
2079
2080 p = xprt_skip_transport_header(req->rq_xprt, p);
2081 *p++ = req->rq_xid; /* XID */
2082 *p++ = htonl(RPC_CALL); /* CALL */
2083 *p++ = htonl(RPC_VERSION); /* RPC version */
2084 *p++ = htonl(clnt->cl_prog); /* program number */
2085 *p++ = htonl(clnt->cl_vers); /* program version */
2086 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
2087 p = rpcauth_marshcred(task, p);
2088 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
2089 return p;
2090 }
2091
2092 static __be32 *
2093 rpc_verify_header(struct rpc_task *task)
2094 {
2095 struct rpc_clnt *clnt = task->tk_client;
2096 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
2097 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
2098 __be32 *p = iov->iov_base;
2099 u32 n;
2100 int error = -EACCES;
2101
2102 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
2103 /* RFC-1014 says that the representation of XDR data must be a
2104 * multiple of four bytes
2105 * - if it isn't pointer subtraction in the NFS client may give
2106 * undefined results
2107 */
2108 dprintk("RPC: %5u %s: XDR representation not a multiple of"
2109 " 4 bytes: 0x%x\n", task->tk_pid, __func__,
2110 task->tk_rqstp->rq_rcv_buf.len);
2111 error = -EIO;
2112 goto out_err;
2113 }
2114 if ((len -= 3) < 0)
2115 goto out_overflow;
2116
2117 p += 1; /* skip XID */
2118 if ((n = ntohl(*p++)) != RPC_REPLY) {
2119 dprintk("RPC: %5u %s: not an RPC reply: %x\n",
2120 task->tk_pid, __func__, n);
2121 error = -EIO;
2122 goto out_garbage;
2123 }
2124
2125 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
2126 if (--len < 0)
2127 goto out_overflow;
2128 switch ((n = ntohl(*p++))) {
2129 case RPC_AUTH_ERROR:
2130 break;
2131 case RPC_MISMATCH:
2132 dprintk("RPC: %5u %s: RPC call version mismatch!\n",
2133 task->tk_pid, __func__);
2134 error = -EPROTONOSUPPORT;
2135 goto out_err;
2136 default:
2137 dprintk("RPC: %5u %s: RPC call rejected, "
2138 "unknown error: %x\n",
2139 task->tk_pid, __func__, n);
2140 error = -EIO;
2141 goto out_err;
2142 }
2143 if (--len < 0)
2144 goto out_overflow;
2145 switch ((n = ntohl(*p++))) {
2146 case RPC_AUTH_REJECTEDCRED:
2147 case RPC_AUTH_REJECTEDVERF:
2148 case RPCSEC_GSS_CREDPROBLEM:
2149 case RPCSEC_GSS_CTXPROBLEM:
2150 if (!task->tk_cred_retry)
2151 break;
2152 task->tk_cred_retry--;
2153 dprintk("RPC: %5u %s: retry stale creds\n",
2154 task->tk_pid, __func__);
2155 rpcauth_invalcred(task);
2156 /* Ensure we obtain a new XID! */
2157 xprt_release(task);
2158 task->tk_action = call_reserve;
2159 goto out_retry;
2160 case RPC_AUTH_BADCRED:
2161 case RPC_AUTH_BADVERF:
2162 /* possibly garbled cred/verf? */
2163 if (!task->tk_garb_retry)
2164 break;
2165 task->tk_garb_retry--;
2166 dprintk("RPC: %5u %s: retry garbled creds\n",
2167 task->tk_pid, __func__);
2168 task->tk_action = call_bind;
2169 goto out_retry;
2170 case RPC_AUTH_TOOWEAK:
2171 rcu_read_lock();
2172 printk(KERN_NOTICE "RPC: server %s requires stronger "
2173 "authentication.\n",
2174 rcu_dereference(clnt->cl_xprt)->servername);
2175 rcu_read_unlock();
2176 break;
2177 default:
2178 dprintk("RPC: %5u %s: unknown auth error: %x\n",
2179 task->tk_pid, __func__, n);
2180 error = -EIO;
2181 }
2182 dprintk("RPC: %5u %s: call rejected %d\n",
2183 task->tk_pid, __func__, n);
2184 goto out_err;
2185 }
2186 p = rpcauth_checkverf(task, p);
2187 if (IS_ERR(p)) {
2188 error = PTR_ERR(p);
2189 dprintk("RPC: %5u %s: auth check failed with %d\n",
2190 task->tk_pid, __func__, error);
2191 goto out_garbage; /* bad verifier, retry */
2192 }
2193 len = p - (__be32 *)iov->iov_base - 1;
2194 if (len < 0)
2195 goto out_overflow;
2196 switch ((n = ntohl(*p++))) {
2197 case RPC_SUCCESS:
2198 return p;
2199 case RPC_PROG_UNAVAIL:
2200 dprintk_rcu("RPC: %5u %s: program %u is unsupported "
2201 "by server %s\n", task->tk_pid, __func__,
2202 (unsigned int)clnt->cl_prog,
2203 rcu_dereference(clnt->cl_xprt)->servername);
2204 error = -EPFNOSUPPORT;
2205 goto out_err;
2206 case RPC_PROG_MISMATCH:
2207 dprintk_rcu("RPC: %5u %s: program %u, version %u unsupported "
2208 "by server %s\n", task->tk_pid, __func__,
2209 (unsigned int)clnt->cl_prog,
2210 (unsigned int)clnt->cl_vers,
2211 rcu_dereference(clnt->cl_xprt)->servername);
2212 error = -EPROTONOSUPPORT;
2213 goto out_err;
2214 case RPC_PROC_UNAVAIL:
2215 dprintk_rcu("RPC: %5u %s: proc %s unsupported by program %u, "
2216 "version %u on server %s\n",
2217 task->tk_pid, __func__,
2218 rpc_proc_name(task),
2219 clnt->cl_prog, clnt->cl_vers,
2220 rcu_dereference(clnt->cl_xprt)->servername);
2221 error = -EOPNOTSUPP;
2222 goto out_err;
2223 case RPC_GARBAGE_ARGS:
2224 dprintk("RPC: %5u %s: server saw garbage\n",
2225 task->tk_pid, __func__);
2226 break; /* retry */
2227 default:
2228 dprintk("RPC: %5u %s: server accept status: %x\n",
2229 task->tk_pid, __func__, n);
2230 /* Also retry */
2231 }
2232
2233 out_garbage:
2234 clnt->cl_stats->rpcgarbage++;
2235 if (task->tk_garb_retry) {
2236 task->tk_garb_retry--;
2237 dprintk("RPC: %5u %s: retrying\n",
2238 task->tk_pid, __func__);
2239 task->tk_action = call_bind;
2240 out_retry:
2241 return ERR_PTR(-EAGAIN);
2242 }
2243 out_err:
2244 rpc_exit(task, error);
2245 dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid,
2246 __func__, error);
2247 return ERR_PTR(error);
2248 out_overflow:
2249 dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid,
2250 __func__);
2251 goto out_garbage;
2252 }
2253
2254 static void rpcproc_encode_null(void *rqstp, struct xdr_stream *xdr, void *obj)
2255 {
2256 }
2257
2258 static int rpcproc_decode_null(void *rqstp, struct xdr_stream *xdr, void *obj)
2259 {
2260 return 0;
2261 }
2262
2263 static struct rpc_procinfo rpcproc_null = {
2264 .p_encode = rpcproc_encode_null,
2265 .p_decode = rpcproc_decode_null,
2266 };
2267
2268 static int rpc_ping(struct rpc_clnt *clnt)
2269 {
2270 struct rpc_message msg = {
2271 .rpc_proc = &rpcproc_null,
2272 };
2273 int err;
2274 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
2275 err = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN);
2276 put_rpccred(msg.rpc_cred);
2277 return err;
2278 }
2279
2280 struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
2281 {
2282 struct rpc_message msg = {
2283 .rpc_proc = &rpcproc_null,
2284 .rpc_cred = cred,
2285 };
2286 struct rpc_task_setup task_setup_data = {
2287 .rpc_client = clnt,
2288 .rpc_message = &msg,
2289 .callback_ops = &rpc_default_ops,
2290 .flags = flags,
2291 };
2292 return rpc_run_task(&task_setup_data);
2293 }
2294 EXPORT_SYMBOL_GPL(rpc_call_null);
2295
2296 #ifdef RPC_DEBUG
2297 static void rpc_show_header(void)
2298 {
2299 printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
2300 "-timeout ---ops--\n");
2301 }
2302
2303 static void rpc_show_task(const struct rpc_clnt *clnt,
2304 const struct rpc_task *task)
2305 {
2306 const char *rpc_waitq = "none";
2307
2308 if (RPC_IS_QUEUED(task))
2309 rpc_waitq = rpc_qname(task->tk_waitqueue);
2310
2311 printk(KERN_INFO "%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%ps q:%s\n",
2312 task->tk_pid, task->tk_flags, task->tk_status,
2313 clnt, task->tk_rqstp, task->tk_timeout, task->tk_ops,
2314 clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
2315 task->tk_action, rpc_waitq);
2316 }
2317
2318 void rpc_show_tasks(struct net *net)
2319 {
2320 struct rpc_clnt *clnt;
2321 struct rpc_task *task;
2322 int header = 0;
2323 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
2324
2325 spin_lock(&sn->rpc_client_lock);
2326 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
2327 spin_lock(&clnt->cl_lock);
2328 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
2329 if (!header) {
2330 rpc_show_header();
2331 header++;
2332 }
2333 rpc_show_task(clnt, task);
2334 }
2335 spin_unlock(&clnt->cl_lock);
2336 }
2337 spin_unlock(&sn->rpc_client_lock);
2338 }
2339 #endif
This page took 0.081105 seconds and 5 git commands to generate.