svc.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 <= vec->iov_len;
  152. }
  153. static inline int
  154. xdr_ressize_check(struct svc_rqst *rqstp, u32 *p)
  155. {
  156. struct kvec *vec = &rqstp->rq_res.head[0];
  157. char *cp = (char*)p;
  158. vec->iov_len = cp - (char*)vec->iov_base;
  159. return vec->iov_len <= PAGE_SIZE;
  160. }
  161. static inline struct page *
  162. svc_take_res_page(struct svc_rqst *rqstp)
  163. {
  164. if (rqstp->rq_arghi <= rqstp->rq_argused)
  165. return NULL;
  166. rqstp->rq_arghi--;
  167. rqstp->rq_respages[rqstp->rq_resused] =
  168. rqstp->rq_argpages[rqstp->rq_arghi];
  169. return rqstp->rq_respages[rqstp->rq_resused++];
  170. }
  171. static inline int svc_take_page(struct svc_rqst *rqstp)
  172. {
  173. if (rqstp->rq_arghi <= rqstp->rq_argused)
  174. return -ENOMEM;
  175. rqstp->rq_arghi--;
  176. rqstp->rq_respages[rqstp->rq_resused] =
  177. rqstp->rq_argpages[rqstp->rq_arghi];
  178. rqstp->rq_resused++;
  179. return 0;
  180. }
  181. static inline void svc_pushback_allpages(struct svc_rqst *rqstp)
  182. {
  183. while (rqstp->rq_resused) {
  184. if (rqstp->rq_respages[--rqstp->rq_resused] == NULL)
  185. continue;
  186. rqstp->rq_argpages[rqstp->rq_arghi++] =
  187. rqstp->rq_respages[rqstp->rq_resused];
  188. rqstp->rq_respages[rqstp->rq_resused] = NULL;
  189. }
  190. }
  191. static inline void svc_pushback_unused_pages(struct svc_rqst *rqstp)
  192. {
  193. while (rqstp->rq_resused &&
  194. rqstp->rq_res.pages != &rqstp->rq_respages[rqstp->rq_resused]) {
  195. if (rqstp->rq_respages[--rqstp->rq_resused] != NULL) {
  196. rqstp->rq_argpages[rqstp->rq_arghi++] =
  197. rqstp->rq_respages[rqstp->rq_resused];
  198. rqstp->rq_respages[rqstp->rq_resused] = NULL;
  199. }
  200. }
  201. }
  202. static inline void svc_free_allpages(struct svc_rqst *rqstp)
  203. {
  204. while (rqstp->rq_resused) {
  205. if (rqstp->rq_respages[--rqstp->rq_resused] == NULL)
  206. continue;
  207. put_page(rqstp->rq_respages[rqstp->rq_resused]);
  208. rqstp->rq_respages[rqstp->rq_resused] = NULL;
  209. }
  210. }
  211. struct svc_deferred_req {
  212. u32 prot; /* protocol (UDP or TCP) */
  213. struct sockaddr_in addr;
  214. struct svc_sock *svsk; /* where reply must go */
  215. struct cache_deferred_req handle;
  216. int argslen;
  217. u32 args[0];
  218. };
  219. /*
  220. * List of RPC programs on the same transport endpoint
  221. */
  222. struct svc_program {
  223. struct svc_program * pg_next; /* other programs (same xprt) */
  224. u32 pg_prog; /* program number */
  225. unsigned int pg_lovers; /* lowest version */
  226. unsigned int pg_hivers; /* lowest version */
  227. unsigned int pg_nvers; /* number of versions */
  228. struct svc_version ** pg_vers; /* version array */
  229. char * pg_name; /* service name */
  230. char * pg_class; /* class name: services sharing authentication */
  231. struct svc_stat * pg_stats; /* rpc statistics */
  232. int (*pg_authenticate)(struct svc_rqst *);
  233. };
  234. /*
  235. * RPC program version
  236. */
  237. struct svc_version {
  238. u32 vs_vers; /* version number */
  239. u32 vs_nproc; /* number of procedures */
  240. struct svc_procedure * vs_proc; /* per-procedure info */
  241. u32 vs_xdrsize; /* xdrsize needed for this version */
  242. /* Override dispatch function (e.g. when caching replies).
  243. * A return value of 0 means drop the request.
  244. * vs_dispatch == NULL means use default dispatcher.
  245. */
  246. int (*vs_dispatch)(struct svc_rqst *, u32 *);
  247. };
  248. /*
  249. * RPC procedure info
  250. */
  251. typedef int (*svc_procfunc)(struct svc_rqst *, void *argp, void *resp);
  252. struct svc_procedure {
  253. svc_procfunc pc_func; /* process the request */
  254. kxdrproc_t pc_decode; /* XDR decode args */
  255. kxdrproc_t pc_encode; /* XDR encode result */
  256. kxdrproc_t pc_release; /* XDR free result */
  257. unsigned int pc_argsize; /* argument struct size */
  258. unsigned int pc_ressize; /* result struct size */
  259. unsigned int pc_count; /* call count */
  260. unsigned int pc_cachetype; /* cache info (NFS) */
  261. unsigned int pc_xdrressize; /* maximum size of XDR reply */
  262. };
  263. /*
  264. * This is the RPC server thread function prototype
  265. */
  266. typedef void (*svc_thread_fn)(struct svc_rqst *);
  267. /*
  268. * Function prototypes.
  269. */
  270. struct svc_serv * svc_create(struct svc_program *, unsigned int);
  271. int svc_create_thread(svc_thread_fn, struct svc_serv *);
  272. void svc_exit_thread(struct svc_rqst *);
  273. void svc_destroy(struct svc_serv *);
  274. int svc_process(struct svc_serv *, struct svc_rqst *);
  275. int svc_register(struct svc_serv *, int, unsigned short);
  276. void svc_wake_up(struct svc_serv *);
  277. void svc_reserve(struct svc_rqst *rqstp, int space);
  278. #endif /* SUNRPC_SVC_H */