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