[PATCH] uml: workaround old problematic sed behaviour
[linux-2.6/linux-loongson.git] / net / sunrpc / svc.c
blobbb2d99f33315f3565e937c1c42eb5bf44a93d170
1 /*
2 * linux/net/sunrpc/svc.c
4 * High-level RPC service routines
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7 */
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>
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>
22 #define RPCDBG_FACILITY RPCDBG_SVCDSP
23 #define RPC_PARANOIA 1
26 * Create an RPC service
28 struct svc_serv *
29 svc_create(struct svc_program *prog, unsigned int bufsize)
31 struct svc_serv *serv;
32 int vers;
33 unsigned int xdrsize;
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;
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);
59 serv->sv_name = prog->pg_name;
61 /* Remove any stale portmap registrations */
62 svc_register(serv, 0, 0);
64 return serv;
68 * Destroy an RPC service
70 void
71 svc_destroy(struct svc_serv *serv)
73 struct svc_sock *svsk;
75 dprintk("RPC: svc_destroy(%s, %d)\n",
76 serv->sv_program->pg_name,
77 serv->sv_nrthreads);
79 if (serv->sv_nrthreads) {
80 if (--(serv->sv_nrthreads) != 0) {
81 svc_sock_update_bufs(serv);
82 return;
84 } else
85 printk("svc_destroy: no threads for serv=%p!\n", serv);
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);
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);
100 cache_clean_deferred(serv);
102 /* Unregister service with the portmapper */
103 svc_register(serv, 0, 0);
104 kfree(serv);
108 * Allocate an RPC server's buffer space.
109 * We allocate pages and place them in rq_argpages.
111 static int
112 svc_init_buffer(struct svc_rqst *rqstp, unsigned int size)
114 int pages;
115 int arghi;
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--;
132 rqstp->rq_arghi = arghi;
133 return ! pages;
137 * Release an RPC server buffer
139 static void
140 svc_release_buffer(struct svc_rqst *rqstp)
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]);
149 rqstp->rq_argused = 0;
153 * Create a server thread
156 svc_create_thread(svc_thread_fn func, struct svc_serv *serv)
158 struct svc_rqst *rqstp;
159 int error = -ENOMEM;
161 rqstp = kmalloc(sizeof(*rqstp), GFP_KERNEL);
162 if (!rqstp)
163 goto out;
165 memset(rqstp, 0, sizeof(*rqstp));
166 init_waitqueue_head(&rqstp->rq_wait);
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;
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;
183 out_thread:
184 svc_exit_thread(rqstp);
185 goto out;
189 * Destroy an RPC server thread
191 void
192 svc_exit_thread(struct svc_rqst *rqstp)
194 struct svc_serv *serv = rqstp->rq_server;
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);
205 /* Release the server */
206 if (serv)
207 svc_destroy(serv);
211 * Register an RPC service with the local portmapper.
212 * To unregister a service, call this routine with
213 * proto and port == 0.
216 svc_register(struct svc_serv *serv, int proto, unsigned short port)
218 struct svc_program *progp;
219 unsigned long flags;
220 int i, error = 0, dummy;
222 progp = serv->sv_program;
224 dprintk("RPC: svc_register(%s, %s, %d)\n",
225 progp->pg_name, proto == IPPROTO_UDP? "udp" : "tcp", port);
227 if (!port)
228 clear_thread_flag(TIF_SIGPENDING);
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;
242 if (!port) {
243 spin_lock_irqsave(&current->sighand->siglock, flags);
244 recalc_sigpending();
245 spin_unlock_irqrestore(&current->sighand->siglock, flags);
248 return error;
252 * Process the RPC request.
255 svc_process(struct svc_serv *serv, struct svc_rqst *rqstp)
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;
269 rpc_stat = rpc_success;
271 if (argv->iov_len < 6*4)
272 goto err_short_len;
274 /* setup response xdr_buf.
275 * Initially it has just one page
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);
289 rqstp->rq_xid = svc_getu32(argv);
290 svc_putu32(resv, rqstp->rq_xid);
292 dir = ntohl(svc_getu32(argv));
293 vers = ntohl(svc_getu32(argv));
295 /* First words of reply: */
296 svc_putu32(resv, xdr_one); /* REPLY */
298 if (dir != 0) /* direction != CALL */
299 goto err_bad_dir;
300 if (vers != 2) /* RPC version number */
301 goto err_bad_rpc;
303 /* Save position in case we later decide to reject: */
304 accept_statp = resv->iov_base + resv->iov_len;
306 svc_putu32(resv, xdr_zero); /* ACCEPT */
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 */
312 progp = serv->sv_program;
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.
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);
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;
341 if (prog != progp->pg_prog)
342 goto err_bad_prog;
344 if (vers >= progp->pg_nvers ||
345 !(versp = progp->pg_vers[vers]))
346 goto err_bad_vers;
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;
354 /* Syntactic check complete */
355 serv->sv_stats->rpccnt++;
357 /* Build the reply header. */
358 statp = resv->iov_base +resv->iov_len;
359 svc_putu32(resv, rpc_success); /* RPC_SUCCESS */
361 /* Bump per-procedure stats counter */
362 procp->pc_count++;
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);
368 /* un-reserve some of the out-queue now that we have a
369 * better idea of reply size
371 if (procp->pc_xdrressize)
372 svc_reserve(rqstp, procp->pc_xdrressize<<2);
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;
381 *statp = procp->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp);
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;
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;
400 /* Check RPC status result */
401 if (*statp != rpc_success)
402 resv->iov_len = ((void*)statp) - resv->iov_base + 4;
404 /* Release reply info */
405 if (procp->pc_release)
406 procp->pc_release(rqstp, NULL, rqstp->rq_resp);
408 if (procp->pc_encode == NULL)
409 goto dropit;
411 sendit:
412 if (svc_authorise(rqstp))
413 goto dropit;
414 return svc_send(rqstp);
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;
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 */
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 */
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;
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;
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;
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;
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;
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;