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>
15 #include <linux/interrupt.h>
16 #include <linux/module.h>
18 #include <linux/sunrpc/types.h>
19 #include <linux/sunrpc/xdr.h>
20 #include <linux/sunrpc/stats.h>
21 #include <linux/sunrpc/svcsock.h>
22 #include <linux/sunrpc/clnt.h>
24 #define RPCDBG_FACILITY RPCDBG_SVCDSP
25 #define RPC_PARANOIA 1
28 * Create an RPC service
30 static struct svc_serv *
31 __svc_create(struct svc_program *prog, unsigned int bufsize, int npools,
32 void (*shutdown)(struct svc_serv *serv))
34 struct svc_serv *serv;
39 if (!(serv = kzalloc(sizeof(*serv), GFP_KERNEL)))
41 serv->sv_name = prog->pg_name;
42 serv->sv_program = prog;
43 serv->sv_nrthreads = 1;
44 serv->sv_stats = prog->pg_stats;
45 serv->sv_bufsz = bufsize? bufsize : 4096;
46 serv->sv_shutdown = shutdown;
49 prog->pg_lovers = prog->pg_nvers-1;
50 for (vers=0; vers<prog->pg_nvers ; vers++)
51 if (prog->pg_vers[vers]) {
52 prog->pg_hivers = vers;
53 if (prog->pg_lovers > vers)
54 prog->pg_lovers = vers;
55 if (prog->pg_vers[vers]->vs_xdrsize > xdrsize)
56 xdrsize = prog->pg_vers[vers]->vs_xdrsize;
60 serv->sv_xdrsize = xdrsize;
61 INIT_LIST_HEAD(&serv->sv_tempsocks);
62 INIT_LIST_HEAD(&serv->sv_permsocks);
63 init_timer(&serv->sv_temptimer);
64 spin_lock_init(&serv->sv_lock);
66 serv->sv_nrpools = npools;
68 kcalloc(sizeof(struct svc_pool), serv->sv_nrpools,
70 if (!serv->sv_pools) {
75 for (i = 0; i < serv->sv_nrpools; i++) {
76 struct svc_pool *pool = &serv->sv_pools[i];
78 dprintk("initialising pool %u for %s\n",
82 INIT_LIST_HEAD(&pool->sp_threads);
83 INIT_LIST_HEAD(&pool->sp_sockets);
84 INIT_LIST_HEAD(&pool->sp_all_threads);
85 spin_lock_init(&pool->sp_lock);
89 /* Remove any stale portmap registrations */
90 svc_register(serv, 0, 0);
96 svc_create(struct svc_program *prog, unsigned int bufsize,
97 void (*shutdown)(struct svc_serv *serv))
99 return __svc_create(prog, bufsize, /*npools*/1, shutdown);
103 svc_create_pooled(struct svc_program *prog, unsigned int bufsize,
104 void (*shutdown)(struct svc_serv *serv),
105 svc_thread_fn func, int sig, struct module *mod)
107 struct svc_serv *serv;
109 serv = __svc_create(prog, bufsize, /*npools*/1, shutdown);
112 serv->sv_function = func;
113 serv->sv_kill_signal = sig;
114 serv->sv_module = mod;
121 * Destroy an RPC service. Should be called with the BKL held
124 svc_destroy(struct svc_serv *serv)
126 struct svc_sock *svsk;
128 dprintk("RPC: svc_destroy(%s, %d)\n",
129 serv->sv_program->pg_name,
132 if (serv->sv_nrthreads) {
133 if (--(serv->sv_nrthreads) != 0) {
134 svc_sock_update_bufs(serv);
138 printk("svc_destroy: no threads for serv=%p!\n", serv);
140 del_timer_sync(&serv->sv_temptimer);
142 while (!list_empty(&serv->sv_tempsocks)) {
143 svsk = list_entry(serv->sv_tempsocks.next,
146 svc_delete_socket(svsk);
148 if (serv->sv_shutdown)
149 serv->sv_shutdown(serv);
151 while (!list_empty(&serv->sv_permsocks)) {
152 svsk = list_entry(serv->sv_permsocks.next,
155 svc_delete_socket(svsk);
158 cache_clean_deferred(serv);
160 /* Unregister service with the portmapper */
161 svc_register(serv, 0, 0);
162 kfree(serv->sv_pools);
167 * Allocate an RPC server's buffer space.
168 * We allocate pages and place them in rq_argpages.
171 svc_init_buffer(struct svc_rqst *rqstp, unsigned int size)
176 if (size > RPCSVC_MAXPAYLOAD)
177 size = RPCSVC_MAXPAYLOAD;
178 pages = 2 + (size+ PAGE_SIZE -1) / PAGE_SIZE;
179 rqstp->rq_argused = 0;
180 rqstp->rq_resused = 0;
182 BUG_ON(pages > RPCSVC_MAXPAGES);
184 struct page *p = alloc_page(GFP_KERNEL);
187 rqstp->rq_argpages[arghi++] = p;
190 rqstp->rq_arghi = arghi;
195 * Release an RPC server buffer
198 svc_release_buffer(struct svc_rqst *rqstp)
200 while (rqstp->rq_arghi)
201 put_page(rqstp->rq_argpages[--rqstp->rq_arghi]);
202 while (rqstp->rq_resused) {
203 if (rqstp->rq_respages[--rqstp->rq_resused] == NULL)
205 put_page(rqstp->rq_respages[rqstp->rq_resused]);
207 rqstp->rq_argused = 0;
211 * Create a thread in the given pool. Caller must hold BKL.
214 __svc_create_thread(svc_thread_fn func, struct svc_serv *serv,
215 struct svc_pool *pool)
217 struct svc_rqst *rqstp;
220 rqstp = kzalloc(sizeof(*rqstp), GFP_KERNEL);
224 init_waitqueue_head(&rqstp->rq_wait);
226 if (!(rqstp->rq_argp = kmalloc(serv->sv_xdrsize, GFP_KERNEL))
227 || !(rqstp->rq_resp = kmalloc(serv->sv_xdrsize, GFP_KERNEL))
228 || !svc_init_buffer(rqstp, serv->sv_bufsz))
231 serv->sv_nrthreads++;
232 spin_lock_bh(&pool->sp_lock);
233 pool->sp_nrthreads++;
234 list_add(&rqstp->rq_all, &pool->sp_all_threads);
235 spin_unlock_bh(&pool->sp_lock);
236 rqstp->rq_server = serv;
237 rqstp->rq_pool = pool;
238 error = kernel_thread((int (*)(void *)) func, rqstp, 0);
241 svc_sock_update_bufs(serv);
247 svc_exit_thread(rqstp);
252 * Create a thread in the default pool. Caller must hold BKL.
255 svc_create_thread(svc_thread_fn func, struct svc_serv *serv)
257 return __svc_create_thread(func, serv, &serv->sv_pools[0]);
261 * Choose a pool in which to create a new thread, for svc_set_num_threads
263 static inline struct svc_pool *
264 choose_pool(struct svc_serv *serv, struct svc_pool *pool, unsigned int *state)
269 return &serv->sv_pools[(*state)++ % serv->sv_nrpools];
273 * Choose a thread to kill, for svc_set_num_threads
275 static inline struct task_struct *
276 choose_victim(struct svc_serv *serv, struct svc_pool *pool, unsigned int *state)
279 struct task_struct *task = NULL;
282 spin_lock_bh(&pool->sp_lock);
284 /* choose a pool in round-robin fashion */
285 for (i = 0; i < serv->sv_nrpools; i++) {
286 pool = &serv->sv_pools[--(*state) % serv->sv_nrpools];
287 spin_lock_bh(&pool->sp_lock);
288 if (!list_empty(&pool->sp_all_threads))
290 spin_unlock_bh(&pool->sp_lock);
296 if (!list_empty(&pool->sp_all_threads)) {
297 struct svc_rqst *rqstp;
300 * Remove from the pool->sp_all_threads list
301 * so we don't try to kill it again.
303 rqstp = list_entry(pool->sp_all_threads.next, struct svc_rqst, rq_all);
304 list_del_init(&rqstp->rq_all);
305 task = rqstp->rq_task;
307 spin_unlock_bh(&pool->sp_lock);
313 * Create or destroy enough new threads to make the number
314 * of threads the given number. If `pool' is non-NULL, applies
315 * only to threads in that pool, otherwise round-robins between
316 * all pools. Must be called with a svc_get() reference and
319 * Destroying threads relies on the service threads filling in
320 * rqstp->rq_task, which only the nfs ones do. Assumes the serv
321 * has been created using svc_create_pooled().
323 * Based on code that used to be in nfsd_svc() but tweaked
327 svc_set_num_threads(struct svc_serv *serv, struct svc_pool *pool, int nrservs)
329 struct task_struct *victim;
331 unsigned int state = serv->sv_nrthreads-1;
334 /* The -1 assumes caller has done a svc_get() */
335 nrservs -= (serv->sv_nrthreads-1);
337 spin_lock_bh(&pool->sp_lock);
338 nrservs -= pool->sp_nrthreads;
339 spin_unlock_bh(&pool->sp_lock);
342 /* create new threads */
343 while (nrservs > 0) {
345 __module_get(serv->sv_module);
346 error = __svc_create_thread(serv->sv_function, serv,
347 choose_pool(serv, pool, &state));
349 module_put(serv->sv_module);
353 /* destroy old threads */
354 while (nrservs < 0 &&
355 (victim = choose_victim(serv, pool, &state)) != NULL) {
356 send_sig(serv->sv_kill_signal, victim, 1);
364 * Called from a server thread as it's exiting. Caller must hold BKL.
367 svc_exit_thread(struct svc_rqst *rqstp)
369 struct svc_serv *serv = rqstp->rq_server;
370 struct svc_pool *pool = rqstp->rq_pool;
372 svc_release_buffer(rqstp);
373 kfree(rqstp->rq_resp);
374 kfree(rqstp->rq_argp);
375 kfree(rqstp->rq_auth_data);
377 spin_lock_bh(&pool->sp_lock);
378 pool->sp_nrthreads--;
379 list_del(&rqstp->rq_all);
380 spin_unlock_bh(&pool->sp_lock);
384 /* Release the server */
390 * Register an RPC service with the local portmapper.
391 * To unregister a service, call this routine with
392 * proto and port == 0.
395 svc_register(struct svc_serv *serv, int proto, unsigned short port)
397 struct svc_program *progp;
399 int i, error = 0, dummy;
401 progp = serv->sv_program;
403 dprintk("RPC: svc_register(%s, %s, %d)\n",
404 progp->pg_name, proto == IPPROTO_UDP? "udp" : "tcp", port);
407 clear_thread_flag(TIF_SIGPENDING);
409 for (i = 0; i < progp->pg_nvers; i++) {
410 if (progp->pg_vers[i] == NULL)
412 error = rpc_register(progp->pg_prog, i, proto, port, &dummy);
415 if (port && !dummy) {
422 spin_lock_irqsave(¤t->sighand->siglock, flags);
424 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
431 * Process the RPC request.
434 svc_process(struct svc_rqst *rqstp)
436 struct svc_program *progp;
437 struct svc_version *versp = NULL; /* compiler food */
438 struct svc_procedure *procp = NULL;
439 struct kvec * argv = &rqstp->rq_arg.head[0];
440 struct kvec * resv = &rqstp->rq_res.head[0];
441 struct svc_serv *serv = rqstp->rq_server;
444 u32 dir, prog, vers, proc;
445 __be32 auth_stat, rpc_stat;
447 __be32 *accept_statp;
449 rpc_stat = rpc_success;
451 if (argv->iov_len < 6*4)
454 /* setup response xdr_buf.
455 * Initially it has just one page
457 svc_take_page(rqstp); /* must succeed */
458 resv->iov_base = page_address(rqstp->rq_respages[0]);
460 rqstp->rq_res.pages = rqstp->rq_respages+1;
461 rqstp->rq_res.len = 0;
462 rqstp->rq_res.page_base = 0;
463 rqstp->rq_res.page_len = 0;
464 rqstp->rq_res.buflen = PAGE_SIZE;
465 rqstp->rq_res.tail[0].iov_base = NULL;
466 rqstp->rq_res.tail[0].iov_len = 0;
467 /* Will be turned off only in gss privacy case: */
468 rqstp->rq_sendfile_ok = 1;
469 /* tcp needs a space for the record length... */
470 if (rqstp->rq_prot == IPPROTO_TCP)
473 rqstp->rq_xid = svc_getu32(argv);
474 svc_putu32(resv, rqstp->rq_xid);
476 dir = svc_getnl(argv);
477 vers = svc_getnl(argv);
479 /* First words of reply: */
480 svc_putnl(resv, 1); /* REPLY */
482 if (dir != 0) /* direction != CALL */
484 if (vers != 2) /* RPC version number */
487 /* Save position in case we later decide to reject: */
488 accept_statp = resv->iov_base + resv->iov_len;
490 svc_putnl(resv, 0); /* ACCEPT */
492 rqstp->rq_prog = prog = svc_getnl(argv); /* program number */
493 rqstp->rq_vers = vers = svc_getnl(argv); /* version number */
494 rqstp->rq_proc = proc = svc_getnl(argv); /* procedure number */
496 progp = serv->sv_program;
498 for (progp = serv->sv_program; progp; progp = progp->pg_next)
499 if (prog == progp->pg_prog)
503 * Decode auth data, and add verifier to reply buffer.
504 * We do this before anything else in order to get a decent
507 auth_res = svc_authenticate(rqstp, &auth_stat);
508 /* Also give the program a chance to reject this call: */
509 if (auth_res == SVC_OK && progp) {
510 auth_stat = rpc_autherr_badcred;
511 auth_res = progp->pg_authenticate(rqstp);
517 rpc_stat = rpc_garbage_args;
520 rpc_stat = rpc_system_err;
533 if (vers >= progp->pg_nvers ||
534 !(versp = progp->pg_vers[vers]))
537 procp = versp->vs_proc + proc;
538 if (proc >= versp->vs_nproc || !procp->pc_func)
540 rqstp->rq_server = serv;
541 rqstp->rq_procinfo = procp;
543 /* Syntactic check complete */
544 serv->sv_stats->rpccnt++;
546 /* Build the reply header. */
547 statp = resv->iov_base +resv->iov_len;
548 svc_putnl(resv, RPC_SUCCESS);
550 /* Bump per-procedure stats counter */
553 /* Initialize storage for argp and resp */
554 memset(rqstp->rq_argp, 0, procp->pc_argsize);
555 memset(rqstp->rq_resp, 0, procp->pc_ressize);
557 /* un-reserve some of the out-queue now that we have a
558 * better idea of reply size
560 if (procp->pc_xdrressize)
561 svc_reserve(rqstp, procp->pc_xdrressize<<2);
563 /* Call the function that processes the request. */
564 if (!versp->vs_dispatch) {
565 /* Decode arguments */
566 xdr = procp->pc_decode;
567 if (xdr && !xdr(rqstp, argv->iov_base, rqstp->rq_argp))
570 *statp = procp->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp);
573 if (*statp == rpc_success && (xdr = procp->pc_encode)
574 && !xdr(rqstp, resv->iov_base+resv->iov_len, rqstp->rq_resp)) {
575 dprintk("svc: failed to encode reply\n");
576 /* serv->sv_stats->rpcsystemerr++; */
577 *statp = rpc_system_err;
580 dprintk("svc: calling dispatcher\n");
581 if (!versp->vs_dispatch(rqstp, statp)) {
582 /* Release reply info */
583 if (procp->pc_release)
584 procp->pc_release(rqstp, NULL, rqstp->rq_resp);
589 /* Check RPC status result */
590 if (*statp != rpc_success)
591 resv->iov_len = ((void*)statp) - resv->iov_base + 4;
593 /* Release reply info */
594 if (procp->pc_release)
595 procp->pc_release(rqstp, NULL, rqstp->rq_resp);
597 if (procp->pc_encode == NULL)
601 if (svc_authorise(rqstp))
603 return svc_send(rqstp);
606 svc_authorise(rqstp); /* doesn't hurt to call this twice */
607 dprintk("svc: svc_process dropit\n");
613 printk("svc: short len %Zd, dropping request\n", argv->iov_len);
615 goto dropit; /* drop request */
619 printk("svc: bad direction %d, dropping request\n", dir);
621 serv->sv_stats->rpcbadfmt++;
622 goto dropit; /* drop request */
625 serv->sv_stats->rpcbadfmt++;
626 svc_putnl(resv, 1); /* REJECT */
627 svc_putnl(resv, 0); /* RPC_MISMATCH */
628 svc_putnl(resv, 2); /* Only RPCv2 supported */
633 dprintk("svc: authentication failed (%d)\n", ntohl(auth_stat));
634 serv->sv_stats->rpcbadauth++;
635 /* Restore write pointer to location of accept status: */
636 xdr_ressize_check(rqstp, accept_statp);
637 svc_putnl(resv, 1); /* REJECT */
638 svc_putnl(resv, 1); /* AUTH_ERROR */
639 svc_putnl(resv, ntohl(auth_stat)); /* status */
643 dprintk("svc: unknown program %d\n", prog);
644 serv->sv_stats->rpcbadfmt++;
645 svc_putnl(resv, RPC_PROG_UNAVAIL);
650 printk("svc: unknown version (%d)\n", vers);
652 serv->sv_stats->rpcbadfmt++;
653 svc_putnl(resv, RPC_PROG_MISMATCH);
654 svc_putnl(resv, progp->pg_lovers);
655 svc_putnl(resv, progp->pg_hivers);
660 printk("svc: unknown procedure (%d)\n", proc);
662 serv->sv_stats->rpcbadfmt++;
663 svc_putnl(resv, RPC_PROC_UNAVAIL);
668 printk("svc: failed to decode args\n");
670 rpc_stat = rpc_garbage_args;
672 serv->sv_stats->rpcbadfmt++;
673 svc_putnl(resv, ntohl(rpc_stat));