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