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
= (struct svc_serv
*) kmalloc(sizeof(*serv
), GFP_KERNEL
)))
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;
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);
68 * Destroy an RPC service
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
,
79 if (serv
->sv_nrthreads
) {
80 if (--(serv
->sv_nrthreads
) != 0) {
81 svc_sock_update_bufs(serv
);
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
,
91 svc_delete_socket(svsk
);
93 while (!list_empty(&serv
->sv_permsocks
)) {
94 svsk
= list_entry(serv
->sv_permsocks
.next
,
97 svc_delete_socket(svsk
);
100 cache_clean_deferred(serv
);
102 /* Unregister service with the portmapper */
103 svc_register(serv
, 0, 0);
108 * Allocate an RPC server's buffer space.
109 * We allocate pages and place them in rq_argpages.
112 svc_init_buffer(struct svc_rqst
*rqstp
, unsigned int size
)
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;
123 if (pages
> RPCSVC_MAXPAGES
)
126 struct page
*p
= alloc_page(GFP_KERNEL
);
129 rqstp
->rq_argpages
[arghi
++] = p
;
132 rqstp
->rq_arghi
= arghi
;
137 * Release an RPC server buffer
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
)
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
;
161 rqstp
= kmalloc(sizeof(*rqstp
), GFP_KERNEL
);
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
))
173 serv
->sv_nrthreads
++;
174 rqstp
->rq_server
= serv
;
175 error
= kernel_thread((int (*)(void *)) func
, rqstp
, 0);
178 svc_sock_update_bufs(serv
);
184 svc_exit_thread(rqstp
);
189 * Destroy an RPC server thread
192 svc_exit_thread(struct svc_rqst
*rqstp
)
194 struct svc_serv
*serv
= rqstp
->rq_server
;
196 svc_release_buffer(rqstp
);
198 kfree(rqstp
->rq_resp
);
200 kfree(rqstp
->rq_argp
);
201 if (rqstp
->rq_auth_data
)
202 kfree(rqstp
->rq_auth_data
);
205 /* Release the server */
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
;
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
);
228 clear_thread_flag(TIF_SIGPENDING
);
230 for (i
= 0; i
< progp
->pg_nvers
; i
++) {
231 if (progp
->pg_vers
[i
] == NULL
)
233 error
= rpc_register(progp
->pg_prog
, i
, proto
, port
, &dummy
);
236 if (port
&& !dummy
) {
243 spin_lock_irqsave(¤t
->sighand
->siglock
, flags
);
245 spin_unlock_irqrestore(¤t
->sighand
->siglock
, flags
);
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];
264 u32 dir
, prog
, vers
, proc
,
269 rpc_stat
= rpc_success
;
271 if (argv
->iov_len
< 6*4)
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]);
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
)
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 */
300 if (vers
!= 2) /* RPC version number */
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
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
);
328 rpc_stat
= rpc_garbage_args
;
331 rpc_stat
= rpc_system_err
;
341 if (prog
!= progp
->pg_prog
)
344 if (vers
>= progp
->pg_nvers
||
345 !(versp
= progp
->pg_vers
[vers
]))
348 procp
= versp
->vs_proc
+ proc
;
349 if (proc
>= versp
->vs_nproc
|| !procp
->pc_func
)
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 */
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
))
381 *statp
= procp
->pc_func(rqstp
, rqstp
->rq_argp
, rqstp
->rq_resp
);
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
;
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
);
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
)
412 if (svc_authorise(rqstp
))
414 return svc_send(rqstp
);
417 svc_authorise(rqstp
); /* doesn't hurt to call this twice */
418 dprintk("svc: svc_process dropit\n");
424 printk("svc: short len %Zd, dropping request\n", argv
->iov_len
);
426 goto dropit
; /* drop request */
430 printk("svc: bad direction %d, dropping request\n", dir
);
432 serv
->sv_stats
->rpcbadfmt
++;
433 goto dropit
; /* drop request */
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
);
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 */
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 */
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
);