blob: 5eabded1106197acc70bef74377f9627904de4c7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/include/linux/sunrpc/svc.h
3 *
4 * RPC server declarations.
5 *
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7 */
8
9
10#ifndef SUNRPC_SVC_H
11#define SUNRPC_SVC_H
12
13#include <linux/in.h>
14#include <linux/sunrpc/types.h>
15#include <linux/sunrpc/xdr.h>
16#include <linux/sunrpc/svcauth.h>
17#include <linux/wait.h>
18#include <linux/mm.h>
19
20/*
21 * RPC service.
22 *
23 * An RPC service is a ``daemon,'' possibly multithreaded, which
24 * receives and processes incoming RPC messages.
25 * It has one or more transport sockets associated with it, and maintains
26 * a list of idle threads waiting for input.
27 *
28 * We currently do not support more than one RPC program per daemon.
29 */
30struct svc_serv {
31 struct list_head sv_threads; /* idle server threads */
32 struct list_head sv_sockets; /* pending sockets */
33 struct svc_program * sv_program; /* RPC program */
34 struct svc_stat * sv_stats; /* RPC statistics */
35 spinlock_t sv_lock;
36 unsigned int sv_nrthreads; /* # of server threads */
37 unsigned int sv_bufsz; /* datagram buffer size */
38 unsigned int sv_xdrsize; /* XDR buffer size */
39
40 struct list_head sv_permsocks; /* all permanent sockets */
41 struct list_head sv_tempsocks; /* all temporary sockets */
42 int sv_tmpcnt; /* count of temporary sockets */
Greg Banks36bdfc82006-10-02 02:17:54 -070043 struct timer_list sv_temptimer; /* timer for aging temporary sockets */
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45 char * sv_name; /* service name */
NeilBrownbc591cc2006-10-02 02:17:44 -070046
47 void (*sv_shutdown)(struct svc_serv *serv);
48 /* Callback to use when last thread
49 * exits.
50 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051};
52
53/*
54 * Maximum payload size supported by a kernel RPC server.
55 * This is use to determine the max number of pages nfsd is
56 * willing to return in a single READ operation.
57 */
58#define RPCSVC_MAXPAYLOAD (64*1024u)
59
60/*
61 * RPC Requsts and replies are stored in one or more pages.
62 * We maintain an array of pages for each server thread.
63 * Requests are copied into these pages as they arrive. Remaining
64 * pages are available to write the reply into.
65 *
66 * Pages are sent using ->sendpage so each server thread needs to
67 * allocate more to replace those used in sending. To help keep track
68 * of these pages we have a receive list where all pages initialy live,
69 * and a send list where pages are moved to when there are to be part
70 * of a reply.
71 *
72 * We use xdr_buf for holding responses as it fits well with NFS
73 * read responses (that have a header, and some data pages, and possibly
74 * a tail) and means we can share some client side routines.
75 *
76 * The xdr_buf.head kvec always points to the first page in the rq_*pages
77 * list. The xdr_buf.pages pointer points to the second page on that
78 * list. xdr_buf.tail points to the end of the first page.
79 * This assumes that the non-page part of an rpc reply will fit
80 * in a page - NFSd ensures this. lockd also has no trouble.
81 *
82 * Each request/reply pair can have at most one "payload", plus two pages,
83 * one for the request, and one for the reply.
84 */
85#define RPCSVC_MAXPAGES ((RPCSVC_MAXPAYLOAD+PAGE_SIZE-1)/PAGE_SIZE + 2)
86
Alexey Dobriyan76994312006-09-26 22:28:46 -070087static inline u32 svc_getnl(struct kvec *iov)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Alexey Dobriyan76994312006-09-26 22:28:46 -070089 __be32 val, *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 vp = iov->iov_base;
91 val = *vp++;
92 iov->iov_base = (void*)vp;
Alexey Dobriyan76994312006-09-26 22:28:46 -070093 iov->iov_len -= sizeof(__be32);
94 return ntohl(val);
95}
96
97static inline void svc_putnl(struct kvec *iov, u32 val)
98{
99 __be32 *vp = iov->iov_base + iov->iov_len;
100 *vp = htonl(val);
101 iov->iov_len += sizeof(__be32);
102}
103
104static inline __be32 svc_getu32(struct kvec *iov)
105{
106 __be32 val, *vp;
107 vp = iov->iov_base;
108 val = *vp++;
109 iov->iov_base = (void*)vp;
110 iov->iov_len -= sizeof(__be32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return val;
112}
113
114static inline void svc_ungetu32(struct kvec *iov)
115{
Alexey Dobriyan76994312006-09-26 22:28:46 -0700116 __be32 *vp = (__be32 *)iov->iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 iov->iov_base = (void *)(vp - 1);
118 iov->iov_len += sizeof(*vp);
119}
120
Alexey Dobriyan76994312006-09-26 22:28:46 -0700121static inline void svc_putu32(struct kvec *iov, __be32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Alexey Dobriyan76994312006-09-26 22:28:46 -0700123 __be32 *vp = iov->iov_base + iov->iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 *vp = val;
Alexey Dobriyan76994312006-09-26 22:28:46 -0700125 iov->iov_len += sizeof(__be32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
128
129/*
130 * The context of a single thread, including the request currently being
131 * processed.
132 * NOTE: First two items must be prev/next.
133 */
134struct svc_rqst {
135 struct list_head rq_list; /* idle list */
136 struct svc_sock * rq_sock; /* socket */
137 struct sockaddr_in rq_addr; /* peer address */
138 int rq_addrlen;
139
140 struct svc_serv * rq_server; /* RPC service definition */
141 struct svc_procedure * rq_procinfo; /* procedure info */
142 struct auth_ops * rq_authop; /* authentication flavour */
143 struct svc_cred rq_cred; /* auth info */
144 struct sk_buff * rq_skbuff; /* fast recv inet buffer */
145 struct svc_deferred_req*rq_deferred; /* deferred request we are replaying */
146
147 struct xdr_buf rq_arg;
148 struct xdr_buf rq_res;
149 struct page * rq_argpages[RPCSVC_MAXPAGES];
150 struct page * rq_respages[RPCSVC_MAXPAGES];
151 int rq_restailpage;
152 short rq_argused; /* pages used for argument */
153 short rq_arghi; /* pages available in argument page list */
154 short rq_resused; /* pages used for result */
155
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700156 __be32 rq_xid; /* transmission id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 u32 rq_prog; /* program number */
158 u32 rq_vers; /* program version */
159 u32 rq_proc; /* procedure number */
160 u32 rq_prot; /* IP protocol */
161 unsigned short
162 rq_secure : 1; /* secure port */
163
164
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700165 __be32 rq_daddr; /* dest addr of request - reply from here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 void * rq_argp; /* decoded arguments */
168 void * rq_resp; /* xdr'd results */
169 void * rq_auth_data; /* flavor-specific data */
170
171 int rq_reserved; /* space on socket outq
172 * reserved for this request
173 */
174
175 struct cache_req rq_chandle; /* handle passed to caches for
176 * request delaying
177 */
178 /* Catering to nfsd */
179 struct auth_domain * rq_client; /* RPC peer info */
180 struct svc_cacherep * rq_cacherep; /* cache info */
181 struct knfsd_fh * rq_reffh; /* Referrence filehandle, used to
182 * determine what device number
183 * to report (real or virtual)
184 */
J. Bruce Fields5c04c462006-06-30 01:56:19 -0700185 int rq_sendfile_ok; /* turned off in gss privacy
186 * to prevent encrypting page
187 * cache pages */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 wait_queue_head_t rq_wait; /* synchronization */
189};
190
191/*
192 * Check buffer bounds after decoding arguments
193 */
194static inline int
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700195xdr_argsize_check(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 char *cp = (char *)p;
198 struct kvec *vec = &rqstp->rq_arg.head[0];
NeilBrown0ba75362005-11-07 01:00:26 -0800199 return cp >= (char*)vec->iov_base
200 && cp <= (char*)vec->iov_base + vec->iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
203static inline int
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700204xdr_ressize_check(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 struct kvec *vec = &rqstp->rq_res.head[0];
207 char *cp = (char*)p;
208
209 vec->iov_len = cp - (char*)vec->iov_base;
210
211 return vec->iov_len <= PAGE_SIZE;
212}
213
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000214static inline struct page *
215svc_take_res_page(struct svc_rqst *rqstp)
216{
217 if (rqstp->rq_arghi <= rqstp->rq_argused)
218 return NULL;
219 rqstp->rq_arghi--;
220 rqstp->rq_respages[rqstp->rq_resused] =
221 rqstp->rq_argpages[rqstp->rq_arghi];
222 return rqstp->rq_respages[rqstp->rq_resused++];
223}
224
J. Bruce Fields6f54e2d2006-04-10 22:55:36 -0700225static inline void svc_take_page(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
J. Bruce Fields6f54e2d2006-04-10 22:55:36 -0700227 if (rqstp->rq_arghi <= rqstp->rq_argused) {
228 WARN_ON(1);
229 return;
230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 rqstp->rq_arghi--;
232 rqstp->rq_respages[rqstp->rq_resused] =
233 rqstp->rq_argpages[rqstp->rq_arghi];
234 rqstp->rq_resused++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
237static inline void svc_pushback_allpages(struct svc_rqst *rqstp)
238{
239 while (rqstp->rq_resused) {
240 if (rqstp->rq_respages[--rqstp->rq_resused] == NULL)
241 continue;
242 rqstp->rq_argpages[rqstp->rq_arghi++] =
243 rqstp->rq_respages[rqstp->rq_resused];
244 rqstp->rq_respages[rqstp->rq_resused] = NULL;
245 }
246}
247
248static inline void svc_pushback_unused_pages(struct svc_rqst *rqstp)
249{
250 while (rqstp->rq_resused &&
251 rqstp->rq_res.pages != &rqstp->rq_respages[rqstp->rq_resused]) {
252
253 if (rqstp->rq_respages[--rqstp->rq_resused] != NULL) {
254 rqstp->rq_argpages[rqstp->rq_arghi++] =
255 rqstp->rq_respages[rqstp->rq_resused];
256 rqstp->rq_respages[rqstp->rq_resused] = NULL;
257 }
258 }
259}
260
261static inline void svc_free_allpages(struct svc_rqst *rqstp)
262{
263 while (rqstp->rq_resused) {
264 if (rqstp->rq_respages[--rqstp->rq_resused] == NULL)
265 continue;
266 put_page(rqstp->rq_respages[rqstp->rq_resused]);
267 rqstp->rq_respages[rqstp->rq_resused] = NULL;
268 }
269}
270
271struct svc_deferred_req {
272 u32 prot; /* protocol (UDP or TCP) */
273 struct sockaddr_in addr;
274 struct svc_sock *svsk; /* where reply must go */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700275 __be32 daddr; /* where reply must come from */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 struct cache_deferred_req handle;
277 int argslen;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700278 __be32 args[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279};
280
281/*
Andreas Gruenbacher9ba02632005-06-22 17:16:24 +0000282 * List of RPC programs on the same transport endpoint
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 */
284struct svc_program {
Andreas Gruenbacher9ba02632005-06-22 17:16:24 +0000285 struct svc_program * pg_next; /* other programs (same xprt) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 u32 pg_prog; /* program number */
287 unsigned int pg_lovers; /* lowest version */
288 unsigned int pg_hivers; /* lowest version */
289 unsigned int pg_nvers; /* number of versions */
290 struct svc_version ** pg_vers; /* version array */
291 char * pg_name; /* service name */
292 char * pg_class; /* class name: services sharing authentication */
293 struct svc_stat * pg_stats; /* rpc statistics */
294 int (*pg_authenticate)(struct svc_rqst *);
295};
296
297/*
298 * RPC program version
299 */
300struct svc_version {
301 u32 vs_vers; /* version number */
302 u32 vs_nproc; /* number of procedures */
303 struct svc_procedure * vs_proc; /* per-procedure info */
304 u32 vs_xdrsize; /* xdrsize needed for this version */
305
306 /* Override dispatch function (e.g. when caching replies).
307 * A return value of 0 means drop the request.
308 * vs_dispatch == NULL means use default dispatcher.
309 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700310 int (*vs_dispatch)(struct svc_rqst *, __be32 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311};
312
313/*
314 * RPC procedure info
315 */
316typedef int (*svc_procfunc)(struct svc_rqst *, void *argp, void *resp);
317struct svc_procedure {
318 svc_procfunc pc_func; /* process the request */
319 kxdrproc_t pc_decode; /* XDR decode args */
320 kxdrproc_t pc_encode; /* XDR encode result */
321 kxdrproc_t pc_release; /* XDR free result */
322 unsigned int pc_argsize; /* argument struct size */
323 unsigned int pc_ressize; /* result struct size */
324 unsigned int pc_count; /* call count */
325 unsigned int pc_cachetype; /* cache info (NFS) */
326 unsigned int pc_xdrressize; /* maximum size of XDR reply */
327};
328
329/*
330 * This is the RPC server thread function prototype
331 */
332typedef void (*svc_thread_fn)(struct svc_rqst *);
333
334/*
335 * Function prototypes.
336 */
NeilBrownbc591cc2006-10-02 02:17:44 -0700337struct svc_serv * svc_create(struct svc_program *, unsigned int,
338 void (*shutdown)(struct svc_serv*));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339int svc_create_thread(svc_thread_fn, struct svc_serv *);
340void svc_exit_thread(struct svc_rqst *);
341void svc_destroy(struct svc_serv *);
NeilBrown6fb2b472006-10-02 02:17:50 -0700342int svc_process(struct svc_rqst *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343int svc_register(struct svc_serv *, int, unsigned short);
344void svc_wake_up(struct svc_serv *);
345void svc_reserve(struct svc_rqst *rqstp, int space);
346
347#endif /* SUNRPC_SVC_H */