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> | |
9867d76c | 21 | #include <linux/kthread.h> |
5a0e3ad6 | 22 | #include <linux/slab.h> |
1da177e4 LT |
23 | |
24 | #include <linux/sunrpc/types.h> | |
25 | #include <linux/sunrpc/xdr.h> | |
26 | #include <linux/sunrpc/stats.h> | |
27 | #include <linux/sunrpc/svcsock.h> | |
28 | #include <linux/sunrpc/clnt.h> | |
4d6bbb62 | 29 | #include <linux/sunrpc/bc_xprt.h> |
1da177e4 | 30 | |
860a0d9e JL |
31 | #include <trace/events/sunrpc.h> |
32 | ||
1da177e4 | 33 | #define RPCDBG_FACILITY RPCDBG_SVCDSP |
1da177e4 | 34 | |
5247fab5 | 35 | static void svc_unregister(const struct svc_serv *serv, struct net *net); |
7252d575 | 36 | |
42a7fc4a GB |
37 | #define svc_serv_is_pooled(serv) ((serv)->sv_function) |
38 | ||
bfd24160 GB |
39 | /* |
40 | * Mode for mapping cpus to pools. | |
41 | */ | |
42 | enum { | |
42a7fc4a | 43 | SVC_POOL_AUTO = -1, /* choose one of the others */ |
bfd24160 GB |
44 | SVC_POOL_GLOBAL, /* no mapping, just a single global pool |
45 | * (legacy & UP mode) */ | |
46 | SVC_POOL_PERCPU, /* one pool per cpu */ | |
47 | SVC_POOL_PERNODE /* one pool per numa node */ | |
48 | }; | |
42a7fc4a | 49 | #define SVC_POOL_DEFAULT SVC_POOL_GLOBAL |
bfd24160 GB |
50 | |
51 | /* | |
52 | * Structure for mapping cpus to pools and vice versa. | |
53 | * Setup once during sunrpc initialisation. | |
54 | */ | |
55 | static struct svc_pool_map { | |
42a7fc4a | 56 | int count; /* How many svc_servs use us */ |
bfd24160 GB |
57 | int mode; /* Note: int not enum to avoid |
58 | * warnings about "enumeration value | |
59 | * not handled in switch" */ | |
60 | unsigned int npools; | |
61 | unsigned int *pool_to; /* maps pool id to cpu or node */ | |
62 | unsigned int *to_pool; /* maps cpu or node to pool id */ | |
63 | } svc_pool_map = { | |
42a7fc4a GB |
64 | .count = 0, |
65 | .mode = SVC_POOL_DEFAULT | |
bfd24160 | 66 | }; |
42a7fc4a GB |
67 | static DEFINE_MUTEX(svc_pool_map_mutex);/* protects svc_pool_map.count only */ |
68 | ||
69 | static int | |
70 | param_set_pool_mode(const char *val, struct kernel_param *kp) | |
71 | { | |
72 | int *ip = (int *)kp->arg; | |
73 | struct svc_pool_map *m = &svc_pool_map; | |
74 | int err; | |
75 | ||
76 | mutex_lock(&svc_pool_map_mutex); | |
77 | ||
78 | err = -EBUSY; | |
79 | if (m->count) | |
80 | goto out; | |
81 | ||
82 | err = 0; | |
83 | if (!strncmp(val, "auto", 4)) | |
84 | *ip = SVC_POOL_AUTO; | |
85 | else if (!strncmp(val, "global", 6)) | |
86 | *ip = SVC_POOL_GLOBAL; | |
87 | else if (!strncmp(val, "percpu", 6)) | |
88 | *ip = SVC_POOL_PERCPU; | |
89 | else if (!strncmp(val, "pernode", 7)) | |
90 | *ip = SVC_POOL_PERNODE; | |
91 | else | |
92 | err = -EINVAL; | |
93 | ||
94 | out: | |
95 | mutex_unlock(&svc_pool_map_mutex); | |
96 | return err; | |
97 | } | |
98 | ||
99 | static int | |
100 | param_get_pool_mode(char *buf, struct kernel_param *kp) | |
101 | { | |
102 | int *ip = (int *)kp->arg; | |
103 | ||
104 | switch (*ip) | |
105 | { | |
106 | case SVC_POOL_AUTO: | |
107 | return strlcpy(buf, "auto", 20); | |
108 | case SVC_POOL_GLOBAL: | |
109 | return strlcpy(buf, "global", 20); | |
110 | case SVC_POOL_PERCPU: | |
111 | return strlcpy(buf, "percpu", 20); | |
112 | case SVC_POOL_PERNODE: | |
113 | return strlcpy(buf, "pernode", 20); | |
114 | default: | |
115 | return sprintf(buf, "%d", *ip); | |
116 | } | |
117 | } | |
bfd24160 | 118 | |
42a7fc4a GB |
119 | module_param_call(pool_mode, param_set_pool_mode, param_get_pool_mode, |
120 | &svc_pool_map.mode, 0644); | |
bfd24160 GB |
121 | |
122 | /* | |
123 | * Detect best pool mapping mode heuristically, | |
124 | * according to the machine's topology. | |
125 | */ | |
126 | static int | |
127 | svc_pool_map_choose_mode(void) | |
128 | { | |
129 | unsigned int node; | |
130 | ||
62bc62a8 | 131 | if (nr_online_nodes > 1) { |
bfd24160 GB |
132 | /* |
133 | * Actually have multiple NUMA nodes, | |
134 | * so split pools on NUMA node boundaries | |
135 | */ | |
136 | return SVC_POOL_PERNODE; | |
137 | } | |
138 | ||
72c33688 | 139 | node = first_online_node; |
bfd24160 GB |
140 | if (nr_cpus_node(node) > 2) { |
141 | /* | |
142 | * Non-trivial SMP, or CONFIG_NUMA on | |
143 | * non-NUMA hardware, e.g. with a generic | |
144 | * x86_64 kernel on Xeons. In this case we | |
145 | * want to divide the pools on cpu boundaries. | |
146 | */ | |
147 | return SVC_POOL_PERCPU; | |
148 | } | |
149 | ||
150 | /* default: one global pool */ | |
151 | return SVC_POOL_GLOBAL; | |
152 | } | |
153 | ||
154 | /* | |
155 | * Allocate the to_pool[] and pool_to[] arrays. | |
156 | * Returns 0 on success or an errno. | |
157 | */ | |
158 | static int | |
159 | svc_pool_map_alloc_arrays(struct svc_pool_map *m, unsigned int maxpools) | |
160 | { | |
161 | m->to_pool = kcalloc(maxpools, sizeof(unsigned int), GFP_KERNEL); | |
162 | if (!m->to_pool) | |
163 | goto fail; | |
164 | m->pool_to = kcalloc(maxpools, sizeof(unsigned int), GFP_KERNEL); | |
165 | if (!m->pool_to) | |
166 | goto fail_free; | |
167 | ||
168 | return 0; | |
169 | ||
170 | fail_free: | |
171 | kfree(m->to_pool); | |
61c8504c | 172 | m->to_pool = NULL; |
bfd24160 GB |
173 | fail: |
174 | return -ENOMEM; | |
175 | } | |
176 | ||
177 | /* | |
178 | * Initialise the pool map for SVC_POOL_PERCPU mode. | |
179 | * Returns number of pools or <0 on error. | |
180 | */ | |
181 | static int | |
182 | svc_pool_map_init_percpu(struct svc_pool_map *m) | |
183 | { | |
53b8a315 | 184 | unsigned int maxpools = nr_cpu_ids; |
bfd24160 GB |
185 | unsigned int pidx = 0; |
186 | unsigned int cpu; | |
187 | int err; | |
188 | ||
189 | err = svc_pool_map_alloc_arrays(m, maxpools); | |
190 | if (err) | |
191 | return err; | |
192 | ||
193 | for_each_online_cpu(cpu) { | |
eb63192b | 194 | BUG_ON(pidx >= maxpools); |
bfd24160 GB |
195 | m->to_pool[cpu] = pidx; |
196 | m->pool_to[pidx] = cpu; | |
197 | pidx++; | |
198 | } | |
199 | /* cpus brought online later all get mapped to pool0, sorry */ | |
200 | ||
201 | return pidx; | |
202 | }; | |
203 | ||
204 | ||
205 | /* | |
206 | * Initialise the pool map for SVC_POOL_PERNODE mode. | |
207 | * Returns number of pools or <0 on error. | |
208 | */ | |
209 | static int | |
210 | svc_pool_map_init_pernode(struct svc_pool_map *m) | |
211 | { | |
74c7aa8b | 212 | unsigned int maxpools = nr_node_ids; |
bfd24160 GB |
213 | unsigned int pidx = 0; |
214 | unsigned int node; | |
215 | int err; | |
216 | ||
217 | err = svc_pool_map_alloc_arrays(m, maxpools); | |
218 | if (err) | |
219 | return err; | |
220 | ||
221 | for_each_node_with_cpus(node) { | |
222 | /* some architectures (e.g. SN2) have cpuless nodes */ | |
223 | BUG_ON(pidx > maxpools); | |
224 | m->to_pool[node] = pidx; | |
225 | m->pool_to[pidx] = node; | |
226 | pidx++; | |
227 | } | |
228 | /* nodes brought online later all get mapped to pool0, sorry */ | |
229 | ||
230 | return pidx; | |
231 | } | |
232 | ||
233 | ||
234 | /* | |
42a7fc4a GB |
235 | * Add a reference to the global map of cpus to pools (and |
236 | * vice versa). Initialise the map if we're the first user. | |
237 | * Returns the number of pools. | |
bfd24160 GB |
238 | */ |
239 | static unsigned int | |
42a7fc4a | 240 | svc_pool_map_get(void) |
bfd24160 GB |
241 | { |
242 | struct svc_pool_map *m = &svc_pool_map; | |
243 | int npools = -1; | |
244 | ||
42a7fc4a GB |
245 | mutex_lock(&svc_pool_map_mutex); |
246 | ||
247 | if (m->count++) { | |
248 | mutex_unlock(&svc_pool_map_mutex); | |
bfd24160 | 249 | return m->npools; |
42a7fc4a | 250 | } |
bfd24160 | 251 | |
42a7fc4a GB |
252 | if (m->mode == SVC_POOL_AUTO) |
253 | m->mode = svc_pool_map_choose_mode(); | |
bfd24160 GB |
254 | |
255 | switch (m->mode) { | |
256 | case SVC_POOL_PERCPU: | |
257 | npools = svc_pool_map_init_percpu(m); | |
258 | break; | |
259 | case SVC_POOL_PERNODE: | |
260 | npools = svc_pool_map_init_pernode(m); | |
261 | break; | |
262 | } | |
263 | ||
264 | if (npools < 0) { | |
265 | /* default, or memory allocation failure */ | |
266 | npools = 1; | |
267 | m->mode = SVC_POOL_GLOBAL; | |
268 | } | |
269 | m->npools = npools; | |
270 | ||
42a7fc4a | 271 | mutex_unlock(&svc_pool_map_mutex); |
bfd24160 GB |
272 | return m->npools; |
273 | } | |
274 | ||
42a7fc4a GB |
275 | |
276 | /* | |
277 | * Drop a reference to the global map of cpus to pools. | |
278 | * When the last reference is dropped, the map data is | |
279 | * freed; this allows the sysadmin to change the pool | |
280 | * mode using the pool_mode module option without | |
281 | * rebooting or re-loading sunrpc.ko. | |
282 | */ | |
283 | static void | |
284 | svc_pool_map_put(void) | |
285 | { | |
286 | struct svc_pool_map *m = &svc_pool_map; | |
287 | ||
288 | mutex_lock(&svc_pool_map_mutex); | |
289 | ||
290 | if (!--m->count) { | |
42a7fc4a | 291 | kfree(m->to_pool); |
61c8504c | 292 | m->to_pool = NULL; |
42a7fc4a | 293 | kfree(m->pool_to); |
61c8504c | 294 | m->pool_to = NULL; |
42a7fc4a GB |
295 | m->npools = 0; |
296 | } | |
297 | ||
298 | mutex_unlock(&svc_pool_map_mutex); | |
299 | } | |
300 | ||
301 | ||
11fd165c ED |
302 | static int svc_pool_map_get_node(unsigned int pidx) |
303 | { | |
304 | const struct svc_pool_map *m = &svc_pool_map; | |
305 | ||
306 | if (m->count) { | |
307 | if (m->mode == SVC_POOL_PERCPU) | |
308 | return cpu_to_node(m->pool_to[pidx]); | |
309 | if (m->mode == SVC_POOL_PERNODE) | |
310 | return m->pool_to[pidx]; | |
311 | } | |
312 | return NUMA_NO_NODE; | |
313 | } | |
bfd24160 | 314 | /* |
9867d76c | 315 | * Set the given thread's cpus_allowed mask so that it |
bfd24160 | 316 | * will only run on cpus in the given pool. |
bfd24160 | 317 | */ |
9867d76c JL |
318 | static inline void |
319 | svc_pool_map_set_cpumask(struct task_struct *task, unsigned int pidx) | |
bfd24160 GB |
320 | { |
321 | struct svc_pool_map *m = &svc_pool_map; | |
9867d76c | 322 | unsigned int node = m->pool_to[pidx]; |
bfd24160 GB |
323 | |
324 | /* | |
325 | * The caller checks for sv_nrpools > 1, which | |
42a7fc4a | 326 | * implies that we've been initialized. |
bfd24160 | 327 | */ |
1bd58aaf WAA |
328 | WARN_ON_ONCE(m->count == 0); |
329 | if (m->count == 0) | |
330 | return; | |
bfd24160 | 331 | |
9867d76c | 332 | switch (m->mode) { |
bfd24160 | 333 | case SVC_POOL_PERCPU: |
c5f59f08 | 334 | { |
aa85ea5b | 335 | set_cpus_allowed_ptr(task, cpumask_of(node)); |
9867d76c | 336 | break; |
c5f59f08 | 337 | } |
bfd24160 | 338 | case SVC_POOL_PERNODE: |
c5f59f08 | 339 | { |
a70f7302 | 340 | set_cpus_allowed_ptr(task, cpumask_of_node(node)); |
9867d76c | 341 | break; |
bfd24160 | 342 | } |
c5f59f08 | 343 | } |
bfd24160 GB |
344 | } |
345 | ||
346 | /* | |
347 | * Use the mapping mode to choose a pool for a given CPU. | |
348 | * Used when enqueueing an incoming RPC. Always returns | |
349 | * a non-NULL pool pointer. | |
350 | */ | |
351 | struct svc_pool * | |
352 | svc_pool_for_cpu(struct svc_serv *serv, int cpu) | |
353 | { | |
354 | struct svc_pool_map *m = &svc_pool_map; | |
355 | unsigned int pidx = 0; | |
356 | ||
357 | /* | |
42a7fc4a | 358 | * An uninitialised map happens in a pure client when |
bfd24160 GB |
359 | * lockd is brought up, so silently treat it the |
360 | * same as SVC_POOL_GLOBAL. | |
361 | */ | |
42a7fc4a GB |
362 | if (svc_serv_is_pooled(serv)) { |
363 | switch (m->mode) { | |
364 | case SVC_POOL_PERCPU: | |
365 | pidx = m->to_pool[cpu]; | |
366 | break; | |
367 | case SVC_POOL_PERNODE: | |
368 | pidx = m->to_pool[cpu_to_node(cpu)]; | |
369 | break; | |
370 | } | |
bfd24160 GB |
371 | } |
372 | return &serv->sv_pools[pidx % serv->sv_nrpools]; | |
373 | } | |
374 | ||
bb2224df | 375 | int svc_rpcb_setup(struct svc_serv *serv, struct net *net) |
d9908560 SK |
376 | { |
377 | int err; | |
378 | ||
bee42f68 | 379 | err = rpcb_create_local(net); |
d9908560 SK |
380 | if (err) |
381 | return err; | |
382 | ||
383 | /* Remove any stale portmap registrations */ | |
bee42f68 | 384 | svc_unregister(serv, net); |
d9908560 SK |
385 | return 0; |
386 | } | |
bb2224df | 387 | EXPORT_SYMBOL_GPL(svc_rpcb_setup); |
d9908560 | 388 | |
5ecebb7c | 389 | void svc_rpcb_cleanup(struct svc_serv *serv, struct net *net) |
d9908560 | 390 | { |
5ecebb7c SK |
391 | svc_unregister(serv, net); |
392 | rpcb_put_local(net); | |
d9908560 | 393 | } |
16d05870 | 394 | EXPORT_SYMBOL_GPL(svc_rpcb_cleanup); |
d9908560 SK |
395 | |
396 | static int svc_uses_rpcbind(struct svc_serv *serv) | |
397 | { | |
398 | struct svc_program *progp; | |
399 | unsigned int i; | |
400 | ||
401 | for (progp = serv->sv_program; progp; progp = progp->pg_next) { | |
402 | for (i = 0; i < progp->pg_nvers; i++) { | |
403 | if (progp->pg_vers[i] == NULL) | |
404 | continue; | |
405 | if (progp->pg_vers[i]->vs_hidden == 0) | |
406 | return 1; | |
407 | } | |
408 | } | |
409 | ||
410 | return 0; | |
411 | } | |
bfd24160 | 412 | |
9793f7c8 SK |
413 | int svc_bind(struct svc_serv *serv, struct net *net) |
414 | { | |
415 | if (!svc_uses_rpcbind(serv)) | |
416 | return 0; | |
417 | return svc_rpcb_setup(serv, net); | |
418 | } | |
419 | EXPORT_SYMBOL_GPL(svc_bind); | |
420 | ||
1da177e4 LT |
421 | /* |
422 | * Create an RPC service | |
423 | */ | |
a7455442 GB |
424 | static struct svc_serv * |
425 | __svc_create(struct svc_program *prog, unsigned int bufsize, int npools, | |
5ecebb7c | 426 | void (*shutdown)(struct svc_serv *serv, struct net *net)) |
1da177e4 LT |
427 | { |
428 | struct svc_serv *serv; | |
ea339d46 | 429 | unsigned int vers; |
1da177e4 | 430 | unsigned int xdrsize; |
3262c816 | 431 | unsigned int i; |
1da177e4 | 432 | |
0da974f4 | 433 | if (!(serv = kzalloc(sizeof(*serv), GFP_KERNEL))) |
1da177e4 | 434 | return NULL; |
9ba02638 | 435 | serv->sv_name = prog->pg_name; |
1da177e4 LT |
436 | serv->sv_program = prog; |
437 | serv->sv_nrthreads = 1; | |
438 | serv->sv_stats = prog->pg_stats; | |
c6b0a9f8 N |
439 | if (bufsize > RPCSVC_MAXPAYLOAD) |
440 | bufsize = RPCSVC_MAXPAYLOAD; | |
441 | serv->sv_max_payload = bufsize? bufsize : 4096; | |
442 | serv->sv_max_mesg = roundup(serv->sv_max_payload + PAGE_SIZE, PAGE_SIZE); | |
bc591ccf | 443 | serv->sv_shutdown = shutdown; |
1da177e4 | 444 | xdrsize = 0; |
9ba02638 AG |
445 | while (prog) { |
446 | prog->pg_lovers = prog->pg_nvers-1; | |
447 | for (vers=0; vers<prog->pg_nvers ; vers++) | |
448 | if (prog->pg_vers[vers]) { | |
449 | prog->pg_hivers = vers; | |
450 | if (prog->pg_lovers > vers) | |
451 | prog->pg_lovers = vers; | |
452 | if (prog->pg_vers[vers]->vs_xdrsize > xdrsize) | |
453 | xdrsize = prog->pg_vers[vers]->vs_xdrsize; | |
454 | } | |
455 | prog = prog->pg_next; | |
456 | } | |
1da177e4 | 457 | serv->sv_xdrsize = xdrsize; |
1da177e4 LT |
458 | INIT_LIST_HEAD(&serv->sv_tempsocks); |
459 | INIT_LIST_HEAD(&serv->sv_permsocks); | |
36bdfc8b | 460 | init_timer(&serv->sv_temptimer); |
1da177e4 LT |
461 | spin_lock_init(&serv->sv_lock); |
462 | ||
a7455442 | 463 | serv->sv_nrpools = npools; |
3262c816 | 464 | serv->sv_pools = |
cd861280 | 465 | kcalloc(serv->sv_nrpools, sizeof(struct svc_pool), |
3262c816 GB |
466 | GFP_KERNEL); |
467 | if (!serv->sv_pools) { | |
468 | kfree(serv); | |
469 | return NULL; | |
470 | } | |
471 | ||
472 | for (i = 0; i < serv->sv_nrpools; i++) { | |
473 | struct svc_pool *pool = &serv->sv_pools[i]; | |
474 | ||
46121cf7 | 475 | dprintk("svc: initialising pool %u for %s\n", |
3262c816 GB |
476 | i, serv->sv_name); |
477 | ||
478 | pool->sp_id = i; | |
3262c816 | 479 | INIT_LIST_HEAD(&pool->sp_sockets); |
a7455442 | 480 | INIT_LIST_HEAD(&pool->sp_all_threads); |
3262c816 GB |
481 | spin_lock_init(&pool->sp_lock); |
482 | } | |
483 | ||
1da177e4 LT |
484 | return serv; |
485 | } | |
486 | ||
a7455442 GB |
487 | struct svc_serv * |
488 | svc_create(struct svc_program *prog, unsigned int bufsize, | |
5ecebb7c | 489 | void (*shutdown)(struct svc_serv *serv, struct net *net)) |
a7455442 | 490 | { |
49a9072f | 491 | return __svc_create(prog, bufsize, /*npools*/1, shutdown); |
a7455442 | 492 | } |
24c3767e | 493 | EXPORT_SYMBOL_GPL(svc_create); |
a7455442 GB |
494 | |
495 | struct svc_serv * | |
496 | svc_create_pooled(struct svc_program *prog, unsigned int bufsize, | |
5ecebb7c | 497 | void (*shutdown)(struct svc_serv *serv, struct net *net), |
a75c5d01 | 498 | svc_thread_fn func, struct module *mod) |
a7455442 GB |
499 | { |
500 | struct svc_serv *serv; | |
42a7fc4a | 501 | unsigned int npools = svc_pool_map_get(); |
a7455442 | 502 | |
49a9072f | 503 | serv = __svc_create(prog, bufsize, npools, shutdown); |
067f96ef JL |
504 | if (!serv) |
505 | goto out_err; | |
a7455442 | 506 | |
067f96ef JL |
507 | serv->sv_function = func; |
508 | serv->sv_module = mod; | |
a7455442 | 509 | return serv; |
067f96ef JL |
510 | out_err: |
511 | svc_pool_map_put(); | |
512 | return NULL; | |
a7455442 | 513 | } |
24c3767e | 514 | EXPORT_SYMBOL_GPL(svc_create_pooled); |
a7455442 | 515 | |
074d0f67 SK |
516 | void svc_shutdown_net(struct svc_serv *serv, struct net *net) |
517 | { | |
074d0f67 SK |
518 | svc_close_net(serv, net); |
519 | ||
520 | if (serv->sv_shutdown) | |
521 | serv->sv_shutdown(serv, net); | |
522 | } | |
523 | EXPORT_SYMBOL_GPL(svc_shutdown_net); | |
524 | ||
1da177e4 | 525 | /* |
bedbdd8b NB |
526 | * Destroy an RPC service. Should be called with appropriate locking to |
527 | * protect the sv_nrthreads, sv_permsocks and sv_tempsocks. | |
1da177e4 LT |
528 | */ |
529 | void | |
530 | svc_destroy(struct svc_serv *serv) | |
531 | { | |
46121cf7 | 532 | dprintk("svc: svc_destroy(%s, %d)\n", |
1da177e4 LT |
533 | serv->sv_program->pg_name, |
534 | serv->sv_nrthreads); | |
535 | ||
536 | if (serv->sv_nrthreads) { | |
537 | if (--(serv->sv_nrthreads) != 0) { | |
538 | svc_sock_update_bufs(serv); | |
539 | return; | |
540 | } | |
541 | } else | |
542 | printk("svc_destroy: no threads for serv=%p!\n", serv); | |
543 | ||
36bdfc8b | 544 | del_timer_sync(&serv->sv_temptimer); |
074d0f67 | 545 | |
7b147f1f SK |
546 | /* |
547 | * The last user is gone and thus all sockets have to be destroyed to | |
548 | * the point. Check this. | |
549 | */ | |
550 | BUG_ON(!list_empty(&serv->sv_permsocks)); | |
551 | BUG_ON(!list_empty(&serv->sv_tempsocks)); | |
cda1fd4a | 552 | |
1da177e4 LT |
553 | cache_clean_deferred(serv); |
554 | ||
42a7fc4a GB |
555 | if (svc_serv_is_pooled(serv)) |
556 | svc_pool_map_put(); | |
557 | ||
3262c816 | 558 | kfree(serv->sv_pools); |
1da177e4 LT |
559 | kfree(serv); |
560 | } | |
24c3767e | 561 | EXPORT_SYMBOL_GPL(svc_destroy); |
1da177e4 LT |
562 | |
563 | /* | |
564 | * Allocate an RPC server's buffer space. | |
565 | * We allocate pages and place them in rq_argpages. | |
566 | */ | |
567 | static int | |
11fd165c | 568 | svc_init_buffer(struct svc_rqst *rqstp, unsigned int size, int node) |
1da177e4 | 569 | { |
0dc220f0 | 570 | unsigned int pages, arghi; |
cca5172a | 571 | |
ba17686f AA |
572 | /* bc_xprt uses fore channel allocated buffers */ |
573 | if (svc_is_backchannel(rqstp)) | |
574 | return 1; | |
575 | ||
c6b0a9f8 N |
576 | pages = size / PAGE_SIZE + 1; /* extra page as we hold both request and reply. |
577 | * We assume one is at most one page | |
578 | */ | |
1da177e4 | 579 | arghi = 0; |
b25cd058 WAA |
580 | WARN_ON_ONCE(pages > RPCSVC_MAXPAGES); |
581 | if (pages > RPCSVC_MAXPAGES) | |
582 | pages = RPCSVC_MAXPAGES; | |
1da177e4 | 583 | while (pages) { |
11fd165c | 584 | struct page *p = alloc_pages_node(node, GFP_KERNEL, 0); |
1da177e4 LT |
585 | if (!p) |
586 | break; | |
44524359 | 587 | rqstp->rq_pages[arghi++] = p; |
1da177e4 LT |
588 | pages--; |
589 | } | |
0dc220f0 | 590 | return pages == 0; |
1da177e4 LT |
591 | } |
592 | ||
593 | /* | |
594 | * Release an RPC server buffer | |
595 | */ | |
596 | static void | |
597 | svc_release_buffer(struct svc_rqst *rqstp) | |
598 | { | |
50c8bb13 CL |
599 | unsigned int i; |
600 | ||
601 | for (i = 0; i < ARRAY_SIZE(rqstp->rq_pages); i++) | |
44524359 N |
602 | if (rqstp->rq_pages[i]) |
603 | put_page(rqstp->rq_pages[i]); | |
1da177e4 LT |
604 | } |
605 | ||
0113ab34 | 606 | struct svc_rqst * |
11fd165c | 607 | svc_prepare_thread(struct svc_serv *serv, struct svc_pool *pool, int node) |
1da177e4 LT |
608 | { |
609 | struct svc_rqst *rqstp; | |
1da177e4 | 610 | |
11fd165c | 611 | rqstp = kzalloc_node(sizeof(*rqstp), GFP_KERNEL, node); |
1da177e4 | 612 | if (!rqstp) |
0113ab34 | 613 | goto out_enomem; |
1da177e4 | 614 | |
1da177e4 | 615 | serv->sv_nrthreads++; |
b1691bc0 JL |
616 | __set_bit(RQ_BUSY, &rqstp->rq_flags); |
617 | spin_lock_init(&rqstp->rq_lock); | |
618 | rqstp->rq_server = serv; | |
619 | rqstp->rq_pool = pool; | |
3262c816 GB |
620 | spin_lock_bh(&pool->sp_lock); |
621 | pool->sp_nrthreads++; | |
81244386 | 622 | list_add_rcu(&rqstp->rq_all, &pool->sp_all_threads); |
3262c816 | 623 | spin_unlock_bh(&pool->sp_lock); |
bfd24160 | 624 | |
11fd165c | 625 | rqstp->rq_argp = kmalloc_node(serv->sv_xdrsize, GFP_KERNEL, node); |
0113ab34 JL |
626 | if (!rqstp->rq_argp) |
627 | goto out_thread; | |
628 | ||
11fd165c | 629 | rqstp->rq_resp = kmalloc_node(serv->sv_xdrsize, GFP_KERNEL, node); |
0113ab34 JL |
630 | if (!rqstp->rq_resp) |
631 | goto out_thread; | |
632 | ||
11fd165c | 633 | if (!svc_init_buffer(rqstp, serv->sv_max_mesg, node)) |
0113ab34 JL |
634 | goto out_thread; |
635 | ||
636 | return rqstp; | |
637 | out_thread: | |
638 | svc_exit_thread(rqstp); | |
639 | out_enomem: | |
640 | return ERR_PTR(-ENOMEM); | |
641 | } | |
24c3767e | 642 | EXPORT_SYMBOL_GPL(svc_prepare_thread); |
0113ab34 | 643 | |
a7455442 GB |
644 | /* |
645 | * Choose a pool in which to create a new thread, for svc_set_num_threads | |
646 | */ | |
647 | static inline struct svc_pool * | |
648 | choose_pool(struct svc_serv *serv, struct svc_pool *pool, unsigned int *state) | |
649 | { | |
650 | if (pool != NULL) | |
651 | return pool; | |
652 | ||
cca5172a | 653 | return &serv->sv_pools[(*state)++ % serv->sv_nrpools]; |
a7455442 GB |
654 | } |
655 | ||
656 | /* | |
657 | * Choose a thread to kill, for svc_set_num_threads | |
658 | */ | |
659 | static inline struct task_struct * | |
660 | choose_victim(struct svc_serv *serv, struct svc_pool *pool, unsigned int *state) | |
661 | { | |
662 | unsigned int i; | |
663 | struct task_struct *task = NULL; | |
664 | ||
665 | if (pool != NULL) { | |
666 | spin_lock_bh(&pool->sp_lock); | |
667 | } else { | |
668 | /* choose a pool in round-robin fashion */ | |
cca5172a YH |
669 | for (i = 0; i < serv->sv_nrpools; i++) { |
670 | pool = &serv->sv_pools[--(*state) % serv->sv_nrpools]; | |
a7455442 | 671 | spin_lock_bh(&pool->sp_lock); |
cca5172a YH |
672 | if (!list_empty(&pool->sp_all_threads)) |
673 | goto found_pool; | |
a7455442 | 674 | spin_unlock_bh(&pool->sp_lock); |
cca5172a | 675 | } |
a7455442 GB |
676 | return NULL; |
677 | } | |
678 | ||
679 | found_pool: | |
680 | if (!list_empty(&pool->sp_all_threads)) { | |
681 | struct svc_rqst *rqstp; | |
682 | ||
683 | /* | |
684 | * Remove from the pool->sp_all_threads list | |
685 | * so we don't try to kill it again. | |
686 | */ | |
687 | rqstp = list_entry(pool->sp_all_threads.next, struct svc_rqst, rq_all); | |
81244386 JL |
688 | set_bit(RQ_VICTIM, &rqstp->rq_flags); |
689 | list_del_rcu(&rqstp->rq_all); | |
a7455442 | 690 | task = rqstp->rq_task; |
cca5172a | 691 | } |
a7455442 GB |
692 | spin_unlock_bh(&pool->sp_lock); |
693 | ||
694 | return task; | |
695 | } | |
696 | ||
697 | /* | |
698 | * Create or destroy enough new threads to make the number | |
699 | * of threads the given number. If `pool' is non-NULL, applies | |
700 | * only to threads in that pool, otherwise round-robins between | |
94cf3179 BF |
701 | * all pools. Caller must ensure that mutual exclusion between this and |
702 | * server startup or shutdown. | |
a7455442 GB |
703 | * |
704 | * Destroying threads relies on the service threads filling in | |
705 | * rqstp->rq_task, which only the nfs ones do. Assumes the serv | |
706 | * has been created using svc_create_pooled(). | |
707 | * | |
708 | * Based on code that used to be in nfsd_svc() but tweaked | |
709 | * to be pool-aware. | |
710 | */ | |
711 | int | |
712 | svc_set_num_threads(struct svc_serv *serv, struct svc_pool *pool, int nrservs) | |
713 | { | |
9867d76c JL |
714 | struct svc_rqst *rqstp; |
715 | struct task_struct *task; | |
716 | struct svc_pool *chosen_pool; | |
a7455442 GB |
717 | int error = 0; |
718 | unsigned int state = serv->sv_nrthreads-1; | |
11fd165c | 719 | int node; |
a7455442 GB |
720 | |
721 | if (pool == NULL) { | |
722 | /* The -1 assumes caller has done a svc_get() */ | |
723 | nrservs -= (serv->sv_nrthreads-1); | |
724 | } else { | |
725 | spin_lock_bh(&pool->sp_lock); | |
726 | nrservs -= pool->sp_nrthreads; | |
727 | spin_unlock_bh(&pool->sp_lock); | |
728 | } | |
729 | ||
730 | /* create new threads */ | |
731 | while (nrservs > 0) { | |
732 | nrservs--; | |
9867d76c JL |
733 | chosen_pool = choose_pool(serv, pool, &state); |
734 | ||
11fd165c ED |
735 | node = svc_pool_map_get_node(chosen_pool->sp_id); |
736 | rqstp = svc_prepare_thread(serv, chosen_pool, node); | |
9867d76c JL |
737 | if (IS_ERR(rqstp)) { |
738 | error = PTR_ERR(rqstp); | |
739 | break; | |
740 | } | |
741 | ||
a7455442 | 742 | __module_get(serv->sv_module); |
11fd165c | 743 | task = kthread_create_on_node(serv->sv_function, rqstp, |
f170168b | 744 | node, "%s", serv->sv_name); |
9867d76c JL |
745 | if (IS_ERR(task)) { |
746 | error = PTR_ERR(task); | |
a7455442 | 747 | module_put(serv->sv_module); |
9867d76c | 748 | svc_exit_thread(rqstp); |
a7455442 GB |
749 | break; |
750 | } | |
9867d76c JL |
751 | |
752 | rqstp->rq_task = task; | |
753 | if (serv->sv_nrpools > 1) | |
754 | svc_pool_map_set_cpumask(task, chosen_pool->sp_id); | |
755 | ||
756 | svc_sock_update_bufs(serv); | |
757 | wake_up_process(task); | |
a7455442 GB |
758 | } |
759 | /* destroy old threads */ | |
760 | while (nrservs < 0 && | |
9867d76c | 761 | (task = choose_victim(serv, pool, &state)) != NULL) { |
a75c5d01 | 762 | send_sig(SIGINT, task, 1); |
a7455442 GB |
763 | nrservs++; |
764 | } | |
765 | ||
766 | return error; | |
767 | } | |
24c3767e | 768 | EXPORT_SYMBOL_GPL(svc_set_num_threads); |
a7455442 | 769 | |
3262c816 | 770 | /* |
bedbdd8b NB |
771 | * Called from a server thread as it's exiting. Caller must hold the BKL or |
772 | * the "service mutex", whichever is appropriate for the service. | |
1da177e4 LT |
773 | */ |
774 | void | |
775 | svc_exit_thread(struct svc_rqst *rqstp) | |
776 | { | |
777 | struct svc_serv *serv = rqstp->rq_server; | |
3262c816 | 778 | struct svc_pool *pool = rqstp->rq_pool; |
1da177e4 LT |
779 | |
780 | svc_release_buffer(rqstp); | |
a51482bd JJ |
781 | kfree(rqstp->rq_resp); |
782 | kfree(rqstp->rq_argp); | |
783 | kfree(rqstp->rq_auth_data); | |
3262c816 GB |
784 | |
785 | spin_lock_bh(&pool->sp_lock); | |
786 | pool->sp_nrthreads--; | |
81244386 JL |
787 | if (!test_and_set_bit(RQ_VICTIM, &rqstp->rq_flags)) |
788 | list_del_rcu(&rqstp->rq_all); | |
3262c816 GB |
789 | spin_unlock_bh(&pool->sp_lock); |
790 | ||
81244386 | 791 | kfree_rcu(rqstp, rq_rcu_head); |
1da177e4 LT |
792 | |
793 | /* Release the server */ | |
794 | if (serv) | |
795 | svc_destroy(serv); | |
796 | } | |
24c3767e | 797 | EXPORT_SYMBOL_GPL(svc_exit_thread); |
1da177e4 LT |
798 | |
799 | /* | |
2c7eb0b2 CL |
800 | * Register an "inet" protocol family netid with the local |
801 | * rpcbind daemon via an rpcbind v4 SET request. | |
a26cfad6 | 802 | * |
2c7eb0b2 CL |
803 | * No netconfig infrastructure is available in the kernel, so |
804 | * we map IP_ protocol numbers to netids by hand. | |
a26cfad6 | 805 | * |
2c7eb0b2 CL |
806 | * Returns zero on success; a negative errno value is returned |
807 | * if any error occurs. | |
1da177e4 | 808 | */ |
5247fab5 SK |
809 | static int __svc_rpcb_register4(struct net *net, const u32 program, |
810 | const u32 version, | |
2c7eb0b2 CL |
811 | const unsigned short protocol, |
812 | const unsigned short port) | |
a26cfad6 | 813 | { |
cadc0fa5 | 814 | const struct sockaddr_in sin = { |
a26cfad6 CL |
815 | .sin_family = AF_INET, |
816 | .sin_addr.s_addr = htonl(INADDR_ANY), | |
817 | .sin_port = htons(port), | |
818 | }; | |
cadc0fa5 CL |
819 | const char *netid; |
820 | int error; | |
2c7eb0b2 CL |
821 | |
822 | switch (protocol) { | |
823 | case IPPROTO_UDP: | |
824 | netid = RPCBIND_NETID_UDP; | |
825 | break; | |
826 | case IPPROTO_TCP: | |
827 | netid = RPCBIND_NETID_TCP; | |
828 | break; | |
829 | default: | |
ba5c35e0 | 830 | return -ENOPROTOOPT; |
2c7eb0b2 CL |
831 | } |
832 | ||
5247fab5 | 833 | error = rpcb_v4_register(net, program, version, |
cadc0fa5 CL |
834 | (const struct sockaddr *)&sin, netid); |
835 | ||
836 | /* | |
837 | * User space didn't support rpcbind v4, so retry this | |
838 | * registration request with the legacy rpcbind v2 protocol. | |
839 | */ | |
840 | if (error == -EPROTONOSUPPORT) | |
5247fab5 | 841 | error = rpcb_register(net, program, version, protocol, port); |
cadc0fa5 CL |
842 | |
843 | return error; | |
2c7eb0b2 CL |
844 | } |
845 | ||
dfd56b8b | 846 | #if IS_ENABLED(CONFIG_IPV6) |
2c7eb0b2 CL |
847 | /* |
848 | * Register an "inet6" protocol family netid with the local | |
849 | * rpcbind daemon via an rpcbind v4 SET request. | |
850 | * | |
851 | * No netconfig infrastructure is available in the kernel, so | |
852 | * we map IP_ protocol numbers to netids by hand. | |
853 | * | |
854 | * Returns zero on success; a negative errno value is returned | |
855 | * if any error occurs. | |
856 | */ | |
5247fab5 SK |
857 | static int __svc_rpcb_register6(struct net *net, const u32 program, |
858 | const u32 version, | |
2c7eb0b2 CL |
859 | const unsigned short protocol, |
860 | const unsigned short port) | |
861 | { | |
cadc0fa5 | 862 | const struct sockaddr_in6 sin6 = { |
a26cfad6 CL |
863 | .sin6_family = AF_INET6, |
864 | .sin6_addr = IN6ADDR_ANY_INIT, | |
865 | .sin6_port = htons(port), | |
866 | }; | |
cadc0fa5 CL |
867 | const char *netid; |
868 | int error; | |
a26cfad6 | 869 | |
2c7eb0b2 CL |
870 | switch (protocol) { |
871 | case IPPROTO_UDP: | |
872 | netid = RPCBIND_NETID_UDP6; | |
a26cfad6 | 873 | break; |
2c7eb0b2 | 874 | case IPPROTO_TCP: |
a26cfad6 | 875 | netid = RPCBIND_NETID_TCP6; |
a26cfad6 CL |
876 | break; |
877 | default: | |
ba5c35e0 | 878 | return -ENOPROTOOPT; |
2c7eb0b2 CL |
879 | } |
880 | ||
5247fab5 | 881 | error = rpcb_v4_register(net, program, version, |
cadc0fa5 CL |
882 | (const struct sockaddr *)&sin6, netid); |
883 | ||
884 | /* | |
885 | * User space didn't support rpcbind version 4, so we won't | |
886 | * use a PF_INET6 listener. | |
887 | */ | |
888 | if (error == -EPROTONOSUPPORT) | |
889 | error = -EAFNOSUPPORT; | |
890 | ||
891 | return error; | |
2c7eb0b2 | 892 | } |
dfd56b8b | 893 | #endif /* IS_ENABLED(CONFIG_IPV6) */ |
2c7eb0b2 CL |
894 | |
895 | /* | |
896 | * Register a kernel RPC service via rpcbind version 4. | |
897 | * | |
898 | * Returns zero on success; a negative errno value is returned | |
899 | * if any error occurs. | |
900 | */ | |
5247fab5 | 901 | static int __svc_register(struct net *net, const char *progname, |
363f724c | 902 | const u32 program, const u32 version, |
4b62e58c | 903 | const int family, |
2c7eb0b2 CL |
904 | const unsigned short protocol, |
905 | const unsigned short port) | |
906 | { | |
363f724c | 907 | int error = -EAFNOSUPPORT; |
2c7eb0b2 CL |
908 | |
909 | switch (family) { | |
4b62e58c | 910 | case PF_INET: |
5247fab5 | 911 | error = __svc_rpcb_register4(net, program, version, |
2c7eb0b2 | 912 | protocol, port); |
cadc0fa5 | 913 | break; |
dfd56b8b | 914 | #if IS_ENABLED(CONFIG_IPV6) |
4b62e58c | 915 | case PF_INET6: |
5247fab5 | 916 | error = __svc_rpcb_register6(net, program, version, |
2c7eb0b2 | 917 | protocol, port); |
dfd56b8b | 918 | #endif |
a26cfad6 CL |
919 | } |
920 | ||
363f724c | 921 | return error; |
a26cfad6 | 922 | } |
2c7eb0b2 | 923 | |
a26cfad6 CL |
924 | /** |
925 | * svc_register - register an RPC service with the local portmapper | |
926 | * @serv: svc_serv struct for the service to register | |
5247fab5 | 927 | * @net: net namespace for the service to register |
4b62e58c | 928 | * @family: protocol family of service's listener socket |
a26cfad6 CL |
929 | * @proto: transport protocol number to advertise |
930 | * @port: port to advertise | |
931 | * | |
4b62e58c | 932 | * Service is registered for any address in the passed-in protocol family |
a26cfad6 | 933 | */ |
5247fab5 SK |
934 | int svc_register(const struct svc_serv *serv, struct net *net, |
935 | const int family, const unsigned short proto, | |
936 | const unsigned short port) | |
1da177e4 LT |
937 | { |
938 | struct svc_program *progp; | |
7e55b59b | 939 | struct svc_version *vers; |
ea339d46 | 940 | unsigned int i; |
14aeb211 | 941 | int error = 0; |
1da177e4 | 942 | |
0af39507 WAA |
943 | WARN_ON_ONCE(proto == 0 && port == 0); |
944 | if (proto == 0 && port == 0) | |
945 | return -EINVAL; | |
1da177e4 | 946 | |
bc5fea42 OK |
947 | for (progp = serv->sv_program; progp; progp = progp->pg_next) { |
948 | for (i = 0; i < progp->pg_nvers; i++) { | |
7e55b59b KM |
949 | vers = progp->pg_vers[i]; |
950 | if (vers == NULL) | |
bc5fea42 OK |
951 | continue; |
952 | ||
2c7eb0b2 | 953 | dprintk("svc: svc_register(%sv%d, %s, %u, %u)%s\n", |
bc5fea42 | 954 | progp->pg_name, |
2c7eb0b2 | 955 | i, |
bc5fea42 OK |
956 | proto == IPPROTO_UDP? "udp" : "tcp", |
957 | port, | |
4b62e58c | 958 | family, |
7e55b59b KM |
959 | vers->vs_hidden ? |
960 | " (but not telling portmap)" : ""); | |
bc5fea42 | 961 | |
7e55b59b | 962 | if (vers->vs_hidden) |
bc5fea42 OK |
963 | continue; |
964 | ||
5247fab5 | 965 | error = __svc_register(net, progp->pg_name, progp->pg_prog, |
363f724c | 966 | i, family, proto, port); |
7e55b59b KM |
967 | |
968 | if (vers->vs_rpcb_optnl) { | |
969 | error = 0; | |
970 | continue; | |
971 | } | |
972 | ||
973 | if (error < 0) { | |
974 | printk(KERN_WARNING "svc: failed to register " | |
975 | "%sv%u RPC service (errno %d).\n", | |
976 | progp->pg_name, i, -error); | |
bc5fea42 | 977 | break; |
7e55b59b | 978 | } |
1da177e4 LT |
979 | } |
980 | } | |
981 | ||
7252d575 CL |
982 | return error; |
983 | } | |
984 | ||
d5a8620f CL |
985 | /* |
986 | * If user space is running rpcbind, it should take the v4 UNSET | |
987 | * and clear everything for this [program, version]. If user space | |
988 | * is running portmap, it will reject the v4 UNSET, but won't have | |
989 | * any "inet6" entries anyway. So a PMAP_UNSET should be sufficient | |
990 | * in this case to clear all existing entries for [program, version]. | |
991 | */ | |
5247fab5 | 992 | static void __svc_unregister(struct net *net, const u32 program, const u32 version, |
f6fb3f6f CL |
993 | const char *progname) |
994 | { | |
f6fb3f6f CL |
995 | int error; |
996 | ||
5247fab5 | 997 | error = rpcb_v4_register(net, program, version, NULL, ""); |
f6fb3f6f | 998 | |
d5a8620f CL |
999 | /* |
1000 | * User space didn't support rpcbind v4, so retry this | |
1001 | * request with the legacy rpcbind v2 protocol. | |
1002 | */ | |
1003 | if (error == -EPROTONOSUPPORT) | |
5247fab5 | 1004 | error = rpcb_register(net, program, version, 0, 0); |
f6fb3f6f | 1005 | |
f6fb3f6f CL |
1006 | dprintk("svc: %s(%sv%u), error %d\n", |
1007 | __func__, progname, version, error); | |
1008 | } | |
1009 | ||
7252d575 | 1010 | /* |
f6fb3f6f CL |
1011 | * All netids, bind addresses and ports registered for [program, version] |
1012 | * are removed from the local rpcbind database (if the service is not | |
1013 | * hidden) to make way for a new instance of the service. | |
7252d575 | 1014 | * |
f6fb3f6f CL |
1015 | * The result of unregistration is reported via dprintk for those who want |
1016 | * verification of the result, but is otherwise not important. | |
7252d575 | 1017 | */ |
5247fab5 | 1018 | static void svc_unregister(const struct svc_serv *serv, struct net *net) |
7252d575 CL |
1019 | { |
1020 | struct svc_program *progp; | |
1021 | unsigned long flags; | |
1022 | unsigned int i; | |
7252d575 CL |
1023 | |
1024 | clear_thread_flag(TIF_SIGPENDING); | |
1025 | ||
1026 | for (progp = serv->sv_program; progp; progp = progp->pg_next) { | |
1027 | for (i = 0; i < progp->pg_nvers; i++) { | |
1028 | if (progp->pg_vers[i] == NULL) | |
1029 | continue; | |
1030 | if (progp->pg_vers[i]->vs_hidden) | |
1031 | continue; | |
1032 | ||
7402ab19 CL |
1033 | dprintk("svc: attempting to unregister %sv%u\n", |
1034 | progp->pg_name, i); | |
5247fab5 | 1035 | __svc_unregister(net, progp->pg_prog, i, progp->pg_name); |
7252d575 | 1036 | } |
1da177e4 LT |
1037 | } |
1038 | ||
7252d575 CL |
1039 | spin_lock_irqsave(¤t->sighand->siglock, flags); |
1040 | recalc_sigpending(); | |
1041 | spin_unlock_irqrestore(¤t->sighand->siglock, flags); | |
1da177e4 LT |
1042 | } |
1043 | ||
354ecbb9 | 1044 | /* |
7032a3dd | 1045 | * dprintk the given error with the address of the client that caused it. |
354ecbb9 | 1046 | */ |
f895b252 | 1047 | #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) |
b9075fa9 | 1048 | static __printf(2, 3) |
e87cc472 | 1049 | void svc_printk(struct svc_rqst *rqstp, const char *fmt, ...) |
354ecbb9 | 1050 | { |
e87cc472 | 1051 | struct va_format vaf; |
354ecbb9 | 1052 | va_list args; |
354ecbb9 DDAG |
1053 | char buf[RPC_MAX_ADDRBUFLEN]; |
1054 | ||
e87cc472 | 1055 | va_start(args, fmt); |
354ecbb9 | 1056 | |
e87cc472 JP |
1057 | vaf.fmt = fmt; |
1058 | vaf.va = &args; | |
354ecbb9 | 1059 | |
7032a3dd | 1060 | dprintk("svc: %s: %pV", svc_print_addr(rqstp, buf, sizeof(buf)), &vaf); |
354ecbb9 | 1061 | |
e87cc472 | 1062 | va_end(args); |
354ecbb9 | 1063 | } |
624ab464 BF |
1064 | #else |
1065 | static __printf(2,3) void svc_printk(struct svc_rqst *rqstp, const char *fmt, ...) {} | |
1066 | #endif | |
354ecbb9 | 1067 | |
1da177e4 | 1068 | /* |
1cad7ea6 | 1069 | * Common routine for processing the RPC request. |
1da177e4 | 1070 | */ |
1cad7ea6 RL |
1071 | static int |
1072 | svc_process_common(struct svc_rqst *rqstp, struct kvec *argv, struct kvec *resv) | |
1da177e4 LT |
1073 | { |
1074 | struct svc_program *progp; | |
1075 | struct svc_version *versp = NULL; /* compiler food */ | |
1076 | struct svc_procedure *procp = NULL; | |
6fb2b47f | 1077 | struct svc_serv *serv = rqstp->rq_server; |
1da177e4 | 1078 | kxdrproc_t xdr; |
d8ed029d | 1079 | __be32 *statp; |
1cad7ea6 | 1080 | u32 prog, vers, proc; |
d8ed029d | 1081 | __be32 auth_stat, rpc_stat; |
1da177e4 | 1082 | int auth_res; |
8f8e05c5 | 1083 | __be32 *reply_statp; |
1da177e4 LT |
1084 | |
1085 | rpc_stat = rpc_success; | |
1086 | ||
1087 | if (argv->iov_len < 6*4) | |
1088 | goto err_short_len; | |
1089 | ||
5c04c46a | 1090 | /* Will be turned off only in gss privacy case: */ |
779fb0f3 | 1091 | set_bit(RQ_SPLICE_OK, &rqstp->rq_flags); |
2f425878 | 1092 | /* Will be turned off only when NFSv4 Sessions are used */ |
30660e04 | 1093 | set_bit(RQ_USEDEFERRAL, &rqstp->rq_flags); |
78b65eb3 | 1094 | clear_bit(RQ_DROPME, &rqstp->rq_flags); |
e831fe65 TT |
1095 | |
1096 | /* Setup reply header */ | |
1097 | rqstp->rq_xprt->xpt_ops->xpo_prep_reply_hdr(rqstp); | |
1da177e4 | 1098 | |
1da177e4 LT |
1099 | svc_putu32(resv, rqstp->rq_xid); |
1100 | ||
76994313 | 1101 | vers = svc_getnl(argv); |
1da177e4 LT |
1102 | |
1103 | /* First words of reply: */ | |
76994313 | 1104 | svc_putnl(resv, 1); /* REPLY */ |
1da177e4 | 1105 | |
1da177e4 LT |
1106 | if (vers != 2) /* RPC version number */ |
1107 | goto err_bad_rpc; | |
1108 | ||
1109 | /* Save position in case we later decide to reject: */ | |
8f8e05c5 | 1110 | reply_statp = resv->iov_base + resv->iov_len; |
1da177e4 | 1111 | |
76994313 | 1112 | svc_putnl(resv, 0); /* ACCEPT */ |
1da177e4 | 1113 | |
76994313 AD |
1114 | rqstp->rq_prog = prog = svc_getnl(argv); /* program number */ |
1115 | rqstp->rq_vers = vers = svc_getnl(argv); /* version number */ | |
1116 | rqstp->rq_proc = proc = svc_getnl(argv); /* procedure number */ | |
1da177e4 | 1117 | |
80d188a6 N |
1118 | for (progp = serv->sv_program; progp; progp = progp->pg_next) |
1119 | if (prog == progp->pg_prog) | |
1120 | break; | |
1121 | ||
1da177e4 LT |
1122 | /* |
1123 | * Decode auth data, and add verifier to reply buffer. | |
1124 | * We do this before anything else in order to get a decent | |
1125 | * auth verifier. | |
1126 | */ | |
1127 | auth_res = svc_authenticate(rqstp, &auth_stat); | |
1128 | /* Also give the program a chance to reject this call: */ | |
80d188a6 | 1129 | if (auth_res == SVC_OK && progp) { |
1da177e4 LT |
1130 | auth_stat = rpc_autherr_badcred; |
1131 | auth_res = progp->pg_authenticate(rqstp); | |
1132 | } | |
1133 | switch (auth_res) { | |
1134 | case SVC_OK: | |
1135 | break; | |
1136 | case SVC_GARBAGE: | |
dd35210e | 1137 | goto err_garbage; |
1da177e4 LT |
1138 | case SVC_SYSERR: |
1139 | rpc_stat = rpc_system_err; | |
1140 | goto err_bad; | |
1141 | case SVC_DENIED: | |
1142 | goto err_bad_auth; | |
1ebede86 N |
1143 | case SVC_CLOSE: |
1144 | if (test_bit(XPT_TEMP, &rqstp->rq_xprt->xpt_flags)) | |
1145 | svc_close_xprt(rqstp->rq_xprt); | |
1da177e4 LT |
1146 | case SVC_DROP: |
1147 | goto dropit; | |
1148 | case SVC_COMPLETE: | |
1149 | goto sendit; | |
1150 | } | |
80d188a6 | 1151 | |
9ba02638 | 1152 | if (progp == NULL) |
1da177e4 LT |
1153 | goto err_bad_prog; |
1154 | ||
1155 | if (vers >= progp->pg_nvers || | |
1156 | !(versp = progp->pg_vers[vers])) | |
1157 | goto err_bad_vers; | |
1158 | ||
1159 | procp = versp->vs_proc + proc; | |
1160 | if (proc >= versp->vs_nproc || !procp->pc_func) | |
1161 | goto err_bad_proc; | |
1da177e4 LT |
1162 | rqstp->rq_procinfo = procp; |
1163 | ||
1164 | /* Syntactic check complete */ | |
1165 | serv->sv_stats->rpccnt++; | |
1166 | ||
1167 | /* Build the reply header. */ | |
1168 | statp = resv->iov_base +resv->iov_len; | |
76994313 | 1169 | svc_putnl(resv, RPC_SUCCESS); |
1da177e4 LT |
1170 | |
1171 | /* Bump per-procedure stats counter */ | |
1172 | procp->pc_count++; | |
1173 | ||
1174 | /* Initialize storage for argp and resp */ | |
1175 | memset(rqstp->rq_argp, 0, procp->pc_argsize); | |
1176 | memset(rqstp->rq_resp, 0, procp->pc_ressize); | |
1177 | ||
cca5172a | 1178 | /* un-reserve some of the out-queue now that we have a |
1da177e4 LT |
1179 | * better idea of reply size |
1180 | */ | |
1181 | if (procp->pc_xdrressize) | |
cd123012 | 1182 | svc_reserve_auth(rqstp, procp->pc_xdrressize<<2); |
1da177e4 LT |
1183 | |
1184 | /* Call the function that processes the request. */ | |
1185 | if (!versp->vs_dispatch) { | |
1186 | /* Decode arguments */ | |
1187 | xdr = procp->pc_decode; | |
1188 | if (xdr && !xdr(rqstp, argv->iov_base, rqstp->rq_argp)) | |
1189 | goto err_garbage; | |
1190 | ||
1191 | *statp = procp->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp); | |
1192 | ||
1193 | /* Encode reply */ | |
78b65eb3 | 1194 | if (test_bit(RQ_DROPME, &rqstp->rq_flags)) { |
d343fce1 N |
1195 | if (procp->pc_release) |
1196 | procp->pc_release(rqstp, NULL, rqstp->rq_resp); | |
1197 | goto dropit; | |
1198 | } | |
f64f9e71 JP |
1199 | if (*statp == rpc_success && |
1200 | (xdr = procp->pc_encode) && | |
1201 | !xdr(rqstp, resv->iov_base+resv->iov_len, rqstp->rq_resp)) { | |
1da177e4 LT |
1202 | dprintk("svc: failed to encode reply\n"); |
1203 | /* serv->sv_stats->rpcsystemerr++; */ | |
1204 | *statp = rpc_system_err; | |
1205 | } | |
1206 | } else { | |
1207 | dprintk("svc: calling dispatcher\n"); | |
1208 | if (!versp->vs_dispatch(rqstp, statp)) { | |
1209 | /* Release reply info */ | |
1210 | if (procp->pc_release) | |
1211 | procp->pc_release(rqstp, NULL, rqstp->rq_resp); | |
1212 | goto dropit; | |
1213 | } | |
1214 | } | |
1215 | ||
1216 | /* Check RPC status result */ | |
1217 | if (*statp != rpc_success) | |
1218 | resv->iov_len = ((void*)statp) - resv->iov_base + 4; | |
1219 | ||
1220 | /* Release reply info */ | |
1221 | if (procp->pc_release) | |
1222 | procp->pc_release(rqstp, NULL, rqstp->rq_resp); | |
1223 | ||
1224 | if (procp->pc_encode == NULL) | |
1225 | goto dropit; | |
1226 | ||
1227 | sendit: | |
1228 | if (svc_authorise(rqstp)) | |
1229 | goto dropit; | |
1cad7ea6 | 1230 | return 1; /* Caller can now send it */ |
1da177e4 LT |
1231 | |
1232 | dropit: | |
1233 | svc_authorise(rqstp); /* doesn't hurt to call this twice */ | |
1234 | dprintk("svc: svc_process dropit\n"); | |
1da177e4 LT |
1235 | return 0; |
1236 | ||
1237 | err_short_len: | |
354ecbb9 DDAG |
1238 | svc_printk(rqstp, "short len %Zd, dropping request\n", |
1239 | argv->iov_len); | |
34e9a63b | 1240 | |
1da177e4 LT |
1241 | goto dropit; /* drop request */ |
1242 | ||
1da177e4 LT |
1243 | err_bad_rpc: |
1244 | serv->sv_stats->rpcbadfmt++; | |
76994313 AD |
1245 | svc_putnl(resv, 1); /* REJECT */ |
1246 | svc_putnl(resv, 0); /* RPC_MISMATCH */ | |
1247 | svc_putnl(resv, 2); /* Only RPCv2 supported */ | |
1248 | svc_putnl(resv, 2); | |
1da177e4 LT |
1249 | goto sendit; |
1250 | ||
1251 | err_bad_auth: | |
1252 | dprintk("svc: authentication failed (%d)\n", ntohl(auth_stat)); | |
1253 | serv->sv_stats->rpcbadauth++; | |
1254 | /* Restore write pointer to location of accept status: */ | |
8f8e05c5 | 1255 | xdr_ressize_check(rqstp, reply_statp); |
76994313 AD |
1256 | svc_putnl(resv, 1); /* REJECT */ |
1257 | svc_putnl(resv, 1); /* AUTH_ERROR */ | |
1258 | svc_putnl(resv, ntohl(auth_stat)); /* status */ | |
1da177e4 LT |
1259 | goto sendit; |
1260 | ||
1261 | err_bad_prog: | |
9ba02638 | 1262 | dprintk("svc: unknown program %d\n", prog); |
1da177e4 | 1263 | serv->sv_stats->rpcbadfmt++; |
76994313 | 1264 | svc_putnl(resv, RPC_PROG_UNAVAIL); |
1da177e4 LT |
1265 | goto sendit; |
1266 | ||
1267 | err_bad_vers: | |
354ecbb9 | 1268 | svc_printk(rqstp, "unknown version (%d for prog %d, %s)\n", |
34e9a63b N |
1269 | vers, prog, progp->pg_name); |
1270 | ||
1da177e4 | 1271 | serv->sv_stats->rpcbadfmt++; |
76994313 AD |
1272 | svc_putnl(resv, RPC_PROG_MISMATCH); |
1273 | svc_putnl(resv, progp->pg_lovers); | |
1274 | svc_putnl(resv, progp->pg_hivers); | |
1da177e4 LT |
1275 | goto sendit; |
1276 | ||
1277 | err_bad_proc: | |
354ecbb9 | 1278 | svc_printk(rqstp, "unknown procedure (%d)\n", proc); |
34e9a63b | 1279 | |
1da177e4 | 1280 | serv->sv_stats->rpcbadfmt++; |
76994313 | 1281 | svc_putnl(resv, RPC_PROC_UNAVAIL); |
1da177e4 LT |
1282 | goto sendit; |
1283 | ||
1284 | err_garbage: | |
354ecbb9 | 1285 | svc_printk(rqstp, "failed to decode args\n"); |
34e9a63b | 1286 | |
1da177e4 LT |
1287 | rpc_stat = rpc_garbage_args; |
1288 | err_bad: | |
1289 | serv->sv_stats->rpcbadfmt++; | |
76994313 | 1290 | svc_putnl(resv, ntohl(rpc_stat)); |
1da177e4 LT |
1291 | goto sendit; |
1292 | } | |
24c3767e | 1293 | EXPORT_SYMBOL_GPL(svc_process); |
7adae489 | 1294 | |
1cad7ea6 RL |
1295 | /* |
1296 | * Process the RPC request. | |
1297 | */ | |
1298 | int | |
1299 | svc_process(struct svc_rqst *rqstp) | |
1300 | { | |
1301 | struct kvec *argv = &rqstp->rq_arg.head[0]; | |
1302 | struct kvec *resv = &rqstp->rq_res.head[0]; | |
1303 | struct svc_serv *serv = rqstp->rq_server; | |
1304 | u32 dir; | |
1cad7ea6 RL |
1305 | |
1306 | /* | |
1307 | * Setup response xdr_buf. | |
1308 | * Initially it has just one page | |
1309 | */ | |
afc59400 | 1310 | rqstp->rq_next_page = &rqstp->rq_respages[1]; |
1cad7ea6 RL |
1311 | resv->iov_base = page_address(rqstp->rq_respages[0]); |
1312 | resv->iov_len = 0; | |
1313 | rqstp->rq_res.pages = rqstp->rq_respages + 1; | |
1314 | rqstp->rq_res.len = 0; | |
1315 | rqstp->rq_res.page_base = 0; | |
1316 | rqstp->rq_res.page_len = 0; | |
1317 | rqstp->rq_res.buflen = PAGE_SIZE; | |
1318 | rqstp->rq_res.tail[0].iov_base = NULL; | |
1319 | rqstp->rq_res.tail[0].iov_len = 0; | |
1320 | ||
1cad7ea6 RL |
1321 | dir = svc_getnl(argv); |
1322 | if (dir != 0) { | |
1323 | /* direction != CALL */ | |
1324 | svc_printk(rqstp, "bad direction %d, dropping request\n", dir); | |
1325 | serv->sv_stats->rpcbadfmt++; | |
860a0d9e | 1326 | goto out_drop; |
1cad7ea6 RL |
1327 | } |
1328 | ||
4b5b3ba1 | 1329 | /* Returns 1 for send, 0 for drop */ |
860a0d9e JL |
1330 | if (likely(svc_process_common(rqstp, argv, resv))) { |
1331 | int ret = svc_send(rqstp); | |
1332 | ||
1333 | trace_svc_process(rqstp, ret); | |
1334 | return ret; | |
4b5b3ba1 | 1335 | } |
860a0d9e JL |
1336 | out_drop: |
1337 | trace_svc_process(rqstp, 0); | |
1338 | svc_drop(rqstp); | |
1339 | return 0; | |
1cad7ea6 RL |
1340 | } |
1341 | ||
9e00abc3 | 1342 | #if defined(CONFIG_SUNRPC_BACKCHANNEL) |
4d6bbb62 RL |
1343 | /* |
1344 | * Process a backchannel RPC request that arrived over an existing | |
1345 | * outbound connection | |
1346 | */ | |
1347 | int | |
1348 | bc_svc_process(struct svc_serv *serv, struct rpc_rqst *req, | |
1349 | struct svc_rqst *rqstp) | |
1350 | { | |
1351 | struct kvec *argv = &rqstp->rq_arg.head[0]; | |
1352 | struct kvec *resv = &rqstp->rq_res.head[0]; | |
4d6bbb62 RL |
1353 | |
1354 | /* Build the svc_rqst used by the common processing routine */ | |
4a19de0f | 1355 | rqstp->rq_xprt = serv->sv_bc_xprt; |
4d6bbb62 RL |
1356 | rqstp->rq_xid = req->rq_xid; |
1357 | rqstp->rq_prot = req->rq_xprt->prot; | |
1358 | rqstp->rq_server = serv; | |
1359 | ||
1360 | rqstp->rq_addrlen = sizeof(req->rq_xprt->addr); | |
1361 | memcpy(&rqstp->rq_addr, &req->rq_xprt->addr, rqstp->rq_addrlen); | |
1362 | memcpy(&rqstp->rq_arg, &req->rq_rcv_buf, sizeof(rqstp->rq_arg)); | |
1363 | memcpy(&rqstp->rq_res, &req->rq_snd_buf, sizeof(rqstp->rq_res)); | |
1364 | ||
1365 | /* reset result send buffer "put" position */ | |
1366 | resv->iov_len = 0; | |
1367 | ||
1368 | if (rqstp->rq_prot != IPPROTO_TCP) { | |
1369 | printk(KERN_ERR "No support for Non-TCP transports!\n"); | |
1370 | BUG(); | |
1371 | } | |
1372 | ||
1373 | /* | |
1374 | * Skip the next two words because they've already been | |
1375 | * processed in the trasport | |
1376 | */ | |
1377 | svc_getu32(argv); /* XID */ | |
1378 | svc_getnl(argv); /* CALLDIR */ | |
1379 | ||
4b5b3ba1 AA |
1380 | /* Returns 1 for send, 0 for drop */ |
1381 | if (svc_process_common(rqstp, argv, resv)) { | |
1382 | memcpy(&req->rq_snd_buf, &rqstp->rq_res, | |
1383 | sizeof(req->rq_snd_buf)); | |
1384 | return bc_send(req); | |
1385 | } else { | |
b3b02ae5 TM |
1386 | /* drop request */ |
1387 | xprt_free_bc_request(req); | |
4b5b3ba1 AA |
1388 | return 0; |
1389 | } | |
4d6bbb62 | 1390 | } |
0d961aa9 | 1391 | EXPORT_SYMBOL_GPL(bc_svc_process); |
9e00abc3 | 1392 | #endif /* CONFIG_SUNRPC_BACKCHANNEL */ |
4d6bbb62 | 1393 | |
7adae489 GB |
1394 | /* |
1395 | * Return (transport-specific) limit on the rpc payload. | |
1396 | */ | |
1397 | u32 svc_max_payload(const struct svc_rqst *rqstp) | |
1398 | { | |
49023155 | 1399 | u32 max = rqstp->rq_xprt->xpt_class->xcl_max_payload; |
7adae489 | 1400 | |
c6b0a9f8 N |
1401 | if (rqstp->rq_server->sv_max_payload < max) |
1402 | max = rqstp->rq_server->sv_max_payload; | |
7adae489 GB |
1403 | return max; |
1404 | } | |
1405 | EXPORT_SYMBOL_GPL(svc_max_payload); |