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