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