diff options
author | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
commit | 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch) | |
tree | 0bba044c4ce775e45a88a51686b5d9f90697ea9d /net/sunrpc/svc.c |
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history,
even though we have it. We can create a separate "historical" git
archive of that later if we want to, and in the meantime it's about
3.2GB when imported into git - space that would just make the early
git days unnecessarily complicated, when we don't have a lot of good
infrastructure for it.
Let it rip!
Diffstat (limited to 'net/sunrpc/svc.c')
-rw-r--r-- | net/sunrpc/svc.c | 490 |
1 files changed, 490 insertions, 0 deletions
diff --git a/net/sunrpc/svc.c b/net/sunrpc/svc.c new file mode 100644 index 000000000000..bb2d99f33315 --- /dev/null +++ b/net/sunrpc/svc.c | |||
@@ -0,0 +1,490 @@ | |||
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(¤t->sighand->siglock, flags); | ||
244 | recalc_sigpending(); | ||
245 | spin_unlock_irqrestore(¤t->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 kvec * argv = &rqstp->rq_arg.head[0]; | ||
261 | struct kvec * 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 | int auth_res; | ||
267 | u32 *accept_statp; | ||
268 | |||
269 | rpc_stat = rpc_success; | ||
270 | |||
271 | if (argv->iov_len < 6*4) | ||
272 | goto err_short_len; | ||
273 | |||
274 | /* setup response xdr_buf. | ||
275 | * Initially it has just one page | ||
276 | */ | ||
277 | svc_take_page(rqstp); /* must succeed */ | ||
278 | resv->iov_base = page_address(rqstp->rq_respages[0]); | ||
279 | resv->iov_len = 0; | ||
280 | rqstp->rq_res.pages = rqstp->rq_respages+1; | ||
281 | rqstp->rq_res.len = 0; | ||
282 | rqstp->rq_res.page_base = 0; | ||
283 | rqstp->rq_res.page_len = 0; | ||
284 | rqstp->rq_res.tail[0].iov_len = 0; | ||
285 | /* 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; | ||
313 | /* | ||
314 | * Decode auth data, and add verifier to reply buffer. | ||
315 | * We do this before anything else in order to get a decent | ||
316 | * auth verifier. | ||
317 | */ | ||
318 | auth_res = svc_authenticate(rqstp, &auth_stat); | ||
319 | /* Also give the program a chance to reject this call: */ | ||
320 | if (auth_res == SVC_OK) { | ||
321 | auth_stat = rpc_autherr_badcred; | ||
322 | auth_res = progp->pg_authenticate(rqstp); | ||
323 | } | ||
324 | switch (auth_res) { | ||
325 | case SVC_OK: | ||
326 | break; | ||
327 | case SVC_GARBAGE: | ||
328 | rpc_stat = rpc_garbage_args; | ||
329 | goto err_bad; | ||
330 | case SVC_SYSERR: | ||
331 | rpc_stat = rpc_system_err; | ||
332 | goto err_bad; | ||
333 | case SVC_DENIED: | ||
334 | goto err_bad_auth; | ||
335 | case SVC_DROP: | ||
336 | goto dropit; | ||
337 | case SVC_COMPLETE: | ||
338 | goto sendit; | ||
339 | } | ||
340 | |||
341 | if (prog != progp->pg_prog) | ||
342 | goto err_bad_prog; | ||
343 | |||
344 | if (vers >= progp->pg_nvers || | ||
345 | !(versp = progp->pg_vers[vers])) | ||
346 | goto err_bad_vers; | ||
347 | |||
348 | procp = versp->vs_proc + proc; | ||
349 | if (proc >= versp->vs_nproc || !procp->pc_func) | ||
350 | goto err_bad_proc; | ||
351 | rqstp->rq_server = serv; | ||
352 | rqstp->rq_procinfo = procp; | ||
353 | |||
354 | /* Syntactic check complete */ | ||
355 | serv->sv_stats->rpccnt++; | ||
356 | |||
357 | /* Build the reply header. */ | ||
358 | statp = resv->iov_base +resv->iov_len; | ||
359 | svc_putu32(resv, rpc_success); /* RPC_SUCCESS */ | ||
360 | |||
361 | /* Bump per-procedure stats counter */ | ||
362 | procp->pc_count++; | ||
363 | |||
364 | /* Initialize storage for argp and resp */ | ||
365 | memset(rqstp->rq_argp, 0, procp->pc_argsize); | ||
366 | memset(rqstp->rq_resp, 0, procp->pc_ressize); | ||
367 | |||
368 | /* un-reserve some of the out-queue now that we have a | ||
369 | * better idea of reply size | ||
370 | */ | ||
371 | if (procp->pc_xdrressize) | ||
372 | svc_reserve(rqstp, procp->pc_xdrressize<<2); | ||
373 | |||
374 | /* Call the function that processes the request. */ | ||
375 | if (!versp->vs_dispatch) { | ||
376 | /* Decode arguments */ | ||
377 | xdr = procp->pc_decode; | ||
378 | if (xdr && !xdr(rqstp, argv->iov_base, rqstp->rq_argp)) | ||
379 | goto err_garbage; | ||
380 | |||
381 | *statp = procp->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp); | ||
382 | |||
383 | /* Encode reply */ | ||
384 | if (*statp == rpc_success && (xdr = procp->pc_encode) | ||
385 | && !xdr(rqstp, resv->iov_base+resv->iov_len, rqstp->rq_resp)) { | ||
386 | dprintk("svc: failed to encode reply\n"); | ||
387 | /* serv->sv_stats->rpcsystemerr++; */ | ||
388 | *statp = rpc_system_err; | ||
389 | } | ||
390 | } else { | ||
391 | dprintk("svc: calling dispatcher\n"); | ||
392 | if (!versp->vs_dispatch(rqstp, statp)) { | ||
393 | /* Release reply info */ | ||
394 | if (procp->pc_release) | ||
395 | procp->pc_release(rqstp, NULL, rqstp->rq_resp); | ||
396 | goto dropit; | ||
397 | } | ||
398 | } | ||
399 | |||
400 | /* Check RPC status result */ | ||
401 | if (*statp != rpc_success) | ||
402 | resv->iov_len = ((void*)statp) - resv->iov_base + 4; | ||
403 | |||
404 | /* Release reply info */ | ||
405 | if (procp->pc_release) | ||
406 | procp->pc_release(rqstp, NULL, rqstp->rq_resp); | ||
407 | |||
408 | if (procp->pc_encode == NULL) | ||
409 | goto dropit; | ||
410 | |||
411 | sendit: | ||
412 | if (svc_authorise(rqstp)) | ||
413 | goto dropit; | ||
414 | return svc_send(rqstp); | ||
415 | |||
416 | dropit: | ||
417 | svc_authorise(rqstp); /* doesn't hurt to call this twice */ | ||
418 | dprintk("svc: svc_process dropit\n"); | ||
419 | svc_drop(rqstp); | ||
420 | return 0; | ||
421 | |||
422 | err_short_len: | ||
423 | #ifdef RPC_PARANOIA | ||
424 | printk("svc: short len %Zd, dropping request\n", argv->iov_len); | ||
425 | #endif | ||
426 | goto dropit; /* drop request */ | ||
427 | |||
428 | err_bad_dir: | ||
429 | #ifdef RPC_PARANOIA | ||
430 | printk("svc: bad direction %d, dropping request\n", dir); | ||
431 | #endif | ||
432 | serv->sv_stats->rpcbadfmt++; | ||
433 | goto dropit; /* drop request */ | ||
434 | |||
435 | err_bad_rpc: | ||
436 | serv->sv_stats->rpcbadfmt++; | ||
437 | svc_putu32(resv, xdr_one); /* REJECT */ | ||
438 | svc_putu32(resv, xdr_zero); /* RPC_MISMATCH */ | ||
439 | svc_putu32(resv, xdr_two); /* Only RPCv2 supported */ | ||
440 | svc_putu32(resv, xdr_two); | ||
441 | goto sendit; | ||
442 | |||
443 | err_bad_auth: | ||
444 | dprintk("svc: authentication failed (%d)\n", ntohl(auth_stat)); | ||
445 | serv->sv_stats->rpcbadauth++; | ||
446 | /* Restore write pointer to location of accept status: */ | ||
447 | xdr_ressize_check(rqstp, accept_statp); | ||
448 | svc_putu32(resv, xdr_one); /* REJECT */ | ||
449 | svc_putu32(resv, xdr_one); /* AUTH_ERROR */ | ||
450 | svc_putu32(resv, auth_stat); /* status */ | ||
451 | goto sendit; | ||
452 | |||
453 | err_bad_prog: | ||
454 | #ifdef RPC_PARANOIA | ||
455 | if (prog != 100227 || progp->pg_prog != 100003) | ||
456 | printk("svc: unknown program %d (me %d)\n", prog, progp->pg_prog); | ||
457 | /* else it is just a Solaris client seeing if ACLs are supported */ | ||
458 | #endif | ||
459 | serv->sv_stats->rpcbadfmt++; | ||
460 | svc_putu32(resv, rpc_prog_unavail); | ||
461 | goto sendit; | ||
462 | |||
463 | err_bad_vers: | ||
464 | #ifdef RPC_PARANOIA | ||
465 | printk("svc: unknown version (%d)\n", vers); | ||
466 | #endif | ||
467 | serv->sv_stats->rpcbadfmt++; | ||
468 | svc_putu32(resv, rpc_prog_mismatch); | ||
469 | svc_putu32(resv, htonl(progp->pg_lovers)); | ||
470 | svc_putu32(resv, htonl(progp->pg_hivers)); | ||
471 | goto sendit; | ||
472 | |||
473 | err_bad_proc: | ||
474 | #ifdef RPC_PARANOIA | ||
475 | printk("svc: unknown procedure (%d)\n", proc); | ||
476 | #endif | ||
477 | serv->sv_stats->rpcbadfmt++; | ||
478 | svc_putu32(resv, rpc_proc_unavail); | ||
479 | goto sendit; | ||
480 | |||
481 | err_garbage: | ||
482 | #ifdef RPC_PARANOIA | ||
483 | printk("svc: failed to decode args\n"); | ||
484 | #endif | ||
485 | rpc_stat = rpc_garbage_args; | ||
486 | err_bad: | ||
487 | serv->sv_stats->rpcbadfmt++; | ||
488 | svc_putu32(resv, rpc_stat); | ||
489 | goto sendit; | ||
490 | } | ||