~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/sunrpc/clnt.c

Version: ~ [ linux-5.5-rc7 ] ~ [ linux-5.4.13 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.97 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.166 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.210 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.210 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.81 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp