Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/net/sunrpc/svc.c |
| 3 | * |
| 4 | * High-level RPC service routines |
| 5 | * |
| 6 | * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de> |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 7 | * |
| 8 | * Multiple threads pools and NUMAisation |
| 9 | * Copyright (c) 2006 Silicon Graphics, Inc. |
| 10 | * by Greg Banks <gnb@melbourne.sgi.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <linux/linkage.h> |
| 14 | #include <linux/sched.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/net.h> |
| 17 | #include <linux/in.h> |
| 18 | #include <linux/mm.h> |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | #include <linux/sunrpc/types.h> |
| 23 | #include <linux/sunrpc/xdr.h> |
| 24 | #include <linux/sunrpc/stats.h> |
| 25 | #include <linux/sunrpc/svcsock.h> |
| 26 | #include <linux/sunrpc/clnt.h> |
| 27 | |
| 28 | #define RPCDBG_FACILITY RPCDBG_SVCDSP |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | /* |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 31 | * Mode for mapping cpus to pools. |
| 32 | */ |
| 33 | enum { |
| 34 | SVC_POOL_NONE = -1, /* uninitialised, choose one of the others */ |
| 35 | SVC_POOL_GLOBAL, /* no mapping, just a single global pool |
| 36 | * (legacy & UP mode) */ |
| 37 | SVC_POOL_PERCPU, /* one pool per cpu */ |
| 38 | SVC_POOL_PERNODE /* one pool per numa node */ |
| 39 | }; |
| 40 | |
| 41 | /* |
| 42 | * Structure for mapping cpus to pools and vice versa. |
| 43 | * Setup once during sunrpc initialisation. |
| 44 | */ |
| 45 | static struct svc_pool_map { |
| 46 | int mode; /* Note: int not enum to avoid |
| 47 | * warnings about "enumeration value |
| 48 | * not handled in switch" */ |
| 49 | unsigned int npools; |
| 50 | unsigned int *pool_to; /* maps pool id to cpu or node */ |
| 51 | unsigned int *to_pool; /* maps cpu or node to pool id */ |
| 52 | } svc_pool_map = { |
| 53 | .mode = SVC_POOL_NONE |
| 54 | }; |
| 55 | |
| 56 | |
| 57 | /* |
| 58 | * Detect best pool mapping mode heuristically, |
| 59 | * according to the machine's topology. |
| 60 | */ |
| 61 | static int |
| 62 | svc_pool_map_choose_mode(void) |
| 63 | { |
| 64 | unsigned int node; |
| 65 | |
| 66 | if (num_online_nodes() > 1) { |
| 67 | /* |
| 68 | * Actually have multiple NUMA nodes, |
| 69 | * so split pools on NUMA node boundaries |
| 70 | */ |
| 71 | return SVC_POOL_PERNODE; |
| 72 | } |
| 73 | |
| 74 | node = any_online_node(node_online_map); |
| 75 | if (nr_cpus_node(node) > 2) { |
| 76 | /* |
| 77 | * Non-trivial SMP, or CONFIG_NUMA on |
| 78 | * non-NUMA hardware, e.g. with a generic |
| 79 | * x86_64 kernel on Xeons. In this case we |
| 80 | * want to divide the pools on cpu boundaries. |
| 81 | */ |
| 82 | return SVC_POOL_PERCPU; |
| 83 | } |
| 84 | |
| 85 | /* default: one global pool */ |
| 86 | return SVC_POOL_GLOBAL; |
| 87 | } |
| 88 | |
| 89 | /* |
| 90 | * Allocate the to_pool[] and pool_to[] arrays. |
| 91 | * Returns 0 on success or an errno. |
| 92 | */ |
| 93 | static int |
| 94 | svc_pool_map_alloc_arrays(struct svc_pool_map *m, unsigned int maxpools) |
| 95 | { |
| 96 | m->to_pool = kcalloc(maxpools, sizeof(unsigned int), GFP_KERNEL); |
| 97 | if (!m->to_pool) |
| 98 | goto fail; |
| 99 | m->pool_to = kcalloc(maxpools, sizeof(unsigned int), GFP_KERNEL); |
| 100 | if (!m->pool_to) |
| 101 | goto fail_free; |
| 102 | |
| 103 | return 0; |
| 104 | |
| 105 | fail_free: |
| 106 | kfree(m->to_pool); |
| 107 | fail: |
| 108 | return -ENOMEM; |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | * Initialise the pool map for SVC_POOL_PERCPU mode. |
| 113 | * Returns number of pools or <0 on error. |
| 114 | */ |
| 115 | static int |
| 116 | svc_pool_map_init_percpu(struct svc_pool_map *m) |
| 117 | { |
Christoph Lameter | 53b8a31 | 2007-02-20 13:57:51 -0800 | [diff] [blame] | 118 | unsigned int maxpools = nr_cpu_ids; |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 119 | unsigned int pidx = 0; |
| 120 | unsigned int cpu; |
| 121 | int err; |
| 122 | |
| 123 | err = svc_pool_map_alloc_arrays(m, maxpools); |
| 124 | if (err) |
| 125 | return err; |
| 126 | |
| 127 | for_each_online_cpu(cpu) { |
| 128 | BUG_ON(pidx > maxpools); |
| 129 | m->to_pool[cpu] = pidx; |
| 130 | m->pool_to[pidx] = cpu; |
| 131 | pidx++; |
| 132 | } |
| 133 | /* cpus brought online later all get mapped to pool0, sorry */ |
| 134 | |
| 135 | return pidx; |
| 136 | }; |
| 137 | |
| 138 | |
| 139 | /* |
| 140 | * Initialise the pool map for SVC_POOL_PERNODE mode. |
| 141 | * Returns number of pools or <0 on error. |
| 142 | */ |
| 143 | static int |
| 144 | svc_pool_map_init_pernode(struct svc_pool_map *m) |
| 145 | { |
Christoph Lameter | 74c7aa8 | 2007-02-20 13:57:51 -0800 | [diff] [blame] | 146 | unsigned int maxpools = nr_node_ids; |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 147 | unsigned int pidx = 0; |
| 148 | unsigned int node; |
| 149 | int err; |
| 150 | |
| 151 | err = svc_pool_map_alloc_arrays(m, maxpools); |
| 152 | if (err) |
| 153 | return err; |
| 154 | |
| 155 | for_each_node_with_cpus(node) { |
| 156 | /* some architectures (e.g. SN2) have cpuless nodes */ |
| 157 | BUG_ON(pidx > maxpools); |
| 158 | m->to_pool[node] = pidx; |
| 159 | m->pool_to[pidx] = node; |
| 160 | pidx++; |
| 161 | } |
| 162 | /* nodes brought online later all get mapped to pool0, sorry */ |
| 163 | |
| 164 | return pidx; |
| 165 | } |
| 166 | |
| 167 | |
| 168 | /* |
| 169 | * Build the global map of cpus to pools and vice versa. |
| 170 | */ |
| 171 | static unsigned int |
| 172 | svc_pool_map_init(void) |
| 173 | { |
| 174 | struct svc_pool_map *m = &svc_pool_map; |
| 175 | int npools = -1; |
| 176 | |
| 177 | if (m->mode != SVC_POOL_NONE) |
| 178 | return m->npools; |
| 179 | |
| 180 | m->mode = svc_pool_map_choose_mode(); |
| 181 | |
| 182 | switch (m->mode) { |
| 183 | case SVC_POOL_PERCPU: |
| 184 | npools = svc_pool_map_init_percpu(m); |
| 185 | break; |
| 186 | case SVC_POOL_PERNODE: |
| 187 | npools = svc_pool_map_init_pernode(m); |
| 188 | break; |
| 189 | } |
| 190 | |
| 191 | if (npools < 0) { |
| 192 | /* default, or memory allocation failure */ |
| 193 | npools = 1; |
| 194 | m->mode = SVC_POOL_GLOBAL; |
| 195 | } |
| 196 | m->npools = npools; |
| 197 | |
| 198 | return m->npools; |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * Set the current thread's cpus_allowed mask so that it |
| 203 | * will only run on cpus in the given pool. |
| 204 | * |
| 205 | * Returns 1 and fills in oldmask iff a cpumask was applied. |
| 206 | */ |
| 207 | static inline int |
| 208 | svc_pool_map_set_cpumask(unsigned int pidx, cpumask_t *oldmask) |
| 209 | { |
| 210 | struct svc_pool_map *m = &svc_pool_map; |
| 211 | unsigned int node; /* or cpu */ |
| 212 | |
| 213 | /* |
| 214 | * The caller checks for sv_nrpools > 1, which |
| 215 | * implies that we've been initialized and the |
| 216 | * map mode is not NONE. |
| 217 | */ |
| 218 | BUG_ON(m->mode == SVC_POOL_NONE); |
| 219 | |
| 220 | switch (m->mode) |
| 221 | { |
| 222 | default: |
| 223 | return 0; |
| 224 | case SVC_POOL_PERCPU: |
| 225 | node = m->pool_to[pidx]; |
| 226 | *oldmask = current->cpus_allowed; |
| 227 | set_cpus_allowed(current, cpumask_of_cpu(node)); |
| 228 | return 1; |
| 229 | case SVC_POOL_PERNODE: |
| 230 | node = m->pool_to[pidx]; |
| 231 | *oldmask = current->cpus_allowed; |
| 232 | set_cpus_allowed(current, node_to_cpumask(node)); |
| 233 | return 1; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | * Use the mapping mode to choose a pool for a given CPU. |
| 239 | * Used when enqueueing an incoming RPC. Always returns |
| 240 | * a non-NULL pool pointer. |
| 241 | */ |
| 242 | struct svc_pool * |
| 243 | svc_pool_for_cpu(struct svc_serv *serv, int cpu) |
| 244 | { |
| 245 | struct svc_pool_map *m = &svc_pool_map; |
| 246 | unsigned int pidx = 0; |
| 247 | |
| 248 | /* |
| 249 | * SVC_POOL_NONE happens in a pure client when |
| 250 | * lockd is brought up, so silently treat it the |
| 251 | * same as SVC_POOL_GLOBAL. |
| 252 | */ |
| 253 | |
| 254 | switch (m->mode) { |
| 255 | case SVC_POOL_PERCPU: |
| 256 | pidx = m->to_pool[cpu]; |
| 257 | break; |
| 258 | case SVC_POOL_PERNODE: |
| 259 | pidx = m->to_pool[cpu_to_node(cpu)]; |
| 260 | break; |
| 261 | } |
| 262 | return &serv->sv_pools[pidx % serv->sv_nrpools]; |
| 263 | } |
| 264 | |
| 265 | |
| 266 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | * Create an RPC service |
| 268 | */ |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 269 | static struct svc_serv * |
| 270 | __svc_create(struct svc_program *prog, unsigned int bufsize, int npools, |
NeilBrown | bc591cc | 2006-10-02 02:17:44 -0700 | [diff] [blame] | 271 | void (*shutdown)(struct svc_serv *serv)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | { |
| 273 | struct svc_serv *serv; |
| 274 | int vers; |
| 275 | unsigned int xdrsize; |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 276 | unsigned int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 278 | if (!(serv = kzalloc(sizeof(*serv), GFP_KERNEL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | return NULL; |
Andreas Gruenbacher | 9ba0263 | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 280 | serv->sv_name = prog->pg_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | serv->sv_program = prog; |
| 282 | serv->sv_nrthreads = 1; |
| 283 | serv->sv_stats = prog->pg_stats; |
NeilBrown | c6b0a9f | 2006-10-06 00:44:05 -0700 | [diff] [blame] | 284 | if (bufsize > RPCSVC_MAXPAYLOAD) |
| 285 | bufsize = RPCSVC_MAXPAYLOAD; |
| 286 | serv->sv_max_payload = bufsize? bufsize : 4096; |
| 287 | serv->sv_max_mesg = roundup(serv->sv_max_payload + PAGE_SIZE, PAGE_SIZE); |
NeilBrown | bc591cc | 2006-10-02 02:17:44 -0700 | [diff] [blame] | 288 | serv->sv_shutdown = shutdown; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | xdrsize = 0; |
Andreas Gruenbacher | 9ba0263 | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 290 | while (prog) { |
| 291 | prog->pg_lovers = prog->pg_nvers-1; |
| 292 | for (vers=0; vers<prog->pg_nvers ; vers++) |
| 293 | if (prog->pg_vers[vers]) { |
| 294 | prog->pg_hivers = vers; |
| 295 | if (prog->pg_lovers > vers) |
| 296 | prog->pg_lovers = vers; |
| 297 | if (prog->pg_vers[vers]->vs_xdrsize > xdrsize) |
| 298 | xdrsize = prog->pg_vers[vers]->vs_xdrsize; |
| 299 | } |
| 300 | prog = prog->pg_next; |
| 301 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | serv->sv_xdrsize = xdrsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | INIT_LIST_HEAD(&serv->sv_tempsocks); |
| 304 | INIT_LIST_HEAD(&serv->sv_permsocks); |
Greg Banks | 36bdfc8 | 2006-10-02 02:17:54 -0700 | [diff] [blame] | 305 | init_timer(&serv->sv_temptimer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | spin_lock_init(&serv->sv_lock); |
| 307 | |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 308 | serv->sv_nrpools = npools; |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 309 | serv->sv_pools = |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 310 | kcalloc(serv->sv_nrpools, sizeof(struct svc_pool), |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 311 | GFP_KERNEL); |
| 312 | if (!serv->sv_pools) { |
| 313 | kfree(serv); |
| 314 | return NULL; |
| 315 | } |
| 316 | |
| 317 | for (i = 0; i < serv->sv_nrpools; i++) { |
| 318 | struct svc_pool *pool = &serv->sv_pools[i]; |
| 319 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 320 | dprintk("svc: initialising pool %u for %s\n", |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 321 | i, serv->sv_name); |
| 322 | |
| 323 | pool->sp_id = i; |
| 324 | INIT_LIST_HEAD(&pool->sp_threads); |
| 325 | INIT_LIST_HEAD(&pool->sp_sockets); |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 326 | INIT_LIST_HEAD(&pool->sp_all_threads); |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 327 | spin_lock_init(&pool->sp_lock); |
| 328 | } |
| 329 | |
| 330 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | /* Remove any stale portmap registrations */ |
| 332 | svc_register(serv, 0, 0); |
| 333 | |
| 334 | return serv; |
| 335 | } |
| 336 | |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 337 | struct svc_serv * |
| 338 | svc_create(struct svc_program *prog, unsigned int bufsize, |
| 339 | void (*shutdown)(struct svc_serv *serv)) |
| 340 | { |
| 341 | return __svc_create(prog, bufsize, /*npools*/1, shutdown); |
| 342 | } |
| 343 | |
| 344 | struct svc_serv * |
| 345 | svc_create_pooled(struct svc_program *prog, unsigned int bufsize, |
| 346 | void (*shutdown)(struct svc_serv *serv), |
| 347 | svc_thread_fn func, int sig, struct module *mod) |
| 348 | { |
| 349 | struct svc_serv *serv; |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 350 | unsigned int npools = svc_pool_map_init(); |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 351 | |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 352 | serv = __svc_create(prog, bufsize, npools, shutdown); |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 353 | |
| 354 | if (serv != NULL) { |
| 355 | serv->sv_function = func; |
| 356 | serv->sv_kill_signal = sig; |
| 357 | serv->sv_module = mod; |
| 358 | } |
| 359 | |
| 360 | return serv; |
| 361 | } |
| 362 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | /* |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 364 | * Destroy an RPC service. Should be called with the BKL held |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | */ |
| 366 | void |
| 367 | svc_destroy(struct svc_serv *serv) |
| 368 | { |
| 369 | struct svc_sock *svsk; |
NeilBrown | cda1fd4 | 2007-03-06 01:42:22 -0800 | [diff] [blame^] | 370 | struct svc_sock *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 372 | dprintk("svc: svc_destroy(%s, %d)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | serv->sv_program->pg_name, |
| 374 | serv->sv_nrthreads); |
| 375 | |
| 376 | if (serv->sv_nrthreads) { |
| 377 | if (--(serv->sv_nrthreads) != 0) { |
| 378 | svc_sock_update_bufs(serv); |
| 379 | return; |
| 380 | } |
| 381 | } else |
| 382 | printk("svc_destroy: no threads for serv=%p!\n", serv); |
| 383 | |
Greg Banks | 36bdfc8 | 2006-10-02 02:17:54 -0700 | [diff] [blame] | 384 | del_timer_sync(&serv->sv_temptimer); |
| 385 | |
NeilBrown | cda1fd4 | 2007-03-06 01:42:22 -0800 | [diff] [blame^] | 386 | list_for_each_entry_safe(svsk, tmp, &serv->sv_tempsocks, sk_list) |
| 387 | svc_force_close_socket(svsk); |
| 388 | |
NeilBrown | bc591cc | 2006-10-02 02:17:44 -0700 | [diff] [blame] | 389 | if (serv->sv_shutdown) |
| 390 | serv->sv_shutdown(serv); |
| 391 | |
NeilBrown | cda1fd4 | 2007-03-06 01:42:22 -0800 | [diff] [blame^] | 392 | list_for_each_entry_safe(svsk, tmp, &serv->sv_permsocks, sk_list) |
| 393 | svc_force_close_socket(svsk); |
| 394 | |
| 395 | BUG_ON(!list_empty(&serv->sv_permsocks)); |
| 396 | BUG_ON(!list_empty(&serv->sv_tempsocks)); |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | cache_clean_deferred(serv); |
| 399 | |
| 400 | /* Unregister service with the portmapper */ |
| 401 | svc_register(serv, 0, 0); |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 402 | kfree(serv->sv_pools); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | kfree(serv); |
| 404 | } |
| 405 | |
| 406 | /* |
| 407 | * Allocate an RPC server's buffer space. |
| 408 | * We allocate pages and place them in rq_argpages. |
| 409 | */ |
| 410 | static int |
| 411 | svc_init_buffer(struct svc_rqst *rqstp, unsigned int size) |
| 412 | { |
| 413 | int pages; |
| 414 | int arghi; |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 415 | |
NeilBrown | c6b0a9f | 2006-10-06 00:44:05 -0700 | [diff] [blame] | 416 | pages = size / PAGE_SIZE + 1; /* extra page as we hold both request and reply. |
| 417 | * We assume one is at most one page |
| 418 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | arghi = 0; |
Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 420 | BUG_ON(pages > RPCSVC_MAXPAGES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | while (pages) { |
| 422 | struct page *p = alloc_page(GFP_KERNEL); |
| 423 | if (!p) |
| 424 | break; |
NeilBrown | 4452435 | 2006-10-04 02:15:46 -0700 | [diff] [blame] | 425 | rqstp->rq_pages[arghi++] = p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | pages--; |
| 427 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | return ! pages; |
| 429 | } |
| 430 | |
| 431 | /* |
| 432 | * Release an RPC server buffer |
| 433 | */ |
| 434 | static void |
| 435 | svc_release_buffer(struct svc_rqst *rqstp) |
| 436 | { |
NeilBrown | 4452435 | 2006-10-04 02:15:46 -0700 | [diff] [blame] | 437 | int i; |
| 438 | for (i=0; i<ARRAY_SIZE(rqstp->rq_pages); i++) |
| 439 | if (rqstp->rq_pages[i]) |
| 440 | put_page(rqstp->rq_pages[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | /* |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 444 | * Create a thread in the given pool. Caller must hold BKL. |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 445 | * On a NUMA or SMP machine, with a multi-pool serv, the thread |
| 446 | * will be restricted to run on the cpus belonging to the pool. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | */ |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 448 | static int |
| 449 | __svc_create_thread(svc_thread_fn func, struct svc_serv *serv, |
| 450 | struct svc_pool *pool) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | { |
| 452 | struct svc_rqst *rqstp; |
| 453 | int error = -ENOMEM; |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 454 | int have_oldmask = 0; |
| 455 | cpumask_t oldmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 457 | rqstp = kzalloc(sizeof(*rqstp), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | if (!rqstp) |
| 459 | goto out; |
| 460 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | init_waitqueue_head(&rqstp->rq_wait); |
| 462 | |
Jesper Juhl | 12fe2c5 | 2006-01-10 13:08:21 -0800 | [diff] [blame] | 463 | if (!(rqstp->rq_argp = kmalloc(serv->sv_xdrsize, GFP_KERNEL)) |
| 464 | || !(rqstp->rq_resp = kmalloc(serv->sv_xdrsize, GFP_KERNEL)) |
NeilBrown | c6b0a9f | 2006-10-06 00:44:05 -0700 | [diff] [blame] | 465 | || !svc_init_buffer(rqstp, serv->sv_max_mesg)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | goto out_thread; |
| 467 | |
| 468 | serv->sv_nrthreads++; |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 469 | spin_lock_bh(&pool->sp_lock); |
| 470 | pool->sp_nrthreads++; |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 471 | list_add(&rqstp->rq_all, &pool->sp_all_threads); |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 472 | spin_unlock_bh(&pool->sp_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | rqstp->rq_server = serv; |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 474 | rqstp->rq_pool = pool; |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 475 | |
| 476 | if (serv->sv_nrpools > 1) |
| 477 | have_oldmask = svc_pool_map_set_cpumask(pool->sp_id, &oldmask); |
| 478 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | error = kernel_thread((int (*)(void *)) func, rqstp, 0); |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 480 | |
| 481 | if (have_oldmask) |
| 482 | set_cpus_allowed(current, oldmask); |
| 483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | if (error < 0) |
| 485 | goto out_thread; |
| 486 | svc_sock_update_bufs(serv); |
| 487 | error = 0; |
| 488 | out: |
| 489 | return error; |
| 490 | |
| 491 | out_thread: |
| 492 | svc_exit_thread(rqstp); |
| 493 | goto out; |
| 494 | } |
| 495 | |
| 496 | /* |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 497 | * Create a thread in the default pool. Caller must hold BKL. |
| 498 | */ |
| 499 | int |
| 500 | svc_create_thread(svc_thread_fn func, struct svc_serv *serv) |
| 501 | { |
| 502 | return __svc_create_thread(func, serv, &serv->sv_pools[0]); |
| 503 | } |
| 504 | |
| 505 | /* |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 506 | * Choose a pool in which to create a new thread, for svc_set_num_threads |
| 507 | */ |
| 508 | static inline struct svc_pool * |
| 509 | choose_pool(struct svc_serv *serv, struct svc_pool *pool, unsigned int *state) |
| 510 | { |
| 511 | if (pool != NULL) |
| 512 | return pool; |
| 513 | |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 514 | return &serv->sv_pools[(*state)++ % serv->sv_nrpools]; |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | /* |
| 518 | * Choose a thread to kill, for svc_set_num_threads |
| 519 | */ |
| 520 | static inline struct task_struct * |
| 521 | choose_victim(struct svc_serv *serv, struct svc_pool *pool, unsigned int *state) |
| 522 | { |
| 523 | unsigned int i; |
| 524 | struct task_struct *task = NULL; |
| 525 | |
| 526 | if (pool != NULL) { |
| 527 | spin_lock_bh(&pool->sp_lock); |
| 528 | } else { |
| 529 | /* choose a pool in round-robin fashion */ |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 530 | for (i = 0; i < serv->sv_nrpools; i++) { |
| 531 | pool = &serv->sv_pools[--(*state) % serv->sv_nrpools]; |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 532 | spin_lock_bh(&pool->sp_lock); |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 533 | if (!list_empty(&pool->sp_all_threads)) |
| 534 | goto found_pool; |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 535 | spin_unlock_bh(&pool->sp_lock); |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 536 | } |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 537 | return NULL; |
| 538 | } |
| 539 | |
| 540 | found_pool: |
| 541 | if (!list_empty(&pool->sp_all_threads)) { |
| 542 | struct svc_rqst *rqstp; |
| 543 | |
| 544 | /* |
| 545 | * Remove from the pool->sp_all_threads list |
| 546 | * so we don't try to kill it again. |
| 547 | */ |
| 548 | rqstp = list_entry(pool->sp_all_threads.next, struct svc_rqst, rq_all); |
| 549 | list_del_init(&rqstp->rq_all); |
| 550 | task = rqstp->rq_task; |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 551 | } |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 552 | spin_unlock_bh(&pool->sp_lock); |
| 553 | |
| 554 | return task; |
| 555 | } |
| 556 | |
| 557 | /* |
| 558 | * Create or destroy enough new threads to make the number |
| 559 | * of threads the given number. If `pool' is non-NULL, applies |
| 560 | * only to threads in that pool, otherwise round-robins between |
| 561 | * all pools. Must be called with a svc_get() reference and |
| 562 | * the BKL held. |
| 563 | * |
| 564 | * Destroying threads relies on the service threads filling in |
| 565 | * rqstp->rq_task, which only the nfs ones do. Assumes the serv |
| 566 | * has been created using svc_create_pooled(). |
| 567 | * |
| 568 | * Based on code that used to be in nfsd_svc() but tweaked |
| 569 | * to be pool-aware. |
| 570 | */ |
| 571 | int |
| 572 | svc_set_num_threads(struct svc_serv *serv, struct svc_pool *pool, int nrservs) |
| 573 | { |
| 574 | struct task_struct *victim; |
| 575 | int error = 0; |
| 576 | unsigned int state = serv->sv_nrthreads-1; |
| 577 | |
| 578 | if (pool == NULL) { |
| 579 | /* The -1 assumes caller has done a svc_get() */ |
| 580 | nrservs -= (serv->sv_nrthreads-1); |
| 581 | } else { |
| 582 | spin_lock_bh(&pool->sp_lock); |
| 583 | nrservs -= pool->sp_nrthreads; |
| 584 | spin_unlock_bh(&pool->sp_lock); |
| 585 | } |
| 586 | |
| 587 | /* create new threads */ |
| 588 | while (nrservs > 0) { |
| 589 | nrservs--; |
| 590 | __module_get(serv->sv_module); |
| 591 | error = __svc_create_thread(serv->sv_function, serv, |
| 592 | choose_pool(serv, pool, &state)); |
| 593 | if (error < 0) { |
| 594 | module_put(serv->sv_module); |
| 595 | break; |
| 596 | } |
| 597 | } |
| 598 | /* destroy old threads */ |
| 599 | while (nrservs < 0 && |
| 600 | (victim = choose_victim(serv, pool, &state)) != NULL) { |
| 601 | send_sig(serv->sv_kill_signal, victim, 1); |
| 602 | nrservs++; |
| 603 | } |
| 604 | |
| 605 | return error; |
| 606 | } |
| 607 | |
| 608 | /* |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 609 | * Called from a server thread as it's exiting. Caller must hold BKL. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | */ |
| 611 | void |
| 612 | svc_exit_thread(struct svc_rqst *rqstp) |
| 613 | { |
| 614 | struct svc_serv *serv = rqstp->rq_server; |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 615 | struct svc_pool *pool = rqstp->rq_pool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | |
| 617 | svc_release_buffer(rqstp); |
Jesper Juhl | a51482b | 2005-11-08 09:41:34 -0800 | [diff] [blame] | 618 | kfree(rqstp->rq_resp); |
| 619 | kfree(rqstp->rq_argp); |
| 620 | kfree(rqstp->rq_auth_data); |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 621 | |
| 622 | spin_lock_bh(&pool->sp_lock); |
| 623 | pool->sp_nrthreads--; |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 624 | list_del(&rqstp->rq_all); |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 625 | spin_unlock_bh(&pool->sp_lock); |
| 626 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | kfree(rqstp); |
| 628 | |
| 629 | /* Release the server */ |
| 630 | if (serv) |
| 631 | svc_destroy(serv); |
| 632 | } |
| 633 | |
| 634 | /* |
| 635 | * Register an RPC service with the local portmapper. |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 636 | * To unregister a service, call this routine with |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | * proto and port == 0. |
| 638 | */ |
| 639 | int |
| 640 | svc_register(struct svc_serv *serv, int proto, unsigned short port) |
| 641 | { |
| 642 | struct svc_program *progp; |
| 643 | unsigned long flags; |
| 644 | int i, error = 0, dummy; |
| 645 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | if (!port) |
| 647 | clear_thread_flag(TIF_SIGPENDING); |
| 648 | |
Olaf Kirch | bc5fea4 | 2006-10-04 02:16:05 -0700 | [diff] [blame] | 649 | for (progp = serv->sv_program; progp; progp = progp->pg_next) { |
| 650 | for (i = 0; i < progp->pg_nvers; i++) { |
| 651 | if (progp->pg_vers[i] == NULL) |
| 652 | continue; |
| 653 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 654 | dprintk("svc: svc_register(%s, %s, %d, %d)%s\n", |
Olaf Kirch | bc5fea4 | 2006-10-04 02:16:05 -0700 | [diff] [blame] | 655 | progp->pg_name, |
| 656 | proto == IPPROTO_UDP? "udp" : "tcp", |
| 657 | port, |
| 658 | i, |
| 659 | progp->pg_vers[i]->vs_hidden? |
| 660 | " (but not telling portmap)" : ""); |
| 661 | |
| 662 | if (progp->pg_vers[i]->vs_hidden) |
| 663 | continue; |
| 664 | |
| 665 | error = rpc_register(progp->pg_prog, i, proto, port, &dummy); |
| 666 | if (error < 0) |
| 667 | break; |
| 668 | if (port && !dummy) { |
| 669 | error = -EACCES; |
| 670 | break; |
| 671 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | } |
| 673 | } |
| 674 | |
| 675 | if (!port) { |
| 676 | spin_lock_irqsave(¤t->sighand->siglock, flags); |
| 677 | recalc_sigpending(); |
| 678 | spin_unlock_irqrestore(¤t->sighand->siglock, flags); |
| 679 | } |
| 680 | |
| 681 | return error; |
| 682 | } |
| 683 | |
| 684 | /* |
| 685 | * Process the RPC request. |
| 686 | */ |
| 687 | int |
NeilBrown | 6fb2b47 | 2006-10-02 02:17:50 -0700 | [diff] [blame] | 688 | svc_process(struct svc_rqst *rqstp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | { |
| 690 | struct svc_program *progp; |
| 691 | struct svc_version *versp = NULL; /* compiler food */ |
| 692 | struct svc_procedure *procp = NULL; |
| 693 | struct kvec * argv = &rqstp->rq_arg.head[0]; |
| 694 | struct kvec * resv = &rqstp->rq_res.head[0]; |
NeilBrown | 6fb2b47 | 2006-10-02 02:17:50 -0700 | [diff] [blame] | 695 | struct svc_serv *serv = rqstp->rq_server; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | kxdrproc_t xdr; |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 697 | __be32 *statp; |
| 698 | u32 dir, prog, vers, proc; |
| 699 | __be32 auth_stat, rpc_stat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | int auth_res; |
J.Bruce Fields | 8f8e05c | 2006-10-04 02:16:08 -0700 | [diff] [blame] | 701 | __be32 *reply_statp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | |
| 703 | rpc_stat = rpc_success; |
| 704 | |
| 705 | if (argv->iov_len < 6*4) |
| 706 | goto err_short_len; |
| 707 | |
| 708 | /* setup response xdr_buf. |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 709 | * Initially it has just one page |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | */ |
NeilBrown | 4452435 | 2006-10-04 02:15:46 -0700 | [diff] [blame] | 711 | rqstp->rq_resused = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | resv->iov_base = page_address(rqstp->rq_respages[0]); |
| 713 | resv->iov_len = 0; |
NeilBrown | 4452435 | 2006-10-04 02:15:46 -0700 | [diff] [blame] | 714 | rqstp->rq_res.pages = rqstp->rq_respages + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | rqstp->rq_res.len = 0; |
| 716 | rqstp->rq_res.page_base = 0; |
| 717 | rqstp->rq_res.page_len = 0; |
Trond Myklebust | 334ccfd | 2005-06-22 17:16:19 +0000 | [diff] [blame] | 718 | rqstp->rq_res.buflen = PAGE_SIZE; |
J. Bruce Fields | 7c9fdcf | 2006-06-30 01:56:19 -0700 | [diff] [blame] | 719 | rqstp->rq_res.tail[0].iov_base = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | rqstp->rq_res.tail[0].iov_len = 0; |
J. Bruce Fields | 5c04c46 | 2006-06-30 01:56:19 -0700 | [diff] [blame] | 721 | /* Will be turned off only in gss privacy case: */ |
| 722 | rqstp->rq_sendfile_ok = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | /* tcp needs a space for the record length... */ |
| 724 | if (rqstp->rq_prot == IPPROTO_TCP) |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 725 | svc_putnl(resv, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | |
| 727 | rqstp->rq_xid = svc_getu32(argv); |
| 728 | svc_putu32(resv, rqstp->rq_xid); |
| 729 | |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 730 | dir = svc_getnl(argv); |
| 731 | vers = svc_getnl(argv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | |
| 733 | /* First words of reply: */ |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 734 | svc_putnl(resv, 1); /* REPLY */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | |
| 736 | if (dir != 0) /* direction != CALL */ |
| 737 | goto err_bad_dir; |
| 738 | if (vers != 2) /* RPC version number */ |
| 739 | goto err_bad_rpc; |
| 740 | |
| 741 | /* Save position in case we later decide to reject: */ |
J.Bruce Fields | 8f8e05c | 2006-10-04 02:16:08 -0700 | [diff] [blame] | 742 | reply_statp = resv->iov_base + resv->iov_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 744 | svc_putnl(resv, 0); /* ACCEPT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 746 | rqstp->rq_prog = prog = svc_getnl(argv); /* program number */ |
| 747 | rqstp->rq_vers = vers = svc_getnl(argv); /* version number */ |
| 748 | rqstp->rq_proc = proc = svc_getnl(argv); /* procedure number */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | |
| 750 | progp = serv->sv_program; |
NeilBrown | 80d188a | 2005-11-07 01:00:27 -0800 | [diff] [blame] | 751 | |
| 752 | for (progp = serv->sv_program; progp; progp = progp->pg_next) |
| 753 | if (prog == progp->pg_prog) |
| 754 | break; |
| 755 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | /* |
| 757 | * Decode auth data, and add verifier to reply buffer. |
| 758 | * We do this before anything else in order to get a decent |
| 759 | * auth verifier. |
| 760 | */ |
| 761 | auth_res = svc_authenticate(rqstp, &auth_stat); |
| 762 | /* Also give the program a chance to reject this call: */ |
NeilBrown | 80d188a | 2005-11-07 01:00:27 -0800 | [diff] [blame] | 763 | if (auth_res == SVC_OK && progp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | auth_stat = rpc_autherr_badcred; |
| 765 | auth_res = progp->pg_authenticate(rqstp); |
| 766 | } |
| 767 | switch (auth_res) { |
| 768 | case SVC_OK: |
| 769 | break; |
| 770 | case SVC_GARBAGE: |
| 771 | rpc_stat = rpc_garbage_args; |
| 772 | goto err_bad; |
| 773 | case SVC_SYSERR: |
| 774 | rpc_stat = rpc_system_err; |
| 775 | goto err_bad; |
| 776 | case SVC_DENIED: |
| 777 | goto err_bad_auth; |
| 778 | case SVC_DROP: |
| 779 | goto dropit; |
| 780 | case SVC_COMPLETE: |
| 781 | goto sendit; |
| 782 | } |
NeilBrown | 80d188a | 2005-11-07 01:00:27 -0800 | [diff] [blame] | 783 | |
Andreas Gruenbacher | 9ba0263 | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 784 | if (progp == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | goto err_bad_prog; |
| 786 | |
| 787 | if (vers >= progp->pg_nvers || |
| 788 | !(versp = progp->pg_vers[vers])) |
| 789 | goto err_bad_vers; |
| 790 | |
| 791 | procp = versp->vs_proc + proc; |
| 792 | if (proc >= versp->vs_nproc || !procp->pc_func) |
| 793 | goto err_bad_proc; |
| 794 | rqstp->rq_server = serv; |
| 795 | rqstp->rq_procinfo = procp; |
| 796 | |
| 797 | /* Syntactic check complete */ |
| 798 | serv->sv_stats->rpccnt++; |
| 799 | |
| 800 | /* Build the reply header. */ |
| 801 | statp = resv->iov_base +resv->iov_len; |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 802 | svc_putnl(resv, RPC_SUCCESS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | |
| 804 | /* Bump per-procedure stats counter */ |
| 805 | procp->pc_count++; |
| 806 | |
| 807 | /* Initialize storage for argp and resp */ |
| 808 | memset(rqstp->rq_argp, 0, procp->pc_argsize); |
| 809 | memset(rqstp->rq_resp, 0, procp->pc_ressize); |
| 810 | |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 811 | /* un-reserve some of the out-queue now that we have a |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | * better idea of reply size |
| 813 | */ |
| 814 | if (procp->pc_xdrressize) |
| 815 | svc_reserve(rqstp, procp->pc_xdrressize<<2); |
| 816 | |
| 817 | /* Call the function that processes the request. */ |
| 818 | if (!versp->vs_dispatch) { |
| 819 | /* Decode arguments */ |
| 820 | xdr = procp->pc_decode; |
| 821 | if (xdr && !xdr(rqstp, argv->iov_base, rqstp->rq_argp)) |
| 822 | goto err_garbage; |
| 823 | |
| 824 | *statp = procp->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp); |
| 825 | |
| 826 | /* Encode reply */ |
NeilBrown | d343fce | 2006-10-17 00:10:18 -0700 | [diff] [blame] | 827 | if (*statp == rpc_drop_reply) { |
| 828 | if (procp->pc_release) |
| 829 | procp->pc_release(rqstp, NULL, rqstp->rq_resp); |
| 830 | goto dropit; |
| 831 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | if (*statp == rpc_success && (xdr = procp->pc_encode) |
| 833 | && !xdr(rqstp, resv->iov_base+resv->iov_len, rqstp->rq_resp)) { |
| 834 | dprintk("svc: failed to encode reply\n"); |
| 835 | /* serv->sv_stats->rpcsystemerr++; */ |
| 836 | *statp = rpc_system_err; |
| 837 | } |
| 838 | } else { |
| 839 | dprintk("svc: calling dispatcher\n"); |
| 840 | if (!versp->vs_dispatch(rqstp, statp)) { |
| 841 | /* Release reply info */ |
| 842 | if (procp->pc_release) |
| 843 | procp->pc_release(rqstp, NULL, rqstp->rq_resp); |
| 844 | goto dropit; |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | /* Check RPC status result */ |
| 849 | if (*statp != rpc_success) |
| 850 | resv->iov_len = ((void*)statp) - resv->iov_base + 4; |
| 851 | |
| 852 | /* Release reply info */ |
| 853 | if (procp->pc_release) |
| 854 | procp->pc_release(rqstp, NULL, rqstp->rq_resp); |
| 855 | |
| 856 | if (procp->pc_encode == NULL) |
| 857 | goto dropit; |
| 858 | |
| 859 | sendit: |
| 860 | if (svc_authorise(rqstp)) |
| 861 | goto dropit; |
| 862 | return svc_send(rqstp); |
| 863 | |
| 864 | dropit: |
| 865 | svc_authorise(rqstp); /* doesn't hurt to call this twice */ |
| 866 | dprintk("svc: svc_process dropit\n"); |
| 867 | svc_drop(rqstp); |
| 868 | return 0; |
| 869 | |
| 870 | err_short_len: |
NeilBrown | 34e9a63 | 2007-01-29 13:19:52 -0800 | [diff] [blame] | 871 | if (net_ratelimit()) |
| 872 | printk("svc: short len %Zd, dropping request\n", argv->iov_len); |
| 873 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | goto dropit; /* drop request */ |
| 875 | |
| 876 | err_bad_dir: |
NeilBrown | 34e9a63 | 2007-01-29 13:19:52 -0800 | [diff] [blame] | 877 | if (net_ratelimit()) |
| 878 | printk("svc: bad direction %d, dropping request\n", dir); |
| 879 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | serv->sv_stats->rpcbadfmt++; |
| 881 | goto dropit; /* drop request */ |
| 882 | |
| 883 | err_bad_rpc: |
| 884 | serv->sv_stats->rpcbadfmt++; |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 885 | svc_putnl(resv, 1); /* REJECT */ |
| 886 | svc_putnl(resv, 0); /* RPC_MISMATCH */ |
| 887 | svc_putnl(resv, 2); /* Only RPCv2 supported */ |
| 888 | svc_putnl(resv, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | goto sendit; |
| 890 | |
| 891 | err_bad_auth: |
| 892 | dprintk("svc: authentication failed (%d)\n", ntohl(auth_stat)); |
| 893 | serv->sv_stats->rpcbadauth++; |
| 894 | /* Restore write pointer to location of accept status: */ |
J.Bruce Fields | 8f8e05c | 2006-10-04 02:16:08 -0700 | [diff] [blame] | 895 | xdr_ressize_check(rqstp, reply_statp); |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 896 | svc_putnl(resv, 1); /* REJECT */ |
| 897 | svc_putnl(resv, 1); /* AUTH_ERROR */ |
| 898 | svc_putnl(resv, ntohl(auth_stat)); /* status */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | goto sendit; |
| 900 | |
| 901 | err_bad_prog: |
Andreas Gruenbacher | 9ba0263 | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 902 | dprintk("svc: unknown program %d\n", prog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | serv->sv_stats->rpcbadfmt++; |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 904 | svc_putnl(resv, RPC_PROG_UNAVAIL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | goto sendit; |
| 906 | |
| 907 | err_bad_vers: |
NeilBrown | 34e9a63 | 2007-01-29 13:19:52 -0800 | [diff] [blame] | 908 | if (net_ratelimit()) |
| 909 | printk("svc: unknown version (%d for prog %d, %s)\n", |
| 910 | vers, prog, progp->pg_name); |
| 911 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | serv->sv_stats->rpcbadfmt++; |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 913 | svc_putnl(resv, RPC_PROG_MISMATCH); |
| 914 | svc_putnl(resv, progp->pg_lovers); |
| 915 | svc_putnl(resv, progp->pg_hivers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | goto sendit; |
| 917 | |
| 918 | err_bad_proc: |
NeilBrown | 34e9a63 | 2007-01-29 13:19:52 -0800 | [diff] [blame] | 919 | if (net_ratelimit()) |
| 920 | printk("svc: unknown procedure (%d)\n", proc); |
| 921 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | serv->sv_stats->rpcbadfmt++; |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 923 | svc_putnl(resv, RPC_PROC_UNAVAIL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | goto sendit; |
| 925 | |
| 926 | err_garbage: |
NeilBrown | 34e9a63 | 2007-01-29 13:19:52 -0800 | [diff] [blame] | 927 | if (net_ratelimit()) |
| 928 | printk("svc: failed to decode args\n"); |
| 929 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | rpc_stat = rpc_garbage_args; |
| 931 | err_bad: |
| 932 | serv->sv_stats->rpcbadfmt++; |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 933 | svc_putnl(resv, ntohl(rpc_stat)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | goto sendit; |
| 935 | } |
Greg Banks | 7adae48 | 2006-10-04 02:15:47 -0700 | [diff] [blame] | 936 | |
| 937 | /* |
| 938 | * Return (transport-specific) limit on the rpc payload. |
| 939 | */ |
| 940 | u32 svc_max_payload(const struct svc_rqst *rqstp) |
| 941 | { |
| 942 | int max = RPCSVC_MAXPAYLOAD_TCP; |
| 943 | |
| 944 | if (rqstp->rq_sock->sk_sock->type == SOCK_DGRAM) |
| 945 | max = RPCSVC_MAXPAYLOAD_UDP; |
NeilBrown | c6b0a9f | 2006-10-06 00:44:05 -0700 | [diff] [blame] | 946 | if (rqstp->rq_server->sv_max_payload < max) |
| 947 | max = rqstp->rq_server->sv_max_payload; |
Greg Banks | 7adae48 | 2006-10-04 02:15:47 -0700 | [diff] [blame] | 948 | return max; |
| 949 | } |
| 950 | EXPORT_SYMBOL_GPL(svc_max_payload); |