]>
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 | * | |
1da177e4 LT |
16 | * Copyright (C) 1992,1993 Rick Sladkey <jrs@world.std.com> |
17 | * Copyright (C) 1995,1996 Olaf Kirch <okir@monad.swb.de> | |
18 | */ | |
19 | ||
20 | #include <asm/system.h> | |
21 | ||
22 | #include <linux/module.h> | |
23 | #include <linux/types.h> | |
cb3997b5 | 24 | #include <linux/kallsyms.h> |
1da177e4 | 25 | #include <linux/mm.h> |
23ac6581 TM |
26 | #include <linux/namei.h> |
27 | #include <linux/mount.h> | |
1da177e4 | 28 | #include <linux/slab.h> |
1da177e4 | 29 | #include <linux/utsname.h> |
11c556b3 | 30 | #include <linux/workqueue.h> |
510deb0d | 31 | #include <linux/in6.h> |
1da177e4 LT |
32 | |
33 | #include <linux/sunrpc/clnt.h> | |
1da177e4 | 34 | #include <linux/sunrpc/rpc_pipe_fs.h> |
11c556b3 | 35 | #include <linux/sunrpc/metrics.h> |
55ae1aab | 36 | #include <linux/sunrpc/bc_xprt.h> |
1da177e4 | 37 | |
55ae1aab | 38 | #include "sunrpc.h" |
1da177e4 | 39 | |
1da177e4 LT |
40 | #ifdef RPC_DEBUG |
41 | # define RPCDBG_FACILITY RPCDBG_CALL | |
42 | #endif | |
43 | ||
46121cf7 CL |
44 | #define dprint_status(t) \ |
45 | dprintk("RPC: %5u %s (status %d)\n", t->tk_pid, \ | |
0dc47877 | 46 | __func__, t->tk_status) |
46121cf7 | 47 | |
188fef11 TM |
48 | /* |
49 | * All RPC clients are linked into this list | |
50 | */ | |
51 | static LIST_HEAD(all_clients); | |
52 | static DEFINE_SPINLOCK(rpc_client_lock); | |
53 | ||
1da177e4 LT |
54 | static DECLARE_WAIT_QUEUE_HEAD(destroy_wait); |
55 | ||
56 | ||
57 | static void call_start(struct rpc_task *task); | |
58 | static void call_reserve(struct rpc_task *task); | |
59 | static void call_reserveresult(struct rpc_task *task); | |
60 | static void call_allocate(struct rpc_task *task); | |
1da177e4 LT |
61 | static void call_decode(struct rpc_task *task); |
62 | static void call_bind(struct rpc_task *task); | |
da351878 | 63 | static void call_bind_status(struct rpc_task *task); |
1da177e4 | 64 | static void call_transmit(struct rpc_task *task); |
55ae1aab RL |
65 | #if defined(CONFIG_NFS_V4_1) |
66 | static void call_bc_transmit(struct rpc_task *task); | |
67 | #endif /* CONFIG_NFS_V4_1 */ | |
1da177e4 | 68 | static void call_status(struct rpc_task *task); |
940e3318 | 69 | static void call_transmit_status(struct rpc_task *task); |
1da177e4 LT |
70 | static void call_refresh(struct rpc_task *task); |
71 | static void call_refreshresult(struct rpc_task *task); | |
72 | static void call_timeout(struct rpc_task *task); | |
73 | static void call_connect(struct rpc_task *task); | |
74 | static void call_connect_status(struct rpc_task *task); | |
1da177e4 | 75 | |
b0e1c57e CL |
76 | static __be32 *rpc_encode_header(struct rpc_task *task); |
77 | static __be32 *rpc_verify_header(struct rpc_task *task); | |
caabea8a | 78 | static int rpc_ping(struct rpc_clnt *clnt); |
64c91a1f | 79 | |
188fef11 TM |
80 | static void rpc_register_client(struct rpc_clnt *clnt) |
81 | { | |
82 | spin_lock(&rpc_client_lock); | |
83 | list_add(&clnt->cl_clients, &all_clients); | |
84 | spin_unlock(&rpc_client_lock); | |
85 | } | |
86 | ||
87 | static void rpc_unregister_client(struct rpc_clnt *clnt) | |
88 | { | |
89 | spin_lock(&rpc_client_lock); | |
90 | list_del(&clnt->cl_clients); | |
91 | spin_unlock(&rpc_client_lock); | |
92 | } | |
1da177e4 LT |
93 | |
94 | static int | |
95 | rpc_setup_pipedir(struct rpc_clnt *clnt, char *dir_name) | |
96 | { | |
f134585a | 97 | static uint32_t clntid; |
23ac6581 TM |
98 | struct nameidata nd; |
99 | struct path path; | |
100 | char name[15]; | |
101 | struct qstr q = { | |
102 | .name = name, | |
103 | }; | |
1da177e4 LT |
104 | int error; |
105 | ||
7d217cac TM |
106 | clnt->cl_path.mnt = ERR_PTR(-ENOENT); |
107 | clnt->cl_path.dentry = ERR_PTR(-ENOENT); | |
1da177e4 LT |
108 | if (dir_name == NULL) |
109 | return 0; | |
54281548 | 110 | |
23ac6581 TM |
111 | path.mnt = rpc_get_mount(); |
112 | if (IS_ERR(path.mnt)) | |
113 | return PTR_ERR(path.mnt); | |
114 | error = vfs_path_lookup(path.mnt->mnt_root, path.mnt, dir_name, 0, &nd); | |
115 | if (error) | |
116 | goto err; | |
54281548 | 117 | |
f134585a | 118 | for (;;) { |
23ac6581 TM |
119 | q.len = snprintf(name, sizeof(name), "clnt%x", (unsigned int)clntid++); |
120 | name[sizeof(name) - 1] = '\0'; | |
121 | q.hash = full_name_hash(q.name, q.len); | |
122 | path.dentry = rpc_create_client_dir(nd.path.dentry, &q, clnt); | |
123 | if (!IS_ERR(path.dentry)) | |
124 | break; | |
125 | error = PTR_ERR(path.dentry); | |
f134585a | 126 | if (error != -EEXIST) { |
23ac6581 TM |
127 | printk(KERN_INFO "RPC: Couldn't create pipefs entry" |
128 | " %s/%s, error %d\n", | |
129 | dir_name, name, error); | |
130 | goto err_path_put; | |
f134585a | 131 | } |
1da177e4 | 132 | } |
23ac6581 TM |
133 | path_put(&nd.path); |
134 | clnt->cl_path = path; | |
135 | return 0; | |
136 | err_path_put: | |
137 | path_put(&nd.path); | |
138 | err: | |
139 | rpc_put_mount(); | |
140 | return error; | |
1da177e4 LT |
141 | } |
142 | ||
698b6d08 | 143 | static struct rpc_clnt * rpc_new_client(const struct rpc_create_args *args, struct rpc_xprt *xprt) |
1da177e4 | 144 | { |
698b6d08 | 145 | struct rpc_program *program = args->program; |
1da177e4 LT |
146 | struct rpc_version *version; |
147 | struct rpc_clnt *clnt = NULL; | |
6a19275a | 148 | struct rpc_auth *auth; |
1da177e4 | 149 | int err; |
06b8d255 CL |
150 | size_t len; |
151 | ||
152 | /* sanity check the name before trying to print it */ | |
153 | err = -EINVAL; | |
698b6d08 | 154 | len = strlen(args->servername); |
06b8d255 CL |
155 | if (len > RPC_MAXNETNAMELEN) |
156 | goto out_no_rpciod; | |
157 | len++; | |
1da177e4 | 158 | |
46121cf7 | 159 | dprintk("RPC: creating %s client for %s (xprt %p)\n", |
698b6d08 | 160 | program->name, args->servername, xprt); |
1da177e4 | 161 | |
4ada539e TM |
162 | err = rpciod_up(); |
163 | if (err) | |
164 | goto out_no_rpciod; | |
1da177e4 LT |
165 | err = -EINVAL; |
166 | if (!xprt) | |
712917d1 | 167 | goto out_no_xprt; |
698b6d08 TM |
168 | |
169 | if (args->version >= program->nrvers) | |
170 | goto out_err; | |
171 | version = program->version[args->version]; | |
172 | if (version == NULL) | |
1da177e4 LT |
173 | goto out_err; |
174 | ||
175 | err = -ENOMEM; | |
0da974f4 | 176 | clnt = kzalloc(sizeof(*clnt), GFP_KERNEL); |
1da177e4 LT |
177 | if (!clnt) |
178 | goto out_err; | |
1da177e4 LT |
179 | clnt->cl_parent = clnt; |
180 | ||
181 | clnt->cl_server = clnt->cl_inline_name; | |
1da177e4 LT |
182 | if (len > sizeof(clnt->cl_inline_name)) { |
183 | char *buf = kmalloc(len, GFP_KERNEL); | |
698b6d08 | 184 | if (buf != NULL) |
1da177e4 LT |
185 | clnt->cl_server = buf; |
186 | else | |
187 | len = sizeof(clnt->cl_inline_name); | |
188 | } | |
698b6d08 | 189 | strlcpy(clnt->cl_server, args->servername, len); |
1da177e4 LT |
190 | |
191 | clnt->cl_xprt = xprt; | |
192 | clnt->cl_procinfo = version->procs; | |
193 | clnt->cl_maxproc = version->nrprocs; | |
194 | clnt->cl_protname = program->name; | |
d5b337b4 | 195 | clnt->cl_prog = args->prognumber ? : program->number; |
1da177e4 | 196 | clnt->cl_vers = version->number; |
1da177e4 | 197 | clnt->cl_stats = program->stats; |
11c556b3 | 198 | clnt->cl_metrics = rpc_alloc_iostats(clnt); |
23bf85ba TM |
199 | err = -ENOMEM; |
200 | if (clnt->cl_metrics == NULL) | |
201 | goto out_no_stats; | |
3e32a5d9 | 202 | clnt->cl_program = program; |
6529eba0 | 203 | INIT_LIST_HEAD(&clnt->cl_tasks); |
4bef61ff | 204 | spin_lock_init(&clnt->cl_lock); |
1da177e4 | 205 | |
ec739ef0 | 206 | if (!xprt_bound(clnt->cl_xprt)) |
1da177e4 LT |
207 | clnt->cl_autobind = 1; |
208 | ||
ba7392bb TM |
209 | clnt->cl_timeout = xprt->timeout; |
210 | if (args->timeout != NULL) { | |
211 | memcpy(&clnt->cl_timeout_default, args->timeout, | |
212 | sizeof(clnt->cl_timeout_default)); | |
213 | clnt->cl_timeout = &clnt->cl_timeout_default; | |
214 | } | |
215 | ||
1da177e4 | 216 | clnt->cl_rtt = &clnt->cl_rtt_default; |
ba7392bb | 217 | rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval); |
608207e8 OK |
218 | clnt->cl_principal = NULL; |
219 | if (args->client_name) { | |
220 | clnt->cl_principal = kstrdup(args->client_name, GFP_KERNEL); | |
221 | if (!clnt->cl_principal) | |
222 | goto out_no_principal; | |
223 | } | |
1da177e4 | 224 | |
006abe88 | 225 | atomic_set(&clnt->cl_count, 1); |
34f52e35 | 226 | |
1da177e4 LT |
227 | err = rpc_setup_pipedir(clnt, program->pipe_dir_name); |
228 | if (err < 0) | |
229 | goto out_no_path; | |
230 | ||
698b6d08 | 231 | auth = rpcauth_create(args->authflavor, clnt); |
6a19275a | 232 | if (IS_ERR(auth)) { |
1da177e4 | 233 | printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %u)\n", |
698b6d08 | 234 | args->authflavor); |
6a19275a | 235 | err = PTR_ERR(auth); |
1da177e4 LT |
236 | goto out_no_auth; |
237 | } | |
238 | ||
239 | /* save the nodename */ | |
63ffc23d | 240 | clnt->cl_nodelen = strlen(init_utsname()->nodename); |
1da177e4 LT |
241 | if (clnt->cl_nodelen > UNX_MAXNODENAME) |
242 | clnt->cl_nodelen = UNX_MAXNODENAME; | |
63ffc23d | 243 | memcpy(clnt->cl_nodename, init_utsname()->nodename, clnt->cl_nodelen); |
6529eba0 | 244 | rpc_register_client(clnt); |
1da177e4 LT |
245 | return clnt; |
246 | ||
247 | out_no_auth: | |
7d217cac TM |
248 | if (!IS_ERR(clnt->cl_path.dentry)) { |
249 | rpc_remove_client_dir(clnt->cl_path.dentry); | |
54281548 TM |
250 | rpc_put_mount(); |
251 | } | |
1da177e4 | 252 | out_no_path: |
608207e8 OK |
253 | kfree(clnt->cl_principal); |
254 | out_no_principal: | |
23bf85ba TM |
255 | rpc_free_iostats(clnt->cl_metrics); |
256 | out_no_stats: | |
1da177e4 LT |
257 | if (clnt->cl_server != clnt->cl_inline_name) |
258 | kfree(clnt->cl_server); | |
259 | kfree(clnt); | |
260 | out_err: | |
6b6ca86b | 261 | xprt_put(xprt); |
712917d1 | 262 | out_no_xprt: |
4ada539e TM |
263 | rpciod_down(); |
264 | out_no_rpciod: | |
1da177e4 LT |
265 | return ERR_PTR(err); |
266 | } | |
267 | ||
c2866763 CL |
268 | /* |
269 | * rpc_create - create an RPC client and transport with one call | |
270 | * @args: rpc_clnt create argument structure | |
271 | * | |
272 | * Creates and initializes an RPC transport and an RPC client. | |
273 | * | |
274 | * It can ping the server in order to determine if it is up, and to see if | |
275 | * it supports this program and version. RPC_CLNT_CREATE_NOPING disables | |
276 | * this behavior so asynchronous tasks can also use rpc_create. | |
277 | */ | |
278 | struct rpc_clnt *rpc_create(struct rpc_create_args *args) | |
279 | { | |
280 | struct rpc_xprt *xprt; | |
281 | struct rpc_clnt *clnt; | |
3c341b0b | 282 | struct xprt_create xprtargs = { |
9a23e332 | 283 | .net = args->net, |
4fa016eb | 284 | .ident = args->protocol, |
d3bc9a1d | 285 | .srcaddr = args->saddress, |
96802a09 FM |
286 | .dstaddr = args->address, |
287 | .addrlen = args->addrsize, | |
f300baba | 288 | .bc_xprt = args->bc_xprt, |
96802a09 | 289 | }; |
510deb0d | 290 | char servername[48]; |
c2866763 | 291 | |
43780b87 CL |
292 | /* |
293 | * If the caller chooses not to specify a hostname, whip | |
294 | * up a string representation of the passed-in address. | |
295 | */ | |
296 | if (args->servername == NULL) { | |
da09eb93 CL |
297 | struct sockaddr_in *sin = |
298 | (struct sockaddr_in *)args->address; | |
299 | struct sockaddr_in6 *sin6 = | |
300 | (struct sockaddr_in6 *)args->address; | |
301 | ||
510deb0d CL |
302 | servername[0] = '\0'; |
303 | switch (args->address->sa_family) { | |
da09eb93 | 304 | case AF_INET: |
21454aaa HH |
305 | snprintf(servername, sizeof(servername), "%pI4", |
306 | &sin->sin_addr.s_addr); | |
510deb0d | 307 | break; |
da09eb93 | 308 | case AF_INET6: |
5b095d98 | 309 | snprintf(servername, sizeof(servername), "%pI6", |
da09eb93 | 310 | &sin6->sin6_addr); |
510deb0d | 311 | break; |
510deb0d CL |
312 | default: |
313 | /* caller wants default server name, but | |
314 | * address family isn't recognized. */ | |
315 | return ERR_PTR(-EINVAL); | |
316 | } | |
43780b87 CL |
317 | args->servername = servername; |
318 | } | |
319 | ||
510deb0d CL |
320 | xprt = xprt_create_transport(&xprtargs); |
321 | if (IS_ERR(xprt)) | |
322 | return (struct rpc_clnt *)xprt; | |
323 | ||
c2866763 CL |
324 | /* |
325 | * By default, kernel RPC client connects from a reserved port. | |
326 | * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters, | |
327 | * but it is always enabled for rpciod, which handles the connect | |
328 | * operation. | |
329 | */ | |
330 | xprt->resvport = 1; | |
331 | if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT) | |
332 | xprt->resvport = 0; | |
333 | ||
698b6d08 | 334 | clnt = rpc_new_client(args, xprt); |
c2866763 CL |
335 | if (IS_ERR(clnt)) |
336 | return clnt; | |
337 | ||
338 | if (!(args->flags & RPC_CLNT_CREATE_NOPING)) { | |
caabea8a | 339 | int err = rpc_ping(clnt); |
c2866763 CL |
340 | if (err != 0) { |
341 | rpc_shutdown_client(clnt); | |
342 | return ERR_PTR(err); | |
343 | } | |
344 | } | |
345 | ||
346 | clnt->cl_softrtry = 1; | |
347 | if (args->flags & RPC_CLNT_CREATE_HARDRTRY) | |
348 | clnt->cl_softrtry = 0; | |
349 | ||
c2866763 CL |
350 | if (args->flags & RPC_CLNT_CREATE_AUTOBIND) |
351 | clnt->cl_autobind = 1; | |
43d78ef2 CL |
352 | if (args->flags & RPC_CLNT_CREATE_DISCRTRY) |
353 | clnt->cl_discrtry = 1; | |
b6b6152c OK |
354 | if (!(args->flags & RPC_CLNT_CREATE_QUIET)) |
355 | clnt->cl_chatty = 1; | |
c2866763 CL |
356 | |
357 | return clnt; | |
358 | } | |
b86acd50 | 359 | EXPORT_SYMBOL_GPL(rpc_create); |
c2866763 | 360 | |
1da177e4 LT |
361 | /* |
362 | * This function clones the RPC client structure. It allows us to share the | |
363 | * same transport while varying parameters such as the authentication | |
364 | * flavour. | |
365 | */ | |
366 | struct rpc_clnt * | |
367 | rpc_clone_client(struct rpc_clnt *clnt) | |
368 | { | |
369 | struct rpc_clnt *new; | |
3e32a5d9 | 370 | int err = -ENOMEM; |
1da177e4 | 371 | |
e69062b4 | 372 | new = kmemdup(clnt, sizeof(*new), GFP_KERNEL); |
1da177e4 LT |
373 | if (!new) |
374 | goto out_no_clnt; | |
d431a555 TM |
375 | new->cl_parent = clnt; |
376 | /* Turn off autobind on clones */ | |
377 | new->cl_autobind = 0; | |
378 | INIT_LIST_HEAD(&new->cl_tasks); | |
379 | spin_lock_init(&new->cl_lock); | |
ba7392bb | 380 | rpc_init_rtt(&new->cl_rtt_default, clnt->cl_timeout->to_initval); |
23bf85ba TM |
381 | new->cl_metrics = rpc_alloc_iostats(clnt); |
382 | if (new->cl_metrics == NULL) | |
383 | goto out_no_stats; | |
608207e8 OK |
384 | if (clnt->cl_principal) { |
385 | new->cl_principal = kstrdup(clnt->cl_principal, GFP_KERNEL); | |
386 | if (new->cl_principal == NULL) | |
387 | goto out_no_principal; | |
388 | } | |
006abe88 | 389 | atomic_set(&new->cl_count, 1); |
3e32a5d9 TM |
390 | err = rpc_setup_pipedir(new, clnt->cl_program->pipe_dir_name); |
391 | if (err != 0) | |
392 | goto out_no_path; | |
1da177e4 LT |
393 | if (new->cl_auth) |
394 | atomic_inc(&new->cl_auth->au_count); | |
d431a555 | 395 | xprt_get(clnt->cl_xprt); |
006abe88 | 396 | atomic_inc(&clnt->cl_count); |
6529eba0 | 397 | rpc_register_client(new); |
4ada539e | 398 | rpciod_up(); |
1da177e4 | 399 | return new; |
3e32a5d9 | 400 | out_no_path: |
608207e8 OK |
401 | kfree(new->cl_principal); |
402 | out_no_principal: | |
3e32a5d9 | 403 | rpc_free_iostats(new->cl_metrics); |
23bf85ba TM |
404 | out_no_stats: |
405 | kfree(new); | |
1da177e4 | 406 | out_no_clnt: |
0dc47877 | 407 | dprintk("RPC: %s: returned error %d\n", __func__, err); |
3e32a5d9 | 408 | return ERR_PTR(err); |
1da177e4 | 409 | } |
e8914c65 | 410 | EXPORT_SYMBOL_GPL(rpc_clone_client); |
1da177e4 | 411 | |
58f9612c TM |
412 | /* |
413 | * Kill all tasks for the given client. | |
414 | * XXX: kill their descendants as well? | |
415 | */ | |
416 | void rpc_killall_tasks(struct rpc_clnt *clnt) | |
417 | { | |
418 | struct rpc_task *rovr; | |
419 | ||
420 | ||
421 | if (list_empty(&clnt->cl_tasks)) | |
422 | return; | |
423 | dprintk("RPC: killing all tasks for client %p\n", clnt); | |
424 | /* | |
425 | * Spin lock all_tasks to prevent changes... | |
426 | */ | |
427 | spin_lock(&clnt->cl_lock); | |
428 | list_for_each_entry(rovr, &clnt->cl_tasks, tk_task) { | |
429 | if (!RPC_IS_ACTIVATED(rovr)) | |
430 | continue; | |
431 | if (!(rovr->tk_flags & RPC_TASK_KILLED)) { | |
432 | rovr->tk_flags |= RPC_TASK_KILLED; | |
433 | rpc_exit(rovr, -EIO); | |
8e26de23 SK |
434 | if (RPC_IS_QUEUED(rovr)) |
435 | rpc_wake_up_queued_task(rovr->tk_waitqueue, | |
436 | rovr); | |
58f9612c TM |
437 | } |
438 | } | |
439 | spin_unlock(&clnt->cl_lock); | |
440 | } | |
441 | EXPORT_SYMBOL_GPL(rpc_killall_tasks); | |
442 | ||
1da177e4 LT |
443 | /* |
444 | * Properly shut down an RPC client, terminating all outstanding | |
90c5755f | 445 | * requests. |
1da177e4 | 446 | */ |
4c402b40 | 447 | void rpc_shutdown_client(struct rpc_clnt *clnt) |
1da177e4 | 448 | { |
34f52e35 TM |
449 | dprintk("RPC: shutting down %s client for %s\n", |
450 | clnt->cl_protname, clnt->cl_server); | |
1da177e4 | 451 | |
34f52e35 | 452 | while (!list_empty(&clnt->cl_tasks)) { |
1da177e4 | 453 | rpc_killall_tasks(clnt); |
532347e2 | 454 | wait_event_timeout(destroy_wait, |
34f52e35 | 455 | list_empty(&clnt->cl_tasks), 1*HZ); |
1da177e4 LT |
456 | } |
457 | ||
4c402b40 | 458 | rpc_release_client(clnt); |
1da177e4 | 459 | } |
e8914c65 | 460 | EXPORT_SYMBOL_GPL(rpc_shutdown_client); |
1da177e4 LT |
461 | |
462 | /* | |
34f52e35 | 463 | * Free an RPC client |
1da177e4 | 464 | */ |
34f52e35 | 465 | static void |
006abe88 | 466 | rpc_free_client(struct rpc_clnt *clnt) |
1da177e4 | 467 | { |
46121cf7 | 468 | dprintk("RPC: destroying %s client for %s\n", |
1da177e4 | 469 | clnt->cl_protname, clnt->cl_server); |
7d217cac TM |
470 | if (!IS_ERR(clnt->cl_path.dentry)) { |
471 | rpc_remove_client_dir(clnt->cl_path.dentry); | |
8f8e7a50 TM |
472 | rpc_put_mount(); |
473 | } | |
3e32a5d9 | 474 | if (clnt->cl_parent != clnt) { |
8ad7c892 | 475 | rpc_release_client(clnt->cl_parent); |
3e32a5d9 TM |
476 | goto out_free; |
477 | } | |
1da177e4 LT |
478 | if (clnt->cl_server != clnt->cl_inline_name) |
479 | kfree(clnt->cl_server); | |
480 | out_free: | |
6529eba0 | 481 | rpc_unregister_client(clnt); |
11c556b3 | 482 | rpc_free_iostats(clnt->cl_metrics); |
608207e8 | 483 | kfree(clnt->cl_principal); |
11c556b3 | 484 | clnt->cl_metrics = NULL; |
6b6ca86b | 485 | xprt_put(clnt->cl_xprt); |
4ada539e | 486 | rpciod_down(); |
1da177e4 | 487 | kfree(clnt); |
1da177e4 LT |
488 | } |
489 | ||
1dd17ec6 TM |
490 | /* |
491 | * Free an RPC client | |
492 | */ | |
493 | static void | |
006abe88 | 494 | rpc_free_auth(struct rpc_clnt *clnt) |
1dd17ec6 | 495 | { |
1dd17ec6 | 496 | if (clnt->cl_auth == NULL) { |
006abe88 | 497 | rpc_free_client(clnt); |
1dd17ec6 TM |
498 | return; |
499 | } | |
500 | ||
501 | /* | |
502 | * Note: RPCSEC_GSS may need to send NULL RPC calls in order to | |
503 | * release remaining GSS contexts. This mechanism ensures | |
504 | * that it can do so safely. | |
505 | */ | |
006abe88 | 506 | atomic_inc(&clnt->cl_count); |
1dd17ec6 TM |
507 | rpcauth_release(clnt->cl_auth); |
508 | clnt->cl_auth = NULL; | |
006abe88 TM |
509 | if (atomic_dec_and_test(&clnt->cl_count)) |
510 | rpc_free_client(clnt); | |
1dd17ec6 TM |
511 | } |
512 | ||
1da177e4 | 513 | /* |
34f52e35 | 514 | * Release reference to the RPC client |
1da177e4 LT |
515 | */ |
516 | void | |
517 | rpc_release_client(struct rpc_clnt *clnt) | |
518 | { | |
34f52e35 | 519 | dprintk("RPC: rpc_release_client(%p)\n", clnt); |
1da177e4 | 520 | |
34f52e35 TM |
521 | if (list_empty(&clnt->cl_tasks)) |
522 | wake_up(&destroy_wait); | |
006abe88 TM |
523 | if (atomic_dec_and_test(&clnt->cl_count)) |
524 | rpc_free_auth(clnt); | |
34f52e35 TM |
525 | } |
526 | ||
007e251f AG |
527 | /** |
528 | * rpc_bind_new_program - bind a new RPC program to an existing client | |
65b6e42c RD |
529 | * @old: old rpc_client |
530 | * @program: rpc program to set | |
531 | * @vers: rpc program version | |
007e251f AG |
532 | * |
533 | * Clones the rpc client and sets up a new RPC program. This is mainly | |
534 | * of use for enabling different RPC programs to share the same transport. | |
535 | * The Sun NFSv2/v3 ACL protocol can do this. | |
536 | */ | |
537 | struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old, | |
538 | struct rpc_program *program, | |
89eb21c3 | 539 | u32 vers) |
007e251f AG |
540 | { |
541 | struct rpc_clnt *clnt; | |
542 | struct rpc_version *version; | |
543 | int err; | |
544 | ||
545 | BUG_ON(vers >= program->nrvers || !program->version[vers]); | |
546 | version = program->version[vers]; | |
547 | clnt = rpc_clone_client(old); | |
548 | if (IS_ERR(clnt)) | |
549 | goto out; | |
550 | clnt->cl_procinfo = version->procs; | |
551 | clnt->cl_maxproc = version->nrprocs; | |
552 | clnt->cl_protname = program->name; | |
553 | clnt->cl_prog = program->number; | |
554 | clnt->cl_vers = version->number; | |
555 | clnt->cl_stats = program->stats; | |
caabea8a | 556 | err = rpc_ping(clnt); |
007e251f AG |
557 | if (err != 0) { |
558 | rpc_shutdown_client(clnt); | |
559 | clnt = ERR_PTR(err); | |
560 | } | |
cca5172a | 561 | out: |
007e251f AG |
562 | return clnt; |
563 | } | |
e8914c65 | 564 | EXPORT_SYMBOL_GPL(rpc_bind_new_program); |
007e251f | 565 | |
58f9612c TM |
566 | void rpc_task_release_client(struct rpc_task *task) |
567 | { | |
568 | struct rpc_clnt *clnt = task->tk_client; | |
569 | ||
570 | if (clnt != NULL) { | |
571 | /* Remove from client task list */ | |
572 | spin_lock(&clnt->cl_lock); | |
573 | list_del(&task->tk_task); | |
574 | spin_unlock(&clnt->cl_lock); | |
575 | task->tk_client = NULL; | |
576 | ||
577 | rpc_release_client(clnt); | |
578 | } | |
579 | } | |
580 | ||
581 | static | |
582 | void rpc_task_set_client(struct rpc_task *task, struct rpc_clnt *clnt) | |
583 | { | |
584 | if (clnt != NULL) { | |
585 | rpc_task_release_client(task); | |
586 | task->tk_client = clnt; | |
006abe88 | 587 | atomic_inc(&clnt->cl_count); |
58f9612c TM |
588 | if (clnt->cl_softrtry) |
589 | task->tk_flags |= RPC_TASK_SOFT; | |
590 | /* Add to the client's list of all tasks */ | |
591 | spin_lock(&clnt->cl_lock); | |
592 | list_add_tail(&task->tk_task, &clnt->cl_tasks); | |
593 | spin_unlock(&clnt->cl_lock); | |
594 | } | |
595 | } | |
596 | ||
cbdabc7f AA |
597 | void rpc_task_reset_client(struct rpc_task *task, struct rpc_clnt *clnt) |
598 | { | |
599 | rpc_task_release_client(task); | |
600 | rpc_task_set_client(task, clnt); | |
601 | } | |
602 | EXPORT_SYMBOL_GPL(rpc_task_reset_client); | |
603 | ||
604 | ||
58f9612c TM |
605 | static void |
606 | rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg) | |
607 | { | |
608 | if (msg != NULL) { | |
609 | task->tk_msg.rpc_proc = msg->rpc_proc; | |
610 | task->tk_msg.rpc_argp = msg->rpc_argp; | |
611 | task->tk_msg.rpc_resp = msg->rpc_resp; | |
a17c2153 TM |
612 | if (msg->rpc_cred != NULL) |
613 | task->tk_msg.rpc_cred = get_rpccred(msg->rpc_cred); | |
58f9612c TM |
614 | } |
615 | } | |
616 | ||
1da177e4 LT |
617 | /* |
618 | * Default callback for async RPC calls | |
619 | */ | |
620 | static void | |
963d8fe5 | 621 | rpc_default_callback(struct rpc_task *task, void *data) |
1da177e4 LT |
622 | { |
623 | } | |
624 | ||
963d8fe5 TM |
625 | static const struct rpc_call_ops rpc_default_ops = { |
626 | .rpc_call_done = rpc_default_callback, | |
627 | }; | |
628 | ||
c970aa85 TM |
629 | /** |
630 | * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it | |
631 | * @task_setup_data: pointer to task initialisation data | |
632 | */ | |
633 | struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data) | |
6e5b70e9 | 634 | { |
19445b99 | 635 | struct rpc_task *task; |
6e5b70e9 | 636 | |
84115e1c | 637 | task = rpc_new_task(task_setup_data); |
19445b99 | 638 | if (IS_ERR(task)) |
50859259 | 639 | goto out; |
6e5b70e9 | 640 | |
58f9612c TM |
641 | rpc_task_set_client(task, task_setup_data->rpc_client); |
642 | rpc_task_set_rpc_message(task, task_setup_data->rpc_message); | |
643 | ||
58f9612c TM |
644 | if (task->tk_action == NULL) |
645 | rpc_call_start(task); | |
646 | ||
6e5b70e9 TM |
647 | atomic_inc(&task->tk_count); |
648 | rpc_execute(task); | |
6e5b70e9 | 649 | out: |
19445b99 | 650 | return task; |
6e5b70e9 | 651 | } |
c970aa85 | 652 | EXPORT_SYMBOL_GPL(rpc_run_task); |
6e5b70e9 TM |
653 | |
654 | /** | |
655 | * rpc_call_sync - Perform a synchronous RPC call | |
656 | * @clnt: pointer to RPC client | |
657 | * @msg: RPC call parameters | |
658 | * @flags: RPC call flags | |
1da177e4 | 659 | */ |
cbc20059 | 660 | int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags) |
1da177e4 LT |
661 | { |
662 | struct rpc_task *task; | |
84115e1c TM |
663 | struct rpc_task_setup task_setup_data = { |
664 | .rpc_client = clnt, | |
665 | .rpc_message = msg, | |
666 | .callback_ops = &rpc_default_ops, | |
667 | .flags = flags, | |
668 | }; | |
6e5b70e9 | 669 | int status; |
1da177e4 | 670 | |
1da177e4 LT |
671 | BUG_ON(flags & RPC_TASK_ASYNC); |
672 | ||
c970aa85 | 673 | task = rpc_run_task(&task_setup_data); |
6e5b70e9 TM |
674 | if (IS_ERR(task)) |
675 | return PTR_ERR(task); | |
e60859ac | 676 | status = task->tk_status; |
bde8f00c | 677 | rpc_put_task(task); |
1da177e4 LT |
678 | return status; |
679 | } | |
e8914c65 | 680 | EXPORT_SYMBOL_GPL(rpc_call_sync); |
1da177e4 | 681 | |
6e5b70e9 TM |
682 | /** |
683 | * rpc_call_async - Perform an asynchronous RPC call | |
684 | * @clnt: pointer to RPC client | |
685 | * @msg: RPC call parameters | |
686 | * @flags: RPC call flags | |
65b6e42c | 687 | * @tk_ops: RPC call ops |
6e5b70e9 | 688 | * @data: user call data |
1da177e4 LT |
689 | */ |
690 | int | |
cbc20059 | 691 | rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags, |
963d8fe5 | 692 | const struct rpc_call_ops *tk_ops, void *data) |
1da177e4 LT |
693 | { |
694 | struct rpc_task *task; | |
84115e1c TM |
695 | struct rpc_task_setup task_setup_data = { |
696 | .rpc_client = clnt, | |
697 | .rpc_message = msg, | |
698 | .callback_ops = tk_ops, | |
699 | .callback_data = data, | |
700 | .flags = flags|RPC_TASK_ASYNC, | |
701 | }; | |
1da177e4 | 702 | |
c970aa85 | 703 | task = rpc_run_task(&task_setup_data); |
6e5b70e9 TM |
704 | if (IS_ERR(task)) |
705 | return PTR_ERR(task); | |
706 | rpc_put_task(task); | |
707 | return 0; | |
1da177e4 | 708 | } |
e8914c65 | 709 | EXPORT_SYMBOL_GPL(rpc_call_async); |
1da177e4 | 710 | |
55ae1aab RL |
711 | #if defined(CONFIG_NFS_V4_1) |
712 | /** | |
713 | * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run | |
714 | * rpc_execute against it | |
7a73fdde JSR |
715 | * @req: RPC request |
716 | * @tk_ops: RPC call ops | |
55ae1aab RL |
717 | */ |
718 | struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req, | |
7a73fdde | 719 | const struct rpc_call_ops *tk_ops) |
55ae1aab RL |
720 | { |
721 | struct rpc_task *task; | |
722 | struct xdr_buf *xbufp = &req->rq_snd_buf; | |
723 | struct rpc_task_setup task_setup_data = { | |
724 | .callback_ops = tk_ops, | |
725 | }; | |
726 | ||
727 | dprintk("RPC: rpc_run_bc_task req= %p\n", req); | |
728 | /* | |
729 | * Create an rpc_task to send the data | |
730 | */ | |
731 | task = rpc_new_task(&task_setup_data); | |
19445b99 | 732 | if (IS_ERR(task)) { |
55ae1aab RL |
733 | xprt_free_bc_request(req); |
734 | goto out; | |
735 | } | |
736 | task->tk_rqstp = req; | |
737 | ||
738 | /* | |
739 | * Set up the xdr_buf length. | |
740 | * This also indicates that the buffer is XDR encoded already. | |
741 | */ | |
742 | xbufp->len = xbufp->head[0].iov_len + xbufp->page_len + | |
743 | xbufp->tail[0].iov_len; | |
744 | ||
745 | task->tk_action = call_bc_transmit; | |
746 | atomic_inc(&task->tk_count); | |
747 | BUG_ON(atomic_read(&task->tk_count) != 2); | |
748 | rpc_execute(task); | |
749 | ||
750 | out: | |
751 | dprintk("RPC: rpc_run_bc_task: task= %p\n", task); | |
752 | return task; | |
753 | } | |
754 | #endif /* CONFIG_NFS_V4_1 */ | |
755 | ||
77de2c59 TM |
756 | void |
757 | rpc_call_start(struct rpc_task *task) | |
758 | { | |
759 | task->tk_action = call_start; | |
760 | } | |
761 | EXPORT_SYMBOL_GPL(rpc_call_start); | |
762 | ||
ed39440a CL |
763 | /** |
764 | * rpc_peeraddr - extract remote peer address from clnt's xprt | |
765 | * @clnt: RPC client structure | |
766 | * @buf: target buffer | |
65b6e42c | 767 | * @bufsize: length of target buffer |
ed39440a CL |
768 | * |
769 | * Returns the number of bytes that are actually in the stored address. | |
770 | */ | |
771 | size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize) | |
772 | { | |
773 | size_t bytes; | |
774 | struct rpc_xprt *xprt = clnt->cl_xprt; | |
775 | ||
776 | bytes = sizeof(xprt->addr); | |
777 | if (bytes > bufsize) | |
778 | bytes = bufsize; | |
779 | memcpy(buf, &clnt->cl_xprt->addr, bytes); | |
c4efcb1d | 780 | return xprt->addrlen; |
ed39440a | 781 | } |
b86acd50 | 782 | EXPORT_SYMBOL_GPL(rpc_peeraddr); |
ed39440a | 783 | |
f425eba4 CL |
784 | /** |
785 | * rpc_peeraddr2str - return remote peer address in printable format | |
786 | * @clnt: RPC client structure | |
787 | * @format: address format | |
788 | * | |
789 | */ | |
b454ae90 CL |
790 | const char *rpc_peeraddr2str(struct rpc_clnt *clnt, |
791 | enum rpc_display_format_t format) | |
f425eba4 CL |
792 | { |
793 | struct rpc_xprt *xprt = clnt->cl_xprt; | |
7559c7a2 CL |
794 | |
795 | if (xprt->address_strings[format] != NULL) | |
796 | return xprt->address_strings[format]; | |
797 | else | |
798 | return "unprintable"; | |
f425eba4 | 799 | } |
b86acd50 | 800 | EXPORT_SYMBOL_GPL(rpc_peeraddr2str); |
f425eba4 | 801 | |
1da177e4 LT |
802 | void |
803 | rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize) | |
804 | { | |
805 | struct rpc_xprt *xprt = clnt->cl_xprt; | |
470056c2 CL |
806 | if (xprt->ops->set_buffer_size) |
807 | xprt->ops->set_buffer_size(xprt, sndsize, rcvsize); | |
1da177e4 | 808 | } |
e8914c65 | 809 | EXPORT_SYMBOL_GPL(rpc_setbufsize); |
1da177e4 LT |
810 | |
811 | /* | |
812 | * Return size of largest payload RPC client can support, in bytes | |
813 | * | |
814 | * For stream transports, this is one RPC record fragment (see RFC | |
815 | * 1831), as we don't support multi-record requests yet. For datagram | |
816 | * transports, this is the size of an IP packet minus the IP, UDP, and | |
817 | * RPC header sizes. | |
818 | */ | |
819 | size_t rpc_max_payload(struct rpc_clnt *clnt) | |
820 | { | |
821 | return clnt->cl_xprt->max_payload; | |
822 | } | |
b86acd50 | 823 | EXPORT_SYMBOL_GPL(rpc_max_payload); |
1da177e4 | 824 | |
35f5a422 CL |
825 | /** |
826 | * rpc_force_rebind - force transport to check that remote port is unchanged | |
827 | * @clnt: client to rebind | |
828 | * | |
829 | */ | |
830 | void rpc_force_rebind(struct rpc_clnt *clnt) | |
831 | { | |
832 | if (clnt->cl_autobind) | |
ec739ef0 | 833 | xprt_clear_bound(clnt->cl_xprt); |
35f5a422 | 834 | } |
b86acd50 | 835 | EXPORT_SYMBOL_GPL(rpc_force_rebind); |
35f5a422 | 836 | |
aae2006e AA |
837 | /* |
838 | * Restart an (async) RPC call from the call_prepare state. | |
839 | * Usually called from within the exit handler. | |
840 | */ | |
f1f88fc7 | 841 | int |
aae2006e AA |
842 | rpc_restart_call_prepare(struct rpc_task *task) |
843 | { | |
844 | if (RPC_ASSASSINATED(task)) | |
f1f88fc7 | 845 | return 0; |
aae2006e | 846 | task->tk_action = rpc_prepare_task; |
f1f88fc7 | 847 | return 1; |
aae2006e AA |
848 | } |
849 | EXPORT_SYMBOL_GPL(rpc_restart_call_prepare); | |
850 | ||
1da177e4 LT |
851 | /* |
852 | * Restart an (async) RPC call. Usually called from within the | |
853 | * exit handler. | |
854 | */ | |
f1f88fc7 | 855 | int |
1da177e4 LT |
856 | rpc_restart_call(struct rpc_task *task) |
857 | { | |
858 | if (RPC_ASSASSINATED(task)) | |
f1f88fc7 | 859 | return 0; |
1da177e4 | 860 | task->tk_action = call_start; |
f1f88fc7 | 861 | return 1; |
1da177e4 | 862 | } |
e8914c65 | 863 | EXPORT_SYMBOL_GPL(rpc_restart_call); |
1da177e4 | 864 | |
3748f1e4 CL |
865 | #ifdef RPC_DEBUG |
866 | static const char *rpc_proc_name(const struct rpc_task *task) | |
867 | { | |
868 | const struct rpc_procinfo *proc = task->tk_msg.rpc_proc; | |
869 | ||
870 | if (proc) { | |
871 | if (proc->p_name) | |
872 | return proc->p_name; | |
873 | else | |
874 | return "NULL"; | |
875 | } else | |
876 | return "no proc"; | |
877 | } | |
878 | #endif | |
879 | ||
1da177e4 LT |
880 | /* |
881 | * 0. Initial state | |
882 | * | |
883 | * Other FSM states can be visited zero or more times, but | |
884 | * this state is visited exactly once for each RPC. | |
885 | */ | |
886 | static void | |
887 | call_start(struct rpc_task *task) | |
888 | { | |
889 | struct rpc_clnt *clnt = task->tk_client; | |
890 | ||
3748f1e4 | 891 | dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task->tk_pid, |
46121cf7 | 892 | clnt->cl_protname, clnt->cl_vers, |
3748f1e4 | 893 | rpc_proc_name(task), |
46121cf7 | 894 | (RPC_IS_ASYNC(task) ? "async" : "sync")); |
1da177e4 LT |
895 | |
896 | /* Increment call count */ | |
897 | task->tk_msg.rpc_proc->p_count++; | |
898 | clnt->cl_stats->rpccnt++; | |
899 | task->tk_action = call_reserve; | |
900 | } | |
901 | ||
902 | /* | |
903 | * 1. Reserve an RPC call slot | |
904 | */ | |
905 | static void | |
906 | call_reserve(struct rpc_task *task) | |
907 | { | |
46121cf7 | 908 | dprint_status(task); |
1da177e4 | 909 | |
1da177e4 LT |
910 | task->tk_status = 0; |
911 | task->tk_action = call_reserveresult; | |
912 | xprt_reserve(task); | |
913 | } | |
914 | ||
915 | /* | |
916 | * 1b. Grok the result of xprt_reserve() | |
917 | */ | |
918 | static void | |
919 | call_reserveresult(struct rpc_task *task) | |
920 | { | |
921 | int status = task->tk_status; | |
922 | ||
46121cf7 | 923 | dprint_status(task); |
1da177e4 LT |
924 | |
925 | /* | |
926 | * After a call to xprt_reserve(), we must have either | |
927 | * a request slot or else an error status. | |
928 | */ | |
929 | task->tk_status = 0; | |
930 | if (status >= 0) { | |
931 | if (task->tk_rqstp) { | |
f2d47d02 | 932 | task->tk_action = call_refresh; |
1da177e4 LT |
933 | return; |
934 | } | |
935 | ||
936 | printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n", | |
0dc47877 | 937 | __func__, status); |
1da177e4 LT |
938 | rpc_exit(task, -EIO); |
939 | return; | |
940 | } | |
941 | ||
942 | /* | |
943 | * Even though there was an error, we may have acquired | |
944 | * a request slot somehow. Make sure not to leak it. | |
945 | */ | |
946 | if (task->tk_rqstp) { | |
947 | printk(KERN_ERR "%s: status=%d, request allocated anyway\n", | |
0dc47877 | 948 | __func__, status); |
1da177e4 LT |
949 | xprt_release(task); |
950 | } | |
951 | ||
952 | switch (status) { | |
953 | case -EAGAIN: /* woken up; retry */ | |
954 | task->tk_action = call_reserve; | |
955 | return; | |
956 | case -EIO: /* probably a shutdown */ | |
957 | break; | |
958 | default: | |
959 | printk(KERN_ERR "%s: unrecognized error %d, exiting\n", | |
0dc47877 | 960 | __func__, status); |
1da177e4 LT |
961 | break; |
962 | } | |
963 | rpc_exit(task, status); | |
964 | } | |
965 | ||
966 | /* | |
55576244 BF |
967 | * 2. Bind and/or refresh the credentials |
968 | */ | |
969 | static void | |
970 | call_refresh(struct rpc_task *task) | |
971 | { | |
972 | dprint_status(task); | |
973 | ||
974 | task->tk_action = call_refreshresult; | |
975 | task->tk_status = 0; | |
976 | task->tk_client->cl_stats->rpcauthrefresh++; | |
977 | rpcauth_refreshcred(task); | |
978 | } | |
979 | ||
980 | /* | |
981 | * 2a. Process the results of a credential refresh | |
982 | */ | |
983 | static void | |
984 | call_refreshresult(struct rpc_task *task) | |
985 | { | |
986 | int status = task->tk_status; | |
987 | ||
988 | dprint_status(task); | |
989 | ||
990 | task->tk_status = 0; | |
5fc43978 | 991 | task->tk_action = call_refresh; |
55576244 | 992 | switch (status) { |
5fc43978 TM |
993 | case 0: |
994 | if (rpcauth_uptodatecred(task)) | |
995 | task->tk_action = call_allocate; | |
55576244 BF |
996 | return; |
997 | case -ETIMEDOUT: | |
998 | rpc_delay(task, 3*HZ); | |
5fc43978 TM |
999 | case -EAGAIN: |
1000 | status = -EACCES; | |
1001 | if (!task->tk_cred_retry) | |
1002 | break; | |
1003 | task->tk_cred_retry--; | |
1004 | dprintk("RPC: %5u %s: retry refresh creds\n", | |
1005 | task->tk_pid, __func__); | |
1006 | return; | |
55576244 | 1007 | } |
5fc43978 TM |
1008 | dprintk("RPC: %5u %s: refresh creds failed with error %d\n", |
1009 | task->tk_pid, __func__, status); | |
1010 | rpc_exit(task, status); | |
55576244 BF |
1011 | } |
1012 | ||
1013 | /* | |
1014 | * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc. | |
02107148 | 1015 | * (Note: buffer memory is freed in xprt_release). |
1da177e4 LT |
1016 | */ |
1017 | static void | |
1018 | call_allocate(struct rpc_task *task) | |
1019 | { | |
f2d47d02 | 1020 | unsigned int slack = task->tk_rqstp->rq_cred->cr_auth->au_cslack; |
02107148 CL |
1021 | struct rpc_rqst *req = task->tk_rqstp; |
1022 | struct rpc_xprt *xprt = task->tk_xprt; | |
2bea90d4 | 1023 | struct rpc_procinfo *proc = task->tk_msg.rpc_proc; |
1da177e4 | 1024 | |
46121cf7 CL |
1025 | dprint_status(task); |
1026 | ||
2bea90d4 | 1027 | task->tk_status = 0; |
f2d47d02 | 1028 | task->tk_action = call_bind; |
2bea90d4 | 1029 | |
02107148 | 1030 | if (req->rq_buffer) |
1da177e4 LT |
1031 | return; |
1032 | ||
2bea90d4 CL |
1033 | if (proc->p_proc != 0) { |
1034 | BUG_ON(proc->p_arglen == 0); | |
1035 | if (proc->p_decode != NULL) | |
1036 | BUG_ON(proc->p_replen == 0); | |
1037 | } | |
1da177e4 | 1038 | |
2bea90d4 CL |
1039 | /* |
1040 | * Calculate the size (in quads) of the RPC call | |
1041 | * and reply headers, and convert both values | |
1042 | * to byte sizes. | |
1043 | */ | |
1044 | req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen; | |
1045 | req->rq_callsize <<= 2; | |
1046 | req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen; | |
1047 | req->rq_rcvsize <<= 2; | |
1048 | ||
c5a4dd8b CL |
1049 | req->rq_buffer = xprt->ops->buf_alloc(task, |
1050 | req->rq_callsize + req->rq_rcvsize); | |
2bea90d4 | 1051 | if (req->rq_buffer != NULL) |
1da177e4 | 1052 | return; |
46121cf7 CL |
1053 | |
1054 | dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid); | |
1da177e4 | 1055 | |
14b218a8 | 1056 | if (RPC_IS_ASYNC(task) || !signalled()) { |
b6e9c713 | 1057 | task->tk_action = call_allocate; |
1da177e4 LT |
1058 | rpc_delay(task, HZ>>4); |
1059 | return; | |
1060 | } | |
1061 | ||
1062 | rpc_exit(task, -ERESTARTSYS); | |
1063 | } | |
1064 | ||
940e3318 TM |
1065 | static inline int |
1066 | rpc_task_need_encode(struct rpc_task *task) | |
1067 | { | |
1068 | return task->tk_rqstp->rq_snd_buf.len == 0; | |
1069 | } | |
1070 | ||
1071 | static inline void | |
1072 | rpc_task_force_reencode(struct rpc_task *task) | |
1073 | { | |
1074 | task->tk_rqstp->rq_snd_buf.len = 0; | |
2574cc9f | 1075 | task->tk_rqstp->rq_bytes_sent = 0; |
940e3318 TM |
1076 | } |
1077 | ||
2bea90d4 CL |
1078 | static inline void |
1079 | rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len) | |
1080 | { | |
1081 | buf->head[0].iov_base = start; | |
1082 | buf->head[0].iov_len = len; | |
1083 | buf->tail[0].iov_len = 0; | |
1084 | buf->page_len = 0; | |
4f22ccc3 | 1085 | buf->flags = 0; |
2bea90d4 CL |
1086 | buf->len = 0; |
1087 | buf->buflen = len; | |
1088 | } | |
1089 | ||
1da177e4 LT |
1090 | /* |
1091 | * 3. Encode arguments of an RPC call | |
1092 | */ | |
1093 | static void | |
b0e1c57e | 1094 | rpc_xdr_encode(struct rpc_task *task) |
1da177e4 | 1095 | { |
1da177e4 | 1096 | struct rpc_rqst *req = task->tk_rqstp; |
9f06c719 | 1097 | kxdreproc_t encode; |
d8ed029d | 1098 | __be32 *p; |
1da177e4 | 1099 | |
46121cf7 | 1100 | dprint_status(task); |
1da177e4 | 1101 | |
2bea90d4 CL |
1102 | rpc_xdr_buf_init(&req->rq_snd_buf, |
1103 | req->rq_buffer, | |
1104 | req->rq_callsize); | |
1105 | rpc_xdr_buf_init(&req->rq_rcv_buf, | |
1106 | (char *)req->rq_buffer + req->rq_callsize, | |
1107 | req->rq_rcvsize); | |
1da177e4 | 1108 | |
b0e1c57e CL |
1109 | p = rpc_encode_header(task); |
1110 | if (p == NULL) { | |
1111 | printk(KERN_INFO "RPC: couldn't encode RPC header, exit EIO\n"); | |
1da177e4 LT |
1112 | rpc_exit(task, -EIO); |
1113 | return; | |
1114 | } | |
b0e1c57e CL |
1115 | |
1116 | encode = task->tk_msg.rpc_proc->p_encode; | |
f3680312 BF |
1117 | if (encode == NULL) |
1118 | return; | |
1119 | ||
1120 | task->tk_status = rpcauth_wrap_req(task, encode, req, p, | |
1121 | task->tk_msg.rpc_argp); | |
1da177e4 LT |
1122 | } |
1123 | ||
1124 | /* | |
1125 | * 4. Get the server port number if not yet set | |
1126 | */ | |
1127 | static void | |
1128 | call_bind(struct rpc_task *task) | |
1129 | { | |
ec739ef0 | 1130 | struct rpc_xprt *xprt = task->tk_xprt; |
1da177e4 | 1131 | |
46121cf7 | 1132 | dprint_status(task); |
1da177e4 | 1133 | |
da351878 | 1134 | task->tk_action = call_connect; |
ec739ef0 | 1135 | if (!xprt_bound(xprt)) { |
da351878 | 1136 | task->tk_action = call_bind_status; |
ec739ef0 | 1137 | task->tk_timeout = xprt->bind_timeout; |
bbf7c1dd | 1138 | xprt->ops->rpcbind(task); |
1da177e4 LT |
1139 | } |
1140 | } | |
1141 | ||
1142 | /* | |
da351878 CL |
1143 | * 4a. Sort out bind result |
1144 | */ | |
1145 | static void | |
1146 | call_bind_status(struct rpc_task *task) | |
1147 | { | |
906462af | 1148 | int status = -EIO; |
da351878 CL |
1149 | |
1150 | if (task->tk_status >= 0) { | |
46121cf7 | 1151 | dprint_status(task); |
da351878 CL |
1152 | task->tk_status = 0; |
1153 | task->tk_action = call_connect; | |
1154 | return; | |
1155 | } | |
1156 | ||
1157 | switch (task->tk_status) { | |
381ba74a TM |
1158 | case -ENOMEM: |
1159 | dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid); | |
1160 | rpc_delay(task, HZ >> 2); | |
2429cbf6 | 1161 | goto retry_timeout; |
da351878 | 1162 | case -EACCES: |
46121cf7 CL |
1163 | dprintk("RPC: %5u remote rpcbind: RPC program/version " |
1164 | "unavailable\n", task->tk_pid); | |
b79dc8ce CL |
1165 | /* fail immediately if this is an RPC ping */ |
1166 | if (task->tk_msg.rpc_proc->p_proc == 0) { | |
1167 | status = -EOPNOTSUPP; | |
1168 | break; | |
1169 | } | |
ea635a51 | 1170 | rpc_delay(task, 3*HZ); |
da45828e | 1171 | goto retry_timeout; |
da351878 | 1172 | case -ETIMEDOUT: |
46121cf7 | 1173 | dprintk("RPC: %5u rpcbind request timed out\n", |
da351878 | 1174 | task->tk_pid); |
da45828e | 1175 | goto retry_timeout; |
da351878 | 1176 | case -EPFNOSUPPORT: |
906462af | 1177 | /* server doesn't support any rpcbind version we know of */ |
012da158 | 1178 | dprintk("RPC: %5u unrecognized remote rpcbind service\n", |
da351878 CL |
1179 | task->tk_pid); |
1180 | break; | |
1181 | case -EPROTONOSUPPORT: | |
00a6e7bb | 1182 | dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n", |
da351878 | 1183 | task->tk_pid); |
00a6e7bb CL |
1184 | task->tk_status = 0; |
1185 | task->tk_action = call_bind; | |
1186 | return; | |
012da158 CL |
1187 | case -ECONNREFUSED: /* connection problems */ |
1188 | case -ECONNRESET: | |
1189 | case -ENOTCONN: | |
1190 | case -EHOSTDOWN: | |
1191 | case -EHOSTUNREACH: | |
1192 | case -ENETUNREACH: | |
1193 | case -EPIPE: | |
1194 | dprintk("RPC: %5u remote rpcbind unreachable: %d\n", | |
1195 | task->tk_pid, task->tk_status); | |
1196 | if (!RPC_IS_SOFTCONN(task)) { | |
1197 | rpc_delay(task, 5*HZ); | |
1198 | goto retry_timeout; | |
1199 | } | |
1200 | status = task->tk_status; | |
1201 | break; | |
da351878 | 1202 | default: |
46121cf7 | 1203 | dprintk("RPC: %5u unrecognized rpcbind error (%d)\n", |
da351878 | 1204 | task->tk_pid, -task->tk_status); |
da351878 CL |
1205 | } |
1206 | ||
1207 | rpc_exit(task, status); | |
1208 | return; | |
1209 | ||
da45828e TM |
1210 | retry_timeout: |
1211 | task->tk_action = call_timeout; | |
da351878 CL |
1212 | } |
1213 | ||
1214 | /* | |
1215 | * 4b. Connect to the RPC server | |
1da177e4 LT |
1216 | */ |
1217 | static void | |
1218 | call_connect(struct rpc_task *task) | |
1219 | { | |
da351878 | 1220 | struct rpc_xprt *xprt = task->tk_xprt; |
1da177e4 | 1221 | |
46121cf7 | 1222 | dprintk("RPC: %5u call_connect xprt %p %s connected\n", |
da351878 CL |
1223 | task->tk_pid, xprt, |
1224 | (xprt_connected(xprt) ? "is" : "is not")); | |
1da177e4 | 1225 | |
da351878 CL |
1226 | task->tk_action = call_transmit; |
1227 | if (!xprt_connected(xprt)) { | |
1228 | task->tk_action = call_connect_status; | |
1229 | if (task->tk_status < 0) | |
1230 | return; | |
1231 | xprt_connect(task); | |
1da177e4 | 1232 | } |
1da177e4 LT |
1233 | } |
1234 | ||
1235 | /* | |
da351878 | 1236 | * 4c. Sort out connect result |
1da177e4 LT |
1237 | */ |
1238 | static void | |
1239 | call_connect_status(struct rpc_task *task) | |
1240 | { | |
1241 | struct rpc_clnt *clnt = task->tk_client; | |
1242 | int status = task->tk_status; | |
1243 | ||
46121cf7 | 1244 | dprint_status(task); |
da351878 | 1245 | |
1da177e4 | 1246 | task->tk_status = 0; |
2a491991 | 1247 | if (status >= 0 || status == -EAGAIN) { |
1da177e4 LT |
1248 | clnt->cl_stats->netreconn++; |
1249 | task->tk_action = call_transmit; | |
1250 | return; | |
1251 | } | |
1252 | ||
1da177e4 | 1253 | switch (status) { |
da45828e TM |
1254 | /* if soft mounted, test if we've timed out */ |
1255 | case -ETIMEDOUT: | |
1256 | task->tk_action = call_timeout; | |
2a491991 TM |
1257 | break; |
1258 | default: | |
1259 | rpc_exit(task, -EIO); | |
1da177e4 LT |
1260 | } |
1261 | } | |
1262 | ||
1263 | /* | |
1264 | * 5. Transmit the RPC request, and wait for reply | |
1265 | */ | |
1266 | static void | |
1267 | call_transmit(struct rpc_task *task) | |
1268 | { | |
46121cf7 | 1269 | dprint_status(task); |
1da177e4 LT |
1270 | |
1271 | task->tk_action = call_status; | |
1272 | if (task->tk_status < 0) | |
1273 | return; | |
1274 | task->tk_status = xprt_prepare_transmit(task); | |
1275 | if (task->tk_status != 0) | |
1276 | return; | |
e0ab53de | 1277 | task->tk_action = call_transmit_status; |
1da177e4 | 1278 | /* Encode here so that rpcsec_gss can use correct sequence number. */ |
940e3318 | 1279 | if (rpc_task_need_encode(task)) { |
e0ab53de | 1280 | BUG_ON(task->tk_rqstp->rq_bytes_sent != 0); |
b0e1c57e | 1281 | rpc_xdr_encode(task); |
5e5ce5be | 1282 | /* Did the encode result in an error condition? */ |
8b39f2b4 TM |
1283 | if (task->tk_status != 0) { |
1284 | /* Was the error nonfatal? */ | |
1285 | if (task->tk_status == -EAGAIN) | |
1286 | rpc_delay(task, HZ >> 4); | |
1287 | else | |
1288 | rpc_exit(task, task->tk_status); | |
e0ab53de | 1289 | return; |
8b39f2b4 | 1290 | } |
5e5ce5be | 1291 | } |
1da177e4 LT |
1292 | xprt_transmit(task); |
1293 | if (task->tk_status < 0) | |
1294 | return; | |
e0ab53de TM |
1295 | /* |
1296 | * On success, ensure that we call xprt_end_transmit() before sleeping | |
1297 | * in order to allow access to the socket to other RPC requests. | |
1298 | */ | |
1299 | call_transmit_status(task); | |
55ae1aab | 1300 | if (rpc_reply_expected(task)) |
e0ab53de TM |
1301 | return; |
1302 | task->tk_action = rpc_exit_task; | |
fda13939 | 1303 | rpc_wake_up_queued_task(&task->tk_xprt->pending, task); |
e0ab53de TM |
1304 | } |
1305 | ||
1306 | /* | |
1307 | * 5a. Handle cleanup after a transmission | |
1308 | */ | |
1309 | static void | |
1310 | call_transmit_status(struct rpc_task *task) | |
1311 | { | |
1312 | task->tk_action = call_status; | |
206a134b CL |
1313 | |
1314 | /* | |
1315 | * Common case: success. Force the compiler to put this | |
1316 | * test first. | |
1317 | */ | |
1318 | if (task->tk_status == 0) { | |
1319 | xprt_end_transmit(task); | |
1320 | rpc_task_force_reencode(task); | |
1321 | return; | |
1322 | } | |
1323 | ||
15f081ca TM |
1324 | switch (task->tk_status) { |
1325 | case -EAGAIN: | |
1326 | break; | |
1327 | default: | |
206a134b | 1328 | dprint_status(task); |
15f081ca | 1329 | xprt_end_transmit(task); |
09a21c41 CL |
1330 | rpc_task_force_reencode(task); |
1331 | break; | |
15f081ca TM |
1332 | /* |
1333 | * Special cases: if we've been waiting on the | |
1334 | * socket's write_space() callback, or if the | |
1335 | * socket just returned a connection error, | |
1336 | * then hold onto the transport lock. | |
1337 | */ | |
1338 | case -ECONNREFUSED: | |
15f081ca TM |
1339 | case -EHOSTDOWN: |
1340 | case -EHOSTUNREACH: | |
1341 | case -ENETUNREACH: | |
09a21c41 CL |
1342 | if (RPC_IS_SOFTCONN(task)) { |
1343 | xprt_end_transmit(task); | |
1344 | rpc_exit(task, task->tk_status); | |
1345 | break; | |
1346 | } | |
1347 | case -ECONNRESET: | |
1348 | case -ENOTCONN: | |
c8485e4d | 1349 | case -EPIPE: |
15f081ca TM |
1350 | rpc_task_force_reencode(task); |
1351 | } | |
1da177e4 LT |
1352 | } |
1353 | ||
55ae1aab RL |
1354 | #if defined(CONFIG_NFS_V4_1) |
1355 | /* | |
1356 | * 5b. Send the backchannel RPC reply. On error, drop the reply. In | |
1357 | * addition, disconnect on connectivity errors. | |
1358 | */ | |
1359 | static void | |
1360 | call_bc_transmit(struct rpc_task *task) | |
1361 | { | |
1362 | struct rpc_rqst *req = task->tk_rqstp; | |
1363 | ||
1364 | BUG_ON(task->tk_status != 0); | |
1365 | task->tk_status = xprt_prepare_transmit(task); | |
1366 | if (task->tk_status == -EAGAIN) { | |
1367 | /* | |
1368 | * Could not reserve the transport. Try again after the | |
1369 | * transport is released. | |
1370 | */ | |
1371 | task->tk_status = 0; | |
1372 | task->tk_action = call_bc_transmit; | |
1373 | return; | |
1374 | } | |
1375 | ||
1376 | task->tk_action = rpc_exit_task; | |
1377 | if (task->tk_status < 0) { | |
1378 | printk(KERN_NOTICE "RPC: Could not send backchannel reply " | |
1379 | "error: %d\n", task->tk_status); | |
1380 | return; | |
1381 | } | |
1382 | ||
1383 | xprt_transmit(task); | |
1384 | xprt_end_transmit(task); | |
1385 | dprint_status(task); | |
1386 | switch (task->tk_status) { | |
1387 | case 0: | |
1388 | /* Success */ | |
1389 | break; | |
1390 | case -EHOSTDOWN: | |
1391 | case -EHOSTUNREACH: | |
1392 | case -ENETUNREACH: | |
1393 | case -ETIMEDOUT: | |
1394 | /* | |
1395 | * Problem reaching the server. Disconnect and let the | |
1396 | * forechannel reestablish the connection. The server will | |
1397 | * have to retransmit the backchannel request and we'll | |
1398 | * reprocess it. Since these ops are idempotent, there's no | |
1399 | * need to cache our reply at this time. | |
1400 | */ | |
1401 | printk(KERN_NOTICE "RPC: Could not send backchannel reply " | |
1402 | "error: %d\n", task->tk_status); | |
1403 | xprt_conditional_disconnect(task->tk_xprt, | |
1404 | req->rq_connect_cookie); | |
1405 | break; | |
1406 | default: | |
1407 | /* | |
1408 | * We were unable to reply and will have to drop the | |
1409 | * request. The server should reconnect and retransmit. | |
1410 | */ | |
1411 | BUG_ON(task->tk_status == -EAGAIN); | |
1412 | printk(KERN_NOTICE "RPC: Could not send backchannel reply " | |
1413 | "error: %d\n", task->tk_status); | |
1414 | break; | |
1415 | } | |
1416 | rpc_wake_up_queued_task(&req->rq_xprt->pending, task); | |
1417 | } | |
1418 | #endif /* CONFIG_NFS_V4_1 */ | |
1419 | ||
1da177e4 LT |
1420 | /* |
1421 | * 6. Sort out the RPC call status | |
1422 | */ | |
1423 | static void | |
1424 | call_status(struct rpc_task *task) | |
1425 | { | |
1426 | struct rpc_clnt *clnt = task->tk_client; | |
1427 | struct rpc_rqst *req = task->tk_rqstp; | |
1428 | int status; | |
1429 | ||
dd2b63d0 RL |
1430 | if (req->rq_reply_bytes_recvd > 0 && !req->rq_bytes_sent) |
1431 | task->tk_status = req->rq_reply_bytes_recvd; | |
1da177e4 | 1432 | |
46121cf7 | 1433 | dprint_status(task); |
1da177e4 LT |
1434 | |
1435 | status = task->tk_status; | |
1436 | if (status >= 0) { | |
1437 | task->tk_action = call_decode; | |
1438 | return; | |
1439 | } | |
1440 | ||
1441 | task->tk_status = 0; | |
1442 | switch(status) { | |
76303992 TM |
1443 | case -EHOSTDOWN: |
1444 | case -EHOSTUNREACH: | |
1445 | case -ENETUNREACH: | |
1446 | /* | |
1447 | * Delay any retries for 3 seconds, then handle as if it | |
1448 | * were a timeout. | |
1449 | */ | |
1450 | rpc_delay(task, 3*HZ); | |
1da177e4 LT |
1451 | case -ETIMEDOUT: |
1452 | task->tk_action = call_timeout; | |
241c39b9 | 1453 | if (task->tk_client->cl_discrtry) |
7c1d71cf TM |
1454 | xprt_conditional_disconnect(task->tk_xprt, |
1455 | req->rq_connect_cookie); | |
1da177e4 | 1456 | break; |
c8485e4d | 1457 | case -ECONNRESET: |
1da177e4 | 1458 | case -ECONNREFUSED: |
35f5a422 | 1459 | rpc_force_rebind(clnt); |
c8485e4d TM |
1460 | rpc_delay(task, 3*HZ); |
1461 | case -EPIPE: | |
1462 | case -ENOTCONN: | |
1da177e4 LT |
1463 | task->tk_action = call_bind; |
1464 | break; | |
1465 | case -EAGAIN: | |
1466 | task->tk_action = call_transmit; | |
1467 | break; | |
1468 | case -EIO: | |
1469 | /* shutdown or soft timeout */ | |
1470 | rpc_exit(task, status); | |
1471 | break; | |
1472 | default: | |
b6b6152c OK |
1473 | if (clnt->cl_chatty) |
1474 | printk("%s: RPC call returned error %d\n", | |
1da177e4 LT |
1475 | clnt->cl_protname, -status); |
1476 | rpc_exit(task, status); | |
1da177e4 LT |
1477 | } |
1478 | } | |
1479 | ||
1480 | /* | |
e0ab53de | 1481 | * 6a. Handle RPC timeout |
1da177e4 LT |
1482 | * We do not release the request slot, so we keep using the |
1483 | * same XID for all retransmits. | |
1484 | */ | |
1485 | static void | |
1486 | call_timeout(struct rpc_task *task) | |
1487 | { | |
1488 | struct rpc_clnt *clnt = task->tk_client; | |
1489 | ||
1490 | if (xprt_adjust_timeout(task->tk_rqstp) == 0) { | |
46121cf7 | 1491 | dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid); |
1da177e4 LT |
1492 | goto retry; |
1493 | } | |
1494 | ||
46121cf7 | 1495 | dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid); |
ef759a2e CL |
1496 | task->tk_timeouts++; |
1497 | ||
3a28becc CL |
1498 | if (RPC_IS_SOFTCONN(task)) { |
1499 | rpc_exit(task, -ETIMEDOUT); | |
1500 | return; | |
1501 | } | |
1da177e4 | 1502 | if (RPC_IS_SOFT(task)) { |
b6b6152c OK |
1503 | if (clnt->cl_chatty) |
1504 | printk(KERN_NOTICE "%s: server %s not responding, timed out\n", | |
1da177e4 | 1505 | clnt->cl_protname, clnt->cl_server); |
7494d00c TM |
1506 | if (task->tk_flags & RPC_TASK_TIMEOUT) |
1507 | rpc_exit(task, -ETIMEDOUT); | |
1508 | else | |
1509 | rpc_exit(task, -EIO); | |
1da177e4 LT |
1510 | return; |
1511 | } | |
1512 | ||
f518e35a | 1513 | if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) { |
1da177e4 | 1514 | task->tk_flags |= RPC_CALL_MAJORSEEN; |
b6b6152c OK |
1515 | if (clnt->cl_chatty) |
1516 | printk(KERN_NOTICE "%s: server %s not responding, still trying\n", | |
1da177e4 LT |
1517 | clnt->cl_protname, clnt->cl_server); |
1518 | } | |
35f5a422 | 1519 | rpc_force_rebind(clnt); |
b48633bd TM |
1520 | /* |
1521 | * Did our request time out due to an RPCSEC_GSS out-of-sequence | |
1522 | * event? RFC2203 requires the server to drop all such requests. | |
1523 | */ | |
1524 | rpcauth_invalcred(task); | |
1da177e4 LT |
1525 | |
1526 | retry: | |
1527 | clnt->cl_stats->rpcretrans++; | |
1528 | task->tk_action = call_bind; | |
1529 | task->tk_status = 0; | |
1530 | } | |
1531 | ||
1532 | /* | |
1533 | * 7. Decode the RPC reply | |
1534 | */ | |
1535 | static void | |
1536 | call_decode(struct rpc_task *task) | |
1537 | { | |
1538 | struct rpc_clnt *clnt = task->tk_client; | |
1539 | struct rpc_rqst *req = task->tk_rqstp; | |
bf269551 | 1540 | kxdrdproc_t decode = task->tk_msg.rpc_proc->p_decode; |
d8ed029d | 1541 | __be32 *p; |
1da177e4 | 1542 | |
46121cf7 CL |
1543 | dprintk("RPC: %5u call_decode (status %d)\n", |
1544 | task->tk_pid, task->tk_status); | |
1da177e4 | 1545 | |
f518e35a | 1546 | if (task->tk_flags & RPC_CALL_MAJORSEEN) { |
b6b6152c OK |
1547 | if (clnt->cl_chatty) |
1548 | printk(KERN_NOTICE "%s: server %s OK\n", | |
1549 | clnt->cl_protname, clnt->cl_server); | |
1da177e4 LT |
1550 | task->tk_flags &= ~RPC_CALL_MAJORSEEN; |
1551 | } | |
1552 | ||
43ac3f29 TM |
1553 | /* |
1554 | * Ensure that we see all writes made by xprt_complete_rqst() | |
dd2b63d0 | 1555 | * before it changed req->rq_reply_bytes_recvd. |
43ac3f29 TM |
1556 | */ |
1557 | smp_rmb(); | |
1da177e4 LT |
1558 | req->rq_rcv_buf.len = req->rq_private_buf.len; |
1559 | ||
1560 | /* Check that the softirq receive buffer is valid */ | |
1561 | WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf, | |
1562 | sizeof(req->rq_rcv_buf)) != 0); | |
1563 | ||
1e799b67 TM |
1564 | if (req->rq_rcv_buf.len < 12) { |
1565 | if (!RPC_IS_SOFT(task)) { | |
1566 | task->tk_action = call_bind; | |
1567 | clnt->cl_stats->rpcretrans++; | |
1568 | goto out_retry; | |
1569 | } | |
1570 | dprintk("RPC: %s: too small RPC reply size (%d bytes)\n", | |
1571 | clnt->cl_protname, task->tk_status); | |
1572 | task->tk_action = call_timeout; | |
1573 | goto out_retry; | |
1574 | } | |
1575 | ||
b0e1c57e | 1576 | p = rpc_verify_header(task); |
abbcf28f TM |
1577 | if (IS_ERR(p)) { |
1578 | if (p == ERR_PTR(-EAGAIN)) | |
1579 | goto out_retry; | |
1580 | return; | |
1da177e4 LT |
1581 | } |
1582 | ||
abbcf28f | 1583 | task->tk_action = rpc_exit_task; |
1da177e4 | 1584 | |
6d5fcb5a | 1585 | if (decode) { |
1da177e4 LT |
1586 | task->tk_status = rpcauth_unwrap_resp(task, decode, req, p, |
1587 | task->tk_msg.rpc_resp); | |
6d5fcb5a | 1588 | } |
46121cf7 CL |
1589 | dprintk("RPC: %5u call_decode result %d\n", task->tk_pid, |
1590 | task->tk_status); | |
1da177e4 LT |
1591 | return; |
1592 | out_retry: | |
1da177e4 | 1593 | task->tk_status = 0; |
b0e1c57e | 1594 | /* Note: rpc_verify_header() may have freed the RPC slot */ |
24b74bf0 | 1595 | if (task->tk_rqstp == req) { |
dd2b63d0 | 1596 | req->rq_reply_bytes_recvd = req->rq_rcv_buf.len = 0; |
24b74bf0 | 1597 | if (task->tk_client->cl_discrtry) |
7c1d71cf TM |
1598 | xprt_conditional_disconnect(task->tk_xprt, |
1599 | req->rq_connect_cookie); | |
24b74bf0 | 1600 | } |
1da177e4 LT |
1601 | } |
1602 | ||
d8ed029d | 1603 | static __be32 * |
b0e1c57e | 1604 | rpc_encode_header(struct rpc_task *task) |
1da177e4 LT |
1605 | { |
1606 | struct rpc_clnt *clnt = task->tk_client; | |
1da177e4 | 1607 | struct rpc_rqst *req = task->tk_rqstp; |
d8ed029d | 1608 | __be32 *p = req->rq_svec[0].iov_base; |
1da177e4 LT |
1609 | |
1610 | /* FIXME: check buffer size? */ | |
808012fb CL |
1611 | |
1612 | p = xprt_skip_transport_header(task->tk_xprt, p); | |
1da177e4 LT |
1613 | *p++ = req->rq_xid; /* XID */ |
1614 | *p++ = htonl(RPC_CALL); /* CALL */ | |
1615 | *p++ = htonl(RPC_VERSION); /* RPC version */ | |
1616 | *p++ = htonl(clnt->cl_prog); /* program number */ | |
1617 | *p++ = htonl(clnt->cl_vers); /* program version */ | |
1618 | *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */ | |
334ccfd5 TM |
1619 | p = rpcauth_marshcred(task, p); |
1620 | req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p); | |
1621 | return p; | |
1da177e4 LT |
1622 | } |
1623 | ||
d8ed029d | 1624 | static __be32 * |
b0e1c57e | 1625 | rpc_verify_header(struct rpc_task *task) |
1da177e4 LT |
1626 | { |
1627 | struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0]; | |
1628 | int len = task->tk_rqstp->rq_rcv_buf.len >> 2; | |
d8ed029d AD |
1629 | __be32 *p = iov->iov_base; |
1630 | u32 n; | |
1da177e4 LT |
1631 | int error = -EACCES; |
1632 | ||
e8896495 DH |
1633 | if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) { |
1634 | /* RFC-1014 says that the representation of XDR data must be a | |
1635 | * multiple of four bytes | |
1636 | * - if it isn't pointer subtraction in the NFS client may give | |
1637 | * undefined results | |
1638 | */ | |
8a702bbb | 1639 | dprintk("RPC: %5u %s: XDR representation not a multiple of" |
0dc47877 | 1640 | " 4 bytes: 0x%x\n", task->tk_pid, __func__, |
8a702bbb | 1641 | task->tk_rqstp->rq_rcv_buf.len); |
e8896495 DH |
1642 | goto out_eio; |
1643 | } | |
1da177e4 LT |
1644 | if ((len -= 3) < 0) |
1645 | goto out_overflow; | |
1da177e4 | 1646 | |
f4a2e418 | 1647 | p += 1; /* skip XID */ |
1da177e4 | 1648 | if ((n = ntohl(*p++)) != RPC_REPLY) { |
8a702bbb | 1649 | dprintk("RPC: %5u %s: not an RPC reply: %x\n", |
f4a2e418 | 1650 | task->tk_pid, __func__, n); |
abbcf28f | 1651 | goto out_garbage; |
1da177e4 | 1652 | } |
f4a2e418 | 1653 | |
1da177e4 LT |
1654 | if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) { |
1655 | if (--len < 0) | |
1656 | goto out_overflow; | |
1657 | switch ((n = ntohl(*p++))) { | |
1658 | case RPC_AUTH_ERROR: | |
1659 | break; | |
1660 | case RPC_MISMATCH: | |
46121cf7 CL |
1661 | dprintk("RPC: %5u %s: RPC call version " |
1662 | "mismatch!\n", | |
0dc47877 | 1663 | task->tk_pid, __func__); |
cdf47706 AG |
1664 | error = -EPROTONOSUPPORT; |
1665 | goto out_err; | |
1da177e4 | 1666 | default: |
46121cf7 CL |
1667 | dprintk("RPC: %5u %s: RPC call rejected, " |
1668 | "unknown error: %x\n", | |
0dc47877 | 1669 | task->tk_pid, __func__, n); |
1da177e4 LT |
1670 | goto out_eio; |
1671 | } | |
1672 | if (--len < 0) | |
1673 | goto out_overflow; | |
1674 | switch ((n = ntohl(*p++))) { | |
1675 | case RPC_AUTH_REJECTEDCRED: | |
1676 | case RPC_AUTH_REJECTEDVERF: | |
1677 | case RPCSEC_GSS_CREDPROBLEM: | |
1678 | case RPCSEC_GSS_CTXPROBLEM: | |
1679 | if (!task->tk_cred_retry) | |
1680 | break; | |
1681 | task->tk_cred_retry--; | |
46121cf7 | 1682 | dprintk("RPC: %5u %s: retry stale creds\n", |
0dc47877 | 1683 | task->tk_pid, __func__); |
1da177e4 | 1684 | rpcauth_invalcred(task); |
220bcc2a TM |
1685 | /* Ensure we obtain a new XID! */ |
1686 | xprt_release(task); | |
118df3d1 | 1687 | task->tk_action = call_reserve; |
abbcf28f | 1688 | goto out_retry; |
1da177e4 LT |
1689 | case RPC_AUTH_BADCRED: |
1690 | case RPC_AUTH_BADVERF: | |
1691 | /* possibly garbled cred/verf? */ | |
1692 | if (!task->tk_garb_retry) | |
1693 | break; | |
1694 | task->tk_garb_retry--; | |
46121cf7 | 1695 | dprintk("RPC: %5u %s: retry garbled creds\n", |
0dc47877 | 1696 | task->tk_pid, __func__); |
1da177e4 | 1697 | task->tk_action = call_bind; |
abbcf28f | 1698 | goto out_retry; |
1da177e4 | 1699 | case RPC_AUTH_TOOWEAK: |
b0e1c57e | 1700 | printk(KERN_NOTICE "RPC: server %s requires stronger " |
1356b8c2 | 1701 | "authentication.\n", task->tk_client->cl_server); |
1da177e4 LT |
1702 | break; |
1703 | default: | |
8a702bbb | 1704 | dprintk("RPC: %5u %s: unknown auth error: %x\n", |
0dc47877 | 1705 | task->tk_pid, __func__, n); |
1da177e4 LT |
1706 | error = -EIO; |
1707 | } | |
46121cf7 | 1708 | dprintk("RPC: %5u %s: call rejected %d\n", |
0dc47877 | 1709 | task->tk_pid, __func__, n); |
1da177e4 LT |
1710 | goto out_err; |
1711 | } | |
1712 | if (!(p = rpcauth_checkverf(task, p))) { | |
8a702bbb | 1713 | dprintk("RPC: %5u %s: auth check failed\n", |
0dc47877 | 1714 | task->tk_pid, __func__); |
abbcf28f | 1715 | goto out_garbage; /* bad verifier, retry */ |
1da177e4 | 1716 | } |
d8ed029d | 1717 | len = p - (__be32 *)iov->iov_base - 1; |
1da177e4 LT |
1718 | if (len < 0) |
1719 | goto out_overflow; | |
1720 | switch ((n = ntohl(*p++))) { | |
1721 | case RPC_SUCCESS: | |
1722 | return p; | |
1723 | case RPC_PROG_UNAVAIL: | |
46121cf7 | 1724 | dprintk("RPC: %5u %s: program %u is unsupported by server %s\n", |
0dc47877 | 1725 | task->tk_pid, __func__, |
1da177e4 LT |
1726 | (unsigned int)task->tk_client->cl_prog, |
1727 | task->tk_client->cl_server); | |
cdf47706 AG |
1728 | error = -EPFNOSUPPORT; |
1729 | goto out_err; | |
1da177e4 | 1730 | case RPC_PROG_MISMATCH: |
46121cf7 | 1731 | dprintk("RPC: %5u %s: program %u, version %u unsupported by " |
0dc47877 | 1732 | "server %s\n", task->tk_pid, __func__, |
1da177e4 LT |
1733 | (unsigned int)task->tk_client->cl_prog, |
1734 | (unsigned int)task->tk_client->cl_vers, | |
1735 | task->tk_client->cl_server); | |
cdf47706 AG |
1736 | error = -EPROTONOSUPPORT; |
1737 | goto out_err; | |
1da177e4 | 1738 | case RPC_PROC_UNAVAIL: |
3748f1e4 | 1739 | dprintk("RPC: %5u %s: proc %s unsupported by program %u, " |
46121cf7 | 1740 | "version %u on server %s\n", |
0dc47877 | 1741 | task->tk_pid, __func__, |
3748f1e4 | 1742 | rpc_proc_name(task), |
1da177e4 LT |
1743 | task->tk_client->cl_prog, |
1744 | task->tk_client->cl_vers, | |
1745 | task->tk_client->cl_server); | |
cdf47706 AG |
1746 | error = -EOPNOTSUPP; |
1747 | goto out_err; | |
1da177e4 | 1748 | case RPC_GARBAGE_ARGS: |
46121cf7 | 1749 | dprintk("RPC: %5u %s: server saw garbage\n", |
0dc47877 | 1750 | task->tk_pid, __func__); |
1da177e4 LT |
1751 | break; /* retry */ |
1752 | default: | |
8a702bbb | 1753 | dprintk("RPC: %5u %s: server accept status: %x\n", |
0dc47877 | 1754 | task->tk_pid, __func__, n); |
1da177e4 LT |
1755 | /* Also retry */ |
1756 | } | |
1757 | ||
abbcf28f | 1758 | out_garbage: |
1da177e4 LT |
1759 | task->tk_client->cl_stats->rpcgarbage++; |
1760 | if (task->tk_garb_retry) { | |
1761 | task->tk_garb_retry--; | |
46121cf7 | 1762 | dprintk("RPC: %5u %s: retrying\n", |
0dc47877 | 1763 | task->tk_pid, __func__); |
1da177e4 | 1764 | task->tk_action = call_bind; |
abbcf28f TM |
1765 | out_retry: |
1766 | return ERR_PTR(-EAGAIN); | |
1da177e4 | 1767 | } |
1da177e4 LT |
1768 | out_eio: |
1769 | error = -EIO; | |
1770 | out_err: | |
1771 | rpc_exit(task, error); | |
8a702bbb | 1772 | dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid, |
0dc47877 | 1773 | __func__, error); |
abbcf28f | 1774 | return ERR_PTR(error); |
1da177e4 | 1775 | out_overflow: |
8a702bbb | 1776 | dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid, |
0dc47877 | 1777 | __func__); |
abbcf28f | 1778 | goto out_garbage; |
1da177e4 | 1779 | } |
5ee0ed7d | 1780 | |
9f06c719 | 1781 | static void rpcproc_encode_null(void *rqstp, struct xdr_stream *xdr, void *obj) |
5ee0ed7d | 1782 | { |
5ee0ed7d TM |
1783 | } |
1784 | ||
bf269551 | 1785 | static int rpcproc_decode_null(void *rqstp, struct xdr_stream *xdr, void *obj) |
5ee0ed7d TM |
1786 | { |
1787 | return 0; | |
1788 | } | |
1789 | ||
1790 | static struct rpc_procinfo rpcproc_null = { | |
1791 | .p_encode = rpcproc_encode_null, | |
1792 | .p_decode = rpcproc_decode_null, | |
1793 | }; | |
1794 | ||
caabea8a | 1795 | static int rpc_ping(struct rpc_clnt *clnt) |
5ee0ed7d TM |
1796 | { |
1797 | struct rpc_message msg = { | |
1798 | .rpc_proc = &rpcproc_null, | |
1799 | }; | |
1800 | int err; | |
1801 | msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0); | |
caabea8a | 1802 | err = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN); |
5ee0ed7d TM |
1803 | put_rpccred(msg.rpc_cred); |
1804 | return err; | |
1805 | } | |
188fef11 | 1806 | |
5e1550d6 TM |
1807 | struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags) |
1808 | { | |
1809 | struct rpc_message msg = { | |
1810 | .rpc_proc = &rpcproc_null, | |
1811 | .rpc_cred = cred, | |
1812 | }; | |
84115e1c TM |
1813 | struct rpc_task_setup task_setup_data = { |
1814 | .rpc_client = clnt, | |
1815 | .rpc_message = &msg, | |
1816 | .callback_ops = &rpc_default_ops, | |
1817 | .flags = flags, | |
1818 | }; | |
c970aa85 | 1819 | return rpc_run_task(&task_setup_data); |
5e1550d6 | 1820 | } |
e8914c65 | 1821 | EXPORT_SYMBOL_GPL(rpc_call_null); |
5e1550d6 | 1822 | |
188fef11 | 1823 | #ifdef RPC_DEBUG |
68a23ee9 CL |
1824 | static void rpc_show_header(void) |
1825 | { | |
cb3997b5 CL |
1826 | printk(KERN_INFO "-pid- flgs status -client- --rqstp- " |
1827 | "-timeout ---ops--\n"); | |
68a23ee9 CL |
1828 | } |
1829 | ||
38e886e0 CL |
1830 | static void rpc_show_task(const struct rpc_clnt *clnt, |
1831 | const struct rpc_task *task) | |
1832 | { | |
1833 | const char *rpc_waitq = "none"; | |
38e886e0 CL |
1834 | |
1835 | if (RPC_IS_QUEUED(task)) | |
1836 | rpc_waitq = rpc_qname(task->tk_waitqueue); | |
1837 | ||
b3bcedad | 1838 | printk(KERN_INFO "%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%ps q:%s\n", |
cb3997b5 CL |
1839 | task->tk_pid, task->tk_flags, task->tk_status, |
1840 | clnt, task->tk_rqstp, task->tk_timeout, task->tk_ops, | |
1841 | clnt->cl_protname, clnt->cl_vers, rpc_proc_name(task), | |
b3bcedad | 1842 | task->tk_action, rpc_waitq); |
38e886e0 CL |
1843 | } |
1844 | ||
188fef11 TM |
1845 | void rpc_show_tasks(void) |
1846 | { | |
1847 | struct rpc_clnt *clnt; | |
38e886e0 | 1848 | struct rpc_task *task; |
68a23ee9 | 1849 | int header = 0; |
188fef11 TM |
1850 | |
1851 | spin_lock(&rpc_client_lock); | |
188fef11 | 1852 | list_for_each_entry(clnt, &all_clients, cl_clients) { |
188fef11 | 1853 | spin_lock(&clnt->cl_lock); |
38e886e0 | 1854 | list_for_each_entry(task, &clnt->cl_tasks, tk_task) { |
68a23ee9 CL |
1855 | if (!header) { |
1856 | rpc_show_header(); | |
1857 | header++; | |
1858 | } | |
38e886e0 | 1859 | rpc_show_task(clnt, task); |
188fef11 TM |
1860 | } |
1861 | spin_unlock(&clnt->cl_lock); | |
1862 | } | |
188fef11 TM |
1863 | spin_unlock(&rpc_client_lock); |
1864 | } | |
1865 | #endif |