Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * Pages are sent using ->sendpage so each server thread needs to |
| 61 | * allocate more to replace those used in sending. To help keep track |
| 62 | * of these pages we have a receive list where all pages initialy live, |
| 63 | * and a send list where pages are moved to when there are to be part |
| 64 | * of a reply. |
| 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 kvec 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 | * Each request/reply pair can have at most one "payload", plus two pages, |
| 77 | * one for the request, and one for the reply. |
| 78 | */ |
| 79 | #define RPCSVC_MAXPAGES ((RPCSVC_MAXPAYLOAD+PAGE_SIZE-1)/PAGE_SIZE + 2) |
| 80 | |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame^] | 81 | static inline u32 svc_getnl(struct kvec *iov) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | { |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame^] | 83 | __be32 val, *vp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | vp = iov->iov_base; |
| 85 | val = *vp++; |
| 86 | iov->iov_base = (void*)vp; |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame^] | 87 | iov->iov_len -= sizeof(__be32); |
| 88 | return ntohl(val); |
| 89 | } |
| 90 | |
| 91 | static inline void svc_putnl(struct kvec *iov, u32 val) |
| 92 | { |
| 93 | __be32 *vp = iov->iov_base + iov->iov_len; |
| 94 | *vp = htonl(val); |
| 95 | iov->iov_len += sizeof(__be32); |
| 96 | } |
| 97 | |
| 98 | static inline __be32 svc_getu32(struct kvec *iov) |
| 99 | { |
| 100 | __be32 val, *vp; |
| 101 | vp = iov->iov_base; |
| 102 | val = *vp++; |
| 103 | iov->iov_base = (void*)vp; |
| 104 | iov->iov_len -= sizeof(__be32); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | return val; |
| 106 | } |
| 107 | |
| 108 | static inline void svc_ungetu32(struct kvec *iov) |
| 109 | { |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame^] | 110 | __be32 *vp = (__be32 *)iov->iov_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | iov->iov_base = (void *)(vp - 1); |
| 112 | iov->iov_len += sizeof(*vp); |
| 113 | } |
| 114 | |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame^] | 115 | static inline void svc_putu32(struct kvec *iov, __be32 val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame^] | 117 | __be32 *vp = iov->iov_base + iov->iov_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | *vp = val; |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame^] | 119 | iov->iov_len += sizeof(__be32); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | |
| 123 | /* |
| 124 | * The context of a single thread, including the request currently being |
| 125 | * processed. |
| 126 | * NOTE: First two items must be prev/next. |
| 127 | */ |
| 128 | struct svc_rqst { |
| 129 | struct list_head rq_list; /* idle list */ |
| 130 | struct svc_sock * rq_sock; /* socket */ |
| 131 | struct sockaddr_in rq_addr; /* peer address */ |
| 132 | int rq_addrlen; |
| 133 | |
| 134 | struct svc_serv * rq_server; /* RPC service definition */ |
| 135 | struct svc_procedure * rq_procinfo; /* procedure info */ |
| 136 | struct auth_ops * rq_authop; /* authentication flavour */ |
| 137 | struct svc_cred rq_cred; /* auth info */ |
| 138 | struct sk_buff * rq_skbuff; /* fast recv inet buffer */ |
| 139 | struct svc_deferred_req*rq_deferred; /* deferred request we are replaying */ |
| 140 | |
| 141 | struct xdr_buf rq_arg; |
| 142 | struct xdr_buf rq_res; |
| 143 | struct page * rq_argpages[RPCSVC_MAXPAGES]; |
| 144 | struct page * rq_respages[RPCSVC_MAXPAGES]; |
| 145 | int rq_restailpage; |
| 146 | short rq_argused; /* pages used for argument */ |
| 147 | short rq_arghi; /* pages available in argument page list */ |
| 148 | short rq_resused; /* pages used for result */ |
| 149 | |
| 150 | u32 rq_xid; /* transmission id */ |
| 151 | u32 rq_prog; /* program number */ |
| 152 | u32 rq_vers; /* program version */ |
| 153 | u32 rq_proc; /* procedure number */ |
| 154 | u32 rq_prot; /* IP protocol */ |
| 155 | unsigned short |
| 156 | rq_secure : 1; /* secure port */ |
| 157 | |
| 158 | |
| 159 | __u32 rq_daddr; /* dest addr of request - reply from here */ |
| 160 | |
| 161 | void * rq_argp; /* decoded arguments */ |
| 162 | void * rq_resp; /* xdr'd results */ |
| 163 | void * rq_auth_data; /* flavor-specific data */ |
| 164 | |
| 165 | int rq_reserved; /* space on socket outq |
| 166 | * reserved for this request |
| 167 | */ |
| 168 | |
| 169 | struct cache_req rq_chandle; /* handle passed to caches for |
| 170 | * request delaying |
| 171 | */ |
| 172 | /* Catering to nfsd */ |
| 173 | struct auth_domain * rq_client; /* RPC peer info */ |
| 174 | struct svc_cacherep * rq_cacherep; /* cache info */ |
| 175 | struct knfsd_fh * rq_reffh; /* Referrence filehandle, used to |
| 176 | * determine what device number |
| 177 | * to report (real or virtual) |
| 178 | */ |
J. Bruce Fields | 5c04c46 | 2006-06-30 01:56:19 -0700 | [diff] [blame] | 179 | int rq_sendfile_ok; /* turned off in gss privacy |
| 180 | * to prevent encrypting page |
| 181 | * cache pages */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | wait_queue_head_t rq_wait; /* synchronization */ |
| 183 | }; |
| 184 | |
| 185 | /* |
| 186 | * Check buffer bounds after decoding arguments |
| 187 | */ |
| 188 | static inline int |
| 189 | xdr_argsize_check(struct svc_rqst *rqstp, u32 *p) |
| 190 | { |
| 191 | char *cp = (char *)p; |
| 192 | struct kvec *vec = &rqstp->rq_arg.head[0]; |
NeilBrown | 0ba7536 | 2005-11-07 01:00:26 -0800 | [diff] [blame] | 193 | return cp >= (char*)vec->iov_base |
| 194 | && cp <= (char*)vec->iov_base + vec->iov_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | static inline int |
| 198 | xdr_ressize_check(struct svc_rqst *rqstp, u32 *p) |
| 199 | { |
| 200 | struct kvec *vec = &rqstp->rq_res.head[0]; |
| 201 | char *cp = (char*)p; |
| 202 | |
| 203 | vec->iov_len = cp - (char*)vec->iov_base; |
| 204 | |
| 205 | return vec->iov_len <= PAGE_SIZE; |
| 206 | } |
| 207 | |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 208 | static inline struct page * |
| 209 | svc_take_res_page(struct svc_rqst *rqstp) |
| 210 | { |
| 211 | if (rqstp->rq_arghi <= rqstp->rq_argused) |
| 212 | return NULL; |
| 213 | rqstp->rq_arghi--; |
| 214 | rqstp->rq_respages[rqstp->rq_resused] = |
| 215 | rqstp->rq_argpages[rqstp->rq_arghi]; |
| 216 | return rqstp->rq_respages[rqstp->rq_resused++]; |
| 217 | } |
| 218 | |
J. Bruce Fields | 6f54e2d | 2006-04-10 22:55:36 -0700 | [diff] [blame] | 219 | static inline void svc_take_page(struct svc_rqst *rqstp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | { |
J. Bruce Fields | 6f54e2d | 2006-04-10 22:55:36 -0700 | [diff] [blame] | 221 | if (rqstp->rq_arghi <= rqstp->rq_argused) { |
| 222 | WARN_ON(1); |
| 223 | return; |
| 224 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | rqstp->rq_arghi--; |
| 226 | rqstp->rq_respages[rqstp->rq_resused] = |
| 227 | rqstp->rq_argpages[rqstp->rq_arghi]; |
| 228 | rqstp->rq_resused++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | static inline void svc_pushback_allpages(struct svc_rqst *rqstp) |
| 232 | { |
| 233 | while (rqstp->rq_resused) { |
| 234 | if (rqstp->rq_respages[--rqstp->rq_resused] == NULL) |
| 235 | continue; |
| 236 | rqstp->rq_argpages[rqstp->rq_arghi++] = |
| 237 | rqstp->rq_respages[rqstp->rq_resused]; |
| 238 | rqstp->rq_respages[rqstp->rq_resused] = NULL; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | static inline void svc_pushback_unused_pages(struct svc_rqst *rqstp) |
| 243 | { |
| 244 | while (rqstp->rq_resused && |
| 245 | rqstp->rq_res.pages != &rqstp->rq_respages[rqstp->rq_resused]) { |
| 246 | |
| 247 | if (rqstp->rq_respages[--rqstp->rq_resused] != NULL) { |
| 248 | rqstp->rq_argpages[rqstp->rq_arghi++] = |
| 249 | rqstp->rq_respages[rqstp->rq_resused]; |
| 250 | rqstp->rq_respages[rqstp->rq_resused] = NULL; |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | static inline void svc_free_allpages(struct svc_rqst *rqstp) |
| 256 | { |
| 257 | while (rqstp->rq_resused) { |
| 258 | if (rqstp->rq_respages[--rqstp->rq_resused] == NULL) |
| 259 | continue; |
| 260 | put_page(rqstp->rq_respages[rqstp->rq_resused]); |
| 261 | rqstp->rq_respages[rqstp->rq_resused] = NULL; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | struct svc_deferred_req { |
| 266 | u32 prot; /* protocol (UDP or TCP) */ |
| 267 | struct sockaddr_in addr; |
| 268 | struct svc_sock *svsk; /* where reply must go */ |
J. Bruce Fields | 1918e34 | 2006-01-18 17:43:16 -0800 | [diff] [blame] | 269 | u32 daddr; /* where reply must come from */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | struct cache_deferred_req handle; |
| 271 | int argslen; |
| 272 | u32 args[0]; |
| 273 | }; |
| 274 | |
| 275 | /* |
Andreas Gruenbacher | 9ba0263 | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 276 | * List of RPC programs on the same transport endpoint |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | */ |
| 278 | struct svc_program { |
Andreas Gruenbacher | 9ba0263 | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 279 | struct svc_program * pg_next; /* other programs (same xprt) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | u32 pg_prog; /* program number */ |
| 281 | unsigned int pg_lovers; /* lowest version */ |
| 282 | unsigned int pg_hivers; /* lowest version */ |
| 283 | unsigned int pg_nvers; /* number of versions */ |
| 284 | struct svc_version ** pg_vers; /* version array */ |
| 285 | char * pg_name; /* service name */ |
| 286 | char * pg_class; /* class name: services sharing authentication */ |
| 287 | struct svc_stat * pg_stats; /* rpc statistics */ |
| 288 | int (*pg_authenticate)(struct svc_rqst *); |
| 289 | }; |
| 290 | |
| 291 | /* |
| 292 | * RPC program version |
| 293 | */ |
| 294 | struct svc_version { |
| 295 | u32 vs_vers; /* version number */ |
| 296 | u32 vs_nproc; /* number of procedures */ |
| 297 | struct svc_procedure * vs_proc; /* per-procedure info */ |
| 298 | u32 vs_xdrsize; /* xdrsize needed for this version */ |
| 299 | |
| 300 | /* Override dispatch function (e.g. when caching replies). |
| 301 | * A return value of 0 means drop the request. |
| 302 | * vs_dispatch == NULL means use default dispatcher. |
| 303 | */ |
| 304 | int (*vs_dispatch)(struct svc_rqst *, u32 *); |
| 305 | }; |
| 306 | |
| 307 | /* |
| 308 | * RPC procedure info |
| 309 | */ |
| 310 | typedef int (*svc_procfunc)(struct svc_rqst *, void *argp, void *resp); |
| 311 | struct svc_procedure { |
| 312 | svc_procfunc pc_func; /* process the request */ |
| 313 | kxdrproc_t pc_decode; /* XDR decode args */ |
| 314 | kxdrproc_t pc_encode; /* XDR encode result */ |
| 315 | kxdrproc_t pc_release; /* XDR free result */ |
| 316 | unsigned int pc_argsize; /* argument struct size */ |
| 317 | unsigned int pc_ressize; /* result struct size */ |
| 318 | unsigned int pc_count; /* call count */ |
| 319 | unsigned int pc_cachetype; /* cache info (NFS) */ |
| 320 | unsigned int pc_xdrressize; /* maximum size of XDR reply */ |
| 321 | }; |
| 322 | |
| 323 | /* |
| 324 | * This is the RPC server thread function prototype |
| 325 | */ |
| 326 | typedef void (*svc_thread_fn)(struct svc_rqst *); |
| 327 | |
| 328 | /* |
| 329 | * Function prototypes. |
| 330 | */ |
| 331 | struct svc_serv * svc_create(struct svc_program *, unsigned int); |
| 332 | int svc_create_thread(svc_thread_fn, struct svc_serv *); |
| 333 | void svc_exit_thread(struct svc_rqst *); |
| 334 | void svc_destroy(struct svc_serv *); |
| 335 | int svc_process(struct svc_serv *, struct svc_rqst *); |
| 336 | int svc_register(struct svc_serv *, int, unsigned short); |
| 337 | void svc_wake_up(struct svc_serv *); |
| 338 | void svc_reserve(struct svc_rqst *rqstp, int space); |
| 339 | |
| 340 | #endif /* SUNRPC_SVC_H */ |