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