blob: 8c75eec4fd6aea5b4de54f1b49404681ff180584 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
7 */
8
9#include <linux/linkage.h>
10#include <linux/sched.h>
11#include <linux/errno.h>
12#include <linux/net.h>
13#include <linux/in.h>
14#include <linux/mm.h>
Greg Banksa7455442006-10-02 02:17:59 -070015#include <linux/interrupt.h>
16#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include <linux/sunrpc/types.h>
19#include <linux/sunrpc/xdr.h>
20#include <linux/sunrpc/stats.h>
21#include <linux/sunrpc/svcsock.h>
22#include <linux/sunrpc/clnt.h>
23
24#define RPCDBG_FACILITY RPCDBG_SVCDSP
25#define RPC_PARANOIA 1
26
27/*
28 * Create an RPC service
29 */
Greg Banksa7455442006-10-02 02:17:59 -070030static struct svc_serv *
31__svc_create(struct svc_program *prog, unsigned int bufsize, int npools,
NeilBrownbc591cc2006-10-02 02:17:44 -070032 void (*shutdown)(struct svc_serv *serv))
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
34 struct svc_serv *serv;
35 int vers;
36 unsigned int xdrsize;
Greg Banks3262c812006-10-02 02:17:58 -070037 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Panagiotis Issaris0da974f2006-07-21 14:51:30 -070039 if (!(serv = kzalloc(sizeof(*serv), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 return NULL;
Andreas Gruenbacher9ba02632005-06-22 17:16:24 +000041 serv->sv_name = prog->pg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 serv->sv_program = prog;
43 serv->sv_nrthreads = 1;
44 serv->sv_stats = prog->pg_stats;
45 serv->sv_bufsz = bufsize? bufsize : 4096;
NeilBrownbc591cc2006-10-02 02:17:44 -070046 serv->sv_shutdown = shutdown;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 xdrsize = 0;
Andreas Gruenbacher9ba02632005-06-22 17:16:24 +000048 while (prog) {
49 prog->pg_lovers = prog->pg_nvers-1;
50 for (vers=0; vers<prog->pg_nvers ; vers++)
51 if (prog->pg_vers[vers]) {
52 prog->pg_hivers = vers;
53 if (prog->pg_lovers > vers)
54 prog->pg_lovers = vers;
55 if (prog->pg_vers[vers]->vs_xdrsize > xdrsize)
56 xdrsize = prog->pg_vers[vers]->vs_xdrsize;
57 }
58 prog = prog->pg_next;
59 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 serv->sv_xdrsize = xdrsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 INIT_LIST_HEAD(&serv->sv_tempsocks);
62 INIT_LIST_HEAD(&serv->sv_permsocks);
Greg Banks36bdfc82006-10-02 02:17:54 -070063 init_timer(&serv->sv_temptimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 spin_lock_init(&serv->sv_lock);
65
Greg Banksa7455442006-10-02 02:17:59 -070066 serv->sv_nrpools = npools;
Greg Banks3262c812006-10-02 02:17:58 -070067 serv->sv_pools =
68 kcalloc(sizeof(struct svc_pool), serv->sv_nrpools,
69 GFP_KERNEL);
70 if (!serv->sv_pools) {
71 kfree(serv);
72 return NULL;
73 }
74
75 for (i = 0; i < serv->sv_nrpools; i++) {
76 struct svc_pool *pool = &serv->sv_pools[i];
77
78 dprintk("initialising pool %u for %s\n",
79 i, serv->sv_name);
80
81 pool->sp_id = i;
82 INIT_LIST_HEAD(&pool->sp_threads);
83 INIT_LIST_HEAD(&pool->sp_sockets);
Greg Banksa7455442006-10-02 02:17:59 -070084 INIT_LIST_HEAD(&pool->sp_all_threads);
Greg Banks3262c812006-10-02 02:17:58 -070085 spin_lock_init(&pool->sp_lock);
86 }
87
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 /* Remove any stale portmap registrations */
90 svc_register(serv, 0, 0);
91
92 return serv;
93}
94
Greg Banksa7455442006-10-02 02:17:59 -070095struct svc_serv *
96svc_create(struct svc_program *prog, unsigned int bufsize,
97 void (*shutdown)(struct svc_serv *serv))
98{
99 return __svc_create(prog, bufsize, /*npools*/1, shutdown);
100}
101
102struct svc_serv *
103svc_create_pooled(struct svc_program *prog, unsigned int bufsize,
104 void (*shutdown)(struct svc_serv *serv),
105 svc_thread_fn func, int sig, struct module *mod)
106{
107 struct svc_serv *serv;
108
109 serv = __svc_create(prog, bufsize, /*npools*/1, shutdown);
110
111 if (serv != NULL) {
112 serv->sv_function = func;
113 serv->sv_kill_signal = sig;
114 serv->sv_module = mod;
115 }
116
117 return serv;
118}
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120/*
Greg Banks3262c812006-10-02 02:17:58 -0700121 * Destroy an RPC service. Should be called with the BKL held
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 */
123void
124svc_destroy(struct svc_serv *serv)
125{
126 struct svc_sock *svsk;
127
128 dprintk("RPC: svc_destroy(%s, %d)\n",
129 serv->sv_program->pg_name,
130 serv->sv_nrthreads);
131
132 if (serv->sv_nrthreads) {
133 if (--(serv->sv_nrthreads) != 0) {
134 svc_sock_update_bufs(serv);
135 return;
136 }
137 } else
138 printk("svc_destroy: no threads for serv=%p!\n", serv);
139
Greg Banks36bdfc82006-10-02 02:17:54 -0700140 del_timer_sync(&serv->sv_temptimer);
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 while (!list_empty(&serv->sv_tempsocks)) {
143 svsk = list_entry(serv->sv_tempsocks.next,
144 struct svc_sock,
145 sk_list);
146 svc_delete_socket(svsk);
147 }
NeilBrownbc591cc2006-10-02 02:17:44 -0700148 if (serv->sv_shutdown)
149 serv->sv_shutdown(serv);
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 while (!list_empty(&serv->sv_permsocks)) {
152 svsk = list_entry(serv->sv_permsocks.next,
153 struct svc_sock,
154 sk_list);
155 svc_delete_socket(svsk);
156 }
157
158 cache_clean_deferred(serv);
159
160 /* Unregister service with the portmapper */
161 svc_register(serv, 0, 0);
Greg Banks3262c812006-10-02 02:17:58 -0700162 kfree(serv->sv_pools);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 kfree(serv);
164}
165
166/*
167 * Allocate an RPC server's buffer space.
168 * We allocate pages and place them in rq_argpages.
169 */
170static int
171svc_init_buffer(struct svc_rqst *rqstp, unsigned int size)
172{
173 int pages;
174 int arghi;
175
176 if (size > RPCSVC_MAXPAYLOAD)
177 size = RPCSVC_MAXPAYLOAD;
178 pages = 2 + (size+ PAGE_SIZE -1) / PAGE_SIZE;
179 rqstp->rq_argused = 0;
180 rqstp->rq_resused = 0;
181 arghi = 0;
Kris Katterjohn09a62662006-01-08 22:24:28 -0800182 BUG_ON(pages > RPCSVC_MAXPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 while (pages) {
184 struct page *p = alloc_page(GFP_KERNEL);
185 if (!p)
186 break;
187 rqstp->rq_argpages[arghi++] = p;
188 pages--;
189 }
190 rqstp->rq_arghi = arghi;
191 return ! pages;
192}
193
194/*
195 * Release an RPC server buffer
196 */
197static void
198svc_release_buffer(struct svc_rqst *rqstp)
199{
200 while (rqstp->rq_arghi)
201 put_page(rqstp->rq_argpages[--rqstp->rq_arghi]);
202 while (rqstp->rq_resused) {
203 if (rqstp->rq_respages[--rqstp->rq_resused] == NULL)
204 continue;
205 put_page(rqstp->rq_respages[rqstp->rq_resused]);
206 }
207 rqstp->rq_argused = 0;
208}
209
210/*
Greg Banks3262c812006-10-02 02:17:58 -0700211 * Create a thread in the given pool. Caller must hold BKL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 */
Greg Banks3262c812006-10-02 02:17:58 -0700213static int
214__svc_create_thread(svc_thread_fn func, struct svc_serv *serv,
215 struct svc_pool *pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 struct svc_rqst *rqstp;
218 int error = -ENOMEM;
219
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700220 rqstp = kzalloc(sizeof(*rqstp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if (!rqstp)
222 goto out;
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 init_waitqueue_head(&rqstp->rq_wait);
225
Jesper Juhl12fe2c52006-01-10 13:08:21 -0800226 if (!(rqstp->rq_argp = kmalloc(serv->sv_xdrsize, GFP_KERNEL))
227 || !(rqstp->rq_resp = kmalloc(serv->sv_xdrsize, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 || !svc_init_buffer(rqstp, serv->sv_bufsz))
229 goto out_thread;
230
231 serv->sv_nrthreads++;
Greg Banks3262c812006-10-02 02:17:58 -0700232 spin_lock_bh(&pool->sp_lock);
233 pool->sp_nrthreads++;
Greg Banksa7455442006-10-02 02:17:59 -0700234 list_add(&rqstp->rq_all, &pool->sp_all_threads);
Greg Banks3262c812006-10-02 02:17:58 -0700235 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 rqstp->rq_server = serv;
Greg Banks3262c812006-10-02 02:17:58 -0700237 rqstp->rq_pool = pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 error = kernel_thread((int (*)(void *)) func, rqstp, 0);
239 if (error < 0)
240 goto out_thread;
241 svc_sock_update_bufs(serv);
242 error = 0;
243out:
244 return error;
245
246out_thread:
247 svc_exit_thread(rqstp);
248 goto out;
249}
250
251/*
Greg Banks3262c812006-10-02 02:17:58 -0700252 * Create a thread in the default pool. Caller must hold BKL.
253 */
254int
255svc_create_thread(svc_thread_fn func, struct svc_serv *serv)
256{
257 return __svc_create_thread(func, serv, &serv->sv_pools[0]);
258}
259
260/*
Greg Banksa7455442006-10-02 02:17:59 -0700261 * Choose a pool in which to create a new thread, for svc_set_num_threads
262 */
263static inline struct svc_pool *
264choose_pool(struct svc_serv *serv, struct svc_pool *pool, unsigned int *state)
265{
266 if (pool != NULL)
267 return pool;
268
269 return &serv->sv_pools[(*state)++ % serv->sv_nrpools];
270}
271
272/*
273 * Choose a thread to kill, for svc_set_num_threads
274 */
275static inline struct task_struct *
276choose_victim(struct svc_serv *serv, struct svc_pool *pool, unsigned int *state)
277{
278 unsigned int i;
279 struct task_struct *task = NULL;
280
281 if (pool != NULL) {
282 spin_lock_bh(&pool->sp_lock);
283 } else {
284 /* choose a pool in round-robin fashion */
285 for (i = 0; i < serv->sv_nrpools; i++) {
286 pool = &serv->sv_pools[--(*state) % serv->sv_nrpools];
287 spin_lock_bh(&pool->sp_lock);
288 if (!list_empty(&pool->sp_all_threads))
289 goto found_pool;
290 spin_unlock_bh(&pool->sp_lock);
291 }
292 return NULL;
293 }
294
295found_pool:
296 if (!list_empty(&pool->sp_all_threads)) {
297 struct svc_rqst *rqstp;
298
299 /*
300 * Remove from the pool->sp_all_threads list
301 * so we don't try to kill it again.
302 */
303 rqstp = list_entry(pool->sp_all_threads.next, struct svc_rqst, rq_all);
304 list_del_init(&rqstp->rq_all);
305 task = rqstp->rq_task;
306 }
307 spin_unlock_bh(&pool->sp_lock);
308
309 return task;
310}
311
312/*
313 * Create or destroy enough new threads to make the number
314 * of threads the given number. If `pool' is non-NULL, applies
315 * only to threads in that pool, otherwise round-robins between
316 * all pools. Must be called with a svc_get() reference and
317 * the BKL held.
318 *
319 * Destroying threads relies on the service threads filling in
320 * rqstp->rq_task, which only the nfs ones do. Assumes the serv
321 * has been created using svc_create_pooled().
322 *
323 * Based on code that used to be in nfsd_svc() but tweaked
324 * to be pool-aware.
325 */
326int
327svc_set_num_threads(struct svc_serv *serv, struct svc_pool *pool, int nrservs)
328{
329 struct task_struct *victim;
330 int error = 0;
331 unsigned int state = serv->sv_nrthreads-1;
332
333 if (pool == NULL) {
334 /* The -1 assumes caller has done a svc_get() */
335 nrservs -= (serv->sv_nrthreads-1);
336 } else {
337 spin_lock_bh(&pool->sp_lock);
338 nrservs -= pool->sp_nrthreads;
339 spin_unlock_bh(&pool->sp_lock);
340 }
341
342 /* create new threads */
343 while (nrservs > 0) {
344 nrservs--;
345 __module_get(serv->sv_module);
346 error = __svc_create_thread(serv->sv_function, serv,
347 choose_pool(serv, pool, &state));
348 if (error < 0) {
349 module_put(serv->sv_module);
350 break;
351 }
352 }
353 /* destroy old threads */
354 while (nrservs < 0 &&
355 (victim = choose_victim(serv, pool, &state)) != NULL) {
356 send_sig(serv->sv_kill_signal, victim, 1);
357 nrservs++;
358 }
359
360 return error;
361}
362
363/*
Greg Banks3262c812006-10-02 02:17:58 -0700364 * Called from a server thread as it's exiting. Caller must hold BKL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 */
366void
367svc_exit_thread(struct svc_rqst *rqstp)
368{
369 struct svc_serv *serv = rqstp->rq_server;
Greg Banks3262c812006-10-02 02:17:58 -0700370 struct svc_pool *pool = rqstp->rq_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 svc_release_buffer(rqstp);
Jesper Juhla51482b2005-11-08 09:41:34 -0800373 kfree(rqstp->rq_resp);
374 kfree(rqstp->rq_argp);
375 kfree(rqstp->rq_auth_data);
Greg Banks3262c812006-10-02 02:17:58 -0700376
377 spin_lock_bh(&pool->sp_lock);
378 pool->sp_nrthreads--;
Greg Banksa7455442006-10-02 02:17:59 -0700379 list_del(&rqstp->rq_all);
Greg Banks3262c812006-10-02 02:17:58 -0700380 spin_unlock_bh(&pool->sp_lock);
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 kfree(rqstp);
383
384 /* Release the server */
385 if (serv)
386 svc_destroy(serv);
387}
388
389/*
390 * Register an RPC service with the local portmapper.
391 * To unregister a service, call this routine with
392 * proto and port == 0.
393 */
394int
395svc_register(struct svc_serv *serv, int proto, unsigned short port)
396{
397 struct svc_program *progp;
398 unsigned long flags;
399 int i, error = 0, dummy;
400
401 progp = serv->sv_program;
402
403 dprintk("RPC: svc_register(%s, %s, %d)\n",
404 progp->pg_name, proto == IPPROTO_UDP? "udp" : "tcp", port);
405
406 if (!port)
407 clear_thread_flag(TIF_SIGPENDING);
408
409 for (i = 0; i < progp->pg_nvers; i++) {
410 if (progp->pg_vers[i] == NULL)
411 continue;
412 error = rpc_register(progp->pg_prog, i, proto, port, &dummy);
413 if (error < 0)
414 break;
415 if (port && !dummy) {
416 error = -EACCES;
417 break;
418 }
419 }
420
421 if (!port) {
422 spin_lock_irqsave(&current->sighand->siglock, flags);
423 recalc_sigpending();
424 spin_unlock_irqrestore(&current->sighand->siglock, flags);
425 }
426
427 return error;
428}
429
430/*
431 * Process the RPC request.
432 */
433int
NeilBrown6fb2b472006-10-02 02:17:50 -0700434svc_process(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 struct svc_program *progp;
437 struct svc_version *versp = NULL; /* compiler food */
438 struct svc_procedure *procp = NULL;
439 struct kvec * argv = &rqstp->rq_arg.head[0];
440 struct kvec * resv = &rqstp->rq_res.head[0];
NeilBrown6fb2b472006-10-02 02:17:50 -0700441 struct svc_serv *serv = rqstp->rq_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 kxdrproc_t xdr;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700443 __be32 *statp;
444 u32 dir, prog, vers, proc;
445 __be32 auth_stat, rpc_stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 int auth_res;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700447 __be32 *accept_statp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 rpc_stat = rpc_success;
450
451 if (argv->iov_len < 6*4)
452 goto err_short_len;
453
454 /* setup response xdr_buf.
455 * Initially it has just one page
456 */
457 svc_take_page(rqstp); /* must succeed */
458 resv->iov_base = page_address(rqstp->rq_respages[0]);
459 resv->iov_len = 0;
460 rqstp->rq_res.pages = rqstp->rq_respages+1;
461 rqstp->rq_res.len = 0;
462 rqstp->rq_res.page_base = 0;
463 rqstp->rq_res.page_len = 0;
Trond Myklebust334ccfd2005-06-22 17:16:19 +0000464 rqstp->rq_res.buflen = PAGE_SIZE;
J. Bruce Fields7c9fdcf2006-06-30 01:56:19 -0700465 rqstp->rq_res.tail[0].iov_base = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 rqstp->rq_res.tail[0].iov_len = 0;
J. Bruce Fields5c04c462006-06-30 01:56:19 -0700467 /* Will be turned off only in gss privacy case: */
468 rqstp->rq_sendfile_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 /* tcp needs a space for the record length... */
470 if (rqstp->rq_prot == IPPROTO_TCP)
Alexey Dobriyan76994312006-09-26 22:28:46 -0700471 svc_putnl(resv, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 rqstp->rq_xid = svc_getu32(argv);
474 svc_putu32(resv, rqstp->rq_xid);
475
Alexey Dobriyan76994312006-09-26 22:28:46 -0700476 dir = svc_getnl(argv);
477 vers = svc_getnl(argv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 /* First words of reply: */
Alexey Dobriyan76994312006-09-26 22:28:46 -0700480 svc_putnl(resv, 1); /* REPLY */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 if (dir != 0) /* direction != CALL */
483 goto err_bad_dir;
484 if (vers != 2) /* RPC version number */
485 goto err_bad_rpc;
486
487 /* Save position in case we later decide to reject: */
488 accept_statp = resv->iov_base + resv->iov_len;
489
Alexey Dobriyan76994312006-09-26 22:28:46 -0700490 svc_putnl(resv, 0); /* ACCEPT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Alexey Dobriyan76994312006-09-26 22:28:46 -0700492 rqstp->rq_prog = prog = svc_getnl(argv); /* program number */
493 rqstp->rq_vers = vers = svc_getnl(argv); /* version number */
494 rqstp->rq_proc = proc = svc_getnl(argv); /* procedure number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 progp = serv->sv_program;
NeilBrown80d188a2005-11-07 01:00:27 -0800497
498 for (progp = serv->sv_program; progp; progp = progp->pg_next)
499 if (prog == progp->pg_prog)
500 break;
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 /*
503 * Decode auth data, and add verifier to reply buffer.
504 * We do this before anything else in order to get a decent
505 * auth verifier.
506 */
507 auth_res = svc_authenticate(rqstp, &auth_stat);
508 /* Also give the program a chance to reject this call: */
NeilBrown80d188a2005-11-07 01:00:27 -0800509 if (auth_res == SVC_OK && progp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 auth_stat = rpc_autherr_badcred;
511 auth_res = progp->pg_authenticate(rqstp);
512 }
513 switch (auth_res) {
514 case SVC_OK:
515 break;
516 case SVC_GARBAGE:
517 rpc_stat = rpc_garbage_args;
518 goto err_bad;
519 case SVC_SYSERR:
520 rpc_stat = rpc_system_err;
521 goto err_bad;
522 case SVC_DENIED:
523 goto err_bad_auth;
524 case SVC_DROP:
525 goto dropit;
526 case SVC_COMPLETE:
527 goto sendit;
528 }
NeilBrown80d188a2005-11-07 01:00:27 -0800529
Andreas Gruenbacher9ba02632005-06-22 17:16:24 +0000530 if (progp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 goto err_bad_prog;
532
533 if (vers >= progp->pg_nvers ||
534 !(versp = progp->pg_vers[vers]))
535 goto err_bad_vers;
536
537 procp = versp->vs_proc + proc;
538 if (proc >= versp->vs_nproc || !procp->pc_func)
539 goto err_bad_proc;
540 rqstp->rq_server = serv;
541 rqstp->rq_procinfo = procp;
542
543 /* Syntactic check complete */
544 serv->sv_stats->rpccnt++;
545
546 /* Build the reply header. */
547 statp = resv->iov_base +resv->iov_len;
Alexey Dobriyan76994312006-09-26 22:28:46 -0700548 svc_putnl(resv, RPC_SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 /* Bump per-procedure stats counter */
551 procp->pc_count++;
552
553 /* Initialize storage for argp and resp */
554 memset(rqstp->rq_argp, 0, procp->pc_argsize);
555 memset(rqstp->rq_resp, 0, procp->pc_ressize);
556
557 /* un-reserve some of the out-queue now that we have a
558 * better idea of reply size
559 */
560 if (procp->pc_xdrressize)
561 svc_reserve(rqstp, procp->pc_xdrressize<<2);
562
563 /* Call the function that processes the request. */
564 if (!versp->vs_dispatch) {
565 /* Decode arguments */
566 xdr = procp->pc_decode;
567 if (xdr && !xdr(rqstp, argv->iov_base, rqstp->rq_argp))
568 goto err_garbage;
569
570 *statp = procp->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp);
571
572 /* Encode reply */
573 if (*statp == rpc_success && (xdr = procp->pc_encode)
574 && !xdr(rqstp, resv->iov_base+resv->iov_len, rqstp->rq_resp)) {
575 dprintk("svc: failed to encode reply\n");
576 /* serv->sv_stats->rpcsystemerr++; */
577 *statp = rpc_system_err;
578 }
579 } else {
580 dprintk("svc: calling dispatcher\n");
581 if (!versp->vs_dispatch(rqstp, statp)) {
582 /* Release reply info */
583 if (procp->pc_release)
584 procp->pc_release(rqstp, NULL, rqstp->rq_resp);
585 goto dropit;
586 }
587 }
588
589 /* Check RPC status result */
590 if (*statp != rpc_success)
591 resv->iov_len = ((void*)statp) - resv->iov_base + 4;
592
593 /* Release reply info */
594 if (procp->pc_release)
595 procp->pc_release(rqstp, NULL, rqstp->rq_resp);
596
597 if (procp->pc_encode == NULL)
598 goto dropit;
599
600 sendit:
601 if (svc_authorise(rqstp))
602 goto dropit;
603 return svc_send(rqstp);
604
605 dropit:
606 svc_authorise(rqstp); /* doesn't hurt to call this twice */
607 dprintk("svc: svc_process dropit\n");
608 svc_drop(rqstp);
609 return 0;
610
611err_short_len:
612#ifdef RPC_PARANOIA
613 printk("svc: short len %Zd, dropping request\n", argv->iov_len);
614#endif
615 goto dropit; /* drop request */
616
617err_bad_dir:
618#ifdef RPC_PARANOIA
619 printk("svc: bad direction %d, dropping request\n", dir);
620#endif
621 serv->sv_stats->rpcbadfmt++;
622 goto dropit; /* drop request */
623
624err_bad_rpc:
625 serv->sv_stats->rpcbadfmt++;
Alexey Dobriyan76994312006-09-26 22:28:46 -0700626 svc_putnl(resv, 1); /* REJECT */
627 svc_putnl(resv, 0); /* RPC_MISMATCH */
628 svc_putnl(resv, 2); /* Only RPCv2 supported */
629 svc_putnl(resv, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 goto sendit;
631
632err_bad_auth:
633 dprintk("svc: authentication failed (%d)\n", ntohl(auth_stat));
634 serv->sv_stats->rpcbadauth++;
635 /* Restore write pointer to location of accept status: */
636 xdr_ressize_check(rqstp, accept_statp);
Alexey Dobriyan76994312006-09-26 22:28:46 -0700637 svc_putnl(resv, 1); /* REJECT */
638 svc_putnl(resv, 1); /* AUTH_ERROR */
639 svc_putnl(resv, ntohl(auth_stat)); /* status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 goto sendit;
641
642err_bad_prog:
Andreas Gruenbacher9ba02632005-06-22 17:16:24 +0000643 dprintk("svc: unknown program %d\n", prog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 serv->sv_stats->rpcbadfmt++;
Alexey Dobriyan76994312006-09-26 22:28:46 -0700645 svc_putnl(resv, RPC_PROG_UNAVAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 goto sendit;
647
648err_bad_vers:
649#ifdef RPC_PARANOIA
650 printk("svc: unknown version (%d)\n", vers);
651#endif
652 serv->sv_stats->rpcbadfmt++;
Alexey Dobriyan76994312006-09-26 22:28:46 -0700653 svc_putnl(resv, RPC_PROG_MISMATCH);
654 svc_putnl(resv, progp->pg_lovers);
655 svc_putnl(resv, progp->pg_hivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 goto sendit;
657
658err_bad_proc:
659#ifdef RPC_PARANOIA
660 printk("svc: unknown procedure (%d)\n", proc);
661#endif
662 serv->sv_stats->rpcbadfmt++;
Alexey Dobriyan76994312006-09-26 22:28:46 -0700663 svc_putnl(resv, RPC_PROC_UNAVAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 goto sendit;
665
666err_garbage:
667#ifdef RPC_PARANOIA
668 printk("svc: failed to decode args\n");
669#endif
670 rpc_stat = rpc_garbage_args;
671err_bad:
672 serv->sv_stats->rpcbadfmt++;
Alexey Dobriyan76994312006-09-26 22:28:46 -0700673 svc_putnl(resv, ntohl(rpc_stat));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 goto sendit;
675}