File: /usr/src/linux/net/sunrpc/clnt.c
1 /*
2 * linux/net/sunrpc/rpcclnt.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 reconnect handling (when finished).
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 * 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.
19 *
20 * Copyright (C) 1992,1993 Rick Sladkey <jrs@world.std.com>
21 * Copyright (C) 1995,1996 Olaf Kirch <okir@monad.swb.de>
22 */
23
24 #include <asm/system.h>
25
26 #include <linux/types.h>
27 #include <linux/mm.h>
28 #include <linux/slab.h>
29 #include <linux/in.h>
30 #include <linux/utsname.h>
31
32 #include <linux/sunrpc/clnt.h>
33
34 #include <linux/nfs.h>
35
36
37 #define RPC_SLACK_SPACE 512 /* total overkill */
38
39 #ifdef RPC_DEBUG
40 # define RPCDBG_FACILITY RPCDBG_CALL
41 #endif
42
43 static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
44
45
46 static void call_reserve(struct rpc_task *task);
47 static void call_reserveresult(struct rpc_task *task);
48 static void call_allocate(struct rpc_task *task);
49 static void call_encode(struct rpc_task *task);
50 static void call_decode(struct rpc_task *task);
51 static void call_bind(struct rpc_task *task);
52 static void call_transmit(struct rpc_task *task);
53 static void call_status(struct rpc_task *task);
54 static void call_refresh(struct rpc_task *task);
55 static void call_refreshresult(struct rpc_task *task);
56 static void call_timeout(struct rpc_task *task);
57 static void call_reconnect(struct rpc_task *task);
58 static void child_reconnect(struct rpc_task *);
59 static void child_reconnect_status(struct rpc_task *);
60 static u32 * call_header(struct rpc_task *task);
61 static u32 * call_verify(struct rpc_task *task);
62
63
64 /*
65 * Create an RPC client
66 * FIXME: This should also take a flags argument (as in task->tk_flags).
67 * It's called (among others) from pmap_create_client, which may in
68 * turn be called by an async task. In this case, rpciod should not be
69 * made to sleep too long.
70 */
71 struct rpc_clnt *
72 rpc_create_client(struct rpc_xprt *xprt, char *servname,
73 struct rpc_program *program, u32 vers, int flavor)
74 {
75 struct rpc_version *version;
76 struct rpc_clnt *clnt = NULL;
77
78 dprintk("RPC: creating %s client for %s (xprt %p)\n",
79 program->name, servname, xprt);
80
81 #ifdef RPC_DEBUG
82 rpc_register_sysctl();
83 #endif
84
85 if (!xprt)
86 goto out;
87 if (vers >= program->nrvers || !(version = program->version[vers]))
88 goto out;
89
90 clnt = (struct rpc_clnt *) rpc_allocate(0, sizeof(*clnt));
91 if (!clnt)
92 goto out_no_clnt;
93 memset(clnt, 0, sizeof(*clnt));
94 atomic_set(&clnt->cl_users, 0);
95
96 clnt->cl_xprt = xprt;
97 clnt->cl_procinfo = version->procs;
98 clnt->cl_maxproc = version->nrprocs;
99 clnt->cl_server = servname;
100 clnt->cl_protname = program->name;
101 clnt->cl_port = xprt->addr.sin_port;
102 clnt->cl_prog = program->number;
103 clnt->cl_vers = version->number;
104 clnt->cl_prot = xprt->prot;
105 clnt->cl_stats = program->stats;
106 clnt->cl_bindwait = RPC_INIT_WAITQ("bindwait");
107
108 if (!clnt->cl_port)
109 clnt->cl_autobind = 1;
110
111 if (!rpcauth_create(flavor, clnt))
112 goto out_no_auth;
113
114 /* save the nodename */
115 clnt->cl_nodelen = strlen(system_utsname.nodename);
116 if (clnt->cl_nodelen > UNX_MAXNODENAME)
117 clnt->cl_nodelen = UNX_MAXNODENAME;
118 memcpy(clnt->cl_nodename, system_utsname.nodename, clnt->cl_nodelen);
119 out:
120 return clnt;
121
122 out_no_clnt:
123 printk(KERN_INFO "RPC: out of memory in rpc_create_client\n");
124 goto out;
125 out_no_auth:
126 printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %d)\n",
127 flavor);
128 rpc_free(clnt);
129 clnt = NULL;
130 goto out;
131 }
132
133 /*
134 * Properly shut down an RPC client, terminating all outstanding
135 * requests. Note that we must be certain that cl_oneshot and
136 * cl_dead are cleared, or else the client would be destroyed
137 * when the last task releases it.
138 */
139 int
140 rpc_shutdown_client(struct rpc_clnt *clnt)
141 {
142 dprintk("RPC: shutting down %s client for %s\n",
143 clnt->cl_protname, clnt->cl_server);
144 while (atomic_read(&clnt->cl_users)) {
145 #ifdef RPC_DEBUG
146 dprintk("RPC: rpc_shutdown_client: client %s, tasks=%d\n",
147 clnt->cl_protname, atomic_read(&clnt->cl_users));
148 #endif
149 /* Don't let rpc_release_client destroy us */
150 clnt->cl_oneshot = 0;
151 clnt->cl_dead = 0;
152 rpc_killall_tasks(clnt);
153 sleep_on_timeout(&destroy_wait, 1*HZ);
154 }
155 return rpc_destroy_client(clnt);
156 }
157
158 /*
159 * Delete an RPC client
160 */
161 int
162 rpc_destroy_client(struct rpc_clnt *clnt)
163 {
164 dprintk("RPC: destroying %s client for %s\n",
165 clnt->cl_protname, clnt->cl_server);
166
167 if (clnt->cl_auth) {
168 rpcauth_destroy(clnt->cl_auth);
169 clnt->cl_auth = NULL;
170 }
171 if (clnt->cl_xprt) {
172 xprt_destroy(clnt->cl_xprt);
173 clnt->cl_xprt = NULL;
174 }
175 rpc_free(clnt);
176 return 0;
177 }
178
179 /*
180 * Release an RPC client
181 */
182 void
183 rpc_release_client(struct rpc_clnt *clnt)
184 {
185 dprintk("RPC: rpc_release_client(%p, %d)\n",
186 clnt, atomic_read(&clnt->cl_users));
187
188 if (!atomic_dec_and_test(&clnt->cl_users))
189 return;
190 wake_up(&destroy_wait);
191 if (clnt->cl_oneshot || clnt->cl_dead)
192 rpc_destroy_client(clnt);
193 }
194
195 /*
196 * Default callback for async RPC calls
197 */
198 static void
199 rpc_default_callback(struct rpc_task *task)
200 {
201 }
202
203 /*
204 * Export the signal mask handling for aysnchronous code that
205 * sleeps on RPC calls
206 */
207
208 void rpc_clnt_sigmask(struct rpc_clnt *clnt, sigset_t *oldset)
209 {
210 unsigned long sigallow = sigmask(SIGKILL);
211 unsigned long irqflags;
212
213 /* Turn off various signals */
214 if (clnt->cl_intr) {
215 struct k_sigaction *action = current->sig->action;
216 if (action[SIGINT-1].sa.sa_handler == SIG_DFL)
217 sigallow |= sigmask(SIGINT);
218 if (action[SIGQUIT-1].sa.sa_handler == SIG_DFL)
219 sigallow |= sigmask(SIGQUIT);
220 }
221 spin_lock_irqsave(¤t->sigmask_lock, irqflags);
222 *oldset = current->blocked;
223 siginitsetinv(¤t->blocked, sigallow & ~oldset->sig[0]);
224 recalc_sigpending(current);
225 spin_unlock_irqrestore(¤t->sigmask_lock, irqflags);
226 }
227
228 void rpc_clnt_sigunmask(struct rpc_clnt *clnt, sigset_t *oldset)
229 {
230 unsigned long irqflags;
231
232 spin_lock_irqsave(¤t->sigmask_lock, irqflags);
233 current->blocked = *oldset;
234 recalc_sigpending(current);
235 spin_unlock_irqrestore(¤t->sigmask_lock, irqflags);
236 }
237
238 /*
239 * New rpc_call implementation
240 */
241 int rpc_call_sync(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
242 {
243 struct rpc_task my_task, *task = &my_task;
244 sigset_t oldset;
245 int status;
246
247 /* If this client is slain all further I/O fails */
248 if (clnt->cl_dead)
249 return -EIO;
250
251 if (flags & RPC_TASK_ASYNC) {
252 printk("rpc_call_sync: Illegal flag combination for synchronous task\n");
253 flags &= ~RPC_TASK_ASYNC;
254 }
255
256 rpc_clnt_sigmask(clnt, &oldset);
257
258 /* Create/initialize a new RPC task */
259 rpc_init_task(task, clnt, NULL, flags);
260 rpc_call_setup(task, msg, 0);
261
262 /* Set up the call info struct and execute the task */
263 if (task->tk_status == 0)
264 status = rpc_execute(task);
265 else {
266 status = task->tk_status;
267 rpc_release_task(task);
268 }
269
270 rpc_clnt_sigunmask(clnt, &oldset);
271
272 return status;
273 }
274
275 /*
276 * New rpc_call implementation
277 */
278 int
279 rpc_call_async(struct rpc_clnt *clnt, struct rpc_message *msg, int flags,
280 rpc_action callback, void *data)
281 {
282 struct rpc_task *task;
283 sigset_t oldset;
284 int status;
285
286 /* If this client is slain all further I/O fails */
287 if (clnt->cl_dead)
288 return -EIO;
289
290 flags |= RPC_TASK_ASYNC;
291
292 rpc_clnt_sigmask(clnt, &oldset);
293
294 /* Create/initialize a new RPC task */
295 if (!callback)
296 callback = rpc_default_callback;
297 status = -ENOMEM;
298 if (!(task = rpc_new_task(clnt, callback, flags)))
299 goto out;
300 task->tk_calldata = data;
301
302 rpc_call_setup(task, msg, 0);
303
304 /* Set up the call info struct and execute the task */
305 if (task->tk_status == 0)
306 status = rpc_execute(task);
307 else {
308 status = task->tk_status;
309 rpc_release_task(task);
310 }
311
312 out:
313 rpc_clnt_sigunmask(clnt, &oldset);
314
315 return status;
316 }
317
318
319 void
320 rpc_call_setup(struct rpc_task *task, struct rpc_message *msg, int flags)
321 {
322 task->tk_msg = *msg;
323 task->tk_flags |= flags;
324 /* Bind the user cred */
325 if (task->tk_msg.rpc_cred != NULL) {
326 rpcauth_holdcred(task);
327 } else
328 rpcauth_bindcred(task);
329
330 if (task->tk_status == 0)
331 task->tk_action = call_reserve;
332 else
333 task->tk_action = NULL;
334
335 /* Increment call count */
336 if (task->tk_msg.rpc_proc < task->tk_client->cl_maxproc)
337 rpcproc_count(task->tk_client, task->tk_msg.rpc_proc)++;
338 }
339
340 /*
341 * Restart an (async) RPC call. Usually called from within the
342 * exit handler.
343 */
344 void
345 rpc_restart_call(struct rpc_task *task)
346 {
347 if (RPC_ASSASSINATED(task))
348 return;
349
350 task->tk_action = call_reserve;
351 rpcproc_count(task->tk_client, task->tk_msg.rpc_proc)++;
352 }
353
354 /*
355 * 1. Reserve an RPC call slot
356 */
357 static void
358 call_reserve(struct rpc_task *task)
359 {
360 struct rpc_clnt *clnt = task->tk_client;
361
362 if (task->tk_msg.rpc_proc > clnt->cl_maxproc) {
363 printk(KERN_WARNING "%s (vers %d): bad procedure number %d\n",
364 clnt->cl_protname, clnt->cl_vers, task->tk_msg.rpc_proc);
365 rpc_exit(task, -EIO);
366 return;
367 }
368
369 dprintk("RPC: %4d call_reserve\n", task->tk_pid);
370 if (!rpcauth_uptodatecred(task)) {
371 task->tk_action = call_refresh;
372 return;
373 }
374
375 task->tk_status = 0;
376 task->tk_action = call_reserveresult;
377 task->tk_timeout = clnt->cl_timeout.to_resrvval;
378 clnt->cl_stats->rpccnt++;
379 xprt_reserve(task);
380 }
381
382 /*
383 * 1b. Grok the result of xprt_reserve()
384 */
385 static void
386 call_reserveresult(struct rpc_task *task)
387 {
388 int status = task->tk_status;
389
390 dprintk("RPC: %4d call_reserveresult (status %d)\n",
391 task->tk_pid, task->tk_status);
392 /*
393 * After a call to xprt_reserve(), we must have either
394 * a request slot or else an error status.
395 */
396 if ((task->tk_status >= 0 && !task->tk_rqstp) ||
397 (task->tk_status < 0 && task->tk_rqstp))
398 printk(KERN_ERR "call_reserveresult: status=%d, request=%p??\n",
399 task->tk_status, task->tk_rqstp);
400
401 if (task->tk_status >= 0) {
402 task->tk_action = call_allocate;
403 return;
404 }
405
406 task->tk_status = 0;
407 switch (status) {
408 case -EAGAIN:
409 case -ENOBUFS:
410 task->tk_timeout = task->tk_client->cl_timeout.to_resrvval;
411 task->tk_action = call_reserve;
412 break;
413 case -ETIMEDOUT:
414 dprintk("RPC: task timed out\n");
415 task->tk_action = call_timeout;
416 break;
417 default:
418 if (!task->tk_rqstp) {
419 printk(KERN_INFO "RPC: task has no request, exit EIO\n");
420 rpc_exit(task, -EIO);
421 } else
422 rpc_exit(task, status);
423 }
424 }
425
426 /*
427 * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
428 * (Note: buffer memory is freed in rpc_task_release).
429 */
430 static void
431 call_allocate(struct rpc_task *task)
432 {
433 struct rpc_clnt *clnt = task->tk_client;
434 unsigned int bufsiz;
435
436 dprintk("RPC: %4d call_allocate (status %d)\n",
437 task->tk_pid, task->tk_status);
438 task->tk_action = call_encode;
439 if (task->tk_buffer)
440 return;
441
442 /* FIXME: compute buffer requirements more exactly using
443 * auth->au_wslack */
444 bufsiz = rpcproc_bufsiz(clnt, task->tk_msg.rpc_proc) + RPC_SLACK_SPACE;
445
446 if ((task->tk_buffer = rpc_malloc(task, bufsiz << 1)) != NULL)
447 return;
448 printk(KERN_INFO "RPC: buffer allocation failed for task %p\n", task);
449
450 if (RPC_IS_ASYNC(task) || !(task->tk_client->cl_intr && signalled())) {
451 xprt_release(task);
452 task->tk_action = call_reserve;
453 rpc_delay(task, HZ>>4);
454 return;
455 }
456
457 rpc_exit(task, -ERESTARTSYS);
458 }
459
460 /*
461 * 3. Encode arguments of an RPC call
462 */
463 static void
464 call_encode(struct rpc_task *task)
465 {
466 struct rpc_clnt *clnt = task->tk_client;
467 struct rpc_rqst *req = task->tk_rqstp;
468 unsigned int bufsiz;
469 kxdrproc_t encode;
470 int status;
471 u32 *p;
472
473 dprintk("RPC: %4d call_encode (status %d)\n",
474 task->tk_pid, task->tk_status);
475
476 task->tk_action = call_bind;
477
478 /* Default buffer setup */
479 bufsiz = rpcproc_bufsiz(clnt, task->tk_msg.rpc_proc)+RPC_SLACK_SPACE;
480 req->rq_svec[0].iov_base = (void *)task->tk_buffer;
481 req->rq_svec[0].iov_len = bufsiz;
482 req->rq_slen = 0;
483 req->rq_snr = 1;
484 req->rq_rvec[0].iov_base = (void *)((char *)task->tk_buffer + bufsiz);
485 req->rq_rvec[0].iov_len = bufsiz;
486 req->rq_rlen = bufsiz;
487 req->rq_rnr = 1;
488
489 /* Zero buffer so we have automatic zero-padding of opaque & string */
490 memset(task->tk_buffer, 0, bufsiz);
491
492 /* Encode header and provided arguments */
493 encode = rpcproc_encode(clnt, task->tk_msg.rpc_proc);
494 if (!(p = call_header(task))) {
495 printk(KERN_INFO "RPC: call_header failed, exit EIO\n");
496 rpc_exit(task, -EIO);
497 } else
498 if (encode && (status = encode(req, p, task->tk_msg.rpc_argp)) < 0) {
499 printk(KERN_WARNING "%s: can't encode arguments: %d\n",
500 clnt->cl_protname, -status);
501 rpc_exit(task, status);
502 }
503 }
504
505 /*
506 * 4. Get the server port number if not yet set
507 */
508 static void
509 call_bind(struct rpc_task *task)
510 {
511 struct rpc_clnt *clnt = task->tk_client;
512 struct rpc_xprt *xprt = clnt->cl_xprt;
513
514 task->tk_action = (xprt_connected(xprt)) ? call_transmit : call_reconnect;
515
516 if (!clnt->cl_port) {
517 task->tk_action = call_reconnect;
518 task->tk_timeout = clnt->cl_timeout.to_maxval;
519 rpc_getport(task, clnt);
520 }
521 }
522
523 /*
524 * 4a. Reconnect to the RPC server (TCP case)
525 */
526 static void
527 call_reconnect(struct rpc_task *task)
528 {
529 struct rpc_clnt *clnt = task->tk_client;
530 struct rpc_task *child;
531
532 dprintk("RPC: %4d call_reconnect status %d\n",
533 task->tk_pid, task->tk_status);
534
535 task->tk_action = call_transmit;
536 if (task->tk_status < 0 || !clnt->cl_xprt->stream)
537 return;
538
539 /* Run as a child to ensure it runs as an rpciod task */
540 child = rpc_new_child(clnt, task);
541 if (child) {
542 child->tk_action = child_reconnect;
543 rpc_run_child(task, child, NULL);
544 }
545 }
546
547 static void child_reconnect(struct rpc_task *task)
548 {
549 task->tk_client->cl_stats->netreconn++;
550 task->tk_status = 0;
551 task->tk_action = child_reconnect_status;
552 xprt_reconnect(task);
553 }
554
555 static void child_reconnect_status(struct rpc_task *task)
556 {
557 if (task->tk_status == -EAGAIN)
558 task->tk_action = child_reconnect;
559 else
560 task->tk_action = NULL;
561 }
562
563 /*
564 * 5. Transmit the RPC request, and wait for reply
565 */
566 static void
567 call_transmit(struct rpc_task *task)
568 {
569 struct rpc_clnt *clnt = task->tk_client;
570
571 dprintk("RPC: %4d call_transmit (status %d)\n",
572 task->tk_pid, task->tk_status);
573
574 task->tk_action = call_status;
575 if (task->tk_status < 0)
576 return;
577 xprt_transmit(task);
578 if (!rpcproc_decode(clnt, task->tk_msg.rpc_proc)) {
579 task->tk_action = NULL;
580 rpc_wake_up_task(task);
581 }
582 }
583
584 /*
585 * 6. Sort out the RPC call status
586 */
587 static void
588 call_status(struct rpc_task *task)
589 {
590 struct rpc_clnt *clnt = task->tk_client;
591 struct rpc_xprt *xprt = clnt->cl_xprt;
592 struct rpc_rqst *req;
593 int status = task->tk_status;
594
595 dprintk("RPC: %4d call_status (status %d)\n",
596 task->tk_pid, task->tk_status);
597
598 if (status >= 0) {
599 task->tk_action = call_decode;
600 return;
601 }
602
603 task->tk_status = 0;
604 req = task->tk_rqstp;
605 switch(status) {
606 case -ETIMEDOUT:
607 task->tk_action = call_timeout;
608 break;
609 case -ECONNREFUSED:
610 case -ENOTCONN:
611 req->rq_bytes_sent = 0;
612 if (clnt->cl_autobind || !clnt->cl_port) {
613 clnt->cl_port = 0;
614 task->tk_action = call_bind;
615 break;
616 }
617 if (xprt->stream) {
618 task->tk_action = call_reconnect;
619 break;
620 }
621 /*
622 * Sleep and dream of an open connection
623 */
624 task->tk_timeout = 5 * HZ;
625 rpc_sleep_on(&xprt->sending, task, NULL, NULL);
626 case -ENOMEM:
627 case -EAGAIN:
628 task->tk_action = call_transmit;
629 clnt->cl_stats->rpcretrans++;
630 break;
631 default:
632 if (clnt->cl_chatty)
633 printk("%s: RPC call returned error %d\n",
634 clnt->cl_protname, -status);
635 rpc_exit(task, status);
636 }
637 }
638
639 /*
640 * 6a. Handle RPC timeout
641 * We do not release the request slot, so we keep using the
642 * same XID for all retransmits.
643 */
644 static void
645 call_timeout(struct rpc_task *task)
646 {
647 struct rpc_clnt *clnt = task->tk_client;
648 struct rpc_rqst *req = task->tk_rqstp;
649
650 if (req) {
651 struct rpc_timeout *to = &req->rq_timeout;
652
653 if (xprt_adjust_timeout(to)) {
654 dprintk("RPC: %4d call_timeout (minor timeo)\n",
655 task->tk_pid);
656 goto minor_timeout;
657 }
658 to->to_retries = clnt->cl_timeout.to_retries;
659 }
660
661 dprintk("RPC: %4d call_timeout (major timeo)\n", task->tk_pid);
662 if (clnt->cl_softrtry) {
663 if (clnt->cl_chatty && !task->tk_exit)
664 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
665 clnt->cl_protname, clnt->cl_server);
666 rpc_exit(task, -EIO);
667 return;
668 }
669 if (clnt->cl_chatty && !(task->tk_flags & RPC_CALL_MAJORSEEN)) {
670 task->tk_flags |= RPC_CALL_MAJORSEEN;
671 if (req)
672 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
673 clnt->cl_protname, clnt->cl_server);
674 #ifdef RPC_DEBUG
675 else
676 printk(KERN_NOTICE "%s: task %d can't get a request slot\n",
677 clnt->cl_protname, task->tk_pid);
678 #endif
679 }
680 if (clnt->cl_autobind)
681 clnt->cl_port = 0;
682
683 minor_timeout:
684 if (!req)
685 task->tk_action = call_reserve;
686 else if (!clnt->cl_port) {
687 task->tk_action = call_bind;
688 clnt->cl_stats->rpcretrans++;
689 } else if (!xprt_connected(clnt->cl_xprt)) {
690 task->tk_action = call_reconnect;
691 clnt->cl_stats->rpcretrans++;
692 } else {
693 task->tk_action = call_transmit;
694 clnt->cl_stats->rpcretrans++;
695 }
696 task->tk_status = 0;
697 }
698
699 /*
700 * 7. Decode the RPC reply
701 */
702 static void
703 call_decode(struct rpc_task *task)
704 {
705 struct rpc_clnt *clnt = task->tk_client;
706 struct rpc_rqst *req = task->tk_rqstp;
707 kxdrproc_t decode = rpcproc_decode(clnt, task->tk_msg.rpc_proc);
708 u32 *p;
709
710 dprintk("RPC: %4d call_decode (status %d)\n",
711 task->tk_pid, task->tk_status);
712
713 if (clnt->cl_chatty && (task->tk_flags & RPC_CALL_MAJORSEEN)) {
714 printk(KERN_NOTICE "%s: server %s OK\n",
715 clnt->cl_protname, clnt->cl_server);
716 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
717 }
718
719 if (task->tk_status < 12) {
720 if (!clnt->cl_softrtry) {
721 task->tk_action = call_transmit;
722 clnt->cl_stats->rpcretrans++;
723 } else {
724 printk(KERN_WARNING "%s: too small RPC reply size (%d bytes)\n",
725 clnt->cl_protname, task->tk_status);
726 rpc_exit(task, -EIO);
727 }
728 return;
729 }
730
731 /* Verify the RPC header */
732 if (!(p = call_verify(task)))
733 return;
734
735 /*
736 * The following is an NFS-specific hack to cater for setuid
737 * processes whose uid is mapped to nobody on the server.
738 */
739 if (task->tk_client->cl_droppriv &&
740 (ntohl(*p) == NFSERR_ACCES || ntohl(*p) == NFSERR_PERM)) {
741 if (RPC_IS_SETUID(task) && task->tk_suid_retry) {
742 dprintk("RPC: %4d retry squashed uid\n", task->tk_pid);
743 task->tk_flags ^= RPC_CALL_REALUID;
744 task->tk_action = call_encode;
745 task->tk_suid_retry--;
746 return;
747 }
748 }
749
750 task->tk_action = NULL;
751
752 if (decode)
753 task->tk_status = decode(req, p, task->tk_msg.rpc_resp);
754 dprintk("RPC: %4d call_decode result %d\n", task->tk_pid,
755 task->tk_status);
756 }
757
758 /*
759 * 8. Refresh the credentials if rejected by the server
760 */
761 static void
762 call_refresh(struct rpc_task *task)
763 {
764 dprintk("RPC: %4d call_refresh\n", task->tk_pid);
765
766 xprt_release(task); /* Must do to obtain new XID */
767 task->tk_action = call_refreshresult;
768 task->tk_status = 0;
769 task->tk_client->cl_stats->rpcauthrefresh++;
770 rpcauth_refreshcred(task);
771 }
772
773 /*
774 * 8a. Process the results of a credential refresh
775 */
776 static void
777 call_refreshresult(struct rpc_task *task)
778 {
779 dprintk("RPC: %4d call_refreshresult (status %d)\n",
780 task->tk_pid, task->tk_status);
781
782 if (task->tk_status < 0)
783 rpc_exit(task, -EACCES);
784 else
785 task->tk_action = call_reserve;
786 }
787
788 /*
789 * Call header serialization
790 */
791 static u32 *
792 call_header(struct rpc_task *task)
793 {
794 struct rpc_clnt *clnt = task->tk_client;
795 struct rpc_xprt *xprt = clnt->cl_xprt;
796 struct rpc_rqst *req = task->tk_rqstp;
797 u32 *p = req->rq_svec[0].iov_base;
798
799 /* FIXME: check buffer size? */
800 if (xprt->stream)
801 *p++ = 0; /* fill in later */
802 *p++ = req->rq_xid; /* XID */
803 *p++ = htonl(RPC_CALL); /* CALL */
804 *p++ = htonl(RPC_VERSION); /* RPC version */
805 *p++ = htonl(clnt->cl_prog); /* program number */
806 *p++ = htonl(clnt->cl_vers); /* program version */
807 *p++ = htonl(task->tk_msg.rpc_proc); /* procedure */
808 return rpcauth_marshcred(task, p);
809 }
810
811 /*
812 * Reply header verification
813 */
814 static u32 *
815 call_verify(struct rpc_task *task)
816 {
817 u32 *p = task->tk_rqstp->rq_rvec[0].iov_base, n;
818
819 p += 1; /* skip XID */
820
821 if ((n = ntohl(*p++)) != RPC_REPLY) {
822 printk(KERN_WARNING "call_verify: not an RPC reply: %x\n", n);
823 goto garbage;
824 }
825 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
826 int error = -EACCES;
827
828 if ((n = ntohl(*p++)) != RPC_AUTH_ERROR) {
829 printk(KERN_WARNING "call_verify: RPC call rejected: %x\n", n);
830 } else
831 switch ((n = ntohl(*p++))) {
832 case RPC_AUTH_REJECTEDCRED:
833 case RPC_AUTH_REJECTEDVERF:
834 if (!task->tk_cred_retry)
835 break;
836 task->tk_cred_retry--;
837 dprintk("RPC: %4d call_verify: retry stale creds\n",
838 task->tk_pid);
839 rpcauth_invalcred(task);
840 task->tk_action = call_refresh;
841 return NULL;
842 case RPC_AUTH_BADCRED:
843 case RPC_AUTH_BADVERF:
844 /* possibly garbled cred/verf? */
845 if (!task->tk_garb_retry)
846 break;
847 task->tk_garb_retry--;
848 dprintk("RPC: %4d call_verify: retry garbled creds\n",
849 task->tk_pid);
850 task->tk_action = call_encode;
851 return NULL;
852 case RPC_AUTH_TOOWEAK:
853 printk(KERN_NOTICE "call_verify: server requires stronger "
854 "authentication.\n");
855 break;
856 default:
857 printk(KERN_WARNING "call_verify: unknown auth error: %x\n", n);
858 error = -EIO;
859 }
860 dprintk("RPC: %4d call_verify: call rejected %d\n",
861 task->tk_pid, n);
862 rpc_exit(task, error);
863 return NULL;
864 }
865 if (!(p = rpcauth_checkverf(task, p))) {
866 printk(KERN_WARNING "call_verify: auth check failed\n");
867 goto garbage; /* bad verifier, retry */
868 }
869 switch ((n = ntohl(*p++))) {
870 case RPC_SUCCESS:
871 return p;
872 case RPC_GARBAGE_ARGS:
873 break; /* retry */
874 default:
875 printk(KERN_WARNING "call_verify: server accept status: %x\n", n);
876 /* Also retry */
877 }
878
879 garbage:
880 dprintk("RPC: %4d call_verify: server saw garbage\n", task->tk_pid);
881 task->tk_client->cl_stats->rpcgarbage++;
882 if (task->tk_garb_retry) {
883 task->tk_garb_retry--;
884 dprintk(KERN_WARNING "RPC: garbage, retrying %4d\n", task->tk_pid);
885 task->tk_action = call_encode;
886 return NULL;
887 }
888 printk(KERN_WARNING "RPC: garbage, exit EIO\n");
889 rpc_exit(task, -EIO);
890 return NULL;
891 }
892