blob: eee45a58f3ee08ab0fc3b6a6fc749f620675d69b [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 *
NeilBrownbc591cc2006-10-02 02:17:44 -070029svc_create(struct svc_program *prog, unsigned int bufsize,
30 void (*shutdown)(struct svc_serv *serv))
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
32 struct svc_serv *serv;
33 int vers;
34 unsigned int xdrsize;
35
Panagiotis Issaris0da974f2006-07-21 14:51:30 -070036 if (!(serv = kzalloc(sizeof(*serv), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 return NULL;
Andreas Gruenbacher9ba02632005-06-22 17:16:24 +000038 serv->sv_name = prog->pg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 serv->sv_program = prog;
40 serv->sv_nrthreads = 1;
41 serv->sv_stats = prog->pg_stats;
42 serv->sv_bufsz = bufsize? bufsize : 4096;
NeilBrownbc591cc2006-10-02 02:17:44 -070043 serv->sv_shutdown = shutdown;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 xdrsize = 0;
Andreas Gruenbacher9ba02632005-06-22 17:16:24 +000045 while (prog) {
46 prog->pg_lovers = prog->pg_nvers-1;
47 for (vers=0; vers<prog->pg_nvers ; vers++)
48 if (prog->pg_vers[vers]) {
49 prog->pg_hivers = vers;
50 if (prog->pg_lovers > vers)
51 prog->pg_lovers = vers;
52 if (prog->pg_vers[vers]->vs_xdrsize > xdrsize)
53 xdrsize = prog->pg_vers[vers]->vs_xdrsize;
54 }
55 prog = prog->pg_next;
56 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 serv->sv_xdrsize = xdrsize;
58 INIT_LIST_HEAD(&serv->sv_threads);
59 INIT_LIST_HEAD(&serv->sv_sockets);
60 INIT_LIST_HEAD(&serv->sv_tempsocks);
61 INIT_LIST_HEAD(&serv->sv_permsocks);
62 spin_lock_init(&serv->sv_lock);
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 /* Remove any stale portmap registrations */
65 svc_register(serv, 0, 0);
66
67 return serv;
68}
69
70/*
71 * Destroy an RPC service
72 */
73void
74svc_destroy(struct svc_serv *serv)
75{
76 struct svc_sock *svsk;
77
78 dprintk("RPC: svc_destroy(%s, %d)\n",
79 serv->sv_program->pg_name,
80 serv->sv_nrthreads);
81
82 if (serv->sv_nrthreads) {
83 if (--(serv->sv_nrthreads) != 0) {
84 svc_sock_update_bufs(serv);
85 return;
86 }
87 } else
88 printk("svc_destroy: no threads for serv=%p!\n", serv);
89
90 while (!list_empty(&serv->sv_tempsocks)) {
91 svsk = list_entry(serv->sv_tempsocks.next,
92 struct svc_sock,
93 sk_list);
94 svc_delete_socket(svsk);
95 }
NeilBrownbc591cc2006-10-02 02:17:44 -070096 if (serv->sv_shutdown)
97 serv->sv_shutdown(serv);
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 while (!list_empty(&serv->sv_permsocks)) {
100 svsk = list_entry(serv->sv_permsocks.next,
101 struct svc_sock,
102 sk_list);
103 svc_delete_socket(svsk);
104 }
105
106 cache_clean_deferred(serv);
107
108 /* Unregister service with the portmapper */
109 svc_register(serv, 0, 0);
110 kfree(serv);
111}
112
113/*
114 * Allocate an RPC server's buffer space.
115 * We allocate pages and place them in rq_argpages.
116 */
117static int
118svc_init_buffer(struct svc_rqst *rqstp, unsigned int size)
119{
120 int pages;
121 int arghi;
122
123 if (size > RPCSVC_MAXPAYLOAD)
124 size = RPCSVC_MAXPAYLOAD;
125 pages = 2 + (size+ PAGE_SIZE -1) / PAGE_SIZE;
126 rqstp->rq_argused = 0;
127 rqstp->rq_resused = 0;
128 arghi = 0;
Kris Katterjohn09a62662006-01-08 22:24:28 -0800129 BUG_ON(pages > RPCSVC_MAXPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 while (pages) {
131 struct page *p = alloc_page(GFP_KERNEL);
132 if (!p)
133 break;
134 rqstp->rq_argpages[arghi++] = p;
135 pages--;
136 }
137 rqstp->rq_arghi = arghi;
138 return ! pages;
139}
140
141/*
142 * Release an RPC server buffer
143 */
144static void
145svc_release_buffer(struct svc_rqst *rqstp)
146{
147 while (rqstp->rq_arghi)
148 put_page(rqstp->rq_argpages[--rqstp->rq_arghi]);
149 while (rqstp->rq_resused) {
150 if (rqstp->rq_respages[--rqstp->rq_resused] == NULL)
151 continue;
152 put_page(rqstp->rq_respages[rqstp->rq_resused]);
153 }
154 rqstp->rq_argused = 0;
155}
156
157/*
158 * Create a server thread
159 */
160int
161svc_create_thread(svc_thread_fn func, struct svc_serv *serv)
162{
163 struct svc_rqst *rqstp;
164 int error = -ENOMEM;
165
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700166 rqstp = kzalloc(sizeof(*rqstp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (!rqstp)
168 goto out;
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 init_waitqueue_head(&rqstp->rq_wait);
171
Jesper Juhl12fe2c52006-01-10 13:08:21 -0800172 if (!(rqstp->rq_argp = kmalloc(serv->sv_xdrsize, GFP_KERNEL))
173 || !(rqstp->rq_resp = kmalloc(serv->sv_xdrsize, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 || !svc_init_buffer(rqstp, serv->sv_bufsz))
175 goto out_thread;
176
177 serv->sv_nrthreads++;
178 rqstp->rq_server = serv;
179 error = kernel_thread((int (*)(void *)) func, rqstp, 0);
180 if (error < 0)
181 goto out_thread;
182 svc_sock_update_bufs(serv);
183 error = 0;
184out:
185 return error;
186
187out_thread:
188 svc_exit_thread(rqstp);
189 goto out;
190}
191
192/*
193 * Destroy an RPC server thread
194 */
195void
196svc_exit_thread(struct svc_rqst *rqstp)
197{
198 struct svc_serv *serv = rqstp->rq_server;
199
200 svc_release_buffer(rqstp);
Jesper Juhla51482b2005-11-08 09:41:34 -0800201 kfree(rqstp->rq_resp);
202 kfree(rqstp->rq_argp);
203 kfree(rqstp->rq_auth_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 kfree(rqstp);
205
206 /* Release the server */
207 if (serv)
208 svc_destroy(serv);
209}
210
211/*
212 * Register an RPC service with the local portmapper.
213 * To unregister a service, call this routine with
214 * proto and port == 0.
215 */
216int
217svc_register(struct svc_serv *serv, int proto, unsigned short port)
218{
219 struct svc_program *progp;
220 unsigned long flags;
221 int i, error = 0, dummy;
222
223 progp = serv->sv_program;
224
225 dprintk("RPC: svc_register(%s, %s, %d)\n",
226 progp->pg_name, proto == IPPROTO_UDP? "udp" : "tcp", port);
227
228 if (!port)
229 clear_thread_flag(TIF_SIGPENDING);
230
231 for (i = 0; i < progp->pg_nvers; i++) {
232 if (progp->pg_vers[i] == NULL)
233 continue;
234 error = rpc_register(progp->pg_prog, i, proto, port, &dummy);
235 if (error < 0)
236 break;
237 if (port && !dummy) {
238 error = -EACCES;
239 break;
240 }
241 }
242
243 if (!port) {
244 spin_lock_irqsave(&current->sighand->siglock, flags);
245 recalc_sigpending();
246 spin_unlock_irqrestore(&current->sighand->siglock, flags);
247 }
248
249 return error;
250}
251
252/*
253 * Process the RPC request.
254 */
255int
NeilBrown6fb2b472006-10-02 02:17:50 -0700256svc_process(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 struct svc_program *progp;
259 struct svc_version *versp = NULL; /* compiler food */
260 struct svc_procedure *procp = NULL;
261 struct kvec * argv = &rqstp->rq_arg.head[0];
262 struct kvec * resv = &rqstp->rq_res.head[0];
NeilBrown6fb2b472006-10-02 02:17:50 -0700263 struct svc_serv *serv = rqstp->rq_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 kxdrproc_t xdr;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700265 __be32 *statp;
266 u32 dir, prog, vers, proc;
267 __be32 auth_stat, rpc_stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 int auth_res;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700269 __be32 *accept_statp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 rpc_stat = rpc_success;
272
273 if (argv->iov_len < 6*4)
274 goto err_short_len;
275
276 /* setup response xdr_buf.
277 * Initially it has just one page
278 */
279 svc_take_page(rqstp); /* must succeed */
280 resv->iov_base = page_address(rqstp->rq_respages[0]);
281 resv->iov_len = 0;
282 rqstp->rq_res.pages = rqstp->rq_respages+1;
283 rqstp->rq_res.len = 0;
284 rqstp->rq_res.page_base = 0;
285 rqstp->rq_res.page_len = 0;
Trond Myklebust334ccfd2005-06-22 17:16:19 +0000286 rqstp->rq_res.buflen = PAGE_SIZE;
J. Bruce Fields7c9fdcf2006-06-30 01:56:19 -0700287 rqstp->rq_res.tail[0].iov_base = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 rqstp->rq_res.tail[0].iov_len = 0;
J. Bruce Fields5c04c462006-06-30 01:56:19 -0700289 /* Will be turned off only in gss privacy case: */
290 rqstp->rq_sendfile_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 /* tcp needs a space for the record length... */
292 if (rqstp->rq_prot == IPPROTO_TCP)
Alexey Dobriyan76994312006-09-26 22:28:46 -0700293 svc_putnl(resv, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 rqstp->rq_xid = svc_getu32(argv);
296 svc_putu32(resv, rqstp->rq_xid);
297
Alexey Dobriyan76994312006-09-26 22:28:46 -0700298 dir = svc_getnl(argv);
299 vers = svc_getnl(argv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 /* First words of reply: */
Alexey Dobriyan76994312006-09-26 22:28:46 -0700302 svc_putnl(resv, 1); /* REPLY */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 if (dir != 0) /* direction != CALL */
305 goto err_bad_dir;
306 if (vers != 2) /* RPC version number */
307 goto err_bad_rpc;
308
309 /* Save position in case we later decide to reject: */
310 accept_statp = resv->iov_base + resv->iov_len;
311
Alexey Dobriyan76994312006-09-26 22:28:46 -0700312 svc_putnl(resv, 0); /* ACCEPT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Alexey Dobriyan76994312006-09-26 22:28:46 -0700314 rqstp->rq_prog = prog = svc_getnl(argv); /* program number */
315 rqstp->rq_vers = vers = svc_getnl(argv); /* version number */
316 rqstp->rq_proc = proc = svc_getnl(argv); /* procedure number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 progp = serv->sv_program;
NeilBrown80d188a2005-11-07 01:00:27 -0800319
320 for (progp = serv->sv_program; progp; progp = progp->pg_next)
321 if (prog == progp->pg_prog)
322 break;
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 /*
325 * Decode auth data, and add verifier to reply buffer.
326 * We do this before anything else in order to get a decent
327 * auth verifier.
328 */
329 auth_res = svc_authenticate(rqstp, &auth_stat);
330 /* Also give the program a chance to reject this call: */
NeilBrown80d188a2005-11-07 01:00:27 -0800331 if (auth_res == SVC_OK && progp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 auth_stat = rpc_autherr_badcred;
333 auth_res = progp->pg_authenticate(rqstp);
334 }
335 switch (auth_res) {
336 case SVC_OK:
337 break;
338 case SVC_GARBAGE:
339 rpc_stat = rpc_garbage_args;
340 goto err_bad;
341 case SVC_SYSERR:
342 rpc_stat = rpc_system_err;
343 goto err_bad;
344 case SVC_DENIED:
345 goto err_bad_auth;
346 case SVC_DROP:
347 goto dropit;
348 case SVC_COMPLETE:
349 goto sendit;
350 }
NeilBrown80d188a2005-11-07 01:00:27 -0800351
Andreas Gruenbacher9ba02632005-06-22 17:16:24 +0000352 if (progp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 goto err_bad_prog;
354
355 if (vers >= progp->pg_nvers ||
356 !(versp = progp->pg_vers[vers]))
357 goto err_bad_vers;
358
359 procp = versp->vs_proc + proc;
360 if (proc >= versp->vs_nproc || !procp->pc_func)
361 goto err_bad_proc;
362 rqstp->rq_server = serv;
363 rqstp->rq_procinfo = procp;
364
365 /* Syntactic check complete */
366 serv->sv_stats->rpccnt++;
367
368 /* Build the reply header. */
369 statp = resv->iov_base +resv->iov_len;
Alexey Dobriyan76994312006-09-26 22:28:46 -0700370 svc_putnl(resv, RPC_SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 /* Bump per-procedure stats counter */
373 procp->pc_count++;
374
375 /* Initialize storage for argp and resp */
376 memset(rqstp->rq_argp, 0, procp->pc_argsize);
377 memset(rqstp->rq_resp, 0, procp->pc_ressize);
378
379 /* un-reserve some of the out-queue now that we have a
380 * better idea of reply size
381 */
382 if (procp->pc_xdrressize)
383 svc_reserve(rqstp, procp->pc_xdrressize<<2);
384
385 /* Call the function that processes the request. */
386 if (!versp->vs_dispatch) {
387 /* Decode arguments */
388 xdr = procp->pc_decode;
389 if (xdr && !xdr(rqstp, argv->iov_base, rqstp->rq_argp))
390 goto err_garbage;
391
392 *statp = procp->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp);
393
394 /* Encode reply */
395 if (*statp == rpc_success && (xdr = procp->pc_encode)
396 && !xdr(rqstp, resv->iov_base+resv->iov_len, rqstp->rq_resp)) {
397 dprintk("svc: failed to encode reply\n");
398 /* serv->sv_stats->rpcsystemerr++; */
399 *statp = rpc_system_err;
400 }
401 } else {
402 dprintk("svc: calling dispatcher\n");
403 if (!versp->vs_dispatch(rqstp, statp)) {
404 /* Release reply info */
405 if (procp->pc_release)
406 procp->pc_release(rqstp, NULL, rqstp->rq_resp);
407 goto dropit;
408 }
409 }
410
411 /* Check RPC status result */
412 if (*statp != rpc_success)
413 resv->iov_len = ((void*)statp) - resv->iov_base + 4;
414
415 /* Release reply info */
416 if (procp->pc_release)
417 procp->pc_release(rqstp, NULL, rqstp->rq_resp);
418
419 if (procp->pc_encode == NULL)
420 goto dropit;
421
422 sendit:
423 if (svc_authorise(rqstp))
424 goto dropit;
425 return svc_send(rqstp);
426
427 dropit:
428 svc_authorise(rqstp); /* doesn't hurt to call this twice */
429 dprintk("svc: svc_process dropit\n");
430 svc_drop(rqstp);
431 return 0;
432
433err_short_len:
434#ifdef RPC_PARANOIA
435 printk("svc: short len %Zd, dropping request\n", argv->iov_len);
436#endif
437 goto dropit; /* drop request */
438
439err_bad_dir:
440#ifdef RPC_PARANOIA
441 printk("svc: bad direction %d, dropping request\n", dir);
442#endif
443 serv->sv_stats->rpcbadfmt++;
444 goto dropit; /* drop request */
445
446err_bad_rpc:
447 serv->sv_stats->rpcbadfmt++;
Alexey Dobriyan76994312006-09-26 22:28:46 -0700448 svc_putnl(resv, 1); /* REJECT */
449 svc_putnl(resv, 0); /* RPC_MISMATCH */
450 svc_putnl(resv, 2); /* Only RPCv2 supported */
451 svc_putnl(resv, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 goto sendit;
453
454err_bad_auth:
455 dprintk("svc: authentication failed (%d)\n", ntohl(auth_stat));
456 serv->sv_stats->rpcbadauth++;
457 /* Restore write pointer to location of accept status: */
458 xdr_ressize_check(rqstp, accept_statp);
Alexey Dobriyan76994312006-09-26 22:28:46 -0700459 svc_putnl(resv, 1); /* REJECT */
460 svc_putnl(resv, 1); /* AUTH_ERROR */
461 svc_putnl(resv, ntohl(auth_stat)); /* status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 goto sendit;
463
464err_bad_prog:
Andreas Gruenbacher9ba02632005-06-22 17:16:24 +0000465 dprintk("svc: unknown program %d\n", prog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 serv->sv_stats->rpcbadfmt++;
Alexey Dobriyan76994312006-09-26 22:28:46 -0700467 svc_putnl(resv, RPC_PROG_UNAVAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 goto sendit;
469
470err_bad_vers:
471#ifdef RPC_PARANOIA
472 printk("svc: unknown version (%d)\n", vers);
473#endif
474 serv->sv_stats->rpcbadfmt++;
Alexey Dobriyan76994312006-09-26 22:28:46 -0700475 svc_putnl(resv, RPC_PROG_MISMATCH);
476 svc_putnl(resv, progp->pg_lovers);
477 svc_putnl(resv, progp->pg_hivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 goto sendit;
479
480err_bad_proc:
481#ifdef RPC_PARANOIA
482 printk("svc: unknown procedure (%d)\n", proc);
483#endif
484 serv->sv_stats->rpcbadfmt++;
Alexey Dobriyan76994312006-09-26 22:28:46 -0700485 svc_putnl(resv, RPC_PROC_UNAVAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 goto sendit;
487
488err_garbage:
489#ifdef RPC_PARANOIA
490 printk("svc: failed to decode args\n");
491#endif
492 rpc_stat = rpc_garbage_args;
493err_bad:
494 serv->sv_stats->rpcbadfmt++;
Alexey Dobriyan76994312006-09-26 22:28:46 -0700495 svc_putnl(resv, ntohl(rpc_stat));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 goto sendit;
497}