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