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