nfsd/sunrpc: move sv_function into sv_ops
[deliverable/linux.git] / net / sunrpc / svc.c
CommitLineData
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 35static void svc_unregister(const struct svc_serv *serv, struct net *net);
7252d575 36
c369014f 37#define svc_serv_is_pooled(serv) ((serv)->sv_ops->svo_function)
42a7fc4a 38
bfd24160
GB
39/*
40 * Mode for mapping cpus to pools.
41 */
42enum {
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 */
55static 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
67static DEFINE_MUTEX(svc_pool_map_mutex);/* protects svc_pool_map.count only */
68
69static int
70param_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
94out:
95 mutex_unlock(&svc_pool_map_mutex);
96 return err;
97}
98
99static int
100param_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
119module_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 */
126static int
127svc_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 */
158static int
159svc_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
170fail_free:
171 kfree(m->to_pool);
61c8504c 172 m->to_pool = NULL;
bfd24160
GB
173fail:
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 */
181static int
182svc_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 */
209static int
210svc_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 */
239static unsigned int
42a7fc4a 240svc_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 */
283static void
284svc_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
302static 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
318static inline void
319svc_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 */
351struct svc_pool *
352svc_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 375int 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 387EXPORT_SYMBOL_GPL(svc_rpcb_setup);
d9908560 388
5ecebb7c 389void 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 394EXPORT_SYMBOL_GPL(svc_rpcb_cleanup);
d9908560
SK
395
396static 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
413int 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}
419EXPORT_SYMBOL_GPL(svc_bind);
420
1da177e4
LT
421/*
422 * Create an RPC service
423 */
a7455442
GB
424static struct svc_serv *
425__svc_create(struct svc_program *prog, unsigned int bufsize, int npools,
ea126e74 426 struct svc_serv_ops *ops)
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);
ea126e74 443 serv->sv_ops = ops;
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
487struct svc_serv *
488svc_create(struct svc_program *prog, unsigned int bufsize,
ea126e74 489 struct svc_serv_ops *ops)
a7455442 490{
ea126e74 491 return __svc_create(prog, bufsize, /*npools*/1, ops);
a7455442 492}
24c3767e 493EXPORT_SYMBOL_GPL(svc_create);
a7455442
GB
494
495struct svc_serv *
496svc_create_pooled(struct svc_program *prog, unsigned int bufsize,
c369014f 497 struct svc_serv_ops *ops, struct module *mod)
a7455442
GB
498{
499 struct svc_serv *serv;
42a7fc4a 500 unsigned int npools = svc_pool_map_get();
a7455442 501
ea126e74 502 serv = __svc_create(prog, bufsize, npools, ops);
067f96ef
JL
503 if (!serv)
504 goto out_err;
a7455442 505
067f96ef 506 serv->sv_module = mod;
a7455442 507 return serv;
067f96ef
JL
508out_err:
509 svc_pool_map_put();
510 return NULL;
a7455442 511}
24c3767e 512EXPORT_SYMBOL_GPL(svc_create_pooled);
a7455442 513
074d0f67
SK
514void svc_shutdown_net(struct svc_serv *serv, struct net *net)
515{
074d0f67
SK
516 svc_close_net(serv, net);
517
ea126e74
JL
518 if (serv->sv_ops->svo_shutdown)
519 serv->sv_ops->svo_shutdown(serv, net);
074d0f67
SK
520}
521EXPORT_SYMBOL_GPL(svc_shutdown_net);
522
1da177e4 523/*
bedbdd8b
NB
524 * Destroy an RPC service. Should be called with appropriate locking to
525 * protect the sv_nrthreads, sv_permsocks and sv_tempsocks.
1da177e4
LT
526 */
527void
528svc_destroy(struct svc_serv *serv)
529{
46121cf7 530 dprintk("svc: svc_destroy(%s, %d)\n",
1da177e4
LT
531 serv->sv_program->pg_name,
532 serv->sv_nrthreads);
533
534 if (serv->sv_nrthreads) {
535 if (--(serv->sv_nrthreads) != 0) {
536 svc_sock_update_bufs(serv);
537 return;
538 }
539 } else
540 printk("svc_destroy: no threads for serv=%p!\n", serv);
541
36bdfc8b 542 del_timer_sync(&serv->sv_temptimer);
074d0f67 543
7b147f1f
SK
544 /*
545 * The last user is gone and thus all sockets have to be destroyed to
546 * the point. Check this.
547 */
548 BUG_ON(!list_empty(&serv->sv_permsocks));
549 BUG_ON(!list_empty(&serv->sv_tempsocks));
cda1fd4a 550
1da177e4
LT
551 cache_clean_deferred(serv);
552
42a7fc4a
GB
553 if (svc_serv_is_pooled(serv))
554 svc_pool_map_put();
555
3262c816 556 kfree(serv->sv_pools);
1da177e4
LT
557 kfree(serv);
558}
24c3767e 559EXPORT_SYMBOL_GPL(svc_destroy);
1da177e4
LT
560
561/*
562 * Allocate an RPC server's buffer space.
563 * We allocate pages and place them in rq_argpages.
564 */
565static int
11fd165c 566svc_init_buffer(struct svc_rqst *rqstp, unsigned int size, int node)
1da177e4 567{
0dc220f0 568 unsigned int pages, arghi;
cca5172a 569
ba17686f
AA
570 /* bc_xprt uses fore channel allocated buffers */
571 if (svc_is_backchannel(rqstp))
572 return 1;
573
c6b0a9f8
N
574 pages = size / PAGE_SIZE + 1; /* extra page as we hold both request and reply.
575 * We assume one is at most one page
576 */
1da177e4 577 arghi = 0;
b25cd058
WAA
578 WARN_ON_ONCE(pages > RPCSVC_MAXPAGES);
579 if (pages > RPCSVC_MAXPAGES)
580 pages = RPCSVC_MAXPAGES;
1da177e4 581 while (pages) {
11fd165c 582 struct page *p = alloc_pages_node(node, GFP_KERNEL, 0);
1da177e4
LT
583 if (!p)
584 break;
44524359 585 rqstp->rq_pages[arghi++] = p;
1da177e4
LT
586 pages--;
587 }
0dc220f0 588 return pages == 0;
1da177e4
LT
589}
590
591/*
592 * Release an RPC server buffer
593 */
594static void
595svc_release_buffer(struct svc_rqst *rqstp)
596{
50c8bb13
CL
597 unsigned int i;
598
599 for (i = 0; i < ARRAY_SIZE(rqstp->rq_pages); i++)
44524359
N
600 if (rqstp->rq_pages[i])
601 put_page(rqstp->rq_pages[i]);
1da177e4
LT
602}
603
0113ab34 604struct svc_rqst *
11fd165c 605svc_prepare_thread(struct svc_serv *serv, struct svc_pool *pool, int node)
1da177e4
LT
606{
607 struct svc_rqst *rqstp;
1da177e4 608
11fd165c 609 rqstp = kzalloc_node(sizeof(*rqstp), GFP_KERNEL, node);
1da177e4 610 if (!rqstp)
0113ab34 611 goto out_enomem;
1da177e4 612
1da177e4 613 serv->sv_nrthreads++;
b1691bc0
JL
614 __set_bit(RQ_BUSY, &rqstp->rq_flags);
615 spin_lock_init(&rqstp->rq_lock);
616 rqstp->rq_server = serv;
617 rqstp->rq_pool = pool;
3262c816
GB
618 spin_lock_bh(&pool->sp_lock);
619 pool->sp_nrthreads++;
81244386 620 list_add_rcu(&rqstp->rq_all, &pool->sp_all_threads);
3262c816 621 spin_unlock_bh(&pool->sp_lock);
bfd24160 622
11fd165c 623 rqstp->rq_argp = kmalloc_node(serv->sv_xdrsize, GFP_KERNEL, node);
0113ab34
JL
624 if (!rqstp->rq_argp)
625 goto out_thread;
626
11fd165c 627 rqstp->rq_resp = kmalloc_node(serv->sv_xdrsize, GFP_KERNEL, node);
0113ab34
JL
628 if (!rqstp->rq_resp)
629 goto out_thread;
630
11fd165c 631 if (!svc_init_buffer(rqstp, serv->sv_max_mesg, node))
0113ab34
JL
632 goto out_thread;
633
634 return rqstp;
635out_thread:
636 svc_exit_thread(rqstp);
637out_enomem:
638 return ERR_PTR(-ENOMEM);
639}
24c3767e 640EXPORT_SYMBOL_GPL(svc_prepare_thread);
0113ab34 641
a7455442
GB
642/*
643 * Choose a pool in which to create a new thread, for svc_set_num_threads
644 */
645static inline struct svc_pool *
646choose_pool(struct svc_serv *serv, struct svc_pool *pool, unsigned int *state)
647{
648 if (pool != NULL)
649 return pool;
650
cca5172a 651 return &serv->sv_pools[(*state)++ % serv->sv_nrpools];
a7455442
GB
652}
653
654/*
655 * Choose a thread to kill, for svc_set_num_threads
656 */
657static inline struct task_struct *
658choose_victim(struct svc_serv *serv, struct svc_pool *pool, unsigned int *state)
659{
660 unsigned int i;
661 struct task_struct *task = NULL;
662
663 if (pool != NULL) {
664 spin_lock_bh(&pool->sp_lock);
665 } else {
666 /* choose a pool in round-robin fashion */
cca5172a
YH
667 for (i = 0; i < serv->sv_nrpools; i++) {
668 pool = &serv->sv_pools[--(*state) % serv->sv_nrpools];
a7455442 669 spin_lock_bh(&pool->sp_lock);
cca5172a
YH
670 if (!list_empty(&pool->sp_all_threads))
671 goto found_pool;
a7455442 672 spin_unlock_bh(&pool->sp_lock);
cca5172a 673 }
a7455442
GB
674 return NULL;
675 }
676
677found_pool:
678 if (!list_empty(&pool->sp_all_threads)) {
679 struct svc_rqst *rqstp;
680
681 /*
682 * Remove from the pool->sp_all_threads list
683 * so we don't try to kill it again.
684 */
685 rqstp = list_entry(pool->sp_all_threads.next, struct svc_rqst, rq_all);
81244386
JL
686 set_bit(RQ_VICTIM, &rqstp->rq_flags);
687 list_del_rcu(&rqstp->rq_all);
a7455442 688 task = rqstp->rq_task;
cca5172a 689 }
a7455442
GB
690 spin_unlock_bh(&pool->sp_lock);
691
692 return task;
693}
694
695/*
696 * Create or destroy enough new threads to make the number
697 * of threads the given number. If `pool' is non-NULL, applies
698 * only to threads in that pool, otherwise round-robins between
94cf3179
BF
699 * all pools. Caller must ensure that mutual exclusion between this and
700 * server startup or shutdown.
a7455442
GB
701 *
702 * Destroying threads relies on the service threads filling in
703 * rqstp->rq_task, which only the nfs ones do. Assumes the serv
704 * has been created using svc_create_pooled().
705 *
706 * Based on code that used to be in nfsd_svc() but tweaked
707 * to be pool-aware.
708 */
709int
710svc_set_num_threads(struct svc_serv *serv, struct svc_pool *pool, int nrservs)
711{
9867d76c
JL
712 struct svc_rqst *rqstp;
713 struct task_struct *task;
714 struct svc_pool *chosen_pool;
a7455442
GB
715 int error = 0;
716 unsigned int state = serv->sv_nrthreads-1;
11fd165c 717 int node;
a7455442
GB
718
719 if (pool == NULL) {
720 /* The -1 assumes caller has done a svc_get() */
721 nrservs -= (serv->sv_nrthreads-1);
722 } else {
723 spin_lock_bh(&pool->sp_lock);
724 nrservs -= pool->sp_nrthreads;
725 spin_unlock_bh(&pool->sp_lock);
726 }
727
728 /* create new threads */
729 while (nrservs > 0) {
730 nrservs--;
9867d76c
JL
731 chosen_pool = choose_pool(serv, pool, &state);
732
11fd165c
ED
733 node = svc_pool_map_get_node(chosen_pool->sp_id);
734 rqstp = svc_prepare_thread(serv, chosen_pool, node);
9867d76c
JL
735 if (IS_ERR(rqstp)) {
736 error = PTR_ERR(rqstp);
737 break;
738 }
739
a7455442 740 __module_get(serv->sv_module);
c369014f 741 task = kthread_create_on_node(serv->sv_ops->svo_function, rqstp,
f170168b 742 node, "%s", serv->sv_name);
9867d76c
JL
743 if (IS_ERR(task)) {
744 error = PTR_ERR(task);
a7455442 745 module_put(serv->sv_module);
9867d76c 746 svc_exit_thread(rqstp);
a7455442
GB
747 break;
748 }
9867d76c
JL
749
750 rqstp->rq_task = task;
751 if (serv->sv_nrpools > 1)
752 svc_pool_map_set_cpumask(task, chosen_pool->sp_id);
753
754 svc_sock_update_bufs(serv);
755 wake_up_process(task);
a7455442
GB
756 }
757 /* destroy old threads */
758 while (nrservs < 0 &&
9867d76c 759 (task = choose_victim(serv, pool, &state)) != NULL) {
a75c5d01 760 send_sig(SIGINT, task, 1);
a7455442
GB
761 nrservs++;
762 }
763
764 return error;
765}
24c3767e 766EXPORT_SYMBOL_GPL(svc_set_num_threads);
a7455442 767
3262c816 768/*
3c519914
JL
769 * Called from a server thread as it's exiting. Caller must hold the "service
770 * mutex" for the service.
1da177e4
LT
771 */
772void
773svc_exit_thread(struct svc_rqst *rqstp)
774{
775 struct svc_serv *serv = rqstp->rq_server;
3262c816 776 struct svc_pool *pool = rqstp->rq_pool;
1da177e4
LT
777
778 svc_release_buffer(rqstp);
a51482bd
JJ
779 kfree(rqstp->rq_resp);
780 kfree(rqstp->rq_argp);
781 kfree(rqstp->rq_auth_data);
3262c816
GB
782
783 spin_lock_bh(&pool->sp_lock);
784 pool->sp_nrthreads--;
81244386
JL
785 if (!test_and_set_bit(RQ_VICTIM, &rqstp->rq_flags))
786 list_del_rcu(&rqstp->rq_all);
3262c816
GB
787 spin_unlock_bh(&pool->sp_lock);
788
81244386 789 kfree_rcu(rqstp, rq_rcu_head);
1da177e4
LT
790
791 /* Release the server */
792 if (serv)
793 svc_destroy(serv);
794}
24c3767e 795EXPORT_SYMBOL_GPL(svc_exit_thread);
1da177e4
LT
796
797/*
2c7eb0b2
CL
798 * Register an "inet" protocol family netid with the local
799 * rpcbind daemon via an rpcbind v4 SET request.
a26cfad6 800 *
2c7eb0b2
CL
801 * No netconfig infrastructure is available in the kernel, so
802 * we map IP_ protocol numbers to netids by hand.
a26cfad6 803 *
2c7eb0b2
CL
804 * Returns zero on success; a negative errno value is returned
805 * if any error occurs.
1da177e4 806 */
5247fab5
SK
807static int __svc_rpcb_register4(struct net *net, const u32 program,
808 const u32 version,
2c7eb0b2
CL
809 const unsigned short protocol,
810 const unsigned short port)
a26cfad6 811{
cadc0fa5 812 const struct sockaddr_in sin = {
a26cfad6
CL
813 .sin_family = AF_INET,
814 .sin_addr.s_addr = htonl(INADDR_ANY),
815 .sin_port = htons(port),
816 };
cadc0fa5
CL
817 const char *netid;
818 int error;
2c7eb0b2
CL
819
820 switch (protocol) {
821 case IPPROTO_UDP:
822 netid = RPCBIND_NETID_UDP;
823 break;
824 case IPPROTO_TCP:
825 netid = RPCBIND_NETID_TCP;
826 break;
827 default:
ba5c35e0 828 return -ENOPROTOOPT;
2c7eb0b2
CL
829 }
830
5247fab5 831 error = rpcb_v4_register(net, program, version,
cadc0fa5
CL
832 (const struct sockaddr *)&sin, netid);
833
834 /*
835 * User space didn't support rpcbind v4, so retry this
836 * registration request with the legacy rpcbind v2 protocol.
837 */
838 if (error == -EPROTONOSUPPORT)
5247fab5 839 error = rpcb_register(net, program, version, protocol, port);
cadc0fa5
CL
840
841 return error;
2c7eb0b2
CL
842}
843
dfd56b8b 844#if IS_ENABLED(CONFIG_IPV6)
2c7eb0b2
CL
845/*
846 * Register an "inet6" protocol family netid with the local
847 * rpcbind daemon via an rpcbind v4 SET request.
848 *
849 * No netconfig infrastructure is available in the kernel, so
850 * we map IP_ protocol numbers to netids by hand.
851 *
852 * Returns zero on success; a negative errno value is returned
853 * if any error occurs.
854 */
5247fab5
SK
855static int __svc_rpcb_register6(struct net *net, const u32 program,
856 const u32 version,
2c7eb0b2
CL
857 const unsigned short protocol,
858 const unsigned short port)
859{
cadc0fa5 860 const struct sockaddr_in6 sin6 = {
a26cfad6
CL
861 .sin6_family = AF_INET6,
862 .sin6_addr = IN6ADDR_ANY_INIT,
863 .sin6_port = htons(port),
864 };
cadc0fa5
CL
865 const char *netid;
866 int error;
a26cfad6 867
2c7eb0b2
CL
868 switch (protocol) {
869 case IPPROTO_UDP:
870 netid = RPCBIND_NETID_UDP6;
a26cfad6 871 break;
2c7eb0b2 872 case IPPROTO_TCP:
a26cfad6 873 netid = RPCBIND_NETID_TCP6;
a26cfad6
CL
874 break;
875 default:
ba5c35e0 876 return -ENOPROTOOPT;
2c7eb0b2
CL
877 }
878
5247fab5 879 error = rpcb_v4_register(net, program, version,
cadc0fa5
CL
880 (const struct sockaddr *)&sin6, netid);
881
882 /*
883 * User space didn't support rpcbind version 4, so we won't
884 * use a PF_INET6 listener.
885 */
886 if (error == -EPROTONOSUPPORT)
887 error = -EAFNOSUPPORT;
888
889 return error;
2c7eb0b2 890}
dfd56b8b 891#endif /* IS_ENABLED(CONFIG_IPV6) */
2c7eb0b2
CL
892
893/*
894 * Register a kernel RPC service via rpcbind version 4.
895 *
896 * Returns zero on success; a negative errno value is returned
897 * if any error occurs.
898 */
5247fab5 899static int __svc_register(struct net *net, const char *progname,
363f724c 900 const u32 program, const u32 version,
4b62e58c 901 const int family,
2c7eb0b2
CL
902 const unsigned short protocol,
903 const unsigned short port)
904{
363f724c 905 int error = -EAFNOSUPPORT;
2c7eb0b2
CL
906
907 switch (family) {
4b62e58c 908 case PF_INET:
5247fab5 909 error = __svc_rpcb_register4(net, program, version,
2c7eb0b2 910 protocol, port);
cadc0fa5 911 break;
dfd56b8b 912#if IS_ENABLED(CONFIG_IPV6)
4b62e58c 913 case PF_INET6:
5247fab5 914 error = __svc_rpcb_register6(net, program, version,
2c7eb0b2 915 protocol, port);
dfd56b8b 916#endif
a26cfad6
CL
917 }
918
363f724c 919 return error;
a26cfad6 920}
2c7eb0b2 921
a26cfad6
CL
922/**
923 * svc_register - register an RPC service with the local portmapper
924 * @serv: svc_serv struct for the service to register
5247fab5 925 * @net: net namespace for the service to register
4b62e58c 926 * @family: protocol family of service's listener socket
a26cfad6
CL
927 * @proto: transport protocol number to advertise
928 * @port: port to advertise
929 *
4b62e58c 930 * Service is registered for any address in the passed-in protocol family
a26cfad6 931 */
5247fab5
SK
932int svc_register(const struct svc_serv *serv, struct net *net,
933 const int family, const unsigned short proto,
934 const unsigned short port)
1da177e4
LT
935{
936 struct svc_program *progp;
7e55b59b 937 struct svc_version *vers;
ea339d46 938 unsigned int i;
14aeb211 939 int error = 0;
1da177e4 940
0af39507
WAA
941 WARN_ON_ONCE(proto == 0 && port == 0);
942 if (proto == 0 && port == 0)
943 return -EINVAL;
1da177e4 944
bc5fea42
OK
945 for (progp = serv->sv_program; progp; progp = progp->pg_next) {
946 for (i = 0; i < progp->pg_nvers; i++) {
7e55b59b
KM
947 vers = progp->pg_vers[i];
948 if (vers == NULL)
bc5fea42
OK
949 continue;
950
2c7eb0b2 951 dprintk("svc: svc_register(%sv%d, %s, %u, %u)%s\n",
bc5fea42 952 progp->pg_name,
2c7eb0b2 953 i,
bc5fea42
OK
954 proto == IPPROTO_UDP? "udp" : "tcp",
955 port,
4b62e58c 956 family,
7e55b59b
KM
957 vers->vs_hidden ?
958 " (but not telling portmap)" : "");
bc5fea42 959
7e55b59b 960 if (vers->vs_hidden)
bc5fea42
OK
961 continue;
962
5247fab5 963 error = __svc_register(net, progp->pg_name, progp->pg_prog,
363f724c 964 i, family, proto, port);
7e55b59b
KM
965
966 if (vers->vs_rpcb_optnl) {
967 error = 0;
968 continue;
969 }
970
971 if (error < 0) {
972 printk(KERN_WARNING "svc: failed to register "
973 "%sv%u RPC service (errno %d).\n",
974 progp->pg_name, i, -error);
bc5fea42 975 break;
7e55b59b 976 }
1da177e4
LT
977 }
978 }
979
7252d575
CL
980 return error;
981}
982
d5a8620f
CL
983/*
984 * If user space is running rpcbind, it should take the v4 UNSET
985 * and clear everything for this [program, version]. If user space
986 * is running portmap, it will reject the v4 UNSET, but won't have
987 * any "inet6" entries anyway. So a PMAP_UNSET should be sufficient
988 * in this case to clear all existing entries for [program, version].
989 */
5247fab5 990static void __svc_unregister(struct net *net, const u32 program, const u32 version,
f6fb3f6f
CL
991 const char *progname)
992{
f6fb3f6f
CL
993 int error;
994
5247fab5 995 error = rpcb_v4_register(net, program, version, NULL, "");
f6fb3f6f 996
d5a8620f
CL
997 /*
998 * User space didn't support rpcbind v4, so retry this
999 * request with the legacy rpcbind v2 protocol.
1000 */
1001 if (error == -EPROTONOSUPPORT)
5247fab5 1002 error = rpcb_register(net, program, version, 0, 0);
f6fb3f6f 1003
f6fb3f6f
CL
1004 dprintk("svc: %s(%sv%u), error %d\n",
1005 __func__, progname, version, error);
1006}
1007
7252d575 1008/*
f6fb3f6f
CL
1009 * All netids, bind addresses and ports registered for [program, version]
1010 * are removed from the local rpcbind database (if the service is not
1011 * hidden) to make way for a new instance of the service.
7252d575 1012 *
f6fb3f6f
CL
1013 * The result of unregistration is reported via dprintk for those who want
1014 * verification of the result, but is otherwise not important.
7252d575 1015 */
5247fab5 1016static void svc_unregister(const struct svc_serv *serv, struct net *net)
7252d575
CL
1017{
1018 struct svc_program *progp;
1019 unsigned long flags;
1020 unsigned int i;
7252d575
CL
1021
1022 clear_thread_flag(TIF_SIGPENDING);
1023
1024 for (progp = serv->sv_program; progp; progp = progp->pg_next) {
1025 for (i = 0; i < progp->pg_nvers; i++) {
1026 if (progp->pg_vers[i] == NULL)
1027 continue;
1028 if (progp->pg_vers[i]->vs_hidden)
1029 continue;
1030
7402ab19
CL
1031 dprintk("svc: attempting to unregister %sv%u\n",
1032 progp->pg_name, i);
5247fab5 1033 __svc_unregister(net, progp->pg_prog, i, progp->pg_name);
7252d575 1034 }
1da177e4
LT
1035 }
1036
7252d575
CL
1037 spin_lock_irqsave(&current->sighand->siglock, flags);
1038 recalc_sigpending();
1039 spin_unlock_irqrestore(&current->sighand->siglock, flags);
1da177e4
LT
1040}
1041
354ecbb9 1042/*
7032a3dd 1043 * dprintk the given error with the address of the client that caused it.
354ecbb9 1044 */
f895b252 1045#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
b9075fa9 1046static __printf(2, 3)
e87cc472 1047void svc_printk(struct svc_rqst *rqstp, const char *fmt, ...)
354ecbb9 1048{
e87cc472 1049 struct va_format vaf;
354ecbb9 1050 va_list args;
354ecbb9
DDAG
1051 char buf[RPC_MAX_ADDRBUFLEN];
1052
e87cc472 1053 va_start(args, fmt);
354ecbb9 1054
e87cc472
JP
1055 vaf.fmt = fmt;
1056 vaf.va = &args;
354ecbb9 1057
7032a3dd 1058 dprintk("svc: %s: %pV", svc_print_addr(rqstp, buf, sizeof(buf)), &vaf);
354ecbb9 1059
e87cc472 1060 va_end(args);
354ecbb9 1061}
624ab464
BF
1062#else
1063static __printf(2,3) void svc_printk(struct svc_rqst *rqstp, const char *fmt, ...) {}
1064#endif
354ecbb9 1065
1da177e4 1066/*
1cad7ea6 1067 * Common routine for processing the RPC request.
1da177e4 1068 */
1cad7ea6
RL
1069static int
1070svc_process_common(struct svc_rqst *rqstp, struct kvec *argv, struct kvec *resv)
1da177e4
LT
1071{
1072 struct svc_program *progp;
1073 struct svc_version *versp = NULL; /* compiler food */
1074 struct svc_procedure *procp = NULL;
6fb2b47f 1075 struct svc_serv *serv = rqstp->rq_server;
1da177e4 1076 kxdrproc_t xdr;
d8ed029d 1077 __be32 *statp;
1cad7ea6 1078 u32 prog, vers, proc;
d8ed029d 1079 __be32 auth_stat, rpc_stat;
1da177e4 1080 int auth_res;
8f8e05c5 1081 __be32 *reply_statp;
1da177e4
LT
1082
1083 rpc_stat = rpc_success;
1084
1085 if (argv->iov_len < 6*4)
1086 goto err_short_len;
1087
5c04c46a 1088 /* Will be turned off only in gss privacy case: */
779fb0f3 1089 set_bit(RQ_SPLICE_OK, &rqstp->rq_flags);
2f425878 1090 /* Will be turned off only when NFSv4 Sessions are used */
30660e04 1091 set_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
78b65eb3 1092 clear_bit(RQ_DROPME, &rqstp->rq_flags);
e831fe65
TT
1093
1094 /* Setup reply header */
1095 rqstp->rq_xprt->xpt_ops->xpo_prep_reply_hdr(rqstp);
1da177e4 1096
1da177e4
LT
1097 svc_putu32(resv, rqstp->rq_xid);
1098
76994313 1099 vers = svc_getnl(argv);
1da177e4
LT
1100
1101 /* First words of reply: */
76994313 1102 svc_putnl(resv, 1); /* REPLY */
1da177e4 1103
1da177e4
LT
1104 if (vers != 2) /* RPC version number */
1105 goto err_bad_rpc;
1106
1107 /* Save position in case we later decide to reject: */
8f8e05c5 1108 reply_statp = resv->iov_base + resv->iov_len;
1da177e4 1109
76994313 1110 svc_putnl(resv, 0); /* ACCEPT */
1da177e4 1111
76994313
AD
1112 rqstp->rq_prog = prog = svc_getnl(argv); /* program number */
1113 rqstp->rq_vers = vers = svc_getnl(argv); /* version number */
1114 rqstp->rq_proc = proc = svc_getnl(argv); /* procedure number */
1da177e4 1115
80d188a6
N
1116 for (progp = serv->sv_program; progp; progp = progp->pg_next)
1117 if (prog == progp->pg_prog)
1118 break;
1119
1da177e4
LT
1120 /*
1121 * Decode auth data, and add verifier to reply buffer.
1122 * We do this before anything else in order to get a decent
1123 * auth verifier.
1124 */
1125 auth_res = svc_authenticate(rqstp, &auth_stat);
1126 /* Also give the program a chance to reject this call: */
80d188a6 1127 if (auth_res == SVC_OK && progp) {
1da177e4
LT
1128 auth_stat = rpc_autherr_badcred;
1129 auth_res = progp->pg_authenticate(rqstp);
1130 }
1131 switch (auth_res) {
1132 case SVC_OK:
1133 break;
1134 case SVC_GARBAGE:
dd35210e 1135 goto err_garbage;
1da177e4
LT
1136 case SVC_SYSERR:
1137 rpc_stat = rpc_system_err;
1138 goto err_bad;
1139 case SVC_DENIED:
1140 goto err_bad_auth;
1ebede86
N
1141 case SVC_CLOSE:
1142 if (test_bit(XPT_TEMP, &rqstp->rq_xprt->xpt_flags))
1143 svc_close_xprt(rqstp->rq_xprt);
1da177e4
LT
1144 case SVC_DROP:
1145 goto dropit;
1146 case SVC_COMPLETE:
1147 goto sendit;
1148 }
80d188a6 1149
9ba02638 1150 if (progp == NULL)
1da177e4
LT
1151 goto err_bad_prog;
1152
1153 if (vers >= progp->pg_nvers ||
1154 !(versp = progp->pg_vers[vers]))
1155 goto err_bad_vers;
1156
1157 procp = versp->vs_proc + proc;
1158 if (proc >= versp->vs_nproc || !procp->pc_func)
1159 goto err_bad_proc;
1da177e4
LT
1160 rqstp->rq_procinfo = procp;
1161
1162 /* Syntactic check complete */
1163 serv->sv_stats->rpccnt++;
1164
1165 /* Build the reply header. */
1166 statp = resv->iov_base +resv->iov_len;
76994313 1167 svc_putnl(resv, RPC_SUCCESS);
1da177e4
LT
1168
1169 /* Bump per-procedure stats counter */
1170 procp->pc_count++;
1171
1172 /* Initialize storage for argp and resp */
1173 memset(rqstp->rq_argp, 0, procp->pc_argsize);
1174 memset(rqstp->rq_resp, 0, procp->pc_ressize);
1175
cca5172a 1176 /* un-reserve some of the out-queue now that we have a
1da177e4
LT
1177 * better idea of reply size
1178 */
1179 if (procp->pc_xdrressize)
cd123012 1180 svc_reserve_auth(rqstp, procp->pc_xdrressize<<2);
1da177e4
LT
1181
1182 /* Call the function that processes the request. */
1183 if (!versp->vs_dispatch) {
1184 /* Decode arguments */
1185 xdr = procp->pc_decode;
1186 if (xdr && !xdr(rqstp, argv->iov_base, rqstp->rq_argp))
1187 goto err_garbage;
1188
1189 *statp = procp->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp);
1190
1191 /* Encode reply */
78b65eb3 1192 if (test_bit(RQ_DROPME, &rqstp->rq_flags)) {
d343fce1
N
1193 if (procp->pc_release)
1194 procp->pc_release(rqstp, NULL, rqstp->rq_resp);
1195 goto dropit;
1196 }
f64f9e71
JP
1197 if (*statp == rpc_success &&
1198 (xdr = procp->pc_encode) &&
1199 !xdr(rqstp, resv->iov_base+resv->iov_len, rqstp->rq_resp)) {
1da177e4
LT
1200 dprintk("svc: failed to encode reply\n");
1201 /* serv->sv_stats->rpcsystemerr++; */
1202 *statp = rpc_system_err;
1203 }
1204 } else {
1205 dprintk("svc: calling dispatcher\n");
1206 if (!versp->vs_dispatch(rqstp, statp)) {
1207 /* Release reply info */
1208 if (procp->pc_release)
1209 procp->pc_release(rqstp, NULL, rqstp->rq_resp);
1210 goto dropit;
1211 }
1212 }
1213
1214 /* Check RPC status result */
1215 if (*statp != rpc_success)
1216 resv->iov_len = ((void*)statp) - resv->iov_base + 4;
1217
1218 /* Release reply info */
1219 if (procp->pc_release)
1220 procp->pc_release(rqstp, NULL, rqstp->rq_resp);
1221
1222 if (procp->pc_encode == NULL)
1223 goto dropit;
1224
1225 sendit:
1226 if (svc_authorise(rqstp))
1227 goto dropit;
1cad7ea6 1228 return 1; /* Caller can now send it */
1da177e4
LT
1229
1230 dropit:
1231 svc_authorise(rqstp); /* doesn't hurt to call this twice */
1232 dprintk("svc: svc_process dropit\n");
1da177e4
LT
1233 return 0;
1234
1235err_short_len:
354ecbb9
DDAG
1236 svc_printk(rqstp, "short len %Zd, dropping request\n",
1237 argv->iov_len);
34e9a63b 1238
1da177e4
LT
1239 goto dropit; /* drop request */
1240
1da177e4
LT
1241err_bad_rpc:
1242 serv->sv_stats->rpcbadfmt++;
76994313
AD
1243 svc_putnl(resv, 1); /* REJECT */
1244 svc_putnl(resv, 0); /* RPC_MISMATCH */
1245 svc_putnl(resv, 2); /* Only RPCv2 supported */
1246 svc_putnl(resv, 2);
1da177e4
LT
1247 goto sendit;
1248
1249err_bad_auth:
1250 dprintk("svc: authentication failed (%d)\n", ntohl(auth_stat));
1251 serv->sv_stats->rpcbadauth++;
1252 /* Restore write pointer to location of accept status: */
8f8e05c5 1253 xdr_ressize_check(rqstp, reply_statp);
76994313
AD
1254 svc_putnl(resv, 1); /* REJECT */
1255 svc_putnl(resv, 1); /* AUTH_ERROR */
1256 svc_putnl(resv, ntohl(auth_stat)); /* status */
1da177e4
LT
1257 goto sendit;
1258
1259err_bad_prog:
9ba02638 1260 dprintk("svc: unknown program %d\n", prog);
1da177e4 1261 serv->sv_stats->rpcbadfmt++;
76994313 1262 svc_putnl(resv, RPC_PROG_UNAVAIL);
1da177e4
LT
1263 goto sendit;
1264
1265err_bad_vers:
354ecbb9 1266 svc_printk(rqstp, "unknown version (%d for prog %d, %s)\n",
34e9a63b
N
1267 vers, prog, progp->pg_name);
1268
1da177e4 1269 serv->sv_stats->rpcbadfmt++;
76994313
AD
1270 svc_putnl(resv, RPC_PROG_MISMATCH);
1271 svc_putnl(resv, progp->pg_lovers);
1272 svc_putnl(resv, progp->pg_hivers);
1da177e4
LT
1273 goto sendit;
1274
1275err_bad_proc:
354ecbb9 1276 svc_printk(rqstp, "unknown procedure (%d)\n", proc);
34e9a63b 1277
1da177e4 1278 serv->sv_stats->rpcbadfmt++;
76994313 1279 svc_putnl(resv, RPC_PROC_UNAVAIL);
1da177e4
LT
1280 goto sendit;
1281
1282err_garbage:
354ecbb9 1283 svc_printk(rqstp, "failed to decode args\n");
34e9a63b 1284
1da177e4
LT
1285 rpc_stat = rpc_garbage_args;
1286err_bad:
1287 serv->sv_stats->rpcbadfmt++;
76994313 1288 svc_putnl(resv, ntohl(rpc_stat));
1da177e4
LT
1289 goto sendit;
1290}
7adae489 1291
1cad7ea6
RL
1292/*
1293 * Process the RPC request.
1294 */
1295int
1296svc_process(struct svc_rqst *rqstp)
1297{
1298 struct kvec *argv = &rqstp->rq_arg.head[0];
1299 struct kvec *resv = &rqstp->rq_res.head[0];
1300 struct svc_serv *serv = rqstp->rq_server;
1301 u32 dir;
1cad7ea6
RL
1302
1303 /*
1304 * Setup response xdr_buf.
1305 * Initially it has just one page
1306 */
afc59400 1307 rqstp->rq_next_page = &rqstp->rq_respages[1];
1cad7ea6
RL
1308 resv->iov_base = page_address(rqstp->rq_respages[0]);
1309 resv->iov_len = 0;
1310 rqstp->rq_res.pages = rqstp->rq_respages + 1;
1311 rqstp->rq_res.len = 0;
1312 rqstp->rq_res.page_base = 0;
1313 rqstp->rq_res.page_len = 0;
1314 rqstp->rq_res.buflen = PAGE_SIZE;
1315 rqstp->rq_res.tail[0].iov_base = NULL;
1316 rqstp->rq_res.tail[0].iov_len = 0;
1317
1cad7ea6
RL
1318 dir = svc_getnl(argv);
1319 if (dir != 0) {
1320 /* direction != CALL */
1321 svc_printk(rqstp, "bad direction %d, dropping request\n", dir);
1322 serv->sv_stats->rpcbadfmt++;
860a0d9e 1323 goto out_drop;
1cad7ea6
RL
1324 }
1325
4b5b3ba1 1326 /* Returns 1 for send, 0 for drop */
860a0d9e
JL
1327 if (likely(svc_process_common(rqstp, argv, resv))) {
1328 int ret = svc_send(rqstp);
1329
1330 trace_svc_process(rqstp, ret);
1331 return ret;
4b5b3ba1 1332 }
860a0d9e
JL
1333out_drop:
1334 trace_svc_process(rqstp, 0);
1335 svc_drop(rqstp);
1336 return 0;
1cad7ea6 1337}
3f87d5d6 1338EXPORT_SYMBOL_GPL(svc_process);
1cad7ea6 1339
9e00abc3 1340#if defined(CONFIG_SUNRPC_BACKCHANNEL)
4d6bbb62
RL
1341/*
1342 * Process a backchannel RPC request that arrived over an existing
1343 * outbound connection
1344 */
1345int
1346bc_svc_process(struct svc_serv *serv, struct rpc_rqst *req,
1347 struct svc_rqst *rqstp)
1348{
1349 struct kvec *argv = &rqstp->rq_arg.head[0];
1350 struct kvec *resv = &rqstp->rq_res.head[0];
632dda83 1351 struct rpc_task *task;
0d2a970d 1352 int proc_error;
632dda83
CL
1353 int error;
1354
1355 dprintk("svc: %s(%p)\n", __func__, req);
4d6bbb62
RL
1356
1357 /* Build the svc_rqst used by the common processing routine */
4a19de0f 1358 rqstp->rq_xprt = serv->sv_bc_xprt;
4d6bbb62
RL
1359 rqstp->rq_xid = req->rq_xid;
1360 rqstp->rq_prot = req->rq_xprt->prot;
1361 rqstp->rq_server = serv;
1362
1363 rqstp->rq_addrlen = sizeof(req->rq_xprt->addr);
1364 memcpy(&rqstp->rq_addr, &req->rq_xprt->addr, rqstp->rq_addrlen);
1365 memcpy(&rqstp->rq_arg, &req->rq_rcv_buf, sizeof(rqstp->rq_arg));
1366 memcpy(&rqstp->rq_res, &req->rq_snd_buf, sizeof(rqstp->rq_res));
1367
1368 /* reset result send buffer "put" position */
1369 resv->iov_len = 0;
1370
1371 if (rqstp->rq_prot != IPPROTO_TCP) {
1372 printk(KERN_ERR "No support for Non-TCP transports!\n");
1373 BUG();
1374 }
1375
1376 /*
1377 * Skip the next two words because they've already been
632dda83 1378 * processed in the transport
4d6bbb62
RL
1379 */
1380 svc_getu32(argv); /* XID */
1381 svc_getnl(argv); /* CALLDIR */
1382
632dda83 1383 /* Parse and execute the bc call */
0d2a970d
TM
1384 proc_error = svc_process_common(rqstp, argv, resv);
1385
1386 atomic_inc(&req->rq_xprt->bc_free_slots);
1387 if (!proc_error) {
632dda83 1388 /* Processing error: drop the request */
b3b02ae5 1389 xprt_free_bc_request(req);
4b5b3ba1
AA
1390 return 0;
1391 }
632dda83
CL
1392
1393 /* Finally, send the reply synchronously */
1394 memcpy(&req->rq_snd_buf, &rqstp->rq_res, sizeof(req->rq_snd_buf));
0f419791 1395 task = rpc_run_bc_task(req);
632dda83
CL
1396 if (IS_ERR(task)) {
1397 error = PTR_ERR(task);
1398 goto out;
1399 }
1400
1401 WARN_ON_ONCE(atomic_read(&task->tk_count) != 1);
1402 error = task->tk_status;
1403 rpc_put_task(task);
1404
1405out:
1406 dprintk("svc: %s(), error=%d\n", __func__, error);
1407 return error;
4d6bbb62 1408}
0d961aa9 1409EXPORT_SYMBOL_GPL(bc_svc_process);
9e00abc3 1410#endif /* CONFIG_SUNRPC_BACKCHANNEL */
4d6bbb62 1411
7adae489
GB
1412/*
1413 * Return (transport-specific) limit on the rpc payload.
1414 */
1415u32 svc_max_payload(const struct svc_rqst *rqstp)
1416{
49023155 1417 u32 max = rqstp->rq_xprt->xpt_class->xcl_max_payload;
7adae489 1418
c6b0a9f8
N
1419 if (rqstp->rq_server->sv_max_payload < max)
1420 max = rqstp->rq_server->sv_max_payload;
7adae489
GB
1421 return max;
1422}
1423EXPORT_SYMBOL_GPL(svc_max_payload);
This page took 1.179972 seconds and 5 git commands to generate.