2 * linux/net/sunrpc/svc.c
4 * High-level RPC service routines
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
9 #include <linux/linkage.h>
10 #include <linux/sched.h>
11 #include <linux/errno.h>
12 #include <linux/net.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
29 svc_create(struct svc_program *prog, unsigned int bufsize)
31 struct svc_serv *serv;
35 if (!(serv = kmalloc(sizeof(*serv), GFP_KERNEL)))
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;
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;
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);
63 /* Remove any stale portmap registrations */
64 svc_register(serv, 0, 0);
70 * Destroy an RPC service
73 svc_destroy(struct svc_serv *serv)
75 struct svc_sock *svsk;
77 dprintk("RPC: svc_destroy(%s, %d)\n",
78 serv->sv_program->pg_name,
81 if (serv->sv_nrthreads) {
82 if (--(serv->sv_nrthreads) != 0) {
83 svc_sock_update_bufs(serv);
87 printk("svc_destroy: no threads for serv=%p!\n", serv);
89 while (!list_empty(&serv->sv_tempsocks)) {
90 svsk = list_entry(serv->sv_tempsocks.next,
93 svc_delete_socket(svsk);
95 while (!list_empty(&serv->sv_permsocks)) {
96 svsk = list_entry(serv->sv_permsocks.next,
99 svc_delete_socket(svsk);
102 cache_clean_deferred(serv);
104 /* Unregister service with the portmapper */
105 svc_register(serv, 0, 0);
110 * Allocate an RPC server's buffer space.
111 * We allocate pages and place them in rq_argpages.
114 svc_init_buffer(struct svc_rqst *rqstp, unsigned int size)
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;
125 BUG_ON(pages > RPCSVC_MAXPAGES);
127 struct page *p = alloc_page(GFP_KERNEL);
130 rqstp->rq_argpages[arghi++] = p;
133 rqstp->rq_arghi = arghi;
138 * Release an RPC server buffer
141 svc_release_buffer(struct svc_rqst *rqstp)
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)
148 put_page(rqstp->rq_respages[rqstp->rq_resused]);
150 rqstp->rq_argused = 0;
154 * Create a server thread
157 svc_create_thread(svc_thread_fn func, struct svc_serv *serv)
159 struct svc_rqst *rqstp;
162 rqstp = kmalloc(sizeof(*rqstp), GFP_KERNEL);
166 memset(rqstp, 0, sizeof(*rqstp));
167 init_waitqueue_head(&rqstp->rq_wait);
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))
174 serv->sv_nrthreads++;
175 rqstp->rq_server = serv;
176 error = kernel_thread((int (*)(void *)) func, rqstp, 0);
179 svc_sock_update_bufs(serv);
185 svc_exit_thread(rqstp);
190 * Destroy an RPC server thread
193 svc_exit_thread(struct svc_rqst *rqstp)
195 struct svc_serv *serv = rqstp->rq_server;
197 svc_release_buffer(rqstp);
198 kfree(rqstp->rq_resp);
199 kfree(rqstp->rq_argp);
200 kfree(rqstp->rq_auth_data);
203 /* Release the server */
209 * Register an RPC service with the local portmapper.
210 * To unregister a service, call this routine with
211 * proto and port == 0.
214 svc_register(struct svc_serv *serv, int proto, unsigned short port)
216 struct svc_program *progp;
218 int i, error = 0, dummy;
220 progp = serv->sv_program;
222 dprintk("RPC: svc_register(%s, %s, %d)\n",
223 progp->pg_name, proto == IPPROTO_UDP? "udp" : "tcp", port);
226 clear_thread_flag(TIF_SIGPENDING);
228 for (i = 0; i < progp->pg_nvers; i++) {
229 if (progp->pg_vers[i] == NULL)
231 error = rpc_register(progp->pg_prog, i, proto, port, &dummy);
234 if (port && !dummy) {
241 spin_lock_irqsave(¤t->sighand->siglock, flags);
243 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
250 * Process the RPC request.
253 svc_process(struct svc_serv *serv, struct svc_rqst *rqstp)
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];
262 u32 dir, prog, vers, proc,
267 rpc_stat = rpc_success;
269 if (argv->iov_len < 6*4)
272 /* setup response xdr_buf.
273 * Initially it has just one page
275 svc_take_page(rqstp); /* must succeed */
276 resv->iov_base = page_address(rqstp->rq_respages[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_len = 0;
284 /* tcp needs a space for the record length... */
285 if (rqstp->rq_prot == IPPROTO_TCP)
288 rqstp->rq_xid = svc_getu32(argv);
289 svc_putu32(resv, rqstp->rq_xid);
291 dir = ntohl(svc_getu32(argv));
292 vers = ntohl(svc_getu32(argv));
294 /* First words of reply: */
295 svc_putu32(resv, xdr_one); /* REPLY */
297 if (dir != 0) /* direction != CALL */
299 if (vers != 2) /* RPC version number */
302 /* Save position in case we later decide to reject: */
303 accept_statp = resv->iov_base + resv->iov_len;
305 svc_putu32(resv, xdr_zero); /* ACCEPT */
307 rqstp->rq_prog = prog = ntohl(svc_getu32(argv)); /* program number */
308 rqstp->rq_vers = vers = ntohl(svc_getu32(argv)); /* version number */
309 rqstp->rq_proc = proc = ntohl(svc_getu32(argv)); /* procedure number */
311 progp = serv->sv_program;
313 for (progp = serv->sv_program; progp; progp = progp->pg_next)
314 if (prog == progp->pg_prog)
318 * Decode auth data, and add verifier to reply buffer.
319 * We do this before anything else in order to get a decent
322 auth_res = svc_authenticate(rqstp, &auth_stat);
323 /* Also give the program a chance to reject this call: */
324 if (auth_res == SVC_OK && progp) {
325 auth_stat = rpc_autherr_badcred;
326 auth_res = progp->pg_authenticate(rqstp);
332 rpc_stat = rpc_garbage_args;
335 rpc_stat = rpc_system_err;
348 if (vers >= progp->pg_nvers ||
349 !(versp = progp->pg_vers[vers]))
352 procp = versp->vs_proc + proc;
353 if (proc >= versp->vs_nproc || !procp->pc_func)
355 rqstp->rq_server = serv;
356 rqstp->rq_procinfo = procp;
358 /* Syntactic check complete */
359 serv->sv_stats->rpccnt++;
361 /* Build the reply header. */
362 statp = resv->iov_base +resv->iov_len;
363 svc_putu32(resv, rpc_success); /* RPC_SUCCESS */
365 /* Bump per-procedure stats counter */
368 /* Initialize storage for argp and resp */
369 memset(rqstp->rq_argp, 0, procp->pc_argsize);
370 memset(rqstp->rq_resp, 0, procp->pc_ressize);
372 /* un-reserve some of the out-queue now that we have a
373 * better idea of reply size
375 if (procp->pc_xdrressize)
376 svc_reserve(rqstp, procp->pc_xdrressize<<2);
378 /* Call the function that processes the request. */
379 if (!versp->vs_dispatch) {
380 /* Decode arguments */
381 xdr = procp->pc_decode;
382 if (xdr && !xdr(rqstp, argv->iov_base, rqstp->rq_argp))
385 *statp = procp->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp);
388 if (*statp == rpc_success && (xdr = procp->pc_encode)
389 && !xdr(rqstp, resv->iov_base+resv->iov_len, rqstp->rq_resp)) {
390 dprintk("svc: failed to encode reply\n");
391 /* serv->sv_stats->rpcsystemerr++; */
392 *statp = rpc_system_err;
395 dprintk("svc: calling dispatcher\n");
396 if (!versp->vs_dispatch(rqstp, statp)) {
397 /* Release reply info */
398 if (procp->pc_release)
399 procp->pc_release(rqstp, NULL, rqstp->rq_resp);
404 /* Check RPC status result */
405 if (*statp != rpc_success)
406 resv->iov_len = ((void*)statp) - resv->iov_base + 4;
408 /* Release reply info */
409 if (procp->pc_release)
410 procp->pc_release(rqstp, NULL, rqstp->rq_resp);
412 if (procp->pc_encode == NULL)
416 if (svc_authorise(rqstp))
418 return svc_send(rqstp);
421 svc_authorise(rqstp); /* doesn't hurt to call this twice */
422 dprintk("svc: svc_process dropit\n");
428 printk("svc: short len %Zd, dropping request\n", argv->iov_len);
430 goto dropit; /* drop request */
434 printk("svc: bad direction %d, dropping request\n", dir);
436 serv->sv_stats->rpcbadfmt++;
437 goto dropit; /* drop request */
440 serv->sv_stats->rpcbadfmt++;
441 svc_putu32(resv, xdr_one); /* REJECT */
442 svc_putu32(resv, xdr_zero); /* RPC_MISMATCH */
443 svc_putu32(resv, xdr_two); /* Only RPCv2 supported */
444 svc_putu32(resv, xdr_two);
448 dprintk("svc: authentication failed (%d)\n", ntohl(auth_stat));
449 serv->sv_stats->rpcbadauth++;
450 /* Restore write pointer to location of accept status: */
451 xdr_ressize_check(rqstp, accept_statp);
452 svc_putu32(resv, xdr_one); /* REJECT */
453 svc_putu32(resv, xdr_one); /* AUTH_ERROR */
454 svc_putu32(resv, auth_stat); /* status */
458 dprintk("svc: unknown program %d\n", prog);
459 serv->sv_stats->rpcbadfmt++;
460 svc_putu32(resv, rpc_prog_unavail);
465 printk("svc: unknown version (%d)\n", vers);
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));
475 printk("svc: unknown procedure (%d)\n", proc);
477 serv->sv_stats->rpcbadfmt++;
478 svc_putu32(resv, rpc_proc_unavail);
483 printk("svc: failed to decode args\n");
485 rpc_stat = rpc_garbage_args;
487 serv->sv_stats->rpcbadfmt++;
488 svc_putu32(resv, rpc_stat);