Pull acpi_device_handle_cleanup into release branch
[deliverable/linux.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 = kmalloc(sizeof(*serv), GFP_KERNEL)))
36 return NULL;
37 memset(serv, 0, sizeof(*serv));
38 serv->sv_name = prog->pg_name;
39 serv->sv_program = prog;
40 serv->sv_nrthreads = 1;
41 serv->sv_stats = prog->pg_stats;
42 serv->sv_bufsz = bufsize? bufsize : 4096;
43 xdrsize = 0;
44 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 }
56 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
63 /* Remove any stale portmap registrations */
64 svc_register(serv, 0, 0);
65
66 return serv;
67 }
68
69 /*
70 * Destroy an RPC service
71 */
72 void
73 svc_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 */
113 static int
114 svc_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 BUG_ON(pages > RPCSVC_MAXPAGES);
126 while (pages) {
127 struct page *p = alloc_page(GFP_KERNEL);
128 if (!p)
129 break;
130 rqstp->rq_argpages[arghi++] = p;
131 pages--;
132 }
133 rqstp->rq_arghi = arghi;
134 return ! pages;
135 }
136
137 /*
138 * Release an RPC server buffer
139 */
140 static void
141 svc_release_buffer(struct svc_rqst *rqstp)
142 {
143 while (rqstp->rq_arghi)
144 put_page(rqstp->rq_argpages[--rqstp->rq_arghi]);
145 while (rqstp->rq_resused) {
146 if (rqstp->rq_respages[--rqstp->rq_resused] == NULL)
147 continue;
148 put_page(rqstp->rq_respages[rqstp->rq_resused]);
149 }
150 rqstp->rq_argused = 0;
151 }
152
153 /*
154 * Create a server thread
155 */
156 int
157 svc_create_thread(svc_thread_fn func, struct svc_serv *serv)
158 {
159 struct svc_rqst *rqstp;
160 int error = -ENOMEM;
161
162 rqstp = kmalloc(sizeof(*rqstp), GFP_KERNEL);
163 if (!rqstp)
164 goto out;
165
166 memset(rqstp, 0, sizeof(*rqstp));
167 init_waitqueue_head(&rqstp->rq_wait);
168
169 if (!(rqstp->rq_argp = kmalloc(serv->sv_xdrsize, GFP_KERNEL))
170 || !(rqstp->rq_resp = kmalloc(serv->sv_xdrsize, GFP_KERNEL))
171 || !svc_init_buffer(rqstp, serv->sv_bufsz))
172 goto out_thread;
173
174 serv->sv_nrthreads++;
175 rqstp->rq_server = serv;
176 error = kernel_thread((int (*)(void *)) func, rqstp, 0);
177 if (error < 0)
178 goto out_thread;
179 svc_sock_update_bufs(serv);
180 error = 0;
181 out:
182 return error;
183
184 out_thread:
185 svc_exit_thread(rqstp);
186 goto out;
187 }
188
189 /*
190 * Destroy an RPC server thread
191 */
192 void
193 svc_exit_thread(struct svc_rqst *rqstp)
194 {
195 struct svc_serv *serv = rqstp->rq_server;
196
197 svc_release_buffer(rqstp);
198 kfree(rqstp->rq_resp);
199 kfree(rqstp->rq_argp);
200 kfree(rqstp->rq_auth_data);
201 kfree(rqstp);
202
203 /* Release the server */
204 if (serv)
205 svc_destroy(serv);
206 }
207
208 /*
209 * Register an RPC service with the local portmapper.
210 * To unregister a service, call this routine with
211 * proto and port == 0.
212 */
213 int
214 svc_register(struct svc_serv *serv, int proto, unsigned short port)
215 {
216 struct svc_program *progp;
217 unsigned long flags;
218 int i, error = 0, dummy;
219
220 progp = serv->sv_program;
221
222 dprintk("RPC: svc_register(%s, %s, %d)\n",
223 progp->pg_name, proto == IPPROTO_UDP? "udp" : "tcp", port);
224
225 if (!port)
226 clear_thread_flag(TIF_SIGPENDING);
227
228 for (i = 0; i < progp->pg_nvers; i++) {
229 if (progp->pg_vers[i] == NULL)
230 continue;
231 error = rpc_register(progp->pg_prog, i, proto, port, &dummy);
232 if (error < 0)
233 break;
234 if (port && !dummy) {
235 error = -EACCES;
236 break;
237 }
238 }
239
240 if (!port) {
241 spin_lock_irqsave(&current->sighand->siglock, flags);
242 recalc_sigpending();
243 spin_unlock_irqrestore(&current->sighand->siglock, flags);
244 }
245
246 return error;
247 }
248
249 /*
250 * Process the RPC request.
251 */
252 int
253 svc_process(struct svc_serv *serv, struct svc_rqst *rqstp)
254 {
255 struct svc_program *progp;
256 struct svc_version *versp = NULL; /* compiler food */
257 struct svc_procedure *procp = NULL;
258 struct kvec * argv = &rqstp->rq_arg.head[0];
259 struct kvec * resv = &rqstp->rq_res.head[0];
260 kxdrproc_t xdr;
261 u32 *statp;
262 u32 dir, prog, vers, proc,
263 auth_stat, rpc_stat;
264 int auth_res;
265 u32 *accept_statp;
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.buflen = PAGE_SIZE;
283 rqstp->rq_res.tail[0].iov_base = NULL;
284 rqstp->rq_res.tail[0].iov_len = 0;
285 /* Will be turned off only in gss privacy case: */
286 rqstp->rq_sendfile_ok = 1;
287 /* tcp needs a space for the record length... */
288 if (rqstp->rq_prot == IPPROTO_TCP)
289 svc_putu32(resv, 0);
290
291 rqstp->rq_xid = svc_getu32(argv);
292 svc_putu32(resv, rqstp->rq_xid);
293
294 dir = ntohl(svc_getu32(argv));
295 vers = ntohl(svc_getu32(argv));
296
297 /* First words of reply: */
298 svc_putu32(resv, xdr_one); /* REPLY */
299
300 if (dir != 0) /* direction != CALL */
301 goto err_bad_dir;
302 if (vers != 2) /* RPC version number */
303 goto err_bad_rpc;
304
305 /* Save position in case we later decide to reject: */
306 accept_statp = resv->iov_base + resv->iov_len;
307
308 svc_putu32(resv, xdr_zero); /* ACCEPT */
309
310 rqstp->rq_prog = prog = ntohl(svc_getu32(argv)); /* program number */
311 rqstp->rq_vers = vers = ntohl(svc_getu32(argv)); /* version number */
312 rqstp->rq_proc = proc = ntohl(svc_getu32(argv)); /* procedure number */
313
314 progp = serv->sv_program;
315
316 for (progp = serv->sv_program; progp; progp = progp->pg_next)
317 if (prog == progp->pg_prog)
318 break;
319
320 /*
321 * Decode auth data, and add verifier to reply buffer.
322 * We do this before anything else in order to get a decent
323 * auth verifier.
324 */
325 auth_res = svc_authenticate(rqstp, &auth_stat);
326 /* Also give the program a chance to reject this call: */
327 if (auth_res == SVC_OK && progp) {
328 auth_stat = rpc_autherr_badcred;
329 auth_res = progp->pg_authenticate(rqstp);
330 }
331 switch (auth_res) {
332 case SVC_OK:
333 break;
334 case SVC_GARBAGE:
335 rpc_stat = rpc_garbage_args;
336 goto err_bad;
337 case SVC_SYSERR:
338 rpc_stat = rpc_system_err;
339 goto err_bad;
340 case SVC_DENIED:
341 goto err_bad_auth;
342 case SVC_DROP:
343 goto dropit;
344 case SVC_COMPLETE:
345 goto sendit;
346 }
347
348 if (progp == NULL)
349 goto err_bad_prog;
350
351 if (vers >= progp->pg_nvers ||
352 !(versp = progp->pg_vers[vers]))
353 goto err_bad_vers;
354
355 procp = versp->vs_proc + proc;
356 if (proc >= versp->vs_nproc || !procp->pc_func)
357 goto err_bad_proc;
358 rqstp->rq_server = serv;
359 rqstp->rq_procinfo = procp;
360
361 /* Syntactic check complete */
362 serv->sv_stats->rpccnt++;
363
364 /* Build the reply header. */
365 statp = resv->iov_base +resv->iov_len;
366 svc_putu32(resv, rpc_success); /* RPC_SUCCESS */
367
368 /* Bump per-procedure stats counter */
369 procp->pc_count++;
370
371 /* Initialize storage for argp and resp */
372 memset(rqstp->rq_argp, 0, procp->pc_argsize);
373 memset(rqstp->rq_resp, 0, procp->pc_ressize);
374
375 /* un-reserve some of the out-queue now that we have a
376 * better idea of reply size
377 */
378 if (procp->pc_xdrressize)
379 svc_reserve(rqstp, procp->pc_xdrressize<<2);
380
381 /* Call the function that processes the request. */
382 if (!versp->vs_dispatch) {
383 /* Decode arguments */
384 xdr = procp->pc_decode;
385 if (xdr && !xdr(rqstp, argv->iov_base, rqstp->rq_argp))
386 goto err_garbage;
387
388 *statp = procp->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp);
389
390 /* Encode reply */
391 if (*statp == rpc_success && (xdr = procp->pc_encode)
392 && !xdr(rqstp, resv->iov_base+resv->iov_len, rqstp->rq_resp)) {
393 dprintk("svc: failed to encode reply\n");
394 /* serv->sv_stats->rpcsystemerr++; */
395 *statp = rpc_system_err;
396 }
397 } else {
398 dprintk("svc: calling dispatcher\n");
399 if (!versp->vs_dispatch(rqstp, statp)) {
400 /* Release reply info */
401 if (procp->pc_release)
402 procp->pc_release(rqstp, NULL, rqstp->rq_resp);
403 goto dropit;
404 }
405 }
406
407 /* Check RPC status result */
408 if (*statp != rpc_success)
409 resv->iov_len = ((void*)statp) - resv->iov_base + 4;
410
411 /* Release reply info */
412 if (procp->pc_release)
413 procp->pc_release(rqstp, NULL, rqstp->rq_resp);
414
415 if (procp->pc_encode == NULL)
416 goto dropit;
417
418 sendit:
419 if (svc_authorise(rqstp))
420 goto dropit;
421 return svc_send(rqstp);
422
423 dropit:
424 svc_authorise(rqstp); /* doesn't hurt to call this twice */
425 dprintk("svc: svc_process dropit\n");
426 svc_drop(rqstp);
427 return 0;
428
429 err_short_len:
430 #ifdef RPC_PARANOIA
431 printk("svc: short len %Zd, dropping request\n", argv->iov_len);
432 #endif
433 goto dropit; /* drop request */
434
435 err_bad_dir:
436 #ifdef RPC_PARANOIA
437 printk("svc: bad direction %d, dropping request\n", dir);
438 #endif
439 serv->sv_stats->rpcbadfmt++;
440 goto dropit; /* drop request */
441
442 err_bad_rpc:
443 serv->sv_stats->rpcbadfmt++;
444 svc_putu32(resv, xdr_one); /* REJECT */
445 svc_putu32(resv, xdr_zero); /* RPC_MISMATCH */
446 svc_putu32(resv, xdr_two); /* Only RPCv2 supported */
447 svc_putu32(resv, xdr_two);
448 goto sendit;
449
450 err_bad_auth:
451 dprintk("svc: authentication failed (%d)\n", ntohl(auth_stat));
452 serv->sv_stats->rpcbadauth++;
453 /* Restore write pointer to location of accept status: */
454 xdr_ressize_check(rqstp, accept_statp);
455 svc_putu32(resv, xdr_one); /* REJECT */
456 svc_putu32(resv, xdr_one); /* AUTH_ERROR */
457 svc_putu32(resv, auth_stat); /* status */
458 goto sendit;
459
460 err_bad_prog:
461 dprintk("svc: unknown program %d\n", prog);
462 serv->sv_stats->rpcbadfmt++;
463 svc_putu32(resv, rpc_prog_unavail);
464 goto sendit;
465
466 err_bad_vers:
467 #ifdef RPC_PARANOIA
468 printk("svc: unknown version (%d)\n", vers);
469 #endif
470 serv->sv_stats->rpcbadfmt++;
471 svc_putu32(resv, rpc_prog_mismatch);
472 svc_putu32(resv, htonl(progp->pg_lovers));
473 svc_putu32(resv, htonl(progp->pg_hivers));
474 goto sendit;
475
476 err_bad_proc:
477 #ifdef RPC_PARANOIA
478 printk("svc: unknown procedure (%d)\n", proc);
479 #endif
480 serv->sv_stats->rpcbadfmt++;
481 svc_putu32(resv, rpc_proc_unavail);
482 goto sendit;
483
484 err_garbage:
485 #ifdef RPC_PARANOIA
486 printk("svc: failed to decode args\n");
487 #endif
488 rpc_stat = rpc_garbage_args;
489 err_bad:
490 serv->sv_stats->rpcbadfmt++;
491 svc_putu32(resv, rpc_stat);
492 goto sendit;
493 }
This page took 0.057411 seconds and 6 git commands to generate.