blob: b76a227dd3ad9aac842f988874bc66cbb27e4a7b [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>
15
16#include <linux/sunrpc/types.h>
17#include <linux/sunrpc/xdr.h>
18#include <linux/sunrpc/stats.h>
19#include <linux/sunrpc/svcsock.h>
20#include <linux/sunrpc/clnt.h>
21
22#define RPCDBG_FACILITY RPCDBG_SVCDSP
23#define RPC_PARANOIA 1
24
25/*
26 * Create an RPC service
27 */
28struct svc_serv *
29svc_create(struct svc_program *prog, unsigned int bufsize)
30{
31 struct svc_serv *serv;
32 int vers;
33 unsigned int xdrsize;
34
Panagiotis Issaris0da974f2006-07-21 14:51:30 -070035 if (!(serv = kzalloc(sizeof(*serv), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 return NULL;
Andreas Gruenbacher9ba02632005-06-22 17:16:24 +000037 serv->sv_name = prog->pg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 serv->sv_program = prog;
39 serv->sv_nrthreads = 1;
40 serv->sv_stats = prog->pg_stats;
41 serv->sv_bufsz = bufsize? bufsize : 4096;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 xdrsize = 0;
Andreas Gruenbacher9ba02632005-06-22 17:16:24 +000043 while (prog) {
44 prog->pg_lovers = prog->pg_nvers-1;
45 for (vers=0; vers<prog->pg_nvers ; vers++)
46 if (prog->pg_vers[vers]) {
47 prog->pg_hivers = vers;
48 if (prog->pg_lovers > vers)
49 prog->pg_lovers = vers;
50 if (prog->pg_vers[vers]->vs_xdrsize > xdrsize)
51 xdrsize = prog->pg_vers[vers]->vs_xdrsize;
52 }
53 prog = prog->pg_next;
54 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 serv->sv_xdrsize = xdrsize;
56 INIT_LIST_HEAD(&serv->sv_threads);
57 INIT_LIST_HEAD(&serv->sv_sockets);
58 INIT_LIST_HEAD(&serv->sv_tempsocks);
59 INIT_LIST_HEAD(&serv->sv_permsocks);
60 spin_lock_init(&serv->sv_lock);
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 /* Remove any stale portmap registrations */
63 svc_register(serv, 0, 0);
64
65 return serv;
66}
67
68/*
69 * Destroy an RPC service
70 */
71void
72svc_destroy(struct svc_serv *serv)
73{
74 struct svc_sock *svsk;
75
76 dprintk("RPC: svc_destroy(%s, %d)\n",
77 serv->sv_program->pg_name,
78 serv->sv_nrthreads);
79
80 if (serv->sv_nrthreads) {
81 if (--(serv->sv_nrthreads) != 0) {
82 svc_sock_update_bufs(serv);
83 return;
84 }
85 } else
86 printk("svc_destroy: no threads for serv=%p!\n", serv);
87
88 while (!list_empty(&serv->sv_tempsocks)) {
89 svsk = list_entry(serv->sv_tempsocks.next,
90 struct svc_sock,
91 sk_list);
92 svc_delete_socket(svsk);
93 }
94 while (!list_empty(&serv->sv_permsocks)) {
95 svsk = list_entry(serv->sv_permsocks.next,
96 struct svc_sock,
97 sk_list);
98 svc_delete_socket(svsk);
99 }
100
101 cache_clean_deferred(serv);
102
103 /* Unregister service with the portmapper */
104 svc_register(serv, 0, 0);
105 kfree(serv);
106}
107
108/*
109 * Allocate an RPC server's buffer space.
110 * We allocate pages and place them in rq_argpages.
111 */
112static int
113svc_init_buffer(struct svc_rqst *rqstp, unsigned int size)
114{
115 int pages;
116 int arghi;
117
118 if (size > RPCSVC_MAXPAYLOAD)
119 size = RPCSVC_MAXPAYLOAD;
120 pages = 2 + (size+ PAGE_SIZE -1) / PAGE_SIZE;
121 rqstp->rq_argused = 0;
122 rqstp->rq_resused = 0;
123 arghi = 0;
Kris Katterjohn09a62662006-01-08 22:24:28 -0800124 BUG_ON(pages > RPCSVC_MAXPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 while (pages) {
126 struct page *p = alloc_page(GFP_KERNEL);
127 if (!p)
128 break;
129 rqstp->rq_argpages[arghi++] = p;
130 pages--;
131 }
132 rqstp->rq_arghi = arghi;
133 return ! pages;
134}
135
136/*
137 * Release an RPC server buffer
138 */
139static void
140svc_release_buffer(struct svc_rqst *rqstp)
141{
142 while (rqstp->rq_arghi)
143 put_page(rqstp->rq_argpages[--rqstp->rq_arghi]);
144 while (rqstp->rq_resused) {
145 if (rqstp->rq_respages[--rqstp->rq_resused] == NULL)
146 continue;
147 put_page(rqstp->rq_respages[rqstp->rq_resused]);
148 }
149 rqstp->rq_argused = 0;
150}
151
152/*
153 * Create a server thread
154 */
155int
156svc_create_thread(svc_thread_fn func, struct svc_serv *serv)
157{
158 struct svc_rqst *rqstp;
159 int error = -ENOMEM;
160
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700161 rqstp = kzalloc(sizeof(*rqstp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (!rqstp)
163 goto out;
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 init_waitqueue_head(&rqstp->rq_wait);
166
Jesper Juhl12fe2c52006-01-10 13:08:21 -0800167 if (!(rqstp->rq_argp = kmalloc(serv->sv_xdrsize, GFP_KERNEL))
168 || !(rqstp->rq_resp = kmalloc(serv->sv_xdrsize, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 || !svc_init_buffer(rqstp, serv->sv_bufsz))
170 goto out_thread;
171
172 serv->sv_nrthreads++;
173 rqstp->rq_server = serv;
174 error = kernel_thread((int (*)(void *)) func, rqstp, 0);
175 if (error < 0)
176 goto out_thread;
177 svc_sock_update_bufs(serv);
178 error = 0;
179out:
180 return error;
181
182out_thread:
183 svc_exit_thread(rqstp);
184 goto out;
185}
186
187/*
188 * Destroy an RPC server thread
189 */
190void
191svc_exit_thread(struct svc_rqst *rqstp)
192{
193 struct svc_serv *serv = rqstp->rq_server;
194
195 svc_release_buffer(rqstp);
Jesper Juhla51482b2005-11-08 09:41:34 -0800196 kfree(rqstp->rq_resp);
197 kfree(rqstp->rq_argp);
198 kfree(rqstp->rq_auth_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 kfree(rqstp);
200
201 /* Release the server */
202 if (serv)
203 svc_destroy(serv);
204}
205
206/*
207 * Register an RPC service with the local portmapper.
208 * To unregister a service, call this routine with
209 * proto and port == 0.
210 */
211int
212svc_register(struct svc_serv *serv, int proto, unsigned short port)
213{
214 struct svc_program *progp;
215 unsigned long flags;
216 int i, error = 0, dummy;
217
218 progp = serv->sv_program;
219
220 dprintk("RPC: svc_register(%s, %s, %d)\n",
221 progp->pg_name, proto == IPPROTO_UDP? "udp" : "tcp", port);
222
223 if (!port)
224 clear_thread_flag(TIF_SIGPENDING);
225
226 for (i = 0; i < progp->pg_nvers; i++) {
227 if (progp->pg_vers[i] == NULL)
228 continue;
229 error = rpc_register(progp->pg_prog, i, proto, port, &dummy);
230 if (error < 0)
231 break;
232 if (port && !dummy) {
233 error = -EACCES;
234 break;
235 }
236 }
237
238 if (!port) {
239 spin_lock_irqsave(&current->sighand->siglock, flags);
240 recalc_sigpending();
241 spin_unlock_irqrestore(&current->sighand->siglock, flags);
242 }
243
244 return error;
245}
246
247/*
248 * Process the RPC request.
249 */
250int
251svc_process(struct svc_serv *serv, struct svc_rqst *rqstp)
252{
253 struct svc_program *progp;
254 struct svc_version *versp = NULL; /* compiler food */
255 struct svc_procedure *procp = NULL;
256 struct kvec * argv = &rqstp->rq_arg.head[0];
257 struct kvec * resv = &rqstp->rq_res.head[0];
258 kxdrproc_t xdr;
259 u32 *statp;
260 u32 dir, prog, vers, proc,
261 auth_stat, rpc_stat;
262 int auth_res;
263 u32 *accept_statp;
264
265 rpc_stat = rpc_success;
266
267 if (argv->iov_len < 6*4)
268 goto err_short_len;
269
270 /* setup response xdr_buf.
271 * Initially it has just one page
272 */
273 svc_take_page(rqstp); /* must succeed */
274 resv->iov_base = page_address(rqstp->rq_respages[0]);
275 resv->iov_len = 0;
276 rqstp->rq_res.pages = rqstp->rq_respages+1;
277 rqstp->rq_res.len = 0;
278 rqstp->rq_res.page_base = 0;
279 rqstp->rq_res.page_len = 0;
Trond Myklebust334ccfd2005-06-22 17:16:19 +0000280 rqstp->rq_res.buflen = PAGE_SIZE;
J. Bruce Fields7c9fdcf2006-06-30 01:56:19 -0700281 rqstp->rq_res.tail[0].iov_base = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 rqstp->rq_res.tail[0].iov_len = 0;
J. Bruce Fields5c04c462006-06-30 01:56:19 -0700283 /* Will be turned off only in gss privacy case: */
284 rqstp->rq_sendfile_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 /* tcp needs a space for the record length... */
286 if (rqstp->rq_prot == IPPROTO_TCP)
287 svc_putu32(resv, 0);
288
289 rqstp->rq_xid = svc_getu32(argv);
290 svc_putu32(resv, rqstp->rq_xid);
291
292 dir = ntohl(svc_getu32(argv));
293 vers = ntohl(svc_getu32(argv));
294
295 /* First words of reply: */
296 svc_putu32(resv, xdr_one); /* REPLY */
297
298 if (dir != 0) /* direction != CALL */
299 goto err_bad_dir;
300 if (vers != 2) /* RPC version number */
301 goto err_bad_rpc;
302
303 /* Save position in case we later decide to reject: */
304 accept_statp = resv->iov_base + resv->iov_len;
305
306 svc_putu32(resv, xdr_zero); /* ACCEPT */
307
308 rqstp->rq_prog = prog = ntohl(svc_getu32(argv)); /* program number */
309 rqstp->rq_vers = vers = ntohl(svc_getu32(argv)); /* version number */
310 rqstp->rq_proc = proc = ntohl(svc_getu32(argv)); /* procedure number */
311
312 progp = serv->sv_program;
NeilBrown80d188a2005-11-07 01:00:27 -0800313
314 for (progp = serv->sv_program; progp; progp = progp->pg_next)
315 if (prog == progp->pg_prog)
316 break;
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 /*
319 * Decode auth data, and add verifier to reply buffer.
320 * We do this before anything else in order to get a decent
321 * auth verifier.
322 */
323 auth_res = svc_authenticate(rqstp, &auth_stat);
324 /* Also give the program a chance to reject this call: */
NeilBrown80d188a2005-11-07 01:00:27 -0800325 if (auth_res == SVC_OK && progp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 auth_stat = rpc_autherr_badcred;
327 auth_res = progp->pg_authenticate(rqstp);
328 }
329 switch (auth_res) {
330 case SVC_OK:
331 break;
332 case SVC_GARBAGE:
333 rpc_stat = rpc_garbage_args;
334 goto err_bad;
335 case SVC_SYSERR:
336 rpc_stat = rpc_system_err;
337 goto err_bad;
338 case SVC_DENIED:
339 goto err_bad_auth;
340 case SVC_DROP:
341 goto dropit;
342 case SVC_COMPLETE:
343 goto sendit;
344 }
NeilBrown80d188a2005-11-07 01:00:27 -0800345
Andreas Gruenbacher9ba02632005-06-22 17:16:24 +0000346 if (progp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 goto err_bad_prog;
348
349 if (vers >= progp->pg_nvers ||
350 !(versp = progp->pg_vers[vers]))
351 goto err_bad_vers;
352
353 procp = versp->vs_proc + proc;
354 if (proc >= versp->vs_nproc || !procp->pc_func)
355 goto err_bad_proc;
356 rqstp->rq_server = serv;
357 rqstp->rq_procinfo = procp;
358
359 /* Syntactic check complete */
360 serv->sv_stats->rpccnt++;
361
362 /* Build the reply header. */
363 statp = resv->iov_base +resv->iov_len;
364 svc_putu32(resv, rpc_success); /* RPC_SUCCESS */
365
366 /* Bump per-procedure stats counter */
367 procp->pc_count++;
368
369 /* Initialize storage for argp and resp */
370 memset(rqstp->rq_argp, 0, procp->pc_argsize);
371 memset(rqstp->rq_resp, 0, procp->pc_ressize);
372
373 /* un-reserve some of the out-queue now that we have a
374 * better idea of reply size
375 */
376 if (procp->pc_xdrressize)
377 svc_reserve(rqstp, procp->pc_xdrressize<<2);
378
379 /* Call the function that processes the request. */
380 if (!versp->vs_dispatch) {
381 /* Decode arguments */
382 xdr = procp->pc_decode;
383 if (xdr && !xdr(rqstp, argv->iov_base, rqstp->rq_argp))
384 goto err_garbage;
385
386 *statp = procp->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp);
387
388 /* Encode reply */
389 if (*statp == rpc_success && (xdr = procp->pc_encode)
390 && !xdr(rqstp, resv->iov_base+resv->iov_len, rqstp->rq_resp)) {
391 dprintk("svc: failed to encode reply\n");
392 /* serv->sv_stats->rpcsystemerr++; */
393 *statp = rpc_system_err;
394 }
395 } else {
396 dprintk("svc: calling dispatcher\n");
397 if (!versp->vs_dispatch(rqstp, statp)) {
398 /* Release reply info */
399 if (procp->pc_release)
400 procp->pc_release(rqstp, NULL, rqstp->rq_resp);
401 goto dropit;
402 }
403 }
404
405 /* Check RPC status result */
406 if (*statp != rpc_success)
407 resv->iov_len = ((void*)statp) - resv->iov_base + 4;
408
409 /* Release reply info */
410 if (procp->pc_release)
411 procp->pc_release(rqstp, NULL, rqstp->rq_resp);
412
413 if (procp->pc_encode == NULL)
414 goto dropit;
415
416 sendit:
417 if (svc_authorise(rqstp))
418 goto dropit;
419 return svc_send(rqstp);
420
421 dropit:
422 svc_authorise(rqstp); /* doesn't hurt to call this twice */
423 dprintk("svc: svc_process dropit\n");
424 svc_drop(rqstp);
425 return 0;
426
427err_short_len:
428#ifdef RPC_PARANOIA
429 printk("svc: short len %Zd, dropping request\n", argv->iov_len);
430#endif
431 goto dropit; /* drop request */
432
433err_bad_dir:
434#ifdef RPC_PARANOIA
435 printk("svc: bad direction %d, dropping request\n", dir);
436#endif
437 serv->sv_stats->rpcbadfmt++;
438 goto dropit; /* drop request */
439
440err_bad_rpc:
441 serv->sv_stats->rpcbadfmt++;
442 svc_putu32(resv, xdr_one); /* REJECT */
443 svc_putu32(resv, xdr_zero); /* RPC_MISMATCH */
444 svc_putu32(resv, xdr_two); /* Only RPCv2 supported */
445 svc_putu32(resv, xdr_two);
446 goto sendit;
447
448err_bad_auth:
449 dprintk("svc: authentication failed (%d)\n", ntohl(auth_stat));
450 serv->sv_stats->rpcbadauth++;
451 /* Restore write pointer to location of accept status: */
452 xdr_ressize_check(rqstp, accept_statp);
453 svc_putu32(resv, xdr_one); /* REJECT */
454 svc_putu32(resv, xdr_one); /* AUTH_ERROR */
455 svc_putu32(resv, auth_stat); /* status */
456 goto sendit;
457
458err_bad_prog:
Andreas Gruenbacher9ba02632005-06-22 17:16:24 +0000459 dprintk("svc: unknown program %d\n", prog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 serv->sv_stats->rpcbadfmt++;
461 svc_putu32(resv, rpc_prog_unavail);
462 goto sendit;
463
464err_bad_vers:
465#ifdef RPC_PARANOIA
466 printk("svc: unknown version (%d)\n", vers);
467#endif
468 serv->sv_stats->rpcbadfmt++;
469 svc_putu32(resv, rpc_prog_mismatch);
470 svc_putu32(resv, htonl(progp->pg_lovers));
471 svc_putu32(resv, htonl(progp->pg_hivers));
472 goto sendit;
473
474err_bad_proc:
475#ifdef RPC_PARANOIA
476 printk("svc: unknown procedure (%d)\n", proc);
477#endif
478 serv->sv_stats->rpcbadfmt++;
479 svc_putu32(resv, rpc_proc_unavail);
480 goto sendit;
481
482err_garbage:
483#ifdef RPC_PARANOIA
484 printk("svc: failed to decode args\n");
485#endif
486 rpc_stat = rpc_garbage_args;
487err_bad:
488 serv->sv_stats->rpcbadfmt++;
489 svc_putu32(resv, rpc_stat);
490 goto sendit;
491}