Merge branch 'upstream' of git://ftp.linux-mips.org/pub/scm/upstream-linus
[powerpc.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  * This is the RPC server thread function prototype
22  */
23 typedef void            (*svc_thread_fn)(struct svc_rqst *);
24
25 /*
26  *
27  * RPC service thread pool.
28  *
29  * Pool of threads and temporary sockets.  Generally there is only
30  * a single one of these per RPC service, but on NUMA machines those
31  * services that can benefit from it (i.e. nfs but not lockd) will
32  * have one pool per NUMA node.  This optimisation reduces cross-
33  * node traffic on multi-node NUMA NFS servers.
34  */
35 struct svc_pool {
36         unsigned int            sp_id;          /* pool id; also node id on NUMA */
37         spinlock_t              sp_lock;        /* protects all fields */
38         struct list_head        sp_threads;     /* idle server threads */
39         struct list_head        sp_sockets;     /* pending sockets */
40         unsigned int            sp_nrthreads;   /* # of threads in pool */
41         struct list_head        sp_all_threads; /* all server threads */
42 } ____cacheline_aligned_in_smp;
43
44 /*
45  * RPC service.
46  *
47  * An RPC service is a ``daemon,'' possibly multithreaded, which
48  * receives and processes incoming RPC messages.
49  * It has one or more transport sockets associated with it, and maintains
50  * a list of idle threads waiting for input.
51  *
52  * We currently do not support more than one RPC program per daemon.
53  */
54 struct svc_serv {
55         struct svc_program *    sv_program;     /* RPC program */
56         struct svc_stat *       sv_stats;       /* RPC statistics */
57         spinlock_t              sv_lock;
58         unsigned int            sv_nrthreads;   /* # of server threads */
59         unsigned int            sv_bufsz;       /* datagram buffer size */
60         unsigned int            sv_xdrsize;     /* XDR buffer size */
61
62         struct list_head        sv_permsocks;   /* all permanent sockets */
63         struct list_head        sv_tempsocks;   /* all temporary sockets */
64         int                     sv_tmpcnt;      /* count of temporary sockets */
65         struct timer_list       sv_temptimer;   /* timer for aging temporary sockets */
66
67         char *                  sv_name;        /* service name */
68
69         unsigned int            sv_nrpools;     /* number of thread pools */
70         struct svc_pool *       sv_pools;       /* array of thread pools */
71
72         void                    (*sv_shutdown)(struct svc_serv *serv);
73                                                 /* Callback to use when last thread
74                                                  * exits.
75                                                  */
76
77         struct module *         sv_module;      /* optional module to count when
78                                                  * adding threads */
79         svc_thread_fn           sv_function;    /* main function for threads */
80         int                     sv_kill_signal; /* signal to kill threads */
81 };
82
83 /*
84  * We use sv_nrthreads as a reference count.  svc_destroy() drops
85  * this refcount, so we need to bump it up around operations that
86  * change the number of threads.  Horrible, but there it is.
87  * Should be called with the BKL held.
88  */
89 static inline void svc_get(struct svc_serv *serv)
90 {
91         serv->sv_nrthreads++;
92 }
93
94 /*
95  * Maximum payload size supported by a kernel RPC server.
96  * This is use to determine the max number of pages nfsd is
97  * willing to return in a single READ operation.
98  */
99 #define RPCSVC_MAXPAYLOAD       (64*1024u)
100
101 /*
102  * RPC Requsts and replies are stored in one or more pages.
103  * We maintain an array of pages for each server thread.
104  * Requests are copied into these pages as they arrive.  Remaining
105  * pages are available to write the reply into.
106  *
107  * Pages are sent using ->sendpage so each server thread needs to
108  * allocate more to replace those used in sending.  To help keep track
109  * of these pages we have a receive list where all pages initialy live,
110  * and a send list where pages are moved to when there are to be part
111  * of a reply.
112  *
113  * We use xdr_buf for holding responses as it fits well with NFS
114  * read responses (that have a header, and some data pages, and possibly
115  * a tail) and means we can share some client side routines.
116  *
117  * The xdr_buf.head kvec always points to the first page in the rq_*pages
118  * list.  The xdr_buf.pages pointer points to the second page on that
119  * list.  xdr_buf.tail points to the end of the first page.
120  * This assumes that the non-page part of an rpc reply will fit
121  * in a page - NFSd ensures this.  lockd also has no trouble.
122  *
123  * Each request/reply pair can have at most one "payload", plus two pages,
124  * one for the request, and one for the reply.
125  */
126 #define RPCSVC_MAXPAGES         ((RPCSVC_MAXPAYLOAD+PAGE_SIZE-1)/PAGE_SIZE + 2)
127
128 static inline u32 svc_getnl(struct kvec *iov)
129 {
130         __be32 val, *vp;
131         vp = iov->iov_base;
132         val = *vp++;
133         iov->iov_base = (void*)vp;
134         iov->iov_len -= sizeof(__be32);
135         return ntohl(val);
136 }
137
138 static inline void svc_putnl(struct kvec *iov, u32 val)
139 {
140         __be32 *vp = iov->iov_base + iov->iov_len;
141         *vp = htonl(val);
142         iov->iov_len += sizeof(__be32);
143 }
144
145 static inline __be32 svc_getu32(struct kvec *iov)
146 {
147         __be32 val, *vp;
148         vp = iov->iov_base;
149         val = *vp++;
150         iov->iov_base = (void*)vp;
151         iov->iov_len -= sizeof(__be32);
152         return val;
153 }
154
155 static inline void svc_ungetu32(struct kvec *iov)
156 {
157         __be32 *vp = (__be32 *)iov->iov_base;
158         iov->iov_base = (void *)(vp - 1);
159         iov->iov_len += sizeof(*vp);
160 }
161
162 static inline void svc_putu32(struct kvec *iov, __be32 val)
163 {
164         __be32 *vp = iov->iov_base + iov->iov_len;
165         *vp = val;
166         iov->iov_len += sizeof(__be32);
167 }
168
169         
170 /*
171  * The context of a single thread, including the request currently being
172  * processed.
173  * NOTE: First two items must be prev/next.
174  */
175 struct svc_rqst {
176         struct list_head        rq_list;        /* idle list */
177         struct list_head        rq_all;         /* all threads list */
178         struct svc_sock *       rq_sock;        /* socket */
179         struct sockaddr_in      rq_addr;        /* peer address */
180         int                     rq_addrlen;
181
182         struct svc_serv *       rq_server;      /* RPC service definition */
183         struct svc_pool *       rq_pool;        /* thread pool */
184         struct svc_procedure *  rq_procinfo;    /* procedure info */
185         struct auth_ops *       rq_authop;      /* authentication flavour */
186         struct svc_cred         rq_cred;        /* auth info */
187         struct sk_buff *        rq_skbuff;      /* fast recv inet buffer */
188         struct svc_deferred_req*rq_deferred;    /* deferred request we are replaying */
189
190         struct xdr_buf          rq_arg;
191         struct xdr_buf          rq_res;
192         struct page *           rq_argpages[RPCSVC_MAXPAGES];
193         struct page *           rq_respages[RPCSVC_MAXPAGES];
194         int                     rq_restailpage;
195         short                   rq_argused;     /* pages used for argument */
196         short                   rq_arghi;       /* pages available in argument page list */
197         short                   rq_resused;     /* pages used for result */
198
199         __be32                  rq_xid;         /* transmission id */
200         u32                     rq_prog;        /* program number */
201         u32                     rq_vers;        /* program version */
202         u32                     rq_proc;        /* procedure number */
203         u32                     rq_prot;        /* IP protocol */
204         unsigned short
205                                 rq_secure  : 1; /* secure port */
206
207
208         __be32                  rq_daddr;       /* dest addr of request - reply from here */
209
210         void *                  rq_argp;        /* decoded arguments */
211         void *                  rq_resp;        /* xdr'd results */
212         void *                  rq_auth_data;   /* flavor-specific data */
213
214         int                     rq_reserved;    /* space on socket outq
215                                                  * reserved for this request
216                                                  */
217
218         struct cache_req        rq_chandle;     /* handle passed to caches for 
219                                                  * request delaying 
220                                                  */
221         /* Catering to nfsd */
222         struct auth_domain *    rq_client;      /* RPC peer info */
223         struct svc_cacherep *   rq_cacherep;    /* cache info */
224         struct knfsd_fh *       rq_reffh;       /* Referrence filehandle, used to
225                                                  * determine what device number
226                                                  * to report (real or virtual)
227                                                  */
228         int                     rq_sendfile_ok; /* turned off in gss privacy
229                                                  * to prevent encrypting page
230                                                  * cache pages */
231         wait_queue_head_t       rq_wait;        /* synchronization */
232         struct task_struct      *rq_task;       /* service thread */
233 };
234
235 /*
236  * Check buffer bounds after decoding arguments
237  */
238 static inline int
239 xdr_argsize_check(struct svc_rqst *rqstp, __be32 *p)
240 {
241         char *cp = (char *)p;
242         struct kvec *vec = &rqstp->rq_arg.head[0];
243         return cp >= (char*)vec->iov_base
244                 && cp <= (char*)vec->iov_base + vec->iov_len;
245 }
246
247 static inline int
248 xdr_ressize_check(struct svc_rqst *rqstp, __be32 *p)
249 {
250         struct kvec *vec = &rqstp->rq_res.head[0];
251         char *cp = (char*)p;
252
253         vec->iov_len = cp - (char*)vec->iov_base;
254
255         return vec->iov_len <= PAGE_SIZE;
256 }
257
258 static inline struct page *
259 svc_take_res_page(struct svc_rqst *rqstp)
260 {
261         if (rqstp->rq_arghi <= rqstp->rq_argused)
262                 return NULL;
263         rqstp->rq_arghi--;
264         rqstp->rq_respages[rqstp->rq_resused] =
265                 rqstp->rq_argpages[rqstp->rq_arghi];
266         return rqstp->rq_respages[rqstp->rq_resused++];
267 }
268
269 static inline void svc_take_page(struct svc_rqst *rqstp)
270 {
271         if (rqstp->rq_arghi <= rqstp->rq_argused) {
272                 WARN_ON(1);
273                 return;
274         }
275         rqstp->rq_arghi--;
276         rqstp->rq_respages[rqstp->rq_resused] =
277                 rqstp->rq_argpages[rqstp->rq_arghi];
278         rqstp->rq_resused++;
279 }
280
281 static inline void svc_pushback_allpages(struct svc_rqst *rqstp)
282 {
283         while (rqstp->rq_resused) {
284                 if (rqstp->rq_respages[--rqstp->rq_resused] == NULL)
285                         continue;
286                 rqstp->rq_argpages[rqstp->rq_arghi++] =
287                         rqstp->rq_respages[rqstp->rq_resused];
288                 rqstp->rq_respages[rqstp->rq_resused] = NULL;
289         }
290 }
291
292 static inline void svc_pushback_unused_pages(struct svc_rqst *rqstp)
293 {
294         while (rqstp->rq_resused &&
295                rqstp->rq_res.pages != &rqstp->rq_respages[rqstp->rq_resused]) {
296
297                 if (rqstp->rq_respages[--rqstp->rq_resused] != NULL) {
298                         rqstp->rq_argpages[rqstp->rq_arghi++] =
299                                 rqstp->rq_respages[rqstp->rq_resused];
300                         rqstp->rq_respages[rqstp->rq_resused] = NULL;
301                 }
302         }
303 }
304
305 static inline void svc_free_allpages(struct svc_rqst *rqstp)
306 {
307         while (rqstp->rq_resused) {
308                 if (rqstp->rq_respages[--rqstp->rq_resused] == NULL)
309                         continue;
310                 put_page(rqstp->rq_respages[rqstp->rq_resused]);
311                 rqstp->rq_respages[rqstp->rq_resused] = NULL;
312         }
313 }
314
315 struct svc_deferred_req {
316         u32                     prot;   /* protocol (UDP or TCP) */
317         struct sockaddr_in      addr;
318         struct svc_sock         *svsk;  /* where reply must go */
319         __be32                  daddr;  /* where reply must come from */
320         struct cache_deferred_req handle;
321         int                     argslen;
322         __be32                  args[0];
323 };
324
325 /*
326  * List of RPC programs on the same transport endpoint
327  */
328 struct svc_program {
329         struct svc_program *    pg_next;        /* other programs (same xprt) */
330         u32                     pg_prog;        /* program number */
331         unsigned int            pg_lovers;      /* lowest version */
332         unsigned int            pg_hivers;      /* lowest version */
333         unsigned int            pg_nvers;       /* number of versions */
334         struct svc_version **   pg_vers;        /* version array */
335         char *                  pg_name;        /* service name */
336         char *                  pg_class;       /* class name: services sharing authentication */
337         struct svc_stat *       pg_stats;       /* rpc statistics */
338         int                     (*pg_authenticate)(struct svc_rqst *);
339 };
340
341 /*
342  * RPC program version
343  */
344 struct svc_version {
345         u32                     vs_vers;        /* version number */
346         u32                     vs_nproc;       /* number of procedures */
347         struct svc_procedure *  vs_proc;        /* per-procedure info */
348         u32                     vs_xdrsize;     /* xdrsize needed for this version */
349
350         /* Override dispatch function (e.g. when caching replies).
351          * A return value of 0 means drop the request. 
352          * vs_dispatch == NULL means use default dispatcher.
353          */
354         int                     (*vs_dispatch)(struct svc_rqst *, __be32 *);
355 };
356
357 /*
358  * RPC procedure info
359  */
360 typedef int     (*svc_procfunc)(struct svc_rqst *, void *argp, void *resp);
361 struct svc_procedure {
362         svc_procfunc            pc_func;        /* process the request */
363         kxdrproc_t              pc_decode;      /* XDR decode args */
364         kxdrproc_t              pc_encode;      /* XDR encode result */
365         kxdrproc_t              pc_release;     /* XDR free result */
366         unsigned int            pc_argsize;     /* argument struct size */
367         unsigned int            pc_ressize;     /* result struct size */
368         unsigned int            pc_count;       /* call count */
369         unsigned int            pc_cachetype;   /* cache info (NFS) */
370         unsigned int            pc_xdrressize;  /* maximum size of XDR reply */
371 };
372
373 /*
374  * Function prototypes.
375  */
376 struct svc_serv *  svc_create(struct svc_program *, unsigned int,
377                               void (*shutdown)(struct svc_serv*));
378 int                svc_create_thread(svc_thread_fn, struct svc_serv *);
379 void               svc_exit_thread(struct svc_rqst *);
380 struct svc_serv *  svc_create_pooled(struct svc_program *, unsigned int,
381                         void (*shutdown)(struct svc_serv*),
382                         svc_thread_fn, int sig, struct module *);
383 int                svc_set_num_threads(struct svc_serv *, struct svc_pool *, int);
384 void               svc_destroy(struct svc_serv *);
385 int                svc_process(struct svc_rqst *);
386 int                svc_register(struct svc_serv *, int, unsigned short);
387 void               svc_wake_up(struct svc_serv *);
388 void               svc_reserve(struct svc_rqst *rqstp, int space);
389 struct svc_pool *  svc_pool_for_cpu(struct svc_serv *serv, int cpu);
390
391 #endif /* SUNRPC_SVC_H */