svc.h 10 KB

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