20773282fadac8168d8f11826622689f369431c9
[linux-flexiantxendom0-3.2.10.git] / include / linux / sunrpc / svc.h
1 /*
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  */
30 struct 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 */
43
44         char *                  sv_name;        /* service name */
45 };
46
47 /*
48  * Maximum payload size supported by a kernel RPC server.
49  * This is use to determine the max number of pages nfsd is
50  * willing to return in a single READ operation.
51  */
52 #define RPCSVC_MAXPAYLOAD       (64*1024u)
53
54 /*
55  * RPC Requsts and replies are stored in one or more pages.
56  * We maintain an array of pages for each server thread.
57  * Requests are copied into these pages as they arrive.  Remaining
58  * pages are available to write the reply into.
59  *
60  * Currently pages are all re-used by the same server.  Later we 
61  * will use ->sendpage to transmit pages with reduced copying.  In
62  * that case we will need to give away the page and allocate new ones.
63  * In preparation for this, we explicitly move pages off the recv
64  * list onto the transmit list, and back.
65  *
66  * We use xdr_buf for holding responses as it fits well with NFS
67  * read responses (that have a header, and some data pages, and possibly
68  * a tail) and means we can share some client side routines.
69  *
70  * The xdr_buf.head iovec always points to the first page in the rq_*pages
71  * list.  The xdr_buf.pages pointer points to the second page on that
72  * list.  xdr_buf.tail points to the end of the first page.
73  * This assumes that the non-page part of an rpc reply will fit
74  * in a page - NFSd ensures this.  lockd also has no trouble.
75  */
76 #define RPCSVC_MAXPAGES         ((RPCSVC_MAXPAYLOAD+PAGE_SIZE-1)/PAGE_SIZE + 1)
77
78 static inline u32 svc_getu32(struct iovec *iov)
79 {
80         u32 val, *vp;
81         vp = iov->iov_base;
82         val = *vp++;
83         iov->iov_base = (void*)vp;
84         iov->iov_len -= sizeof(u32);
85         return val;
86 }
87 static inline void svc_putu32(struct iovec *iov, u32 val)
88 {
89         u32 *vp = iov->iov_base + iov->iov_len;
90         *vp = val;
91         iov->iov_len += sizeof(u32);
92 }
93
94         
95 /*
96  * The context of a single thread, including the request currently being
97  * processed.
98  * NOTE: First two items must be prev/next.
99  */
100 struct svc_rqst {
101         struct list_head        rq_list;        /* idle list */
102         struct svc_sock *       rq_sock;        /* socket */
103         struct sockaddr_in      rq_addr;        /* peer address */
104         int                     rq_addrlen;
105
106         struct svc_serv *       rq_server;      /* RPC service definition */
107         struct svc_procedure *  rq_procinfo;    /* procedure info */
108         struct auth_ops *       rq_authop;      /* authentication flavour */
109         struct svc_cred         rq_cred;        /* auth info */
110         struct sk_buff *        rq_skbuff;      /* fast recv inet buffer */
111         struct svc_deferred_req*rq_deferred;    /* deferred request we are replaying */
112
113         struct xdr_buf          rq_arg;
114         struct xdr_buf          rq_res;
115         struct page *           rq_argpages[RPCSVC_MAXPAGES];
116         struct page *           rq_respages[RPCSVC_MAXPAGES];
117         int                     rq_restailpage;
118         short                   rq_argused;     /* pages used for argument */
119         short                   rq_arghi;       /* pages available in argument page list */
120         short                   rq_resused;     /* pages used for result */
121
122         u32                     rq_xid;         /* transmission id */
123         u32                     rq_prog;        /* program number */
124         u32                     rq_vers;        /* program version */
125         u32                     rq_proc;        /* procedure number */
126         u32                     rq_prot;        /* IP protocol */
127         unsigned short
128                                 rq_secure  : 1; /* secure port */
129
130
131         void *                  rq_argp;        /* decoded arguments */
132         void *                  rq_resp;        /* xdr'd results */
133
134         int                     rq_reserved;    /* space on socket outq
135                                                  * reserved for this request
136                                                  */
137
138         struct cache_req        rq_chandle;     /* handle passed to caches for 
139                                                  * request delaying 
140                                                  */
141         /* Catering to nfsd */
142         struct auth_domain *    rq_client;      /* RPC peer info */
143         struct svc_cacherep *   rq_cacherep;    /* cache info */
144         struct knfsd_fh *       rq_reffh;       /* Referrence filehandle, used to
145                                                  * determine what device number
146                                                  * to report (real or virtual)
147                                                  */
148
149         wait_queue_head_t       rq_wait;        /* synchronization */
150 };
151
152 /*
153  * Check buffer bounds after decoding arguments
154  */
155 static inline int
156 xdr_argsize_check(struct svc_rqst *rqstp, u32 *p)
157 {
158         char *cp = (char *)p;
159         struct iovec *vec = &rqstp->rq_arg.head[0];
160         return cp - (char*)vec->iov_base <= vec->iov_len;
161 }
162
163 static inline int
164 xdr_ressize_check(struct svc_rqst *rqstp, u32 *p)
165 {
166         struct iovec *vec = &rqstp->rq_res.head[0];
167         char *cp = (char*)p;
168
169         vec->iov_len = cp - (char*)vec->iov_base;
170
171         return vec->iov_len <= PAGE_SIZE;
172 }
173
174 static inline int svc_take_page(struct svc_rqst *rqstp)
175 {
176         if (rqstp->rq_arghi <= rqstp->rq_argused)
177                 return -ENOMEM;
178         rqstp->rq_arghi--;
179         rqstp->rq_respages[rqstp->rq_resused] =
180                 rqstp->rq_argpages[rqstp->rq_arghi];
181         rqstp->rq_resused++;
182         return 0;
183 }
184
185 static inline void svc_pushback_allpages(struct svc_rqst *rqstp)
186 {
187         while (rqstp->rq_resused) {
188                 if (rqstp->rq_respages[--rqstp->rq_resused] == NULL)
189                         continue;
190                 rqstp->rq_argpages[rqstp->rq_arghi++] =
191                         rqstp->rq_respages[rqstp->rq_resused];
192                 rqstp->rq_respages[rqstp->rq_resused] = NULL;
193         }
194 }
195
196 static inline void svc_pushback_unused_pages(struct svc_rqst *rqstp)
197 {
198         while (rqstp->rq_resused &&
199                rqstp->rq_res.pages != &rqstp->rq_respages[rqstp->rq_resused]) {
200
201                 if (rqstp->rq_respages[--rqstp->rq_resused] != NULL) {
202                         rqstp->rq_argpages[rqstp->rq_arghi++] =
203                                 rqstp->rq_respages[rqstp->rq_resused];
204                         rqstp->rq_respages[rqstp->rq_resused] = NULL;
205                 }
206         }
207 }
208
209 static inline void svc_free_allpages(struct svc_rqst *rqstp)
210 {
211         while (rqstp->rq_resused) {
212                 if (rqstp->rq_respages[--rqstp->rq_resused] == NULL)
213                         continue;
214                 put_page(rqstp->rq_respages[rqstp->rq_resused]);
215                 rqstp->rq_respages[rqstp->rq_resused] = NULL;
216         }
217 }
218
219 struct svc_deferred_req {
220         u32                     prot;   /* protocol (UDP or TCP) */
221         struct sockaddr_in      addr;
222         struct svc_sock         *svsk;  /* where reply must go */
223         struct cache_deferred_req handle;
224         int                     argslen;
225         u32                     args[0];
226 };
227
228 /*
229  * RPC program
230  */
231 struct svc_program {
232         u32                     pg_prog;        /* program number */
233         unsigned int            pg_lovers;      /* lowest version */
234         unsigned int            pg_hivers;      /* lowest version */
235         unsigned int            pg_nvers;       /* number of versions */
236         struct svc_version **   pg_vers;        /* version array */
237         char *                  pg_name;        /* service name */
238         char *                  pg_class;       /* class name: services sharing authentication */
239         struct svc_stat *       pg_stats;       /* rpc statistics */
240 };
241
242 /*
243  * RPC program version
244  */
245 struct svc_version {
246         u32                     vs_vers;        /* version number */
247         u32                     vs_nproc;       /* number of procedures */
248         struct svc_procedure *  vs_proc;        /* per-procedure info */
249         u32                     vs_xdrsize;     /* xdrsize needed for this version */
250
251         /* Override dispatch function (e.g. when caching replies).
252          * A return value of 0 means drop the request. 
253          * vs_dispatch == NULL means use default dispatcher.
254          */
255         int                     (*vs_dispatch)(struct svc_rqst *, u32 *);
256 };
257
258 /*
259  * RPC procedure info
260  */
261 typedef int     (*svc_procfunc)(struct svc_rqst *, void *argp, void *resp);
262 struct svc_procedure {
263         svc_procfunc            pc_func;        /* process the request */
264         kxdrproc_t              pc_decode;      /* XDR decode args */
265         kxdrproc_t              pc_encode;      /* XDR encode result */
266         kxdrproc_t              pc_release;     /* XDR free result */
267         unsigned int            pc_argsize;     /* argument struct size */
268         unsigned int            pc_ressize;     /* result struct size */
269         unsigned int            pc_count;       /* call count */
270         unsigned int            pc_cachetype;   /* cache info (NFS) */
271         unsigned int            pc_xdrressize;  /* maximum size of XDR reply */
272 };
273
274 /*
275  * This is the RPC server thread function prototype
276  */
277 typedef void            (*svc_thread_fn)(struct svc_rqst *);
278
279 /*
280  * Function prototypes.
281  */
282 struct svc_serv *  svc_create(struct svc_program *, unsigned int);
283 int                svc_create_thread(svc_thread_fn, struct svc_serv *);
284 void               svc_exit_thread(struct svc_rqst *);
285 void               svc_destroy(struct svc_serv *);
286 int                svc_process(struct svc_serv *, struct svc_rqst *);
287 int                svc_register(struct svc_serv *, int, unsigned short);
288 void               svc_wake_up(struct svc_serv *);
289 void               svc_reserve(struct svc_rqst *rqstp, int space);
290
291 #endif /* SUNRPC_SVC_H */