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