]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
55aa4f58 | 2 | * linux/net/sunrpc/clnt.c |
1da177e4 LT |
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 | * 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 <[email protected]> | |
21 | * Copyright (C) 1995,1996 Olaf Kirch <[email protected]> | |
22 | */ | |
23 | ||
24 | #include <asm/system.h> | |
25 | ||
26 | #include <linux/module.h> | |
27 | #include <linux/types.h> | |
28 | #include <linux/mm.h> | |
29 | #include <linux/slab.h> | |
1da177e4 | 30 | #include <linux/utsname.h> |
11c556b3 | 31 | #include <linux/workqueue.h> |
1da177e4 LT |
32 | |
33 | #include <linux/sunrpc/clnt.h> | |
1da177e4 | 34 | #include <linux/sunrpc/rpc_pipe_fs.h> |
11c556b3 | 35 | #include <linux/sunrpc/metrics.h> |
1da177e4 LT |
36 | |
37 | ||
38 | #define RPC_SLACK_SPACE (1024) /* total overkill */ | |
39 | ||
40 | #ifdef RPC_DEBUG | |
41 | # define RPCDBG_FACILITY RPCDBG_CALL | |
42 | #endif | |
43 | ||
44 | static DECLARE_WAIT_QUEUE_HEAD(destroy_wait); | |
45 | ||
46 | ||
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); | |
da351878 | 54 | static void call_bind_status(struct rpc_task *task); |
1da177e4 LT |
55 | static void call_transmit(struct rpc_task *task); |
56 | static void call_status(struct rpc_task *task); | |
940e3318 | 57 | static void call_transmit_status(struct rpc_task *task); |
1da177e4 LT |
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); | |
65 | ||
66 | ||
67 | static int | |
68 | rpc_setup_pipedir(struct rpc_clnt *clnt, char *dir_name) | |
69 | { | |
f134585a | 70 | static uint32_t clntid; |
1da177e4 LT |
71 | int error; |
72 | ||
54281548 TM |
73 | clnt->cl_vfsmnt = ERR_PTR(-ENOENT); |
74 | clnt->cl_dentry = ERR_PTR(-ENOENT); | |
1da177e4 LT |
75 | if (dir_name == NULL) |
76 | return 0; | |
54281548 TM |
77 | |
78 | clnt->cl_vfsmnt = rpc_get_mount(); | |
79 | if (IS_ERR(clnt->cl_vfsmnt)) | |
80 | return PTR_ERR(clnt->cl_vfsmnt); | |
81 | ||
f134585a TM |
82 | for (;;) { |
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)) | |
89 | return 0; | |
1da177e4 | 90 | error = PTR_ERR(clnt->cl_dentry); |
f134585a TM |
91 | if (error != -EEXIST) { |
92 | printk(KERN_INFO "RPC: Couldn't create pipefs entry %s, error %d\n", | |
93 | clnt->cl_pathname, error); | |
54281548 | 94 | rpc_put_mount(); |
f134585a TM |
95 | return error; |
96 | } | |
1da177e4 LT |
97 | } |
98 | } | |
99 | ||
100 | /* | |
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. | |
106 | */ | |
107 | struct rpc_clnt * | |
5ee0ed7d | 108 | rpc_new_client(struct rpc_xprt *xprt, char *servname, |
1da177e4 LT |
109 | struct rpc_program *program, u32 vers, |
110 | rpc_authflavor_t flavor) | |
111 | { | |
112 | struct rpc_version *version; | |
113 | struct rpc_clnt *clnt = NULL; | |
6a19275a | 114 | struct rpc_auth *auth; |
1da177e4 LT |
115 | int err; |
116 | int len; | |
117 | ||
118 | dprintk("RPC: creating %s client for %s (xprt %p)\n", | |
119 | program->name, servname, xprt); | |
120 | ||
121 | err = -EINVAL; | |
122 | if (!xprt) | |
712917d1 | 123 | goto out_no_xprt; |
1da177e4 LT |
124 | if (vers >= program->nrvers || !(version = program->version[vers])) |
125 | goto out_err; | |
126 | ||
127 | err = -ENOMEM; | |
0da974f4 | 128 | clnt = kzalloc(sizeof(*clnt), GFP_KERNEL); |
1da177e4 LT |
129 | if (!clnt) |
130 | goto out_err; | |
1da177e4 LT |
131 | atomic_set(&clnt->cl_users, 0); |
132 | atomic_set(&clnt->cl_count, 1); | |
133 | clnt->cl_parent = clnt; | |
134 | ||
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); | |
139 | if (buf != 0) | |
140 | clnt->cl_server = buf; | |
141 | else | |
142 | len = sizeof(clnt->cl_inline_name); | |
143 | } | |
144 | strlcpy(clnt->cl_server, servname, len); | |
145 | ||
146 | clnt->cl_xprt = xprt; | |
147 | clnt->cl_procinfo = version->procs; | |
148 | clnt->cl_maxproc = version->nrprocs; | |
149 | clnt->cl_protname = program->name; | |
1da177e4 LT |
150 | clnt->cl_prog = program->number; |
151 | clnt->cl_vers = version->number; | |
1da177e4 | 152 | clnt->cl_stats = program->stats; |
11c556b3 | 153 | clnt->cl_metrics = rpc_alloc_iostats(clnt); |
1da177e4 | 154 | |
ec739ef0 | 155 | if (!xprt_bound(clnt->cl_xprt)) |
1da177e4 LT |
156 | clnt->cl_autobind = 1; |
157 | ||
158 | clnt->cl_rtt = &clnt->cl_rtt_default; | |
159 | rpc_init_rtt(&clnt->cl_rtt_default, xprt->timeout.to_initval); | |
160 | ||
161 | err = rpc_setup_pipedir(clnt, program->pipe_dir_name); | |
162 | if (err < 0) | |
163 | goto out_no_path; | |
164 | ||
6a19275a BF |
165 | auth = rpcauth_create(flavor, clnt); |
166 | if (IS_ERR(auth)) { | |
1da177e4 LT |
167 | printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %u)\n", |
168 | flavor); | |
6a19275a | 169 | err = PTR_ERR(auth); |
1da177e4 LT |
170 | goto out_no_auth; |
171 | } | |
172 | ||
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); | |
178 | return clnt; | |
179 | ||
180 | out_no_auth: | |
54281548 | 181 | if (!IS_ERR(clnt->cl_dentry)) { |
dff02cc1 | 182 | rpc_rmdir(clnt->cl_dentry); |
54281548 TM |
183 | rpc_put_mount(); |
184 | } | |
1da177e4 LT |
185 | out_no_path: |
186 | if (clnt->cl_server != clnt->cl_inline_name) | |
187 | kfree(clnt->cl_server); | |
188 | kfree(clnt); | |
189 | out_err: | |
5b616f5d | 190 | xprt_destroy(xprt); |
712917d1 | 191 | out_no_xprt: |
1da177e4 LT |
192 | return ERR_PTR(err); |
193 | } | |
194 | ||
5ee0ed7d TM |
195 | /** |
196 | * Create an RPC client | |
197 | * @xprt - pointer to xprt struct | |
198 | * @servname - name of server | |
199 | * @info - rpc_program | |
200 | * @version - rpc_program version | |
201 | * @authflavor - rpc_auth flavour to use | |
202 | * | |
203 | * Creates an RPC client structure, then pings the server in order to | |
204 | * determine if it is up, and if it supports this program and version. | |
205 | * | |
206 | * This function should never be called by asynchronous tasks such as | |
207 | * the portmapper. | |
208 | */ | |
209 | struct rpc_clnt *rpc_create_client(struct rpc_xprt *xprt, char *servname, | |
210 | struct rpc_program *info, u32 version, rpc_authflavor_t authflavor) | |
211 | { | |
212 | struct rpc_clnt *clnt; | |
213 | int err; | |
214 | ||
215 | clnt = rpc_new_client(xprt, servname, info, version, authflavor); | |
216 | if (IS_ERR(clnt)) | |
217 | return clnt; | |
218 | err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR); | |
219 | if (err == 0) | |
220 | return clnt; | |
221 | rpc_shutdown_client(clnt); | |
222 | return ERR_PTR(err); | |
223 | } | |
224 | ||
1da177e4 LT |
225 | /* |
226 | * This function clones the RPC client structure. It allows us to share the | |
227 | * same transport while varying parameters such as the authentication | |
228 | * flavour. | |
229 | */ | |
230 | struct rpc_clnt * | |
231 | rpc_clone_client(struct rpc_clnt *clnt) | |
232 | { | |
233 | struct rpc_clnt *new; | |
234 | ||
8b3a7005 | 235 | new = kmalloc(sizeof(*new), GFP_KERNEL); |
1da177e4 LT |
236 | if (!new) |
237 | goto out_no_clnt; | |
238 | memcpy(new, clnt, sizeof(*new)); | |
239 | atomic_set(&new->cl_count, 1); | |
240 | atomic_set(&new->cl_users, 0); | |
241 | new->cl_parent = clnt; | |
242 | atomic_inc(&clnt->cl_count); | |
1da177e4 LT |
243 | /* Turn off autobind on clones */ |
244 | new->cl_autobind = 0; | |
245 | new->cl_oneshot = 0; | |
246 | new->cl_dead = 0; | |
8f8e7a50 | 247 | if (!IS_ERR(new->cl_dentry)) |
54281548 | 248 | dget(new->cl_dentry); |
1da177e4 LT |
249 | rpc_init_rtt(&new->cl_rtt_default, clnt->cl_xprt->timeout.to_initval); |
250 | if (new->cl_auth) | |
251 | atomic_inc(&new->cl_auth->au_count); | |
4a68179d | 252 | new->cl_metrics = rpc_alloc_iostats(clnt); |
1da177e4 LT |
253 | return new; |
254 | out_no_clnt: | |
255 | printk(KERN_INFO "RPC: out of memory in %s\n", __FUNCTION__); | |
256 | return ERR_PTR(-ENOMEM); | |
257 | } | |
258 | ||
259 | /* | |
260 | * Properly shut down an RPC client, terminating all outstanding | |
261 | * requests. Note that we must be certain that cl_oneshot and | |
262 | * cl_dead are cleared, or else the client would be destroyed | |
263 | * when the last task releases it. | |
264 | */ | |
265 | int | |
266 | rpc_shutdown_client(struct rpc_clnt *clnt) | |
267 | { | |
268 | dprintk("RPC: shutting down %s client for %s, tasks=%d\n", | |
269 | clnt->cl_protname, clnt->cl_server, | |
270 | atomic_read(&clnt->cl_users)); | |
271 | ||
272 | while (atomic_read(&clnt->cl_users) > 0) { | |
273 | /* Don't let rpc_release_client destroy us */ | |
274 | clnt->cl_oneshot = 0; | |
275 | clnt->cl_dead = 0; | |
276 | rpc_killall_tasks(clnt); | |
532347e2 | 277 | wait_event_timeout(destroy_wait, |
4f47707b | 278 | !atomic_read(&clnt->cl_users), 1*HZ); |
1da177e4 LT |
279 | } |
280 | ||
281 | if (atomic_read(&clnt->cl_users) < 0) { | |
282 | printk(KERN_ERR "RPC: rpc_shutdown_client clnt %p tasks=%d\n", | |
283 | clnt, atomic_read(&clnt->cl_users)); | |
284 | #ifdef RPC_DEBUG | |
285 | rpc_show_tasks(); | |
286 | #endif | |
287 | BUG(); | |
288 | } | |
289 | ||
290 | return rpc_destroy_client(clnt); | |
291 | } | |
292 | ||
293 | /* | |
294 | * Delete an RPC client | |
295 | */ | |
296 | int | |
297 | rpc_destroy_client(struct rpc_clnt *clnt) | |
298 | { | |
299 | if (!atomic_dec_and_test(&clnt->cl_count)) | |
300 | return 1; | |
301 | BUG_ON(atomic_read(&clnt->cl_users) != 0); | |
302 | ||
303 | dprintk("RPC: destroying %s client for %s\n", | |
304 | clnt->cl_protname, clnt->cl_server); | |
305 | if (clnt->cl_auth) { | |
306 | rpcauth_destroy(clnt->cl_auth); | |
307 | clnt->cl_auth = NULL; | |
308 | } | |
309 | if (clnt->cl_parent != clnt) { | |
8f8e7a50 TM |
310 | if (!IS_ERR(clnt->cl_dentry)) |
311 | dput(clnt->cl_dentry); | |
1da177e4 LT |
312 | rpc_destroy_client(clnt->cl_parent); |
313 | goto out_free; | |
314 | } | |
8f8e7a50 | 315 | if (!IS_ERR(clnt->cl_dentry)) { |
dff02cc1 | 316 | rpc_rmdir(clnt->cl_dentry); |
8f8e7a50 TM |
317 | rpc_put_mount(); |
318 | } | |
1da177e4 LT |
319 | if (clnt->cl_xprt) { |
320 | xprt_destroy(clnt->cl_xprt); | |
321 | clnt->cl_xprt = NULL; | |
322 | } | |
323 | if (clnt->cl_server != clnt->cl_inline_name) | |
324 | kfree(clnt->cl_server); | |
325 | out_free: | |
11c556b3 CL |
326 | rpc_free_iostats(clnt->cl_metrics); |
327 | clnt->cl_metrics = NULL; | |
1da177e4 LT |
328 | kfree(clnt); |
329 | return 0; | |
330 | } | |
331 | ||
332 | /* | |
333 | * Release an RPC client | |
334 | */ | |
335 | void | |
336 | rpc_release_client(struct rpc_clnt *clnt) | |
337 | { | |
338 | dprintk("RPC: rpc_release_client(%p, %d)\n", | |
339 | clnt, atomic_read(&clnt->cl_users)); | |
340 | ||
341 | if (!atomic_dec_and_test(&clnt->cl_users)) | |
342 | return; | |
343 | wake_up(&destroy_wait); | |
344 | if (clnt->cl_oneshot || clnt->cl_dead) | |
345 | rpc_destroy_client(clnt); | |
346 | } | |
347 | ||
007e251f AG |
348 | /** |
349 | * rpc_bind_new_program - bind a new RPC program to an existing client | |
350 | * @old - old rpc_client | |
351 | * @program - rpc program to set | |
352 | * @vers - rpc program version | |
353 | * | |
354 | * Clones the rpc client and sets up a new RPC program. This is mainly | |
355 | * of use for enabling different RPC programs to share the same transport. | |
356 | * The Sun NFSv2/v3 ACL protocol can do this. | |
357 | */ | |
358 | struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old, | |
359 | struct rpc_program *program, | |
360 | int vers) | |
361 | { | |
362 | struct rpc_clnt *clnt; | |
363 | struct rpc_version *version; | |
364 | int err; | |
365 | ||
366 | BUG_ON(vers >= program->nrvers || !program->version[vers]); | |
367 | version = program->version[vers]; | |
368 | clnt = rpc_clone_client(old); | |
369 | if (IS_ERR(clnt)) | |
370 | goto out; | |
371 | clnt->cl_procinfo = version->procs; | |
372 | clnt->cl_maxproc = version->nrprocs; | |
373 | clnt->cl_protname = program->name; | |
374 | clnt->cl_prog = program->number; | |
375 | clnt->cl_vers = version->number; | |
376 | clnt->cl_stats = program->stats; | |
377 | err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR); | |
378 | if (err != 0) { | |
379 | rpc_shutdown_client(clnt); | |
380 | clnt = ERR_PTR(err); | |
381 | } | |
382 | out: | |
383 | return clnt; | |
384 | } | |
385 | ||
1da177e4 LT |
386 | /* |
387 | * Default callback for async RPC calls | |
388 | */ | |
389 | static void | |
963d8fe5 | 390 | rpc_default_callback(struct rpc_task *task, void *data) |
1da177e4 LT |
391 | { |
392 | } | |
393 | ||
963d8fe5 TM |
394 | static const struct rpc_call_ops rpc_default_ops = { |
395 | .rpc_call_done = rpc_default_callback, | |
396 | }; | |
397 | ||
1da177e4 | 398 | /* |
14b218a8 | 399 | * Export the signal mask handling for synchronous code that |
1da177e4 LT |
400 | * sleeps on RPC calls |
401 | */ | |
2bd61579 | 402 | #define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM)) |
1da177e4 | 403 | |
14b218a8 TM |
404 | static void rpc_save_sigmask(sigset_t *oldset, int intr) |
405 | { | |
2bd61579 | 406 | unsigned long sigallow = sigmask(SIGKILL); |
14b218a8 TM |
407 | sigset_t sigmask; |
408 | ||
409 | /* Block all signals except those listed in sigallow */ | |
410 | if (intr) | |
411 | sigallow |= RPC_INTR_SIGNALS; | |
412 | siginitsetinv(&sigmask, sigallow); | |
413 | sigprocmask(SIG_BLOCK, &sigmask, oldset); | |
414 | } | |
415 | ||
416 | static inline void rpc_task_sigmask(struct rpc_task *task, sigset_t *oldset) | |
417 | { | |
418 | rpc_save_sigmask(oldset, !RPC_TASK_UNINTERRUPTIBLE(task)); | |
419 | } | |
420 | ||
421 | static inline void rpc_restore_sigmask(sigset_t *oldset) | |
422 | { | |
423 | sigprocmask(SIG_SETMASK, oldset, NULL); | |
424 | } | |
425 | ||
1da177e4 LT |
426 | void rpc_clnt_sigmask(struct rpc_clnt *clnt, sigset_t *oldset) |
427 | { | |
14b218a8 | 428 | rpc_save_sigmask(oldset, clnt->cl_intr); |
1da177e4 LT |
429 | } |
430 | ||
431 | void rpc_clnt_sigunmask(struct rpc_clnt *clnt, sigset_t *oldset) | |
432 | { | |
14b218a8 | 433 | rpc_restore_sigmask(oldset); |
1da177e4 LT |
434 | } |
435 | ||
436 | /* | |
437 | * New rpc_call implementation | |
438 | */ | |
439 | int rpc_call_sync(struct rpc_clnt *clnt, struct rpc_message *msg, int flags) | |
440 | { | |
441 | struct rpc_task *task; | |
442 | sigset_t oldset; | |
443 | int status; | |
444 | ||
445 | /* If this client is slain all further I/O fails */ | |
446 | if (clnt->cl_dead) | |
447 | return -EIO; | |
448 | ||
449 | BUG_ON(flags & RPC_TASK_ASYNC); | |
450 | ||
1da177e4 | 451 | status = -ENOMEM; |
963d8fe5 | 452 | task = rpc_new_task(clnt, flags, &rpc_default_ops, NULL); |
1da177e4 LT |
453 | if (task == NULL) |
454 | goto out; | |
455 | ||
14b218a8 TM |
456 | /* Mask signals on RPC calls _and_ GSS_AUTH upcalls */ |
457 | rpc_task_sigmask(task, &oldset); | |
458 | ||
1da177e4 LT |
459 | rpc_call_setup(task, msg, 0); |
460 | ||
461 | /* Set up the call info struct and execute the task */ | |
e60859ac TM |
462 | status = task->tk_status; |
463 | if (status == 0) { | |
464 | atomic_inc(&task->tk_count); | |
1da177e4 | 465 | status = rpc_execute(task); |
e60859ac TM |
466 | if (status == 0) |
467 | status = task->tk_status; | |
1da177e4 | 468 | } |
14b218a8 | 469 | rpc_restore_sigmask(&oldset); |
e60859ac | 470 | rpc_release_task(task); |
1da177e4 | 471 | out: |
1da177e4 LT |
472 | return status; |
473 | } | |
474 | ||
475 | /* | |
476 | * New rpc_call implementation | |
477 | */ | |
478 | int | |
479 | rpc_call_async(struct rpc_clnt *clnt, struct rpc_message *msg, int flags, | |
963d8fe5 | 480 | const struct rpc_call_ops *tk_ops, void *data) |
1da177e4 LT |
481 | { |
482 | struct rpc_task *task; | |
483 | sigset_t oldset; | |
484 | int status; | |
485 | ||
486 | /* If this client is slain all further I/O fails */ | |
7a1218a2 | 487 | status = -EIO; |
1da177e4 | 488 | if (clnt->cl_dead) |
7a1218a2 | 489 | goto out_release; |
1da177e4 LT |
490 | |
491 | flags |= RPC_TASK_ASYNC; | |
492 | ||
1da177e4 | 493 | /* Create/initialize a new RPC task */ |
1da177e4 | 494 | status = -ENOMEM; |
963d8fe5 | 495 | if (!(task = rpc_new_task(clnt, flags, tk_ops, data))) |
7a1218a2 | 496 | goto out_release; |
1da177e4 | 497 | |
14b218a8 TM |
498 | /* Mask signals on GSS_AUTH upcalls */ |
499 | rpc_task_sigmask(task, &oldset); | |
500 | ||
1da177e4 LT |
501 | rpc_call_setup(task, msg, 0); |
502 | ||
503 | /* Set up the call info struct and execute the task */ | |
504 | status = task->tk_status; | |
505 | if (status == 0) | |
506 | rpc_execute(task); | |
507 | else | |
508 | rpc_release_task(task); | |
509 | ||
14b218a8 | 510 | rpc_restore_sigmask(&oldset); |
7a1218a2 TM |
511 | return status; |
512 | out_release: | |
513 | if (tk_ops->rpc_release != NULL) | |
514 | tk_ops->rpc_release(data); | |
1da177e4 LT |
515 | return status; |
516 | } | |
517 | ||
518 | ||
519 | void | |
520 | rpc_call_setup(struct rpc_task *task, struct rpc_message *msg, int flags) | |
521 | { | |
522 | task->tk_msg = *msg; | |
523 | task->tk_flags |= flags; | |
524 | /* Bind the user cred */ | |
525 | if (task->tk_msg.rpc_cred != NULL) | |
526 | rpcauth_holdcred(task); | |
527 | else | |
528 | rpcauth_bindcred(task); | |
529 | ||
530 | if (task->tk_status == 0) | |
531 | task->tk_action = call_start; | |
532 | else | |
abbcf28f | 533 | task->tk_action = rpc_exit_task; |
1da177e4 LT |
534 | } |
535 | ||
ed39440a CL |
536 | /** |
537 | * rpc_peeraddr - extract remote peer address from clnt's xprt | |
538 | * @clnt: RPC client structure | |
539 | * @buf: target buffer | |
540 | * @size: length of target buffer | |
541 | * | |
542 | * Returns the number of bytes that are actually in the stored address. | |
543 | */ | |
544 | size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize) | |
545 | { | |
546 | size_t bytes; | |
547 | struct rpc_xprt *xprt = clnt->cl_xprt; | |
548 | ||
549 | bytes = sizeof(xprt->addr); | |
550 | if (bytes > bufsize) | |
551 | bytes = bufsize; | |
552 | memcpy(buf, &clnt->cl_xprt->addr, bytes); | |
553 | return sizeof(xprt->addr); | |
554 | } | |
555 | EXPORT_SYMBOL(rpc_peeraddr); | |
556 | ||
1da177e4 LT |
557 | void |
558 | rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize) | |
559 | { | |
560 | struct rpc_xprt *xprt = clnt->cl_xprt; | |
470056c2 CL |
561 | if (xprt->ops->set_buffer_size) |
562 | xprt->ops->set_buffer_size(xprt, sndsize, rcvsize); | |
1da177e4 LT |
563 | } |
564 | ||
565 | /* | |
566 | * Return size of largest payload RPC client can support, in bytes | |
567 | * | |
568 | * For stream transports, this is one RPC record fragment (see RFC | |
569 | * 1831), as we don't support multi-record requests yet. For datagram | |
570 | * transports, this is the size of an IP packet minus the IP, UDP, and | |
571 | * RPC header sizes. | |
572 | */ | |
573 | size_t rpc_max_payload(struct rpc_clnt *clnt) | |
574 | { | |
575 | return clnt->cl_xprt->max_payload; | |
576 | } | |
577 | EXPORT_SYMBOL(rpc_max_payload); | |
578 | ||
35f5a422 CL |
579 | /** |
580 | * rpc_force_rebind - force transport to check that remote port is unchanged | |
581 | * @clnt: client to rebind | |
582 | * | |
583 | */ | |
584 | void rpc_force_rebind(struct rpc_clnt *clnt) | |
585 | { | |
586 | if (clnt->cl_autobind) | |
ec739ef0 | 587 | xprt_clear_bound(clnt->cl_xprt); |
35f5a422 CL |
588 | } |
589 | EXPORT_SYMBOL(rpc_force_rebind); | |
590 | ||
1da177e4 LT |
591 | /* |
592 | * Restart an (async) RPC call. Usually called from within the | |
593 | * exit handler. | |
594 | */ | |
595 | void | |
596 | rpc_restart_call(struct rpc_task *task) | |
597 | { | |
598 | if (RPC_ASSASSINATED(task)) | |
599 | return; | |
600 | ||
601 | task->tk_action = call_start; | |
602 | } | |
603 | ||
604 | /* | |
605 | * 0. Initial state | |
606 | * | |
607 | * Other FSM states can be visited zero or more times, but | |
608 | * this state is visited exactly once for each RPC. | |
609 | */ | |
610 | static void | |
611 | call_start(struct rpc_task *task) | |
612 | { | |
613 | struct rpc_clnt *clnt = task->tk_client; | |
614 | ||
615 | dprintk("RPC: %4d call_start %s%d proc %d (%s)\n", task->tk_pid, | |
616 | clnt->cl_protname, clnt->cl_vers, task->tk_msg.rpc_proc->p_proc, | |
617 | (RPC_IS_ASYNC(task) ? "async" : "sync")); | |
618 | ||
619 | /* Increment call count */ | |
620 | task->tk_msg.rpc_proc->p_count++; | |
621 | clnt->cl_stats->rpccnt++; | |
622 | task->tk_action = call_reserve; | |
623 | } | |
624 | ||
625 | /* | |
626 | * 1. Reserve an RPC call slot | |
627 | */ | |
628 | static void | |
629 | call_reserve(struct rpc_task *task) | |
630 | { | |
631 | dprintk("RPC: %4d call_reserve\n", task->tk_pid); | |
632 | ||
633 | if (!rpcauth_uptodatecred(task)) { | |
634 | task->tk_action = call_refresh; | |
635 | return; | |
636 | } | |
637 | ||
638 | task->tk_status = 0; | |
639 | task->tk_action = call_reserveresult; | |
640 | xprt_reserve(task); | |
641 | } | |
642 | ||
643 | /* | |
644 | * 1b. Grok the result of xprt_reserve() | |
645 | */ | |
646 | static void | |
647 | call_reserveresult(struct rpc_task *task) | |
648 | { | |
649 | int status = task->tk_status; | |
650 | ||
651 | dprintk("RPC: %4d call_reserveresult (status %d)\n", | |
652 | task->tk_pid, task->tk_status); | |
653 | ||
654 | /* | |
655 | * After a call to xprt_reserve(), we must have either | |
656 | * a request slot or else an error status. | |
657 | */ | |
658 | task->tk_status = 0; | |
659 | if (status >= 0) { | |
660 | if (task->tk_rqstp) { | |
661 | task->tk_action = call_allocate; | |
662 | return; | |
663 | } | |
664 | ||
665 | printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n", | |
666 | __FUNCTION__, status); | |
667 | rpc_exit(task, -EIO); | |
668 | return; | |
669 | } | |
670 | ||
671 | /* | |
672 | * Even though there was an error, we may have acquired | |
673 | * a request slot somehow. Make sure not to leak it. | |
674 | */ | |
675 | if (task->tk_rqstp) { | |
676 | printk(KERN_ERR "%s: status=%d, request allocated anyway\n", | |
677 | __FUNCTION__, status); | |
678 | xprt_release(task); | |
679 | } | |
680 | ||
681 | switch (status) { | |
682 | case -EAGAIN: /* woken up; retry */ | |
683 | task->tk_action = call_reserve; | |
684 | return; | |
685 | case -EIO: /* probably a shutdown */ | |
686 | break; | |
687 | default: | |
688 | printk(KERN_ERR "%s: unrecognized error %d, exiting\n", | |
689 | __FUNCTION__, status); | |
690 | break; | |
691 | } | |
692 | rpc_exit(task, status); | |
693 | } | |
694 | ||
695 | /* | |
696 | * 2. Allocate the buffer. For details, see sched.c:rpc_malloc. | |
02107148 | 697 | * (Note: buffer memory is freed in xprt_release). |
1da177e4 LT |
698 | */ |
699 | static void | |
700 | call_allocate(struct rpc_task *task) | |
701 | { | |
02107148 CL |
702 | struct rpc_rqst *req = task->tk_rqstp; |
703 | struct rpc_xprt *xprt = task->tk_xprt; | |
1da177e4 LT |
704 | unsigned int bufsiz; |
705 | ||
706 | dprintk("RPC: %4d call_allocate (status %d)\n", | |
707 | task->tk_pid, task->tk_status); | |
708 | task->tk_action = call_bind; | |
02107148 | 709 | if (req->rq_buffer) |
1da177e4 LT |
710 | return; |
711 | ||
712 | /* FIXME: compute buffer requirements more exactly using | |
713 | * auth->au_wslack */ | |
714 | bufsiz = task->tk_msg.rpc_proc->p_bufsiz + RPC_SLACK_SPACE; | |
715 | ||
02107148 | 716 | if (xprt->ops->buf_alloc(task, bufsiz << 1) != NULL) |
1da177e4 LT |
717 | return; |
718 | printk(KERN_INFO "RPC: buffer allocation failed for task %p\n", task); | |
719 | ||
14b218a8 | 720 | if (RPC_IS_ASYNC(task) || !signalled()) { |
1da177e4 LT |
721 | xprt_release(task); |
722 | task->tk_action = call_reserve; | |
723 | rpc_delay(task, HZ>>4); | |
724 | return; | |
725 | } | |
726 | ||
727 | rpc_exit(task, -ERESTARTSYS); | |
728 | } | |
729 | ||
940e3318 TM |
730 | static inline int |
731 | rpc_task_need_encode(struct rpc_task *task) | |
732 | { | |
733 | return task->tk_rqstp->rq_snd_buf.len == 0; | |
734 | } | |
735 | ||
736 | static inline void | |
737 | rpc_task_force_reencode(struct rpc_task *task) | |
738 | { | |
739 | task->tk_rqstp->rq_snd_buf.len = 0; | |
740 | } | |
741 | ||
1da177e4 LT |
742 | /* |
743 | * 3. Encode arguments of an RPC call | |
744 | */ | |
745 | static void | |
746 | call_encode(struct rpc_task *task) | |
747 | { | |
1da177e4 LT |
748 | struct rpc_rqst *req = task->tk_rqstp; |
749 | struct xdr_buf *sndbuf = &req->rq_snd_buf; | |
750 | struct xdr_buf *rcvbuf = &req->rq_rcv_buf; | |
751 | unsigned int bufsiz; | |
752 | kxdrproc_t encode; | |
1da177e4 LT |
753 | u32 *p; |
754 | ||
755 | dprintk("RPC: %4d call_encode (status %d)\n", | |
756 | task->tk_pid, task->tk_status); | |
757 | ||
758 | /* Default buffer setup */ | |
02107148 CL |
759 | bufsiz = req->rq_bufsize >> 1; |
760 | sndbuf->head[0].iov_base = (void *)req->rq_buffer; | |
1da177e4 LT |
761 | sndbuf->head[0].iov_len = bufsiz; |
762 | sndbuf->tail[0].iov_len = 0; | |
763 | sndbuf->page_len = 0; | |
764 | sndbuf->len = 0; | |
765 | sndbuf->buflen = bufsiz; | |
02107148 | 766 | rcvbuf->head[0].iov_base = (void *)((char *)req->rq_buffer + bufsiz); |
1da177e4 LT |
767 | rcvbuf->head[0].iov_len = bufsiz; |
768 | rcvbuf->tail[0].iov_len = 0; | |
769 | rcvbuf->page_len = 0; | |
770 | rcvbuf->len = 0; | |
771 | rcvbuf->buflen = bufsiz; | |
772 | ||
773 | /* Encode header and provided arguments */ | |
774 | encode = task->tk_msg.rpc_proc->p_encode; | |
775 | if (!(p = call_header(task))) { | |
776 | printk(KERN_INFO "RPC: call_header failed, exit EIO\n"); | |
777 | rpc_exit(task, -EIO); | |
778 | return; | |
779 | } | |
f3680312 BF |
780 | if (encode == NULL) |
781 | return; | |
782 | ||
783 | task->tk_status = rpcauth_wrap_req(task, encode, req, p, | |
784 | task->tk_msg.rpc_argp); | |
785 | if (task->tk_status == -ENOMEM) { | |
786 | /* XXX: Is this sane? */ | |
787 | rpc_delay(task, 3*HZ); | |
788 | task->tk_status = -EAGAIN; | |
789 | } | |
1da177e4 LT |
790 | } |
791 | ||
792 | /* | |
793 | * 4. Get the server port number if not yet set | |
794 | */ | |
795 | static void | |
796 | call_bind(struct rpc_task *task) | |
797 | { | |
ec739ef0 | 798 | struct rpc_xprt *xprt = task->tk_xprt; |
1da177e4 | 799 | |
da351878 CL |
800 | dprintk("RPC: %4d call_bind (status %d)\n", |
801 | task->tk_pid, task->tk_status); | |
1da177e4 | 802 | |
da351878 | 803 | task->tk_action = call_connect; |
ec739ef0 | 804 | if (!xprt_bound(xprt)) { |
da351878 | 805 | task->tk_action = call_bind_status; |
ec739ef0 | 806 | task->tk_timeout = xprt->bind_timeout; |
bbf7c1dd | 807 | xprt->ops->rpcbind(task); |
1da177e4 LT |
808 | } |
809 | } | |
810 | ||
811 | /* | |
da351878 CL |
812 | * 4a. Sort out bind result |
813 | */ | |
814 | static void | |
815 | call_bind_status(struct rpc_task *task) | |
816 | { | |
817 | int status = -EACCES; | |
818 | ||
819 | if (task->tk_status >= 0) { | |
820 | dprintk("RPC: %4d call_bind_status (status %d)\n", | |
821 | task->tk_pid, task->tk_status); | |
822 | task->tk_status = 0; | |
823 | task->tk_action = call_connect; | |
824 | return; | |
825 | } | |
826 | ||
827 | switch (task->tk_status) { | |
828 | case -EACCES: | |
829 | dprintk("RPC: %4d remote rpcbind: RPC program/version unavailable\n", | |
830 | task->tk_pid); | |
ea635a51 CL |
831 | rpc_delay(task, 3*HZ); |
832 | goto retry_bind; | |
da351878 CL |
833 | case -ETIMEDOUT: |
834 | dprintk("RPC: %4d rpcbind request timed out\n", | |
835 | task->tk_pid); | |
836 | if (RPC_IS_SOFT(task)) { | |
837 | status = -EIO; | |
838 | break; | |
839 | } | |
840 | goto retry_bind; | |
841 | case -EPFNOSUPPORT: | |
842 | dprintk("RPC: %4d remote rpcbind service unavailable\n", | |
843 | task->tk_pid); | |
844 | break; | |
845 | case -EPROTONOSUPPORT: | |
846 | dprintk("RPC: %4d remote rpcbind version 2 unavailable\n", | |
847 | task->tk_pid); | |
848 | break; | |
849 | default: | |
850 | dprintk("RPC: %4d unrecognized rpcbind error (%d)\n", | |
851 | task->tk_pid, -task->tk_status); | |
852 | status = -EIO; | |
853 | break; | |
854 | } | |
855 | ||
856 | rpc_exit(task, status); | |
857 | return; | |
858 | ||
859 | retry_bind: | |
860 | task->tk_status = 0; | |
861 | task->tk_action = call_bind; | |
862 | return; | |
863 | } | |
864 | ||
865 | /* | |
866 | * 4b. Connect to the RPC server | |
1da177e4 LT |
867 | */ |
868 | static void | |
869 | call_connect(struct rpc_task *task) | |
870 | { | |
da351878 | 871 | struct rpc_xprt *xprt = task->tk_xprt; |
1da177e4 | 872 | |
da351878 CL |
873 | dprintk("RPC: %4d call_connect xprt %p %s connected\n", |
874 | task->tk_pid, xprt, | |
875 | (xprt_connected(xprt) ? "is" : "is not")); | |
1da177e4 | 876 | |
da351878 CL |
877 | task->tk_action = call_transmit; |
878 | if (!xprt_connected(xprt)) { | |
879 | task->tk_action = call_connect_status; | |
880 | if (task->tk_status < 0) | |
881 | return; | |
882 | xprt_connect(task); | |
1da177e4 | 883 | } |
1da177e4 LT |
884 | } |
885 | ||
886 | /* | |
da351878 | 887 | * 4c. Sort out connect result |
1da177e4 LT |
888 | */ |
889 | static void | |
890 | call_connect_status(struct rpc_task *task) | |
891 | { | |
892 | struct rpc_clnt *clnt = task->tk_client; | |
893 | int status = task->tk_status; | |
894 | ||
da351878 CL |
895 | dprintk("RPC: %5u call_connect_status (status %d)\n", |
896 | task->tk_pid, task->tk_status); | |
897 | ||
1da177e4 LT |
898 | task->tk_status = 0; |
899 | if (status >= 0) { | |
900 | clnt->cl_stats->netreconn++; | |
901 | task->tk_action = call_transmit; | |
902 | return; | |
903 | } | |
904 | ||
da351878 | 905 | /* Something failed: remote service port may have changed */ |
35f5a422 | 906 | rpc_force_rebind(clnt); |
da351878 | 907 | |
1da177e4 LT |
908 | switch (status) { |
909 | case -ENOTCONN: | |
910 | case -ETIMEDOUT: | |
911 | case -EAGAIN: | |
da351878 | 912 | task->tk_action = call_bind; |
1da177e4 LT |
913 | break; |
914 | default: | |
915 | rpc_exit(task, -EIO); | |
da351878 | 916 | break; |
1da177e4 LT |
917 | } |
918 | } | |
919 | ||
920 | /* | |
921 | * 5. Transmit the RPC request, and wait for reply | |
922 | */ | |
923 | static void | |
924 | call_transmit(struct rpc_task *task) | |
925 | { | |
926 | dprintk("RPC: %4d call_transmit (status %d)\n", | |
927 | task->tk_pid, task->tk_status); | |
928 | ||
929 | task->tk_action = call_status; | |
930 | if (task->tk_status < 0) | |
931 | return; | |
932 | task->tk_status = xprt_prepare_transmit(task); | |
933 | if (task->tk_status != 0) | |
934 | return; | |
e0ab53de | 935 | task->tk_action = call_transmit_status; |
1da177e4 | 936 | /* Encode here so that rpcsec_gss can use correct sequence number. */ |
940e3318 | 937 | if (rpc_task_need_encode(task)) { |
e0ab53de | 938 | BUG_ON(task->tk_rqstp->rq_bytes_sent != 0); |
1da177e4 | 939 | call_encode(task); |
5e5ce5be TM |
940 | /* Did the encode result in an error condition? */ |
941 | if (task->tk_status != 0) | |
e0ab53de | 942 | return; |
5e5ce5be | 943 | } |
1da177e4 LT |
944 | xprt_transmit(task); |
945 | if (task->tk_status < 0) | |
946 | return; | |
e0ab53de TM |
947 | /* |
948 | * On success, ensure that we call xprt_end_transmit() before sleeping | |
949 | * in order to allow access to the socket to other RPC requests. | |
950 | */ | |
951 | call_transmit_status(task); | |
952 | if (task->tk_msg.rpc_proc->p_decode != NULL) | |
953 | return; | |
954 | task->tk_action = rpc_exit_task; | |
955 | rpc_wake_up_task(task); | |
956 | } | |
957 | ||
958 | /* | |
959 | * 5a. Handle cleanup after a transmission | |
960 | */ | |
961 | static void | |
962 | call_transmit_status(struct rpc_task *task) | |
963 | { | |
964 | task->tk_action = call_status; | |
965 | /* | |
966 | * Special case: if we've been waiting on the socket's write_space() | |
967 | * callback, then don't call xprt_end_transmit(). | |
968 | */ | |
969 | if (task->tk_status == -EAGAIN) | |
970 | return; | |
971 | xprt_end_transmit(task); | |
940e3318 | 972 | rpc_task_force_reencode(task); |
1da177e4 LT |
973 | } |
974 | ||
975 | /* | |
976 | * 6. Sort out the RPC call status | |
977 | */ | |
978 | static void | |
979 | call_status(struct rpc_task *task) | |
980 | { | |
981 | struct rpc_clnt *clnt = task->tk_client; | |
982 | struct rpc_rqst *req = task->tk_rqstp; | |
983 | int status; | |
984 | ||
985 | if (req->rq_received > 0 && !req->rq_bytes_sent) | |
986 | task->tk_status = req->rq_received; | |
987 | ||
988 | dprintk("RPC: %4d call_status (status %d)\n", | |
989 | task->tk_pid, task->tk_status); | |
990 | ||
991 | status = task->tk_status; | |
992 | if (status >= 0) { | |
993 | task->tk_action = call_decode; | |
994 | return; | |
995 | } | |
996 | ||
997 | task->tk_status = 0; | |
998 | switch(status) { | |
999 | case -ETIMEDOUT: | |
1000 | task->tk_action = call_timeout; | |
1001 | break; | |
1002 | case -ECONNREFUSED: | |
1003 | case -ENOTCONN: | |
35f5a422 | 1004 | rpc_force_rebind(clnt); |
1da177e4 LT |
1005 | task->tk_action = call_bind; |
1006 | break; | |
1007 | case -EAGAIN: | |
1008 | task->tk_action = call_transmit; | |
1009 | break; | |
1010 | case -EIO: | |
1011 | /* shutdown or soft timeout */ | |
1012 | rpc_exit(task, status); | |
1013 | break; | |
1014 | default: | |
f518e35a | 1015 | printk("%s: RPC call returned error %d\n", |
1da177e4 LT |
1016 | clnt->cl_protname, -status); |
1017 | rpc_exit(task, status); | |
1018 | break; | |
1019 | } | |
1020 | } | |
1021 | ||
1022 | /* | |
e0ab53de | 1023 | * 6a. Handle RPC timeout |
1da177e4 LT |
1024 | * We do not release the request slot, so we keep using the |
1025 | * same XID for all retransmits. | |
1026 | */ | |
1027 | static void | |
1028 | call_timeout(struct rpc_task *task) | |
1029 | { | |
1030 | struct rpc_clnt *clnt = task->tk_client; | |
1031 | ||
1032 | if (xprt_adjust_timeout(task->tk_rqstp) == 0) { | |
1033 | dprintk("RPC: %4d call_timeout (minor)\n", task->tk_pid); | |
1034 | goto retry; | |
1035 | } | |
1036 | ||
1037 | dprintk("RPC: %4d call_timeout (major)\n", task->tk_pid); | |
ef759a2e CL |
1038 | task->tk_timeouts++; |
1039 | ||
1da177e4 | 1040 | if (RPC_IS_SOFT(task)) { |
f518e35a | 1041 | printk(KERN_NOTICE "%s: server %s not responding, timed out\n", |
1da177e4 LT |
1042 | clnt->cl_protname, clnt->cl_server); |
1043 | rpc_exit(task, -EIO); | |
1044 | return; | |
1045 | } | |
1046 | ||
f518e35a | 1047 | if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) { |
1da177e4 LT |
1048 | task->tk_flags |= RPC_CALL_MAJORSEEN; |
1049 | printk(KERN_NOTICE "%s: server %s not responding, still trying\n", | |
1050 | clnt->cl_protname, clnt->cl_server); | |
1051 | } | |
35f5a422 | 1052 | rpc_force_rebind(clnt); |
1da177e4 LT |
1053 | |
1054 | retry: | |
1055 | clnt->cl_stats->rpcretrans++; | |
1056 | task->tk_action = call_bind; | |
1057 | task->tk_status = 0; | |
1058 | } | |
1059 | ||
1060 | /* | |
1061 | * 7. Decode the RPC reply | |
1062 | */ | |
1063 | static void | |
1064 | call_decode(struct rpc_task *task) | |
1065 | { | |
1066 | struct rpc_clnt *clnt = task->tk_client; | |
1067 | struct rpc_rqst *req = task->tk_rqstp; | |
1068 | kxdrproc_t decode = task->tk_msg.rpc_proc->p_decode; | |
1069 | u32 *p; | |
1070 | ||
1071 | dprintk("RPC: %4d call_decode (status %d)\n", | |
1072 | task->tk_pid, task->tk_status); | |
1073 | ||
f518e35a | 1074 | if (task->tk_flags & RPC_CALL_MAJORSEEN) { |
1da177e4 LT |
1075 | printk(KERN_NOTICE "%s: server %s OK\n", |
1076 | clnt->cl_protname, clnt->cl_server); | |
1077 | task->tk_flags &= ~RPC_CALL_MAJORSEEN; | |
1078 | } | |
1079 | ||
1080 | if (task->tk_status < 12) { | |
1081 | if (!RPC_IS_SOFT(task)) { | |
1082 | task->tk_action = call_bind; | |
1083 | clnt->cl_stats->rpcretrans++; | |
1084 | goto out_retry; | |
1085 | } | |
1086 | printk(KERN_WARNING "%s: too small RPC reply size (%d bytes)\n", | |
1087 | clnt->cl_protname, task->tk_status); | |
1088 | rpc_exit(task, -EIO); | |
1089 | return; | |
1090 | } | |
1091 | ||
43ac3f29 TM |
1092 | /* |
1093 | * Ensure that we see all writes made by xprt_complete_rqst() | |
1094 | * before it changed req->rq_received. | |
1095 | */ | |
1096 | smp_rmb(); | |
1da177e4 LT |
1097 | req->rq_rcv_buf.len = req->rq_private_buf.len; |
1098 | ||
1099 | /* Check that the softirq receive buffer is valid */ | |
1100 | WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf, | |
1101 | sizeof(req->rq_rcv_buf)) != 0); | |
1102 | ||
1103 | /* Verify the RPC header */ | |
abbcf28f TM |
1104 | p = call_verify(task); |
1105 | if (IS_ERR(p)) { | |
1106 | if (p == ERR_PTR(-EAGAIN)) | |
1107 | goto out_retry; | |
1108 | return; | |
1da177e4 LT |
1109 | } |
1110 | ||
abbcf28f | 1111 | task->tk_action = rpc_exit_task; |
1da177e4 LT |
1112 | |
1113 | if (decode) | |
1114 | task->tk_status = rpcauth_unwrap_resp(task, decode, req, p, | |
1115 | task->tk_msg.rpc_resp); | |
1116 | dprintk("RPC: %4d call_decode result %d\n", task->tk_pid, | |
1117 | task->tk_status); | |
1118 | return; | |
1119 | out_retry: | |
1120 | req->rq_received = req->rq_private_buf.len = 0; | |
1121 | task->tk_status = 0; | |
1122 | } | |
1123 | ||
1124 | /* | |
1125 | * 8. Refresh the credentials if rejected by the server | |
1126 | */ | |
1127 | static void | |
1128 | call_refresh(struct rpc_task *task) | |
1129 | { | |
1130 | dprintk("RPC: %4d call_refresh\n", task->tk_pid); | |
1131 | ||
1132 | xprt_release(task); /* Must do to obtain new XID */ | |
1133 | task->tk_action = call_refreshresult; | |
1134 | task->tk_status = 0; | |
1135 | task->tk_client->cl_stats->rpcauthrefresh++; | |
1136 | rpcauth_refreshcred(task); | |
1137 | } | |
1138 | ||
1139 | /* | |
1140 | * 8a. Process the results of a credential refresh | |
1141 | */ | |
1142 | static void | |
1143 | call_refreshresult(struct rpc_task *task) | |
1144 | { | |
1145 | int status = task->tk_status; | |
1146 | dprintk("RPC: %4d call_refreshresult (status %d)\n", | |
1147 | task->tk_pid, task->tk_status); | |
1148 | ||
1149 | task->tk_status = 0; | |
1150 | task->tk_action = call_reserve; | |
1151 | if (status >= 0 && rpcauth_uptodatecred(task)) | |
1152 | return; | |
1153 | if (status == -EACCES) { | |
1154 | rpc_exit(task, -EACCES); | |
1155 | return; | |
1156 | } | |
1157 | task->tk_action = call_refresh; | |
1158 | if (status != -ETIMEDOUT) | |
1159 | rpc_delay(task, 3*HZ); | |
1160 | return; | |
1161 | } | |
1162 | ||
1163 | /* | |
1164 | * Call header serialization | |
1165 | */ | |
1166 | static u32 * | |
1167 | call_header(struct rpc_task *task) | |
1168 | { | |
1169 | struct rpc_clnt *clnt = task->tk_client; | |
1da177e4 LT |
1170 | struct rpc_rqst *req = task->tk_rqstp; |
1171 | u32 *p = req->rq_svec[0].iov_base; | |
1172 | ||
1173 | /* FIXME: check buffer size? */ | |
808012fb CL |
1174 | |
1175 | p = xprt_skip_transport_header(task->tk_xprt, p); | |
1da177e4 LT |
1176 | *p++ = req->rq_xid; /* XID */ |
1177 | *p++ = htonl(RPC_CALL); /* CALL */ | |
1178 | *p++ = htonl(RPC_VERSION); /* RPC version */ | |
1179 | *p++ = htonl(clnt->cl_prog); /* program number */ | |
1180 | *p++ = htonl(clnt->cl_vers); /* program version */ | |
1181 | *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */ | |
334ccfd5 TM |
1182 | p = rpcauth_marshcred(task, p); |
1183 | req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p); | |
1184 | return p; | |
1da177e4 LT |
1185 | } |
1186 | ||
1187 | /* | |
1188 | * Reply header verification | |
1189 | */ | |
1190 | static u32 * | |
1191 | call_verify(struct rpc_task *task) | |
1192 | { | |
1193 | struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0]; | |
1194 | int len = task->tk_rqstp->rq_rcv_buf.len >> 2; | |
1195 | u32 *p = iov->iov_base, n; | |
1196 | int error = -EACCES; | |
1197 | ||
e8896495 DH |
1198 | if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) { |
1199 | /* RFC-1014 says that the representation of XDR data must be a | |
1200 | * multiple of four bytes | |
1201 | * - if it isn't pointer subtraction in the NFS client may give | |
1202 | * undefined results | |
1203 | */ | |
1204 | printk(KERN_WARNING | |
1205 | "call_verify: XDR representation not a multiple of" | |
1206 | " 4 bytes: 0x%x\n", task->tk_rqstp->rq_rcv_buf.len); | |
1207 | goto out_eio; | |
1208 | } | |
1da177e4 LT |
1209 | if ((len -= 3) < 0) |
1210 | goto out_overflow; | |
1211 | p += 1; /* skip XID */ | |
1212 | ||
1213 | if ((n = ntohl(*p++)) != RPC_REPLY) { | |
1214 | printk(KERN_WARNING "call_verify: not an RPC reply: %x\n", n); | |
abbcf28f | 1215 | goto out_garbage; |
1da177e4 LT |
1216 | } |
1217 | if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) { | |
1218 | if (--len < 0) | |
1219 | goto out_overflow; | |
1220 | switch ((n = ntohl(*p++))) { | |
1221 | case RPC_AUTH_ERROR: | |
1222 | break; | |
1223 | case RPC_MISMATCH: | |
cdf47706 AG |
1224 | dprintk("%s: RPC call version mismatch!\n", __FUNCTION__); |
1225 | error = -EPROTONOSUPPORT; | |
1226 | goto out_err; | |
1da177e4 | 1227 | default: |
cdf47706 | 1228 | dprintk("%s: RPC call rejected, unknown error: %x\n", __FUNCTION__, n); |
1da177e4 LT |
1229 | goto out_eio; |
1230 | } | |
1231 | if (--len < 0) | |
1232 | goto out_overflow; | |
1233 | switch ((n = ntohl(*p++))) { | |
1234 | case RPC_AUTH_REJECTEDCRED: | |
1235 | case RPC_AUTH_REJECTEDVERF: | |
1236 | case RPCSEC_GSS_CREDPROBLEM: | |
1237 | case RPCSEC_GSS_CTXPROBLEM: | |
1238 | if (!task->tk_cred_retry) | |
1239 | break; | |
1240 | task->tk_cred_retry--; | |
1241 | dprintk("RPC: %4d call_verify: retry stale creds\n", | |
1242 | task->tk_pid); | |
1243 | rpcauth_invalcred(task); | |
1244 | task->tk_action = call_refresh; | |
abbcf28f | 1245 | goto out_retry; |
1da177e4 LT |
1246 | case RPC_AUTH_BADCRED: |
1247 | case RPC_AUTH_BADVERF: | |
1248 | /* possibly garbled cred/verf? */ | |
1249 | if (!task->tk_garb_retry) | |
1250 | break; | |
1251 | task->tk_garb_retry--; | |
1252 | dprintk("RPC: %4d call_verify: retry garbled creds\n", | |
1253 | task->tk_pid); | |
1254 | task->tk_action = call_bind; | |
abbcf28f | 1255 | goto out_retry; |
1da177e4 | 1256 | case RPC_AUTH_TOOWEAK: |
1356b8c2 LS |
1257 | printk(KERN_NOTICE "call_verify: server %s requires stronger " |
1258 | "authentication.\n", task->tk_client->cl_server); | |
1da177e4 LT |
1259 | break; |
1260 | default: | |
1261 | printk(KERN_WARNING "call_verify: unknown auth error: %x\n", n); | |
1262 | error = -EIO; | |
1263 | } | |
1264 | dprintk("RPC: %4d call_verify: call rejected %d\n", | |
1265 | task->tk_pid, n); | |
1266 | goto out_err; | |
1267 | } | |
1268 | if (!(p = rpcauth_checkverf(task, p))) { | |
1269 | printk(KERN_WARNING "call_verify: auth check failed\n"); | |
abbcf28f | 1270 | goto out_garbage; /* bad verifier, retry */ |
1da177e4 LT |
1271 | } |
1272 | len = p - (u32 *)iov->iov_base - 1; | |
1273 | if (len < 0) | |
1274 | goto out_overflow; | |
1275 | switch ((n = ntohl(*p++))) { | |
1276 | case RPC_SUCCESS: | |
1277 | return p; | |
1278 | case RPC_PROG_UNAVAIL: | |
cdf47706 | 1279 | dprintk("RPC: call_verify: program %u is unsupported by server %s\n", |
1da177e4 LT |
1280 | (unsigned int)task->tk_client->cl_prog, |
1281 | task->tk_client->cl_server); | |
cdf47706 AG |
1282 | error = -EPFNOSUPPORT; |
1283 | goto out_err; | |
1da177e4 | 1284 | case RPC_PROG_MISMATCH: |
cdf47706 | 1285 | dprintk("RPC: call_verify: program %u, version %u unsupported by server %s\n", |
1da177e4 LT |
1286 | (unsigned int)task->tk_client->cl_prog, |
1287 | (unsigned int)task->tk_client->cl_vers, | |
1288 | task->tk_client->cl_server); | |
cdf47706 AG |
1289 | error = -EPROTONOSUPPORT; |
1290 | goto out_err; | |
1da177e4 | 1291 | case RPC_PROC_UNAVAIL: |
cdf47706 | 1292 | dprintk("RPC: call_verify: proc %p unsupported by program %u, version %u on server %s\n", |
1da177e4 LT |
1293 | task->tk_msg.rpc_proc, |
1294 | task->tk_client->cl_prog, | |
1295 | task->tk_client->cl_vers, | |
1296 | task->tk_client->cl_server); | |
cdf47706 AG |
1297 | error = -EOPNOTSUPP; |
1298 | goto out_err; | |
1da177e4 LT |
1299 | case RPC_GARBAGE_ARGS: |
1300 | dprintk("RPC: %4d %s: server saw garbage\n", task->tk_pid, __FUNCTION__); | |
1301 | break; /* retry */ | |
1302 | default: | |
1303 | printk(KERN_WARNING "call_verify: server accept status: %x\n", n); | |
1304 | /* Also retry */ | |
1305 | } | |
1306 | ||
abbcf28f | 1307 | out_garbage: |
1da177e4 LT |
1308 | task->tk_client->cl_stats->rpcgarbage++; |
1309 | if (task->tk_garb_retry) { | |
1310 | task->tk_garb_retry--; | |
cdf47706 | 1311 | dprintk("RPC %s: retrying %4d\n", __FUNCTION__, task->tk_pid); |
1da177e4 | 1312 | task->tk_action = call_bind; |
abbcf28f TM |
1313 | out_retry: |
1314 | return ERR_PTR(-EAGAIN); | |
1da177e4 LT |
1315 | } |
1316 | printk(KERN_WARNING "RPC %s: retry failed, exit EIO\n", __FUNCTION__); | |
1317 | out_eio: | |
1318 | error = -EIO; | |
1319 | out_err: | |
1320 | rpc_exit(task, error); | |
abbcf28f | 1321 | return ERR_PTR(error); |
1da177e4 LT |
1322 | out_overflow: |
1323 | printk(KERN_WARNING "RPC %s: server reply was truncated.\n", __FUNCTION__); | |
abbcf28f | 1324 | goto out_garbage; |
1da177e4 | 1325 | } |
5ee0ed7d TM |
1326 | |
1327 | static int rpcproc_encode_null(void *rqstp, u32 *data, void *obj) | |
1328 | { | |
1329 | return 0; | |
1330 | } | |
1331 | ||
1332 | static int rpcproc_decode_null(void *rqstp, u32 *data, void *obj) | |
1333 | { | |
1334 | return 0; | |
1335 | } | |
1336 | ||
1337 | static struct rpc_procinfo rpcproc_null = { | |
1338 | .p_encode = rpcproc_encode_null, | |
1339 | .p_decode = rpcproc_decode_null, | |
1340 | }; | |
1341 | ||
1342 | int rpc_ping(struct rpc_clnt *clnt, int flags) | |
1343 | { | |
1344 | struct rpc_message msg = { | |
1345 | .rpc_proc = &rpcproc_null, | |
1346 | }; | |
1347 | int err; | |
1348 | msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0); | |
1349 | err = rpc_call_sync(clnt, &msg, flags); | |
1350 | put_rpccred(msg.rpc_cred); | |
1351 | return err; | |
1352 | } |