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