svc.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. #ifndef SUNRPC_SVC_H
  9. #define SUNRPC_SVC_H
  10. #include <linux/in.h>
  11. #include <linux/sunrpc/types.h>
  12. #include <linux/sunrpc/xdr.h>
  13. #include <linux/sunrpc/svcauth.h>
  14. #include <linux/wait.h>
  15. #include <linux/mm.h>
  16. /*
  17. * RPC service.
  18. *
  19. * An RPC service is a ``daemon,'' possibly multithreaded, which
  20. * receives and processes incoming RPC messages.
  21. * It has one or more transport sockets associated with it, and maintains
  22. * a list of idle threads waiting for input.
  23. *
  24. * We currently do not support more than one RPC program per daemon.
  25. */
  26. struct svc_serv {
  27. struct list_head sv_threads; /* idle server threads */
  28. struct list_head sv_sockets; /* pending sockets */
  29. struct svc_program * sv_program; /* RPC program */
  30. struct svc_stat * sv_stats; /* RPC statistics */
  31. spinlock_t sv_lock;
  32. unsigned int sv_nrthreads; /* # of server threads */
  33. unsigned int sv_bufsz; /* datagram buffer size */
  34. unsigned int sv_xdrsize; /* XDR buffer size */
  35. struct list_head sv_permsocks; /* all permanent sockets */
  36. struct list_head sv_tempsocks; /* all temporary sockets */
  37. int sv_tmpcnt; /* count of temporary sockets */
  38. char * sv_name; /* service name */
  39. };
  40. /*
  41. * Maximum payload size supported by a kernel RPC server.
  42. * This is use to determine the max number of pages nfsd is
  43. * willing to return in a single READ operation.
  44. */
  45. #define RPCSVC_MAXPAYLOAD (64*1024u)
  46. /*
  47. * RPC Requsts and replies are stored in one or more pages.
  48. * We maintain an array of pages for each server thread.
  49. * Requests are copied into these pages as they arrive. Remaining
  50. * pages are available to write the reply into.
  51. *
  52. * Pages are sent using ->sendpage so each server thread needs to
  53. * allocate more to replace those used in sending. To help keep track
  54. * of these pages we have a receive list where all pages initialy live,
  55. * and a send list where pages are moved to when there are to be part
  56. * of a reply.
  57. *
  58. * We use xdr_buf for holding responses as it fits well with NFS
  59. * read responses (that have a header, and some data pages, and possibly
  60. * a tail) and means we can share some client side routines.
  61. *
  62. * The xdr_buf.head kvec always points to the first page in the rq_*pages
  63. * list. The xdr_buf.pages pointer points to the second page on that
  64. * list. xdr_buf.tail points to the end of the first page.
  65. * This assumes that the non-page part of an rpc reply will fit
  66. * in a page - NFSd ensures this. lockd also has no trouble.
  67. *
  68. * Each request/reply pair can have at most one "payload", plus two pages,
  69. * one for the request, and one for the reply.
  70. */
  71. #define RPCSVC_MAXPAGES ((RPCSVC_MAXPAYLOAD+PAGE_SIZE-1)/PAGE_SIZE + 2)
  72. static inline u32 svc_getu32(struct kvec *iov)
  73. {
  74. u32 val, *vp;
  75. vp = iov->iov_base;
  76. val = *vp++;
  77. iov->iov_base = (void*)vp;
  78. iov->iov_len -= sizeof(u32);
  79. return val;
  80. }
  81. static inline void svc_ungetu32(struct kvec *iov)
  82. {
  83. u32 *vp = (u32 *)iov->iov_base;
  84. iov->iov_base = (void *)(vp - 1);
  85. iov->iov_len += sizeof(*vp);
  86. }
  87. static inline void svc_putu32(struct kvec *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. * The context of a single thread, including the request currently being
  95. * processed.
  96. * NOTE: First two items must be prev/next.
  97. */
  98. struct svc_rqst {
  99. struct list_head rq_list; /* idle list */
  100. struct svc_sock * rq_sock; /* socket */
  101. struct sockaddr_in rq_addr; /* peer address */
  102. int rq_addrlen;
  103. struct svc_serv * rq_server; /* RPC service definition */
  104. struct svc_procedure * rq_procinfo; /* procedure info */
  105. struct auth_ops * rq_authop; /* authentication flavour */
  106. struct svc_cred rq_cred; /* auth info */
  107. struct sk_buff * rq_skbuff; /* fast recv inet buffer */
  108. struct svc_deferred_req*rq_deferred; /* deferred request we are replaying */
  109. struct xdr_buf rq_arg;
  110. struct xdr_buf rq_res;
  111. struct page * rq_argpages[RPCSVC_MAXPAGES];
  112. struct page * rq_respages[RPCSVC_MAXPAGES];
  113. int rq_restailpage;
  114. short rq_argused; /* pages used for argument */
  115. short rq_arghi; /* pages available in argument page list */
  116. short rq_resused; /* pages used for result */
  117. u32 rq_xid; /* transmission id */
  118. u32 rq_prog; /* program number */
  119. u32 rq_vers; /* program version */
  120. u32 rq_proc; /* procedure number */
  121. u32 rq_prot; /* IP protocol */
  122. unsigned short
  123. rq_secure : 1; /* secure port */
  124. __u32 rq_daddr; /* dest addr of request - reply from here */
  125. void * rq_argp; /* decoded arguments */
  126. void * rq_resp; /* xdr'd results */
  127. void * rq_auth_data; /* flavor-specific data */
  128. int rq_reserved; /* space on socket outq
  129. * reserved for this request
  130. */
  131. struct cache_req rq_chandle; /* handle passed to caches for
  132. * request delaying
  133. */
  134. /* Catering to nfsd */
  135. struct auth_domain * rq_client; /* RPC peer info */
  136. struct svc_cacherep * rq_cacherep; /* cache info */
  137. struct knfsd_fh * rq_reffh; /* Referrence filehandle, used to
  138. * determine what device number
  139. * to report (real or virtual)
  140. */
  141. wait_queue_head_t rq_wait; /* synchronization */
  142. };
  143. /*
  144. * Check buffer bounds after decoding arguments
  145. */
  146. static inline int
  147. xdr_argsize_check(struct svc_rqst *rqstp, u32 *p)
  148. {
  149. char *cp = (char *)p;
  150. struct kvec *vec = &rqstp->rq_arg.head[0];
  151. return cp >= (char*)vec->iov_base
  152. && cp <= (char*)vec->iov_base + vec->iov_len;
  153. }
  154. static inline int
  155. xdr_ressize_check(struct svc_rqst *rqstp, u32 *p)
  156. {
  157. struct kvec *vec = &rqstp->rq_res.head[0];
  158. char *cp = (char*)p;
  159. vec->iov_len = cp - (char*)vec->iov_base;
  160. return vec->iov_len <= PAGE_SIZE;
  161. }
  162. static inline struct page *
  163. svc_take_res_page(struct svc_rqst *rqstp)
  164. {
  165. if (rqstp->rq_arghi <= rqstp->rq_argused)
  166. return NULL;
  167. rqstp->rq_arghi--;
  168. rqstp->rq_respages[rqstp->rq_resused] =
  169. rqstp->rq_argpages[rqstp->rq_arghi];
  170. return rqstp->rq_respages[rqstp->rq_resused++];
  171. }
  172. static inline int svc_take_page(struct svc_rqst *rqstp)
  173. {
  174. if (rqstp->rq_arghi <= rqstp->rq_argused)
  175. return -ENOMEM;
  176. rqstp->rq_arghi--;
  177. rqstp->rq_respages[rqstp->rq_resused] =
  178. rqstp->rq_argpages[rqstp->rq_arghi];
  179. rqstp->rq_resused++;
  180. return 0;
  181. }
  182. static inline void svc_pushback_allpages(struct svc_rqst *rqstp)
  183. {
  184. while (rqstp->rq_resused) {
  185. if (rqstp->rq_respages[--rqstp->rq_resused] == NULL)
  186. continue;
  187. rqstp->rq_argpages[rqstp->rq_arghi++] =
  188. rqstp->rq_respages[rqstp->rq_resused];
  189. rqstp->rq_respages[rqstp->rq_resused] = NULL;
  190. }
  191. }
  192. static inline void svc_pushback_unused_pages(struct svc_rqst *rqstp)
  193. {
  194. while (rqstp->rq_resused &&
  195. rqstp->rq_res.pages != &rqstp->rq_respages[rqstp->rq_resused]) {
  196. if (rqstp->rq_respages[--rqstp->rq_resused] != NULL) {
  197. rqstp->rq_argpages[rqstp->rq_arghi++] =
  198. rqstp->rq_respages[rqstp->rq_resused];
  199. rqstp->rq_respages[rqstp->rq_resused] = NULL;
  200. }
  201. }
  202. }
  203. static inline void svc_free_allpages(struct svc_rqst *rqstp)
  204. {
  205. while (rqstp->rq_resused) {
  206. if (rqstp->rq_respages[--rqstp->rq_resused] == NULL)
  207. continue;
  208. put_page(rqstp->rq_respages[rqstp->rq_resused]);
  209. rqstp->rq_respages[rqstp->rq_resused] = NULL;
  210. }
  211. }
  212. struct svc_deferred_req {
  213. u32 prot; /* protocol (UDP or TCP) */
  214. struct sockaddr_in addr;
  215. struct svc_sock *svsk; /* where reply must go */
  216. u32 daddr; /* where reply must come from */
  217. struct cache_deferred_req handle;
  218. int argslen;
  219. u32 args[0];
  220. };
  221. /*
  222. * List of RPC programs on the same transport endpoint
  223. */
  224. struct svc_program {
  225. struct svc_program * pg_next; /* other programs (same xprt) */
  226. u32 pg_prog; /* program number */
  227. unsigned int pg_lovers; /* lowest version */
  228. unsigned int pg_hivers; /* lowest version */
  229. unsigned int pg_nvers; /* number of versions */
  230. struct svc_version ** pg_vers; /* version array */
  231. char * pg_name; /* service name */
  232. char * pg_class; /* class name: services sharing authentication */
  233. struct svc_stat * pg_stats; /* rpc statistics */
  234. int (*pg_authenticate)(struct svc_rqst *);
  235. };
  236. /*
  237. * RPC program version
  238. */
  239. struct svc_version {
  240. u32 vs_vers; /* version number */
  241. u32 vs_nproc; /* number of procedures */
  242. struct svc_procedure * vs_proc; /* per-procedure info */
  243. u32 vs_xdrsize; /* xdrsize needed for this version */
  244. /* Override dispatch function (e.g. when caching replies).
  245. * A return value of 0 means drop the request.
  246. * vs_dispatch == NULL means use default dispatcher.
  247. */
  248. int (*vs_dispatch)(struct svc_rqst *, u32 *);
  249. };
  250. /*
  251. * RPC procedure info
  252. */
  253. typedef int (*svc_procfunc)(struct svc_rqst *, void *argp, void *resp);
  254. struct svc_procedure {
  255. svc_procfunc pc_func; /* process the request */
  256. kxdrproc_t pc_decode; /* XDR decode args */
  257. kxdrproc_t pc_encode; /* XDR encode result */
  258. kxdrproc_t pc_release; /* XDR free result */
  259. unsigned int pc_argsize; /* argument struct size */
  260. unsigned int pc_ressize; /* result struct size */
  261. unsigned int pc_count; /* call count */
  262. unsigned int pc_cachetype; /* cache info (NFS) */
  263. unsigned int pc_xdrressize; /* maximum size of XDR reply */
  264. };
  265. /*
  266. * This is the RPC server thread function prototype
  267. */
  268. typedef void (*svc_thread_fn)(struct svc_rqst *);
  269. /*
  270. * Function prototypes.
  271. */
  272. struct svc_serv * svc_create(struct svc_program *, unsigned int);
  273. int svc_create_thread(svc_thread_fn, struct svc_serv *);
  274. void svc_exit_thread(struct svc_rqst *);
  275. void svc_destroy(struct svc_serv *);
  276. int svc_process(struct svc_serv *, struct svc_rqst *);
  277. int svc_register(struct svc_serv *, int, unsigned short);
  278. void svc_wake_up(struct svc_serv *);
  279. void svc_reserve(struct svc_rqst *rqstp, int space);
  280. #endif /* SUNRPC_SVC_H */