ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / sunrpc / svc.c
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>
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  */
28 struct svc_serv *
29 svc_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));
38         serv->sv_program   = prog;
39         serv->sv_nrthreads = 1;
40         serv->sv_stats     = prog->pg_stats;
41         serv->sv_bufsz     = bufsize? bufsize : 4096;
42         prog->pg_lovers = prog->pg_nvers-1;
43         xdrsize = 0;
44         for (vers=0; vers<prog->pg_nvers ; vers++)
45                 if (prog->pg_vers[vers]) {
46                         prog->pg_hivers = vers;
47                         if (prog->pg_lovers > vers)
48                                 prog->pg_lovers = vers;
49                         if (prog->pg_vers[vers]->vs_xdrsize > xdrsize)
50                                 xdrsize = prog->pg_vers[vers]->vs_xdrsize;
51                 }
52         serv->sv_xdrsize   = xdrsize;
53         INIT_LIST_HEAD(&serv->sv_threads);
54         INIT_LIST_HEAD(&serv->sv_sockets);
55         INIT_LIST_HEAD(&serv->sv_tempsocks);
56         INIT_LIST_HEAD(&serv->sv_permsocks);
57         spin_lock_init(&serv->sv_lock);
58
59         serv->sv_name      = prog->pg_name;
60
61         /* Remove any stale portmap registrations */
62         svc_register(serv, 0, 0);
63
64         return serv;
65 }
66
67 /*
68  * Destroy an RPC service
69  */
70 void
71 svc_destroy(struct svc_serv *serv)
72 {
73         struct svc_sock *svsk;
74
75         dprintk("RPC: svc_destroy(%s, %d)\n",
76                                 serv->sv_program->pg_name,
77                                 serv->sv_nrthreads);
78
79         if (serv->sv_nrthreads) {
80                 if (--(serv->sv_nrthreads) != 0) {
81                         svc_sock_update_bufs(serv);
82                         return;
83                 }
84         } else
85                 printk("svc_destroy: no threads for serv=%p!\n", serv);
86
87         while (!list_empty(&serv->sv_tempsocks)) {
88                 svsk = list_entry(serv->sv_tempsocks.next,
89                                   struct svc_sock,
90                                   sk_list);
91                 svc_delete_socket(svsk);
92         }
93         while (!list_empty(&serv->sv_permsocks)) {
94                 svsk = list_entry(serv->sv_permsocks.next,
95                                   struct svc_sock,
96                                   sk_list);
97                 svc_delete_socket(svsk);
98         }
99         
100         cache_clean_deferred(serv);
101
102         /* Unregister service with the portmapper */
103         svc_register(serv, 0, 0);
104         kfree(serv);
105 }
106
107 /*
108  * Allocate an RPC server's buffer space.
109  * We allocate pages and place them in rq_argpages.
110  */
111 static int
112 svc_init_buffer(struct svc_rqst *rqstp, unsigned int size)
113 {
114         int pages;
115         int arghi;
116         
117         if (size > RPCSVC_MAXPAYLOAD)
118                 size = RPCSVC_MAXPAYLOAD;
119         pages = 2 + (size+ PAGE_SIZE -1) / PAGE_SIZE;
120         rqstp->rq_argused = 0;
121         rqstp->rq_resused = 0;
122         arghi = 0;
123         if (pages > RPCSVC_MAXPAGES)
124                 BUG();
125         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  */
139 static void
140 svc_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  */
155 int
156 svc_create_thread(svc_thread_fn func, struct svc_serv *serv)
157 {
158         struct svc_rqst *rqstp;
159         int             error = -ENOMEM;
160
161         rqstp = kmalloc(sizeof(*rqstp), GFP_KERNEL);
162         if (!rqstp)
163                 goto out;
164
165         memset(rqstp, 0, sizeof(*rqstp));
166         init_waitqueue_head(&rqstp->rq_wait);
167
168         if (!(rqstp->rq_argp = (u32 *) kmalloc(serv->sv_xdrsize, GFP_KERNEL))
169          || !(rqstp->rq_resp = (u32 *) kmalloc(serv->sv_xdrsize, GFP_KERNEL))
170          || !svc_init_buffer(rqstp, serv->sv_bufsz))
171                 goto out_thread;
172
173         serv->sv_nrthreads++;
174         rqstp->rq_server = serv;
175         error = kernel_thread((int (*)(void *)) func, rqstp, 0);
176         if (error < 0)
177                 goto out_thread;
178         svc_sock_update_bufs(serv);
179         error = 0;
180 out:
181         return error;
182
183 out_thread:
184         svc_exit_thread(rqstp);
185         goto out;
186 }
187
188 /*
189  * Destroy an RPC server thread
190  */
191 void
192 svc_exit_thread(struct svc_rqst *rqstp)
193 {
194         struct svc_serv *serv = rqstp->rq_server;
195
196         svc_release_buffer(rqstp);
197         if (rqstp->rq_resp)
198                 kfree(rqstp->rq_resp);
199         if (rqstp->rq_argp)
200                 kfree(rqstp->rq_argp);
201         if (rqstp->rq_auth_data)
202                 kfree(rqstp->rq_auth_data);
203         kfree(rqstp);
204
205         /* Release the server */
206         if (serv)
207                 svc_destroy(serv);
208 }
209
210 /*
211  * Register an RPC service with the local portmapper.
212  * To unregister a service, call this routine with 
213  * proto and port == 0.
214  */
215 int
216 svc_register(struct svc_serv *serv, int proto, unsigned short port)
217 {
218         struct svc_program      *progp;
219         unsigned long           flags;
220         int                     i, error = 0, dummy;
221
222         progp = serv->sv_program;
223
224         dprintk("RPC: svc_register(%s, %s, %d)\n",
225                 progp->pg_name, proto == IPPROTO_UDP? "udp" : "tcp", port);
226
227         if (!port)
228                 clear_thread_flag(TIF_SIGPENDING);
229
230         for (i = 0; i < progp->pg_nvers; i++) {
231                 if (progp->pg_vers[i] == NULL)
232                         continue;
233                 error = rpc_register(progp->pg_prog, i, proto, port, &dummy);
234                 if (error < 0)
235                         break;
236                 if (port && !dummy) {
237                         error = -EACCES;
238                         break;
239                 }
240         }
241
242         if (!port) {
243                 spin_lock_irqsave(&current->sighand->siglock, flags);
244                 recalc_sigpending();
245                 spin_unlock_irqrestore(&current->sighand->siglock, flags);
246         }
247
248         return error;
249 }
250
251 /*
252  * Process the RPC request.
253  */
254 int
255 svc_process(struct svc_serv *serv, struct svc_rqst *rqstp)
256 {
257         struct svc_program      *progp;
258         struct svc_version      *versp = NULL;  /* compiler food */
259         struct svc_procedure    *procp = NULL;
260         struct iovec *          argv = &rqstp->rq_arg.head[0];
261         struct iovec *          resv = &rqstp->rq_res.head[0];
262         kxdrproc_t              xdr;
263         u32                     *statp;
264         u32                     dir, prog, vers, proc,
265                                 auth_stat, rpc_stat;
266
267         rpc_stat = rpc_success;
268
269         if (argv->iov_len < 6*4)
270                 goto err_short_len;
271
272         /* setup response xdr_buf.
273          * Initially it has just one page 
274          */
275         svc_take_page(rqstp); /* must succeed */
276         resv->iov_base = page_address(rqstp->rq_respages[0]);
277         resv->iov_len = 0;
278         rqstp->rq_res.pages = rqstp->rq_respages+1;
279         rqstp->rq_res.len = 0;
280         rqstp->rq_res.page_base = 0;
281         rqstp->rq_res.page_len = 0;
282         rqstp->rq_res.tail[0].iov_len = 0;
283         /* tcp needs a space for the record length... */
284         if (rqstp->rq_prot == IPPROTO_TCP)
285                 svc_putu32(resv, 0);
286
287         rqstp->rq_xid = svc_getu32(argv);
288         svc_putu32(resv, rqstp->rq_xid);
289
290         dir  = ntohl(svc_getu32(argv));
291         vers = ntohl(svc_getu32(argv));
292
293         /* First words of reply: */
294         svc_putu32(resv, xdr_one);              /* REPLY */
295
296         if (dir != 0)           /* direction != CALL */
297                 goto err_bad_dir;
298         if (vers != 2)          /* RPC version number */
299                 goto err_bad_rpc;
300
301         svc_putu32(resv, xdr_zero);             /* ACCEPT */
302
303         rqstp->rq_prog = prog = ntohl(svc_getu32(argv));        /* program number */
304         rqstp->rq_vers = vers = ntohl(svc_getu32(argv));        /* version number */
305         rqstp->rq_proc = proc = ntohl(svc_getu32(argv));        /* procedure number */
306
307         /*
308          * Decode auth data, and add verifier to reply buffer.
309          * We do this before anything else in order to get a decent
310          * auth verifier.
311          */
312         switch (svc_authenticate(rqstp, &auth_stat)) {
313         case SVC_OK:
314                 break;
315         case SVC_GARBAGE:
316                 rpc_stat = rpc_garbage_args;
317                 goto err_bad;
318         case SVC_SYSERR:
319                 rpc_stat = rpc_system_err;
320                 goto err_bad;
321         case SVC_DENIED:
322                 goto err_bad_auth;
323         case SVC_DROP:
324                 goto dropit;
325         case SVC_COMPLETE:
326                 goto sendit;
327         }
328                 
329         progp = serv->sv_program;
330         if (prog != progp->pg_prog)
331                 goto err_bad_prog;
332
333         if (vers >= progp->pg_nvers ||
334           !(versp = progp->pg_vers[vers]))
335                 goto err_bad_vers;
336
337         procp = versp->vs_proc + proc;
338         if (proc >= versp->vs_nproc || !procp->pc_func)
339                 goto err_bad_proc;
340         rqstp->rq_server   = serv;
341         rqstp->rq_procinfo = procp;
342
343         /* Syntactic check complete */
344         serv->sv_stats->rpccnt++;
345
346         /* Build the reply header. */
347         statp = resv->iov_base +resv->iov_len;
348         svc_putu32(resv, rpc_success);          /* RPC_SUCCESS */
349
350         /* Bump per-procedure stats counter */
351         procp->pc_count++;
352
353         /* Initialize storage for argp and resp */
354         memset(rqstp->rq_argp, 0, procp->pc_argsize);
355         memset(rqstp->rq_resp, 0, procp->pc_ressize);
356
357         /* un-reserve some of the out-queue now that we have a 
358          * better idea of reply size
359          */
360         if (procp->pc_xdrressize)
361                 svc_reserve(rqstp, procp->pc_xdrressize<<2);
362
363         /* Call the function that processes the request. */
364         if (!versp->vs_dispatch) {
365                 /* Decode arguments */
366                 xdr = procp->pc_decode;
367                 if (xdr && !xdr(rqstp, argv->iov_base, rqstp->rq_argp))
368                         goto err_garbage;
369
370                 *statp = procp->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp);
371
372                 /* Encode reply */
373                 if (*statp == rpc_success && (xdr = procp->pc_encode)
374                  && !xdr(rqstp, resv->iov_base+resv->iov_len, rqstp->rq_resp)) {
375                         dprintk("svc: failed to encode reply\n");
376                         /* serv->sv_stats->rpcsystemerr++; */
377                         *statp = rpc_system_err;
378                 }
379         } else {
380                 dprintk("svc: calling dispatcher\n");
381                 if (!versp->vs_dispatch(rqstp, statp)) {
382                         /* Release reply info */
383                         if (procp->pc_release)
384                                 procp->pc_release(rqstp, NULL, rqstp->rq_resp);
385                         goto dropit;
386                 }
387         }
388
389         /* Check RPC status result */
390         if (*statp != rpc_success)
391                 resv->iov_len = ((void*)statp)  - resv->iov_base + 4;
392
393         /* Release reply info */
394         if (procp->pc_release)
395                 procp->pc_release(rqstp, NULL, rqstp->rq_resp);
396
397         if (procp->pc_encode == NULL)
398                 goto dropit;
399
400  sendit:
401         if (svc_authorise(rqstp))
402                 goto dropit;
403         return svc_send(rqstp);
404
405  dropit:
406         svc_authorise(rqstp);   /* doesn't hurt to call this twice */
407         dprintk("svc: svc_process dropit\n");
408         svc_drop(rqstp);
409         return 0;
410
411 err_short_len:
412 #ifdef RPC_PARANOIA
413         printk("svc: short len %Zd, dropping request\n", argv->iov_len);
414 #endif
415         goto dropit;                    /* drop request */
416
417 err_bad_dir:
418 #ifdef RPC_PARANOIA
419         printk("svc: bad direction %d, dropping request\n", dir);
420 #endif
421         serv->sv_stats->rpcbadfmt++;
422         goto dropit;                    /* drop request */
423
424 err_bad_rpc:
425         serv->sv_stats->rpcbadfmt++;
426         svc_putu32(resv, xdr_one);      /* REJECT */
427         svc_putu32(resv, xdr_zero);     /* RPC_MISMATCH */
428         svc_putu32(resv, xdr_two);      /* Only RPCv2 supported */
429         svc_putu32(resv, xdr_two);
430         goto sendit;
431
432 err_bad_auth:
433         dprintk("svc: authentication failed (%d)\n", ntohl(auth_stat));
434         serv->sv_stats->rpcbadauth++;
435         resv->iov_len -= 4;
436         svc_putu32(resv, xdr_one);      /* REJECT */
437         svc_putu32(resv, xdr_one);      /* AUTH_ERROR */
438         svc_putu32(resv, auth_stat);    /* status */
439         goto sendit;
440
441 err_bad_prog:
442 #ifdef RPC_PARANOIA
443         if (prog != 100227 || progp->pg_prog != 100003)
444                 printk("svc: unknown program %d (me %d)\n", prog, progp->pg_prog);
445         /* else it is just a Solaris client seeing if ACLs are supported */
446 #endif
447         serv->sv_stats->rpcbadfmt++;
448         svc_putu32(resv, rpc_prog_unavail);
449         goto sendit;
450
451 err_bad_vers:
452 #ifdef RPC_PARANOIA
453         printk("svc: unknown version (%d)\n", vers);
454 #endif
455         serv->sv_stats->rpcbadfmt++;
456         svc_putu32(resv, rpc_prog_mismatch);
457         svc_putu32(resv, htonl(progp->pg_lovers));
458         svc_putu32(resv, htonl(progp->pg_hivers));
459         goto sendit;
460
461 err_bad_proc:
462 #ifdef RPC_PARANOIA
463         printk("svc: unknown procedure (%d)\n", proc);
464 #endif
465         serv->sv_stats->rpcbadfmt++;
466         svc_putu32(resv, rpc_proc_unavail);
467         goto sendit;
468
469 err_garbage:
470 #ifdef RPC_PARANOIA
471         printk("svc: failed to decode args\n");
472 #endif
473         rpc_stat = rpc_garbage_args;
474 err_bad:
475         serv->sv_stats->rpcbadfmt++;
476         svc_putu32(resv, rpc_stat);
477         goto sendit;
478 }