2 * linux/net/sunrpc/clnt.c
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.
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.
16 * NB: BSD uses a more intelligent approach to guessing when a request
17 * or reply has been lost by keeping the RTO estimate for each procedure.
18 * We currently make do with a constant timeout value.
20 * Copyright (C) 1992,1993 Rick Sladkey <jrs@world.std.com>
21 * Copyright (C) 1995,1996 Olaf Kirch <okir@monad.swb.de>
24 #include <asm/system.h>
26 #include <linux/module.h>
27 #include <linux/types.h>
29 #include <linux/slab.h>
30 #include <linux/utsname.h>
31 #include <linux/workqueue.h>
33 #include <linux/sunrpc/clnt.h>
34 #include <linux/sunrpc/rpc_pipe_fs.h>
35 #include <linux/sunrpc/metrics.h>
38 #define RPC_SLACK_SPACE (1024) /* total overkill */
41 # define RPCDBG_FACILITY RPCDBG_CALL
44 static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
47 static void call_start(struct rpc_task *task);
48 static void call_reserve(struct rpc_task *task);
49 static void call_reserveresult(struct rpc_task *task);
50 static void call_allocate(struct rpc_task *task);
51 static void call_encode(struct rpc_task *task);
52 static void call_decode(struct rpc_task *task);
53 static void call_bind(struct rpc_task *task);
54 static void call_bind_status(struct rpc_task *task);
55 static void call_transmit(struct rpc_task *task);
56 static void call_status(struct rpc_task *task);
57 static void call_transmit_status(struct rpc_task *task);
58 static void call_refresh(struct rpc_task *task);
59 static void call_refreshresult(struct rpc_task *task);
60 static void call_timeout(struct rpc_task *task);
61 static void call_connect(struct rpc_task *task);
62 static void call_connect_status(struct rpc_task *task);
63 static u32 * call_header(struct rpc_task *task);
64 static u32 * call_verify(struct rpc_task *task);
68 rpc_setup_pipedir(struct rpc_clnt *clnt, char *dir_name)
70 static uint32_t clntid;
73 clnt->cl_vfsmnt = ERR_PTR(-ENOENT);
74 clnt->cl_dentry = ERR_PTR(-ENOENT);
78 clnt->cl_vfsmnt = rpc_get_mount();
79 if (IS_ERR(clnt->cl_vfsmnt))
80 return PTR_ERR(clnt->cl_vfsmnt);
83 snprintf(clnt->cl_pathname, sizeof(clnt->cl_pathname),
84 "%s/clnt%x", dir_name,
85 (unsigned int)clntid++);
86 clnt->cl_pathname[sizeof(clnt->cl_pathname) - 1] = '\0';
87 clnt->cl_dentry = rpc_mkdir(clnt->cl_pathname, clnt);
88 if (!IS_ERR(clnt->cl_dentry))
90 error = PTR_ERR(clnt->cl_dentry);
91 if (error != -EEXIST) {
92 printk(KERN_INFO "RPC: Couldn't create pipefs entry %s, error %d\n",
93 clnt->cl_pathname, error);
101 * Create an RPC client
102 * FIXME: This should also take a flags argument (as in task->tk_flags).
103 * It's called (among others) from pmap_create_client, which may in
104 * turn be called by an async task. In this case, rpciod should not be
105 * made to sleep too long.
108 rpc_new_client(struct rpc_xprt *xprt, char *servname,
109 struct rpc_program *program, u32 vers,
110 rpc_authflavor_t flavor)
112 struct rpc_version *version;
113 struct rpc_clnt *clnt = NULL;
114 struct rpc_auth *auth;
118 dprintk("RPC: creating %s client for %s (xprt %p)\n",
119 program->name, servname, xprt);
124 if (vers >= program->nrvers || !(version = program->version[vers]))
128 clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
131 atomic_set(&clnt->cl_users, 0);
132 atomic_set(&clnt->cl_count, 1);
133 clnt->cl_parent = clnt;
135 clnt->cl_server = clnt->cl_inline_name;
136 len = strlen(servname) + 1;
137 if (len > sizeof(clnt->cl_inline_name)) {
138 char *buf = kmalloc(len, GFP_KERNEL);
140 clnt->cl_server = buf;
142 len = sizeof(clnt->cl_inline_name);
144 strlcpy(clnt->cl_server, servname, len);
146 clnt->cl_xprt = xprt;
147 clnt->cl_procinfo = version->procs;
148 clnt->cl_maxproc = version->nrprocs;
149 clnt->cl_protname = program->name;
150 clnt->cl_prog = program->number;
151 clnt->cl_vers = version->number;
152 clnt->cl_stats = program->stats;
153 clnt->cl_metrics = rpc_alloc_iostats(clnt);
155 if (!xprt_bound(clnt->cl_xprt))
156 clnt->cl_autobind = 1;
158 clnt->cl_rtt = &clnt->cl_rtt_default;
159 rpc_init_rtt(&clnt->cl_rtt_default, xprt->timeout.to_initval);
161 err = rpc_setup_pipedir(clnt, program->pipe_dir_name);
165 auth = rpcauth_create(flavor, clnt);
167 printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %u)\n",
173 /* save the nodename */
174 clnt->cl_nodelen = strlen(system_utsname.nodename);
175 if (clnt->cl_nodelen > UNX_MAXNODENAME)
176 clnt->cl_nodelen = UNX_MAXNODENAME;
177 memcpy(clnt->cl_nodename, system_utsname.nodename, clnt->cl_nodelen);
181 if (!IS_ERR(clnt->cl_dentry)) {
182 rpc_rmdir(clnt->cl_dentry);
186 if (clnt->cl_server != clnt->cl_inline_name)
187 kfree(clnt->cl_server);
196 * rpc_create - create an RPC client and transport with one call
197 * @args: rpc_clnt create argument structure
199 * Creates and initializes an RPC transport and an RPC client.
201 * It can ping the server in order to determine if it is up, and to see if
202 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
203 * this behavior so asynchronous tasks can also use rpc_create.
205 struct rpc_clnt *rpc_create(struct rpc_create_args *args)
207 struct rpc_xprt *xprt;
208 struct rpc_clnt *clnt;
210 xprt = xprt_create_transport(args->protocol, args->address,
211 args->addrsize, args->timeout);
213 return (struct rpc_clnt *)xprt;
216 * By default, kernel RPC client connects from a reserved port.
217 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
218 * but it is always enabled for rpciod, which handles the connect
222 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
225 dprintk("RPC: creating %s client for %s (xprt %p)\n",
226 args->program->name, args->servername, xprt);
228 clnt = rpc_new_client(xprt, args->servername, args->program,
229 args->version, args->authflavor);
233 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
234 int err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
236 rpc_shutdown_client(clnt);
241 clnt->cl_softrtry = 1;
242 if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
243 clnt->cl_softrtry = 0;
245 if (args->flags & RPC_CLNT_CREATE_INTR)
247 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
248 clnt->cl_autobind = 1;
249 if (args->flags & RPC_CLNT_CREATE_ONESHOT)
250 clnt->cl_oneshot = 1;
254 EXPORT_SYMBOL(rpc_create);
257 * Create an RPC client
258 * @xprt - pointer to xprt struct
259 * @servname - name of server
260 * @info - rpc_program
261 * @version - rpc_program version
262 * @authflavor - rpc_auth flavour to use
264 * Creates an RPC client structure, then pings the server in order to
265 * determine if it is up, and if it supports this program and version.
267 * This function should never be called by asynchronous tasks such as
270 struct rpc_clnt *rpc_create_client(struct rpc_xprt *xprt, char *servname,
271 struct rpc_program *info, u32 version, rpc_authflavor_t authflavor)
273 struct rpc_clnt *clnt;
276 clnt = rpc_new_client(xprt, servname, info, version, authflavor);
279 err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
282 rpc_shutdown_client(clnt);
287 * This function clones the RPC client structure. It allows us to share the
288 * same transport while varying parameters such as the authentication
292 rpc_clone_client(struct rpc_clnt *clnt)
294 struct rpc_clnt *new;
296 new = kmalloc(sizeof(*new), GFP_KERNEL);
299 memcpy(new, clnt, sizeof(*new));
300 atomic_set(&new->cl_count, 1);
301 atomic_set(&new->cl_users, 0);
302 new->cl_parent = clnt;
303 atomic_inc(&clnt->cl_count);
304 /* Turn off autobind on clones */
305 new->cl_autobind = 0;
308 if (!IS_ERR(new->cl_dentry))
309 dget(new->cl_dentry);
310 rpc_init_rtt(&new->cl_rtt_default, clnt->cl_xprt->timeout.to_initval);
312 atomic_inc(&new->cl_auth->au_count);
313 new->cl_metrics = rpc_alloc_iostats(clnt);
316 printk(KERN_INFO "RPC: out of memory in %s\n", __FUNCTION__);
317 return ERR_PTR(-ENOMEM);
321 * Properly shut down an RPC client, terminating all outstanding
322 * requests. Note that we must be certain that cl_oneshot and
323 * cl_dead are cleared, or else the client would be destroyed
324 * when the last task releases it.
327 rpc_shutdown_client(struct rpc_clnt *clnt)
329 dprintk("RPC: shutting down %s client for %s, tasks=%d\n",
330 clnt->cl_protname, clnt->cl_server,
331 atomic_read(&clnt->cl_users));
333 while (atomic_read(&clnt->cl_users) > 0) {
334 /* Don't let rpc_release_client destroy us */
335 clnt->cl_oneshot = 0;
337 rpc_killall_tasks(clnt);
338 wait_event_timeout(destroy_wait,
339 !atomic_read(&clnt->cl_users), 1*HZ);
342 if (atomic_read(&clnt->cl_users) < 0) {
343 printk(KERN_ERR "RPC: rpc_shutdown_client clnt %p tasks=%d\n",
344 clnt, atomic_read(&clnt->cl_users));
351 return rpc_destroy_client(clnt);
355 * Delete an RPC client
358 rpc_destroy_client(struct rpc_clnt *clnt)
360 if (!atomic_dec_and_test(&clnt->cl_count))
362 BUG_ON(atomic_read(&clnt->cl_users) != 0);
364 dprintk("RPC: destroying %s client for %s\n",
365 clnt->cl_protname, clnt->cl_server);
367 rpcauth_destroy(clnt->cl_auth);
368 clnt->cl_auth = NULL;
370 if (clnt->cl_parent != clnt) {
371 if (!IS_ERR(clnt->cl_dentry))
372 dput(clnt->cl_dentry);
373 rpc_destroy_client(clnt->cl_parent);
376 if (!IS_ERR(clnt->cl_dentry)) {
377 rpc_rmdir(clnt->cl_dentry);
381 xprt_destroy(clnt->cl_xprt);
382 clnt->cl_xprt = NULL;
384 if (clnt->cl_server != clnt->cl_inline_name)
385 kfree(clnt->cl_server);
387 rpc_free_iostats(clnt->cl_metrics);
388 clnt->cl_metrics = NULL;
394 * Release an RPC client
397 rpc_release_client(struct rpc_clnt *clnt)
399 dprintk("RPC: rpc_release_client(%p, %d)\n",
400 clnt, atomic_read(&clnt->cl_users));
402 if (!atomic_dec_and_test(&clnt->cl_users))
404 wake_up(&destroy_wait);
405 if (clnt->cl_oneshot || clnt->cl_dead)
406 rpc_destroy_client(clnt);
410 * rpc_bind_new_program - bind a new RPC program to an existing client
411 * @old - old rpc_client
412 * @program - rpc program to set
413 * @vers - rpc program version
415 * Clones the rpc client and sets up a new RPC program. This is mainly
416 * of use for enabling different RPC programs to share the same transport.
417 * The Sun NFSv2/v3 ACL protocol can do this.
419 struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
420 struct rpc_program *program,
423 struct rpc_clnt *clnt;
424 struct rpc_version *version;
427 BUG_ON(vers >= program->nrvers || !program->version[vers]);
428 version = program->version[vers];
429 clnt = rpc_clone_client(old);
432 clnt->cl_procinfo = version->procs;
433 clnt->cl_maxproc = version->nrprocs;
434 clnt->cl_protname = program->name;
435 clnt->cl_prog = program->number;
436 clnt->cl_vers = version->number;
437 clnt->cl_stats = program->stats;
438 err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
440 rpc_shutdown_client(clnt);
448 * Default callback for async RPC calls
451 rpc_default_callback(struct rpc_task *task, void *data)
455 static const struct rpc_call_ops rpc_default_ops = {
456 .rpc_call_done = rpc_default_callback,
460 * Export the signal mask handling for synchronous code that
461 * sleeps on RPC calls
463 #define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM))
465 static void rpc_save_sigmask(sigset_t *oldset, int intr)
467 unsigned long sigallow = sigmask(SIGKILL);
470 /* Block all signals except those listed in sigallow */
472 sigallow |= RPC_INTR_SIGNALS;
473 siginitsetinv(&sigmask, sigallow);
474 sigprocmask(SIG_BLOCK, &sigmask, oldset);
477 static inline void rpc_task_sigmask(struct rpc_task *task, sigset_t *oldset)
479 rpc_save_sigmask(oldset, !RPC_TASK_UNINTERRUPTIBLE(task));
482 static inline void rpc_restore_sigmask(sigset_t *oldset)
484 sigprocmask(SIG_SETMASK, oldset, NULL);
487 void rpc_clnt_sigmask(struct rpc_clnt *clnt, sigset_t *oldset)
489 rpc_save_sigmask(oldset, clnt->cl_intr);
492 void rpc_clnt_sigunmask(struct rpc_clnt *clnt, sigset_t *oldset)
494 rpc_restore_sigmask(oldset);
498 * New rpc_call implementation
500 int rpc_call_sync(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
502 struct rpc_task *task;
506 /* If this client is slain all further I/O fails */
510 BUG_ON(flags & RPC_TASK_ASYNC);
513 task = rpc_new_task(clnt, flags, &rpc_default_ops, NULL);
517 /* Mask signals on RPC calls _and_ GSS_AUTH upcalls */
518 rpc_task_sigmask(task, &oldset);
520 rpc_call_setup(task, msg, 0);
522 /* Set up the call info struct and execute the task */
523 status = task->tk_status;
525 atomic_inc(&task->tk_count);
526 status = rpc_execute(task);
528 status = task->tk_status;
530 rpc_restore_sigmask(&oldset);
531 rpc_release_task(task);
537 * New rpc_call implementation
540 rpc_call_async(struct rpc_clnt *clnt, struct rpc_message *msg, int flags,
541 const struct rpc_call_ops *tk_ops, void *data)
543 struct rpc_task *task;
547 /* If this client is slain all further I/O fails */
552 flags |= RPC_TASK_ASYNC;
554 /* Create/initialize a new RPC task */
556 if (!(task = rpc_new_task(clnt, flags, tk_ops, data)))
559 /* Mask signals on GSS_AUTH upcalls */
560 rpc_task_sigmask(task, &oldset);
562 rpc_call_setup(task, msg, 0);
564 /* Set up the call info struct and execute the task */
565 status = task->tk_status;
569 rpc_release_task(task);
571 rpc_restore_sigmask(&oldset);
574 if (tk_ops->rpc_release != NULL)
575 tk_ops->rpc_release(data);
581 rpc_call_setup(struct rpc_task *task, struct rpc_message *msg, int flags)
584 task->tk_flags |= flags;
585 /* Bind the user cred */
586 if (task->tk_msg.rpc_cred != NULL)
587 rpcauth_holdcred(task);
589 rpcauth_bindcred(task);
591 if (task->tk_status == 0)
592 task->tk_action = call_start;
594 task->tk_action = rpc_exit_task;
598 * rpc_peeraddr - extract remote peer address from clnt's xprt
599 * @clnt: RPC client structure
600 * @buf: target buffer
601 * @size: length of target buffer
603 * Returns the number of bytes that are actually in the stored address.
605 size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
608 struct rpc_xprt *xprt = clnt->cl_xprt;
610 bytes = sizeof(xprt->addr);
613 memcpy(buf, &clnt->cl_xprt->addr, bytes);
614 return xprt->addrlen;
616 EXPORT_SYMBOL(rpc_peeraddr);
619 * rpc_peeraddr2str - return remote peer address in printable format
620 * @clnt: RPC client structure
621 * @format: address format
624 char *rpc_peeraddr2str(struct rpc_clnt *clnt, enum rpc_display_format_t format)
626 struct rpc_xprt *xprt = clnt->cl_xprt;
627 return xprt->ops->print_addr(xprt, format);
629 EXPORT_SYMBOL(rpc_peeraddr2str);
632 rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
634 struct rpc_xprt *xprt = clnt->cl_xprt;
635 if (xprt->ops->set_buffer_size)
636 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
640 * Return size of largest payload RPC client can support, in bytes
642 * For stream transports, this is one RPC record fragment (see RFC
643 * 1831), as we don't support multi-record requests yet. For datagram
644 * transports, this is the size of an IP packet minus the IP, UDP, and
647 size_t rpc_max_payload(struct rpc_clnt *clnt)
649 return clnt->cl_xprt->max_payload;
651 EXPORT_SYMBOL(rpc_max_payload);
654 * rpc_force_rebind - force transport to check that remote port is unchanged
655 * @clnt: client to rebind
658 void rpc_force_rebind(struct rpc_clnt *clnt)
660 if (clnt->cl_autobind)
661 xprt_clear_bound(clnt->cl_xprt);
663 EXPORT_SYMBOL(rpc_force_rebind);
666 * Restart an (async) RPC call. Usually called from within the
670 rpc_restart_call(struct rpc_task *task)
672 if (RPC_ASSASSINATED(task))
675 task->tk_action = call_start;
681 * Other FSM states can be visited zero or more times, but
682 * this state is visited exactly once for each RPC.
685 call_start(struct rpc_task *task)
687 struct rpc_clnt *clnt = task->tk_client;
689 dprintk("RPC: %4d call_start %s%d proc %d (%s)\n", task->tk_pid,
690 clnt->cl_protname, clnt->cl_vers, task->tk_msg.rpc_proc->p_proc,
691 (RPC_IS_ASYNC(task) ? "async" : "sync"));
693 /* Increment call count */
694 task->tk_msg.rpc_proc->p_count++;
695 clnt->cl_stats->rpccnt++;
696 task->tk_action = call_reserve;
700 * 1. Reserve an RPC call slot
703 call_reserve(struct rpc_task *task)
705 dprintk("RPC: %4d call_reserve\n", task->tk_pid);
707 if (!rpcauth_uptodatecred(task)) {
708 task->tk_action = call_refresh;
713 task->tk_action = call_reserveresult;
718 * 1b. Grok the result of xprt_reserve()
721 call_reserveresult(struct rpc_task *task)
723 int status = task->tk_status;
725 dprintk("RPC: %4d call_reserveresult (status %d)\n",
726 task->tk_pid, task->tk_status);
729 * After a call to xprt_reserve(), we must have either
730 * a request slot or else an error status.
734 if (task->tk_rqstp) {
735 task->tk_action = call_allocate;
739 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
740 __FUNCTION__, status);
741 rpc_exit(task, -EIO);
746 * Even though there was an error, we may have acquired
747 * a request slot somehow. Make sure not to leak it.
749 if (task->tk_rqstp) {
750 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
751 __FUNCTION__, status);
756 case -EAGAIN: /* woken up; retry */
757 task->tk_action = call_reserve;
759 case -EIO: /* probably a shutdown */
762 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
763 __FUNCTION__, status);
766 rpc_exit(task, status);
770 * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
771 * (Note: buffer memory is freed in xprt_release).
774 call_allocate(struct rpc_task *task)
776 struct rpc_rqst *req = task->tk_rqstp;
777 struct rpc_xprt *xprt = task->tk_xprt;
780 dprintk("RPC: %4d call_allocate (status %d)\n",
781 task->tk_pid, task->tk_status);
782 task->tk_action = call_bind;
786 /* FIXME: compute buffer requirements more exactly using
788 bufsiz = task->tk_msg.rpc_proc->p_bufsiz + RPC_SLACK_SPACE;
790 if (xprt->ops->buf_alloc(task, bufsiz << 1) != NULL)
792 printk(KERN_INFO "RPC: buffer allocation failed for task %p\n", task);
794 if (RPC_IS_ASYNC(task) || !signalled()) {
796 task->tk_action = call_reserve;
797 rpc_delay(task, HZ>>4);
801 rpc_exit(task, -ERESTARTSYS);
805 rpc_task_need_encode(struct rpc_task *task)
807 return task->tk_rqstp->rq_snd_buf.len == 0;
811 rpc_task_force_reencode(struct rpc_task *task)
813 task->tk_rqstp->rq_snd_buf.len = 0;
817 * 3. Encode arguments of an RPC call
820 call_encode(struct rpc_task *task)
822 struct rpc_rqst *req = task->tk_rqstp;
823 struct xdr_buf *sndbuf = &req->rq_snd_buf;
824 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
829 dprintk("RPC: %4d call_encode (status %d)\n",
830 task->tk_pid, task->tk_status);
832 /* Default buffer setup */
833 bufsiz = req->rq_bufsize >> 1;
834 sndbuf->head[0].iov_base = (void *)req->rq_buffer;
835 sndbuf->head[0].iov_len = bufsiz;
836 sndbuf->tail[0].iov_len = 0;
837 sndbuf->page_len = 0;
839 sndbuf->buflen = bufsiz;
840 rcvbuf->head[0].iov_base = (void *)((char *)req->rq_buffer + bufsiz);
841 rcvbuf->head[0].iov_len = bufsiz;
842 rcvbuf->tail[0].iov_len = 0;
843 rcvbuf->page_len = 0;
845 rcvbuf->buflen = bufsiz;
847 /* Encode header and provided arguments */
848 encode = task->tk_msg.rpc_proc->p_encode;
849 if (!(p = call_header(task))) {
850 printk(KERN_INFO "RPC: call_header failed, exit EIO\n");
851 rpc_exit(task, -EIO);
857 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
858 task->tk_msg.rpc_argp);
859 if (task->tk_status == -ENOMEM) {
860 /* XXX: Is this sane? */
861 rpc_delay(task, 3*HZ);
862 task->tk_status = -EAGAIN;
867 * 4. Get the server port number if not yet set
870 call_bind(struct rpc_task *task)
872 struct rpc_xprt *xprt = task->tk_xprt;
874 dprintk("RPC: %4d call_bind (status %d)\n",
875 task->tk_pid, task->tk_status);
877 task->tk_action = call_connect;
878 if (!xprt_bound(xprt)) {
879 task->tk_action = call_bind_status;
880 task->tk_timeout = xprt->bind_timeout;
881 xprt->ops->rpcbind(task);
886 * 4a. Sort out bind result
889 call_bind_status(struct rpc_task *task)
891 int status = -EACCES;
893 if (task->tk_status >= 0) {
894 dprintk("RPC: %4d call_bind_status (status %d)\n",
895 task->tk_pid, task->tk_status);
897 task->tk_action = call_connect;
901 switch (task->tk_status) {
903 dprintk("RPC: %4d remote rpcbind: RPC program/version unavailable\n",
905 rpc_delay(task, 3*HZ);
908 dprintk("RPC: %4d rpcbind request timed out\n",
910 if (RPC_IS_SOFT(task)) {
916 dprintk("RPC: %4d remote rpcbind service unavailable\n",
919 case -EPROTONOSUPPORT:
920 dprintk("RPC: %4d remote rpcbind version 2 unavailable\n",
924 dprintk("RPC: %4d unrecognized rpcbind error (%d)\n",
925 task->tk_pid, -task->tk_status);
930 rpc_exit(task, status);
935 task->tk_action = call_bind;
940 * 4b. Connect to the RPC server
943 call_connect(struct rpc_task *task)
945 struct rpc_xprt *xprt = task->tk_xprt;
947 dprintk("RPC: %4d call_connect xprt %p %s connected\n",
949 (xprt_connected(xprt) ? "is" : "is not"));
951 task->tk_action = call_transmit;
952 if (!xprt_connected(xprt)) {
953 task->tk_action = call_connect_status;
954 if (task->tk_status < 0)
961 * 4c. Sort out connect result
964 call_connect_status(struct rpc_task *task)
966 struct rpc_clnt *clnt = task->tk_client;
967 int status = task->tk_status;
969 dprintk("RPC: %5u call_connect_status (status %d)\n",
970 task->tk_pid, task->tk_status);
974 clnt->cl_stats->netreconn++;
975 task->tk_action = call_transmit;
979 /* Something failed: remote service port may have changed */
980 rpc_force_rebind(clnt);
986 task->tk_action = call_bind;
989 rpc_exit(task, -EIO);
995 * 5. Transmit the RPC request, and wait for reply
998 call_transmit(struct rpc_task *task)
1000 dprintk("RPC: %4d call_transmit (status %d)\n",
1001 task->tk_pid, task->tk_status);
1003 task->tk_action = call_status;
1004 if (task->tk_status < 0)
1006 task->tk_status = xprt_prepare_transmit(task);
1007 if (task->tk_status != 0)
1009 task->tk_action = call_transmit_status;
1010 /* Encode here so that rpcsec_gss can use correct sequence number. */
1011 if (rpc_task_need_encode(task)) {
1012 BUG_ON(task->tk_rqstp->rq_bytes_sent != 0);
1014 /* Did the encode result in an error condition? */
1015 if (task->tk_status != 0)
1018 xprt_transmit(task);
1019 if (task->tk_status < 0)
1022 * On success, ensure that we call xprt_end_transmit() before sleeping
1023 * in order to allow access to the socket to other RPC requests.
1025 call_transmit_status(task);
1026 if (task->tk_msg.rpc_proc->p_decode != NULL)
1028 task->tk_action = rpc_exit_task;
1029 rpc_wake_up_task(task);
1033 * 5a. Handle cleanup after a transmission
1036 call_transmit_status(struct rpc_task *task)
1038 task->tk_action = call_status;
1040 * Special case: if we've been waiting on the socket's write_space()
1041 * callback, then don't call xprt_end_transmit().
1043 if (task->tk_status == -EAGAIN)
1045 xprt_end_transmit(task);
1046 rpc_task_force_reencode(task);
1050 * 6. Sort out the RPC call status
1053 call_status(struct rpc_task *task)
1055 struct rpc_clnt *clnt = task->tk_client;
1056 struct rpc_rqst *req = task->tk_rqstp;
1059 if (req->rq_received > 0 && !req->rq_bytes_sent)
1060 task->tk_status = req->rq_received;
1062 dprintk("RPC: %4d call_status (status %d)\n",
1063 task->tk_pid, task->tk_status);
1065 status = task->tk_status;
1067 task->tk_action = call_decode;
1071 task->tk_status = 0;
1074 task->tk_action = call_timeout;
1078 rpc_force_rebind(clnt);
1079 task->tk_action = call_bind;
1082 task->tk_action = call_transmit;
1085 /* shutdown or soft timeout */
1086 rpc_exit(task, status);
1089 printk("%s: RPC call returned error %d\n",
1090 clnt->cl_protname, -status);
1091 rpc_exit(task, status);
1097 * 6a. Handle RPC timeout
1098 * We do not release the request slot, so we keep using the
1099 * same XID for all retransmits.
1102 call_timeout(struct rpc_task *task)
1104 struct rpc_clnt *clnt = task->tk_client;
1106 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
1107 dprintk("RPC: %4d call_timeout (minor)\n", task->tk_pid);
1111 dprintk("RPC: %4d call_timeout (major)\n", task->tk_pid);
1112 task->tk_timeouts++;
1114 if (RPC_IS_SOFT(task)) {
1115 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
1116 clnt->cl_protname, clnt->cl_server);
1117 rpc_exit(task, -EIO);
1121 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
1122 task->tk_flags |= RPC_CALL_MAJORSEEN;
1123 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
1124 clnt->cl_protname, clnt->cl_server);
1126 rpc_force_rebind(clnt);
1129 clnt->cl_stats->rpcretrans++;
1130 task->tk_action = call_bind;
1131 task->tk_status = 0;
1135 * 7. Decode the RPC reply
1138 call_decode(struct rpc_task *task)
1140 struct rpc_clnt *clnt = task->tk_client;
1141 struct rpc_rqst *req = task->tk_rqstp;
1142 kxdrproc_t decode = task->tk_msg.rpc_proc->p_decode;
1145 dprintk("RPC: %4d call_decode (status %d)\n",
1146 task->tk_pid, task->tk_status);
1148 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
1149 printk(KERN_NOTICE "%s: server %s OK\n",
1150 clnt->cl_protname, clnt->cl_server);
1151 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
1154 if (task->tk_status < 12) {
1155 if (!RPC_IS_SOFT(task)) {
1156 task->tk_action = call_bind;
1157 clnt->cl_stats->rpcretrans++;
1160 printk(KERN_WARNING "%s: too small RPC reply size (%d bytes)\n",
1161 clnt->cl_protname, task->tk_status);
1162 rpc_exit(task, -EIO);
1167 * Ensure that we see all writes made by xprt_complete_rqst()
1168 * before it changed req->rq_received.
1171 req->rq_rcv_buf.len = req->rq_private_buf.len;
1173 /* Check that the softirq receive buffer is valid */
1174 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
1175 sizeof(req->rq_rcv_buf)) != 0);
1177 /* Verify the RPC header */
1178 p = call_verify(task);
1180 if (p == ERR_PTR(-EAGAIN))
1185 task->tk_action = rpc_exit_task;
1188 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
1189 task->tk_msg.rpc_resp);
1190 dprintk("RPC: %4d call_decode result %d\n", task->tk_pid,
1194 req->rq_received = req->rq_private_buf.len = 0;
1195 task->tk_status = 0;
1199 * 8. Refresh the credentials if rejected by the server
1202 call_refresh(struct rpc_task *task)
1204 dprintk("RPC: %4d call_refresh\n", task->tk_pid);
1206 xprt_release(task); /* Must do to obtain new XID */
1207 task->tk_action = call_refreshresult;
1208 task->tk_status = 0;
1209 task->tk_client->cl_stats->rpcauthrefresh++;
1210 rpcauth_refreshcred(task);
1214 * 8a. Process the results of a credential refresh
1217 call_refreshresult(struct rpc_task *task)
1219 int status = task->tk_status;
1220 dprintk("RPC: %4d call_refreshresult (status %d)\n",
1221 task->tk_pid, task->tk_status);
1223 task->tk_status = 0;
1224 task->tk_action = call_reserve;
1225 if (status >= 0 && rpcauth_uptodatecred(task))
1227 if (status == -EACCES) {
1228 rpc_exit(task, -EACCES);
1231 task->tk_action = call_refresh;
1232 if (status != -ETIMEDOUT)
1233 rpc_delay(task, 3*HZ);
1238 * Call header serialization
1241 call_header(struct rpc_task *task)
1243 struct rpc_clnt *clnt = task->tk_client;
1244 struct rpc_rqst *req = task->tk_rqstp;
1245 u32 *p = req->rq_svec[0].iov_base;
1247 /* FIXME: check buffer size? */
1249 p = xprt_skip_transport_header(task->tk_xprt, p);
1250 *p++ = req->rq_xid; /* XID */
1251 *p++ = htonl(RPC_CALL); /* CALL */
1252 *p++ = htonl(RPC_VERSION); /* RPC version */
1253 *p++ = htonl(clnt->cl_prog); /* program number */
1254 *p++ = htonl(clnt->cl_vers); /* program version */
1255 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
1256 p = rpcauth_marshcred(task, p);
1257 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
1262 * Reply header verification
1265 call_verify(struct rpc_task *task)
1267 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
1268 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
1269 u32 *p = iov->iov_base, n;
1270 int error = -EACCES;
1272 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
1273 /* RFC-1014 says that the representation of XDR data must be a
1274 * multiple of four bytes
1275 * - if it isn't pointer subtraction in the NFS client may give
1279 "call_verify: XDR representation not a multiple of"
1280 " 4 bytes: 0x%x\n", task->tk_rqstp->rq_rcv_buf.len);
1285 p += 1; /* skip XID */
1287 if ((n = ntohl(*p++)) != RPC_REPLY) {
1288 printk(KERN_WARNING "call_verify: not an RPC reply: %x\n", n);
1291 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
1294 switch ((n = ntohl(*p++))) {
1295 case RPC_AUTH_ERROR:
1298 dprintk("%s: RPC call version mismatch!\n", __FUNCTION__);
1299 error = -EPROTONOSUPPORT;
1302 dprintk("%s: RPC call rejected, unknown error: %x\n", __FUNCTION__, n);
1307 switch ((n = ntohl(*p++))) {
1308 case RPC_AUTH_REJECTEDCRED:
1309 case RPC_AUTH_REJECTEDVERF:
1310 case RPCSEC_GSS_CREDPROBLEM:
1311 case RPCSEC_GSS_CTXPROBLEM:
1312 if (!task->tk_cred_retry)
1314 task->tk_cred_retry--;
1315 dprintk("RPC: %4d call_verify: retry stale creds\n",
1317 rpcauth_invalcred(task);
1318 task->tk_action = call_refresh;
1320 case RPC_AUTH_BADCRED:
1321 case RPC_AUTH_BADVERF:
1322 /* possibly garbled cred/verf? */
1323 if (!task->tk_garb_retry)
1325 task->tk_garb_retry--;
1326 dprintk("RPC: %4d call_verify: retry garbled creds\n",
1328 task->tk_action = call_bind;
1330 case RPC_AUTH_TOOWEAK:
1331 printk(KERN_NOTICE "call_verify: server %s requires stronger "
1332 "authentication.\n", task->tk_client->cl_server);
1335 printk(KERN_WARNING "call_verify: unknown auth error: %x\n", n);
1338 dprintk("RPC: %4d call_verify: call rejected %d\n",
1342 if (!(p = rpcauth_checkverf(task, p))) {
1343 printk(KERN_WARNING "call_verify: auth check failed\n");
1344 goto out_garbage; /* bad verifier, retry */
1346 len = p - (u32 *)iov->iov_base - 1;
1349 switch ((n = ntohl(*p++))) {
1352 case RPC_PROG_UNAVAIL:
1353 dprintk("RPC: call_verify: program %u is unsupported by server %s\n",
1354 (unsigned int)task->tk_client->cl_prog,
1355 task->tk_client->cl_server);
1356 error = -EPFNOSUPPORT;
1358 case RPC_PROG_MISMATCH:
1359 dprintk("RPC: call_verify: program %u, version %u unsupported by server %s\n",
1360 (unsigned int)task->tk_client->cl_prog,
1361 (unsigned int)task->tk_client->cl_vers,
1362 task->tk_client->cl_server);
1363 error = -EPROTONOSUPPORT;
1365 case RPC_PROC_UNAVAIL:
1366 dprintk("RPC: call_verify: proc %p unsupported by program %u, version %u on server %s\n",
1367 task->tk_msg.rpc_proc,
1368 task->tk_client->cl_prog,
1369 task->tk_client->cl_vers,
1370 task->tk_client->cl_server);
1371 error = -EOPNOTSUPP;
1373 case RPC_GARBAGE_ARGS:
1374 dprintk("RPC: %4d %s: server saw garbage\n", task->tk_pid, __FUNCTION__);
1377 printk(KERN_WARNING "call_verify: server accept status: %x\n", n);
1382 task->tk_client->cl_stats->rpcgarbage++;
1383 if (task->tk_garb_retry) {
1384 task->tk_garb_retry--;
1385 dprintk("RPC %s: retrying %4d\n", __FUNCTION__, task->tk_pid);
1386 task->tk_action = call_bind;
1388 return ERR_PTR(-EAGAIN);
1390 printk(KERN_WARNING "RPC %s: retry failed, exit EIO\n", __FUNCTION__);
1394 rpc_exit(task, error);
1395 return ERR_PTR(error);
1397 printk(KERN_WARNING "RPC %s: server reply was truncated.\n", __FUNCTION__);
1401 static int rpcproc_encode_null(void *rqstp, u32 *data, void *obj)
1406 static int rpcproc_decode_null(void *rqstp, u32 *data, void *obj)
1411 static struct rpc_procinfo rpcproc_null = {
1412 .p_encode = rpcproc_encode_null,
1413 .p_decode = rpcproc_decode_null,
1416 int rpc_ping(struct rpc_clnt *clnt, int flags)
1418 struct rpc_message msg = {
1419 .rpc_proc = &rpcproc_null,
1422 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
1423 err = rpc_call_sync(clnt, &msg, flags);
1424 put_rpccred(msg.rpc_cred);