svcsock.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806
  1. /*
  2. * linux/net/sunrpc/svcsock.c
  3. *
  4. * These are the RPC server socket internals.
  5. *
  6. * The server scheduling algorithm does not always distribute the load
  7. * evenly when servicing a single client. May need to modify the
  8. * svc_sock_enqueue procedure...
  9. *
  10. * TCP support is largely untested and may be a little slow. The problem
  11. * is that we currently do two separate recvfrom's, one for the 4-byte
  12. * record length, and the second for the actual record. This could possibly
  13. * be improved by always reading a minimum size of around 100 bytes and
  14. * tucking any superfluous bytes away in a temporary store. Still, that
  15. * leaves write requests out in the rain. An alternative may be to peek at
  16. * the first skb in the queue, and if it matches the next TCP sequence
  17. * number, to extract the record marker. Yuck.
  18. *
  19. * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
  20. */
  21. #include <linux/sched.h>
  22. #include <linux/errno.h>
  23. #include <linux/fcntl.h>
  24. #include <linux/net.h>
  25. #include <linux/in.h>
  26. #include <linux/inet.h>
  27. #include <linux/udp.h>
  28. #include <linux/tcp.h>
  29. #include <linux/unistd.h>
  30. #include <linux/slab.h>
  31. #include <linux/netdevice.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/file.h>
  34. #include <linux/freezer.h>
  35. #include <net/sock.h>
  36. #include <net/checksum.h>
  37. #include <net/ip.h>
  38. #include <net/tcp_states.h>
  39. #include <asm/uaccess.h>
  40. #include <asm/ioctls.h>
  41. #include <linux/sunrpc/types.h>
  42. #include <linux/sunrpc/xdr.h>
  43. #include <linux/sunrpc/svcsock.h>
  44. #include <linux/sunrpc/stats.h>
  45. /* SMP locking strategy:
  46. *
  47. * svc_pool->sp_lock protects most of the fields of that pool.
  48. * svc_serv->sv_lock protects sv_tempsocks, sv_permsocks, sv_tmpcnt.
  49. * when both need to be taken (rare), svc_serv->sv_lock is first.
  50. * BKL protects svc_serv->sv_nrthread.
  51. * svc_sock->sk_defer_lock protects the svc_sock->sk_deferred list
  52. * svc_sock->sk_flags.SK_BUSY prevents a svc_sock being enqueued multiply.
  53. *
  54. * Some flags can be set to certain values at any time
  55. * providing that certain rules are followed:
  56. *
  57. * SK_CONN, SK_DATA, can be set or cleared at any time.
  58. * after a set, svc_sock_enqueue must be called.
  59. * after a clear, the socket must be read/accepted
  60. * if this succeeds, it must be set again.
  61. * SK_CLOSE can set at any time. It is never cleared.
  62. * sk_inuse contains a bias of '1' until SK_DEAD is set.
  63. * so when sk_inuse hits zero, we know the socket is dead
  64. * and no-one is using it.
  65. * SK_DEAD can only be set while SK_BUSY is held which ensures
  66. * no other thread will be using the socket or will try to
  67. * set SK_DEAD.
  68. *
  69. */
  70. #define RPCDBG_FACILITY RPCDBG_SVCSOCK
  71. static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
  72. int *errp, int flags);
  73. static void svc_delete_socket(struct svc_sock *svsk);
  74. static void svc_udp_data_ready(struct sock *, int);
  75. static int svc_udp_recvfrom(struct svc_rqst *);
  76. static int svc_udp_sendto(struct svc_rqst *);
  77. static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk);
  78. static int svc_deferred_recv(struct svc_rqst *rqstp);
  79. static struct cache_deferred_req *svc_defer(struct cache_req *req);
  80. /* apparently the "standard" is that clients close
  81. * idle connections after 5 minutes, servers after
  82. * 6 minutes
  83. * http://www.connectathon.org/talks96/nfstcp.pdf
  84. */
  85. static int svc_conn_age_period = 6*60;
  86. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  87. static struct lock_class_key svc_key[2];
  88. static struct lock_class_key svc_slock_key[2];
  89. static inline void svc_reclassify_socket(struct socket *sock)
  90. {
  91. struct sock *sk = sock->sk;
  92. BUG_ON(sk->sk_lock.owner != NULL);
  93. switch (sk->sk_family) {
  94. case AF_INET:
  95. sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD",
  96. &svc_slock_key[0], "sk_lock-AF_INET-NFSD", &svc_key[0]);
  97. break;
  98. case AF_INET6:
  99. sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD",
  100. &svc_slock_key[1], "sk_lock-AF_INET6-NFSD", &svc_key[1]);
  101. break;
  102. default:
  103. BUG();
  104. }
  105. }
  106. #else
  107. static inline void svc_reclassify_socket(struct socket *sock)
  108. {
  109. }
  110. #endif
  111. /*
  112. * Queue up an idle server thread. Must have pool->sp_lock held.
  113. * Note: this is really a stack rather than a queue, so that we only
  114. * use as many different threads as we need, and the rest don't pollute
  115. * the cache.
  116. */
  117. static inline void
  118. svc_thread_enqueue(struct svc_pool *pool, struct svc_rqst *rqstp)
  119. {
  120. list_add(&rqstp->rq_list, &pool->sp_threads);
  121. }
  122. /*
  123. * Dequeue an nfsd thread. Must have pool->sp_lock held.
  124. */
  125. static inline void
  126. svc_thread_dequeue(struct svc_pool *pool, struct svc_rqst *rqstp)
  127. {
  128. list_del(&rqstp->rq_list);
  129. }
  130. /*
  131. * Release an skbuff after use
  132. */
  133. static inline void
  134. svc_release_skb(struct svc_rqst *rqstp)
  135. {
  136. struct sk_buff *skb = rqstp->rq_skbuff;
  137. struct svc_deferred_req *dr = rqstp->rq_deferred;
  138. if (skb) {
  139. rqstp->rq_skbuff = NULL;
  140. dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
  141. skb_free_datagram(rqstp->rq_sock->sk_sk, skb);
  142. }
  143. if (dr) {
  144. rqstp->rq_deferred = NULL;
  145. kfree(dr);
  146. }
  147. }
  148. /*
  149. * Any space to write?
  150. */
  151. static inline unsigned long
  152. svc_sock_wspace(struct svc_sock *svsk)
  153. {
  154. int wspace;
  155. if (svsk->sk_sock->type == SOCK_STREAM)
  156. wspace = sk_stream_wspace(svsk->sk_sk);
  157. else
  158. wspace = sock_wspace(svsk->sk_sk);
  159. return wspace;
  160. }
  161. /*
  162. * Queue up a socket with data pending. If there are idle nfsd
  163. * processes, wake 'em up.
  164. *
  165. */
  166. static void
  167. svc_sock_enqueue(struct svc_sock *svsk)
  168. {
  169. struct svc_serv *serv = svsk->sk_server;
  170. struct svc_pool *pool;
  171. struct svc_rqst *rqstp;
  172. int cpu;
  173. if (!(svsk->sk_flags &
  174. ( (1<<SK_CONN)|(1<<SK_DATA)|(1<<SK_CLOSE)|(1<<SK_DEFERRED)) ))
  175. return;
  176. if (test_bit(SK_DEAD, &svsk->sk_flags))
  177. return;
  178. cpu = get_cpu();
  179. pool = svc_pool_for_cpu(svsk->sk_server, cpu);
  180. put_cpu();
  181. spin_lock_bh(&pool->sp_lock);
  182. if (!list_empty(&pool->sp_threads) &&
  183. !list_empty(&pool->sp_sockets))
  184. printk(KERN_ERR
  185. "svc_sock_enqueue: threads and sockets both waiting??\n");
  186. if (test_bit(SK_DEAD, &svsk->sk_flags)) {
  187. /* Don't enqueue dead sockets */
  188. dprintk("svc: socket %p is dead, not enqueued\n", svsk->sk_sk);
  189. goto out_unlock;
  190. }
  191. /* Mark socket as busy. It will remain in this state until the
  192. * server has processed all pending data and put the socket back
  193. * on the idle list. We update SK_BUSY atomically because
  194. * it also guards against trying to enqueue the svc_sock twice.
  195. */
  196. if (test_and_set_bit(SK_BUSY, &svsk->sk_flags)) {
  197. /* Don't enqueue socket while already enqueued */
  198. dprintk("svc: socket %p busy, not enqueued\n", svsk->sk_sk);
  199. goto out_unlock;
  200. }
  201. BUG_ON(svsk->sk_pool != NULL);
  202. svsk->sk_pool = pool;
  203. set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
  204. if (((atomic_read(&svsk->sk_reserved) + serv->sv_max_mesg)*2
  205. > svc_sock_wspace(svsk))
  206. && !test_bit(SK_CLOSE, &svsk->sk_flags)
  207. && !test_bit(SK_CONN, &svsk->sk_flags)) {
  208. /* Don't enqueue while not enough space for reply */
  209. dprintk("svc: socket %p no space, %d*2 > %ld, not enqueued\n",
  210. svsk->sk_sk, atomic_read(&svsk->sk_reserved)+serv->sv_max_mesg,
  211. svc_sock_wspace(svsk));
  212. svsk->sk_pool = NULL;
  213. clear_bit(SK_BUSY, &svsk->sk_flags);
  214. goto out_unlock;
  215. }
  216. clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
  217. if (!list_empty(&pool->sp_threads)) {
  218. rqstp = list_entry(pool->sp_threads.next,
  219. struct svc_rqst,
  220. rq_list);
  221. dprintk("svc: socket %p served by daemon %p\n",
  222. svsk->sk_sk, rqstp);
  223. svc_thread_dequeue(pool, rqstp);
  224. if (rqstp->rq_sock)
  225. printk(KERN_ERR
  226. "svc_sock_enqueue: server %p, rq_sock=%p!\n",
  227. rqstp, rqstp->rq_sock);
  228. rqstp->rq_sock = svsk;
  229. atomic_inc(&svsk->sk_inuse);
  230. rqstp->rq_reserved = serv->sv_max_mesg;
  231. atomic_add(rqstp->rq_reserved, &svsk->sk_reserved);
  232. BUG_ON(svsk->sk_pool != pool);
  233. wake_up(&rqstp->rq_wait);
  234. } else {
  235. dprintk("svc: socket %p put into queue\n", svsk->sk_sk);
  236. list_add_tail(&svsk->sk_ready, &pool->sp_sockets);
  237. BUG_ON(svsk->sk_pool != pool);
  238. }
  239. out_unlock:
  240. spin_unlock_bh(&pool->sp_lock);
  241. }
  242. /*
  243. * Dequeue the first socket. Must be called with the pool->sp_lock held.
  244. */
  245. static inline struct svc_sock *
  246. svc_sock_dequeue(struct svc_pool *pool)
  247. {
  248. struct svc_sock *svsk;
  249. if (list_empty(&pool->sp_sockets))
  250. return NULL;
  251. svsk = list_entry(pool->sp_sockets.next,
  252. struct svc_sock, sk_ready);
  253. list_del_init(&svsk->sk_ready);
  254. dprintk("svc: socket %p dequeued, inuse=%d\n",
  255. svsk->sk_sk, atomic_read(&svsk->sk_inuse));
  256. return svsk;
  257. }
  258. /*
  259. * Having read something from a socket, check whether it
  260. * needs to be re-enqueued.
  261. * Note: SK_DATA only gets cleared when a read-attempt finds
  262. * no (or insufficient) data.
  263. */
  264. static inline void
  265. svc_sock_received(struct svc_sock *svsk)
  266. {
  267. svsk->sk_pool = NULL;
  268. clear_bit(SK_BUSY, &svsk->sk_flags);
  269. svc_sock_enqueue(svsk);
  270. }
  271. /**
  272. * svc_reserve - change the space reserved for the reply to a request.
  273. * @rqstp: The request in question
  274. * @space: new max space to reserve
  275. *
  276. * Each request reserves some space on the output queue of the socket
  277. * to make sure the reply fits. This function reduces that reserved
  278. * space to be the amount of space used already, plus @space.
  279. *
  280. */
  281. void svc_reserve(struct svc_rqst *rqstp, int space)
  282. {
  283. space += rqstp->rq_res.head[0].iov_len;
  284. if (space < rqstp->rq_reserved) {
  285. struct svc_sock *svsk = rqstp->rq_sock;
  286. atomic_sub((rqstp->rq_reserved - space), &svsk->sk_reserved);
  287. rqstp->rq_reserved = space;
  288. svc_sock_enqueue(svsk);
  289. }
  290. }
  291. /*
  292. * Release a socket after use.
  293. */
  294. static inline void
  295. svc_sock_put(struct svc_sock *svsk)
  296. {
  297. if (atomic_dec_and_test(&svsk->sk_inuse)) {
  298. BUG_ON(! test_bit(SK_DEAD, &svsk->sk_flags));
  299. dprintk("svc: releasing dead socket\n");
  300. if (svsk->sk_sock->file)
  301. sockfd_put(svsk->sk_sock);
  302. else
  303. sock_release(svsk->sk_sock);
  304. if (svsk->sk_info_authunix != NULL)
  305. svcauth_unix_info_release(svsk->sk_info_authunix);
  306. kfree(svsk);
  307. }
  308. }
  309. static void
  310. svc_sock_release(struct svc_rqst *rqstp)
  311. {
  312. struct svc_sock *svsk = rqstp->rq_sock;
  313. svc_release_skb(rqstp);
  314. svc_free_res_pages(rqstp);
  315. rqstp->rq_res.page_len = 0;
  316. rqstp->rq_res.page_base = 0;
  317. /* Reset response buffer and release
  318. * the reservation.
  319. * But first, check that enough space was reserved
  320. * for the reply, otherwise we have a bug!
  321. */
  322. if ((rqstp->rq_res.len) > rqstp->rq_reserved)
  323. printk(KERN_ERR "RPC request reserved %d but used %d\n",
  324. rqstp->rq_reserved,
  325. rqstp->rq_res.len);
  326. rqstp->rq_res.head[0].iov_len = 0;
  327. svc_reserve(rqstp, 0);
  328. rqstp->rq_sock = NULL;
  329. svc_sock_put(svsk);
  330. }
  331. /*
  332. * External function to wake up a server waiting for data
  333. * This really only makes sense for services like lockd
  334. * which have exactly one thread anyway.
  335. */
  336. void
  337. svc_wake_up(struct svc_serv *serv)
  338. {
  339. struct svc_rqst *rqstp;
  340. unsigned int i;
  341. struct svc_pool *pool;
  342. for (i = 0; i < serv->sv_nrpools; i++) {
  343. pool = &serv->sv_pools[i];
  344. spin_lock_bh(&pool->sp_lock);
  345. if (!list_empty(&pool->sp_threads)) {
  346. rqstp = list_entry(pool->sp_threads.next,
  347. struct svc_rqst,
  348. rq_list);
  349. dprintk("svc: daemon %p woken up.\n", rqstp);
  350. /*
  351. svc_thread_dequeue(pool, rqstp);
  352. rqstp->rq_sock = NULL;
  353. */
  354. wake_up(&rqstp->rq_wait);
  355. }
  356. spin_unlock_bh(&pool->sp_lock);
  357. }
  358. }
  359. /*
  360. * Generic sendto routine
  361. */
  362. static int
  363. svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr)
  364. {
  365. struct svc_sock *svsk = rqstp->rq_sock;
  366. struct socket *sock = svsk->sk_sock;
  367. int slen;
  368. char buffer[CMSG_SPACE(sizeof(struct in_pktinfo))];
  369. struct cmsghdr *cmh = (struct cmsghdr *)buffer;
  370. struct in_pktinfo *pki = (struct in_pktinfo *)CMSG_DATA(cmh);
  371. int len = 0;
  372. int result;
  373. int size;
  374. struct page **ppage = xdr->pages;
  375. size_t base = xdr->page_base;
  376. unsigned int pglen = xdr->page_len;
  377. unsigned int flags = MSG_MORE;
  378. slen = xdr->len;
  379. if (rqstp->rq_prot == IPPROTO_UDP) {
  380. /* set the source and destination */
  381. struct msghdr msg;
  382. msg.msg_name = &rqstp->rq_addr;
  383. msg.msg_namelen = sizeof(rqstp->rq_addr);
  384. msg.msg_iov = NULL;
  385. msg.msg_iovlen = 0;
  386. msg.msg_flags = MSG_MORE;
  387. msg.msg_control = cmh;
  388. msg.msg_controllen = sizeof(buffer);
  389. cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
  390. cmh->cmsg_level = SOL_IP;
  391. cmh->cmsg_type = IP_PKTINFO;
  392. pki->ipi_ifindex = 0;
  393. pki->ipi_spec_dst.s_addr = rqstp->rq_daddr;
  394. if (sock_sendmsg(sock, &msg, 0) < 0)
  395. goto out;
  396. }
  397. /* send head */
  398. if (slen == xdr->head[0].iov_len)
  399. flags = 0;
  400. len = kernel_sendpage(sock, rqstp->rq_respages[0], 0,
  401. xdr->head[0].iov_len, flags);
  402. if (len != xdr->head[0].iov_len)
  403. goto out;
  404. slen -= xdr->head[0].iov_len;
  405. if (slen == 0)
  406. goto out;
  407. /* send page data */
  408. size = PAGE_SIZE - base < pglen ? PAGE_SIZE - base : pglen;
  409. while (pglen > 0) {
  410. if (slen == size)
  411. flags = 0;
  412. result = kernel_sendpage(sock, *ppage, base, size, flags);
  413. if (result > 0)
  414. len += result;
  415. if (result != size)
  416. goto out;
  417. slen -= size;
  418. pglen -= size;
  419. size = PAGE_SIZE < pglen ? PAGE_SIZE : pglen;
  420. base = 0;
  421. ppage++;
  422. }
  423. /* send tail */
  424. if (xdr->tail[0].iov_len) {
  425. result = kernel_sendpage(sock, rqstp->rq_respages[0],
  426. ((unsigned long)xdr->tail[0].iov_base)
  427. & (PAGE_SIZE-1),
  428. xdr->tail[0].iov_len, 0);
  429. if (result > 0)
  430. len += result;
  431. }
  432. out:
  433. dprintk("svc: socket %p sendto([%p %Zu... ], %d) = %d (addr %x)\n",
  434. rqstp->rq_sock, xdr->head[0].iov_base, xdr->head[0].iov_len, xdr->len, len,
  435. rqstp->rq_addr.sin_addr.s_addr);
  436. return len;
  437. }
  438. /*
  439. * Report socket names for nfsdfs
  440. */
  441. static int one_sock_name(char *buf, struct svc_sock *svsk)
  442. {
  443. int len;
  444. switch(svsk->sk_sk->sk_family) {
  445. case AF_INET:
  446. len = sprintf(buf, "ipv4 %s %u.%u.%u.%u %d\n",
  447. svsk->sk_sk->sk_protocol==IPPROTO_UDP?
  448. "udp" : "tcp",
  449. NIPQUAD(inet_sk(svsk->sk_sk)->rcv_saddr),
  450. inet_sk(svsk->sk_sk)->num);
  451. break;
  452. default:
  453. len = sprintf(buf, "*unknown-%d*\n",
  454. svsk->sk_sk->sk_family);
  455. }
  456. return len;
  457. }
  458. int
  459. svc_sock_names(char *buf, struct svc_serv *serv, char *toclose)
  460. {
  461. struct svc_sock *svsk, *closesk = NULL;
  462. int len = 0;
  463. if (!serv)
  464. return 0;
  465. spin_lock_bh(&serv->sv_lock);
  466. list_for_each_entry(svsk, &serv->sv_permsocks, sk_list) {
  467. int onelen = one_sock_name(buf+len, svsk);
  468. if (toclose && strcmp(toclose, buf+len) == 0)
  469. closesk = svsk;
  470. else
  471. len += onelen;
  472. }
  473. spin_unlock_bh(&serv->sv_lock);
  474. if (closesk)
  475. /* Should unregister with portmap, but you cannot
  476. * unregister just one protocol...
  477. */
  478. svc_close_socket(closesk);
  479. else if (toclose)
  480. return -ENOENT;
  481. return len;
  482. }
  483. EXPORT_SYMBOL(svc_sock_names);
  484. /*
  485. * Check input queue length
  486. */
  487. static int
  488. svc_recv_available(struct svc_sock *svsk)
  489. {
  490. struct socket *sock = svsk->sk_sock;
  491. int avail, err;
  492. err = kernel_sock_ioctl(sock, TIOCINQ, (unsigned long) &avail);
  493. return (err >= 0)? avail : err;
  494. }
  495. /*
  496. * Generic recvfrom routine.
  497. */
  498. static int
  499. svc_recvfrom(struct svc_rqst *rqstp, struct kvec *iov, int nr, int buflen)
  500. {
  501. struct svc_sock *svsk = rqstp->rq_sock;
  502. struct msghdr msg;
  503. struct socket *sock;
  504. int len;
  505. rqstp->rq_addrlen = sizeof(rqstp->rq_addr);
  506. sock = svsk->sk_sock;
  507. msg.msg_name = &rqstp->rq_addr;
  508. msg.msg_namelen = sizeof(rqstp->rq_addr);
  509. msg.msg_control = NULL;
  510. msg.msg_controllen = 0;
  511. msg.msg_flags = MSG_DONTWAIT;
  512. len = kernel_recvmsg(sock, &msg, iov, nr, buflen, MSG_DONTWAIT);
  513. /* sock_recvmsg doesn't fill in the name/namelen, so we must..
  514. */
  515. memcpy(&rqstp->rq_addr, &svsk->sk_remote, svsk->sk_remotelen);
  516. rqstp->rq_addrlen = svsk->sk_remotelen;
  517. dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",
  518. rqstp->rq_sock, iov[0].iov_base, iov[0].iov_len, len);
  519. return len;
  520. }
  521. /*
  522. * Set socket snd and rcv buffer lengths
  523. */
  524. static inline void
  525. svc_sock_setbufsize(struct socket *sock, unsigned int snd, unsigned int rcv)
  526. {
  527. #if 0
  528. mm_segment_t oldfs;
  529. oldfs = get_fs(); set_fs(KERNEL_DS);
  530. sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
  531. (char*)&snd, sizeof(snd));
  532. sock_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
  533. (char*)&rcv, sizeof(rcv));
  534. #else
  535. /* sock_setsockopt limits use to sysctl_?mem_max,
  536. * which isn't acceptable. Until that is made conditional
  537. * on not having CAP_SYS_RESOURCE or similar, we go direct...
  538. * DaveM said I could!
  539. */
  540. lock_sock(sock->sk);
  541. sock->sk->sk_sndbuf = snd * 2;
  542. sock->sk->sk_rcvbuf = rcv * 2;
  543. sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK|SOCK_RCVBUF_LOCK;
  544. release_sock(sock->sk);
  545. #endif
  546. }
  547. /*
  548. * INET callback when data has been received on the socket.
  549. */
  550. static void
  551. svc_udp_data_ready(struct sock *sk, int count)
  552. {
  553. struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
  554. if (svsk) {
  555. dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n",
  556. svsk, sk, count, test_bit(SK_BUSY, &svsk->sk_flags));
  557. set_bit(SK_DATA, &svsk->sk_flags);
  558. svc_sock_enqueue(svsk);
  559. }
  560. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  561. wake_up_interruptible(sk->sk_sleep);
  562. }
  563. /*
  564. * INET callback when space is newly available on the socket.
  565. */
  566. static void
  567. svc_write_space(struct sock *sk)
  568. {
  569. struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
  570. if (svsk) {
  571. dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
  572. svsk, sk, test_bit(SK_BUSY, &svsk->sk_flags));
  573. svc_sock_enqueue(svsk);
  574. }
  575. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) {
  576. dprintk("RPC svc_write_space: someone sleeping on %p\n",
  577. svsk);
  578. wake_up_interruptible(sk->sk_sleep);
  579. }
  580. }
  581. /*
  582. * Receive a datagram from a UDP socket.
  583. */
  584. static int
  585. svc_udp_recvfrom(struct svc_rqst *rqstp)
  586. {
  587. struct svc_sock *svsk = rqstp->rq_sock;
  588. struct svc_serv *serv = svsk->sk_server;
  589. struct sk_buff *skb;
  590. int err, len;
  591. if (test_and_clear_bit(SK_CHNGBUF, &svsk->sk_flags))
  592. /* udp sockets need large rcvbuf as all pending
  593. * requests are still in that buffer. sndbuf must
  594. * also be large enough that there is enough space
  595. * for one reply per thread. We count all threads
  596. * rather than threads in a particular pool, which
  597. * provides an upper bound on the number of threads
  598. * which will access the socket.
  599. */
  600. svc_sock_setbufsize(svsk->sk_sock,
  601. (serv->sv_nrthreads+3) * serv->sv_max_mesg,
  602. (serv->sv_nrthreads+3) * serv->sv_max_mesg);
  603. if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) {
  604. svc_sock_received(svsk);
  605. return svc_deferred_recv(rqstp);
  606. }
  607. if (test_bit(SK_CLOSE, &svsk->sk_flags)) {
  608. svc_delete_socket(svsk);
  609. return 0;
  610. }
  611. clear_bit(SK_DATA, &svsk->sk_flags);
  612. while ((skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err)) == NULL) {
  613. if (err == -EAGAIN) {
  614. svc_sock_received(svsk);
  615. return err;
  616. }
  617. /* possibly an icmp error */
  618. dprintk("svc: recvfrom returned error %d\n", -err);
  619. }
  620. if (skb->tstamp.off_sec == 0) {
  621. struct timeval tv;
  622. tv.tv_sec = xtime.tv_sec;
  623. tv.tv_usec = xtime.tv_nsec / NSEC_PER_USEC;
  624. skb_set_timestamp(skb, &tv);
  625. /* Don't enable netstamp, sunrpc doesn't
  626. need that much accuracy */
  627. }
  628. skb_get_timestamp(skb, &svsk->sk_sk->sk_stamp);
  629. set_bit(SK_DATA, &svsk->sk_flags); /* there may be more data... */
  630. /*
  631. * Maybe more packets - kick another thread ASAP.
  632. */
  633. svc_sock_received(svsk);
  634. len = skb->len - sizeof(struct udphdr);
  635. rqstp->rq_arg.len = len;
  636. rqstp->rq_prot = IPPROTO_UDP;
  637. /* Get sender address */
  638. rqstp->rq_addr.sin_family = AF_INET;
  639. rqstp->rq_addr.sin_port = skb->h.uh->source;
  640. rqstp->rq_addr.sin_addr.s_addr = skb->nh.iph->saddr;
  641. rqstp->rq_daddr = skb->nh.iph->daddr;
  642. if (skb_is_nonlinear(skb)) {
  643. /* we have to copy */
  644. local_bh_disable();
  645. if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) {
  646. local_bh_enable();
  647. /* checksum error */
  648. skb_free_datagram(svsk->sk_sk, skb);
  649. return 0;
  650. }
  651. local_bh_enable();
  652. skb_free_datagram(svsk->sk_sk, skb);
  653. } else {
  654. /* we can use it in-place */
  655. rqstp->rq_arg.head[0].iov_base = skb->data + sizeof(struct udphdr);
  656. rqstp->rq_arg.head[0].iov_len = len;
  657. if (skb_checksum_complete(skb)) {
  658. skb_free_datagram(svsk->sk_sk, skb);
  659. return 0;
  660. }
  661. rqstp->rq_skbuff = skb;
  662. }
  663. rqstp->rq_arg.page_base = 0;
  664. if (len <= rqstp->rq_arg.head[0].iov_len) {
  665. rqstp->rq_arg.head[0].iov_len = len;
  666. rqstp->rq_arg.page_len = 0;
  667. rqstp->rq_respages = rqstp->rq_pages+1;
  668. } else {
  669. rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
  670. rqstp->rq_respages = rqstp->rq_pages + 1 +
  671. (rqstp->rq_arg.page_len + PAGE_SIZE - 1)/ PAGE_SIZE;
  672. }
  673. if (serv->sv_stats)
  674. serv->sv_stats->netudpcnt++;
  675. return len;
  676. }
  677. static int
  678. svc_udp_sendto(struct svc_rqst *rqstp)
  679. {
  680. int error;
  681. error = svc_sendto(rqstp, &rqstp->rq_res);
  682. if (error == -ECONNREFUSED)
  683. /* ICMP error on earlier request. */
  684. error = svc_sendto(rqstp, &rqstp->rq_res);
  685. return error;
  686. }
  687. static void
  688. svc_udp_init(struct svc_sock *svsk)
  689. {
  690. svsk->sk_sk->sk_data_ready = svc_udp_data_ready;
  691. svsk->sk_sk->sk_write_space = svc_write_space;
  692. svsk->sk_recvfrom = svc_udp_recvfrom;
  693. svsk->sk_sendto = svc_udp_sendto;
  694. /* initialise setting must have enough space to
  695. * receive and respond to one request.
  696. * svc_udp_recvfrom will re-adjust if necessary
  697. */
  698. svc_sock_setbufsize(svsk->sk_sock,
  699. 3 * svsk->sk_server->sv_max_mesg,
  700. 3 * svsk->sk_server->sv_max_mesg);
  701. set_bit(SK_DATA, &svsk->sk_flags); /* might have come in before data_ready set up */
  702. set_bit(SK_CHNGBUF, &svsk->sk_flags);
  703. }
  704. /*
  705. * A data_ready event on a listening socket means there's a connection
  706. * pending. Do not use state_change as a substitute for it.
  707. */
  708. static void
  709. svc_tcp_listen_data_ready(struct sock *sk, int count_unused)
  710. {
  711. struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
  712. dprintk("svc: socket %p TCP (listen) state change %d\n",
  713. sk, sk->sk_state);
  714. /*
  715. * This callback may called twice when a new connection
  716. * is established as a child socket inherits everything
  717. * from a parent LISTEN socket.
  718. * 1) data_ready method of the parent socket will be called
  719. * when one of child sockets become ESTABLISHED.
  720. * 2) data_ready method of the child socket may be called
  721. * when it receives data before the socket is accepted.
  722. * In case of 2, we should ignore it silently.
  723. */
  724. if (sk->sk_state == TCP_LISTEN) {
  725. if (svsk) {
  726. set_bit(SK_CONN, &svsk->sk_flags);
  727. svc_sock_enqueue(svsk);
  728. } else
  729. printk("svc: socket %p: no user data\n", sk);
  730. }
  731. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  732. wake_up_interruptible_all(sk->sk_sleep);
  733. }
  734. /*
  735. * A state change on a connected socket means it's dying or dead.
  736. */
  737. static void
  738. svc_tcp_state_change(struct sock *sk)
  739. {
  740. struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
  741. dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
  742. sk, sk->sk_state, sk->sk_user_data);
  743. if (!svsk)
  744. printk("svc: socket %p: no user data\n", sk);
  745. else {
  746. set_bit(SK_CLOSE, &svsk->sk_flags);
  747. svc_sock_enqueue(svsk);
  748. }
  749. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  750. wake_up_interruptible_all(sk->sk_sleep);
  751. }
  752. static void
  753. svc_tcp_data_ready(struct sock *sk, int count)
  754. {
  755. struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
  756. dprintk("svc: socket %p TCP data ready (svsk %p)\n",
  757. sk, sk->sk_user_data);
  758. if (svsk) {
  759. set_bit(SK_DATA, &svsk->sk_flags);
  760. svc_sock_enqueue(svsk);
  761. }
  762. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  763. wake_up_interruptible(sk->sk_sleep);
  764. }
  765. /*
  766. * Accept a TCP connection
  767. */
  768. static void
  769. svc_tcp_accept(struct svc_sock *svsk)
  770. {
  771. struct sockaddr_in sin;
  772. struct svc_serv *serv = svsk->sk_server;
  773. struct socket *sock = svsk->sk_sock;
  774. struct socket *newsock;
  775. struct svc_sock *newsvsk;
  776. int err, slen;
  777. dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
  778. if (!sock)
  779. return;
  780. clear_bit(SK_CONN, &svsk->sk_flags);
  781. err = kernel_accept(sock, &newsock, O_NONBLOCK);
  782. if (err < 0) {
  783. if (err == -ENOMEM)
  784. printk(KERN_WARNING "%s: no more sockets!\n",
  785. serv->sv_name);
  786. else if (err != -EAGAIN && net_ratelimit())
  787. printk(KERN_WARNING "%s: accept failed (err %d)!\n",
  788. serv->sv_name, -err);
  789. return;
  790. }
  791. set_bit(SK_CONN, &svsk->sk_flags);
  792. svc_sock_enqueue(svsk);
  793. slen = sizeof(sin);
  794. err = kernel_getpeername(newsock, (struct sockaddr *) &sin, &slen);
  795. if (err < 0) {
  796. if (net_ratelimit())
  797. printk(KERN_WARNING "%s: peername failed (err %d)!\n",
  798. serv->sv_name, -err);
  799. goto failed; /* aborted connection or whatever */
  800. }
  801. /* Ideally, we would want to reject connections from unauthorized
  802. * hosts here, but when we get encription, the IP of the host won't
  803. * tell us anything. For now just warn about unpriv connections.
  804. */
  805. if (ntohs(sin.sin_port) >= 1024) {
  806. dprintk(KERN_WARNING
  807. "%s: connect from unprivileged port: %u.%u.%u.%u:%d\n",
  808. serv->sv_name,
  809. NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
  810. }
  811. dprintk("%s: connect from %u.%u.%u.%u:%04x\n", serv->sv_name,
  812. NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
  813. /* make sure that a write doesn't block forever when
  814. * low on memory
  815. */
  816. newsock->sk->sk_sndtimeo = HZ*30;
  817. if (!(newsvsk = svc_setup_socket(serv, newsock, &err,
  818. (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY))))
  819. goto failed;
  820. memcpy(&newsvsk->sk_remote, &sin, slen);
  821. newsvsk->sk_remotelen = slen;
  822. svc_sock_received(newsvsk);
  823. /* make sure that we don't have too many active connections.
  824. * If we have, something must be dropped.
  825. *
  826. * There's no point in trying to do random drop here for
  827. * DoS prevention. The NFS clients does 1 reconnect in 15
  828. * seconds. An attacker can easily beat that.
  829. *
  830. * The only somewhat efficient mechanism would be if drop
  831. * old connections from the same IP first. But right now
  832. * we don't even record the client IP in svc_sock.
  833. */
  834. if (serv->sv_tmpcnt > (serv->sv_nrthreads+3)*20) {
  835. struct svc_sock *svsk = NULL;
  836. spin_lock_bh(&serv->sv_lock);
  837. if (!list_empty(&serv->sv_tempsocks)) {
  838. if (net_ratelimit()) {
  839. /* Try to help the admin */
  840. printk(KERN_NOTICE "%s: too many open TCP "
  841. "sockets, consider increasing the "
  842. "number of nfsd threads\n",
  843. serv->sv_name);
  844. printk(KERN_NOTICE "%s: last TCP connect from "
  845. "%u.%u.%u.%u:%d\n",
  846. serv->sv_name,
  847. NIPQUAD(sin.sin_addr.s_addr),
  848. ntohs(sin.sin_port));
  849. }
  850. /*
  851. * Always select the oldest socket. It's not fair,
  852. * but so is life
  853. */
  854. svsk = list_entry(serv->sv_tempsocks.prev,
  855. struct svc_sock,
  856. sk_list);
  857. set_bit(SK_CLOSE, &svsk->sk_flags);
  858. atomic_inc(&svsk->sk_inuse);
  859. }
  860. spin_unlock_bh(&serv->sv_lock);
  861. if (svsk) {
  862. svc_sock_enqueue(svsk);
  863. svc_sock_put(svsk);
  864. }
  865. }
  866. if (serv->sv_stats)
  867. serv->sv_stats->nettcpconn++;
  868. return;
  869. failed:
  870. sock_release(newsock);
  871. return;
  872. }
  873. /*
  874. * Receive data from a TCP socket.
  875. */
  876. static int
  877. svc_tcp_recvfrom(struct svc_rqst *rqstp)
  878. {
  879. struct svc_sock *svsk = rqstp->rq_sock;
  880. struct svc_serv *serv = svsk->sk_server;
  881. int len;
  882. struct kvec *vec;
  883. int pnum, vlen;
  884. dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
  885. svsk, test_bit(SK_DATA, &svsk->sk_flags),
  886. test_bit(SK_CONN, &svsk->sk_flags),
  887. test_bit(SK_CLOSE, &svsk->sk_flags));
  888. if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) {
  889. svc_sock_received(svsk);
  890. return svc_deferred_recv(rqstp);
  891. }
  892. if (test_bit(SK_CLOSE, &svsk->sk_flags)) {
  893. svc_delete_socket(svsk);
  894. return 0;
  895. }
  896. if (svsk->sk_sk->sk_state == TCP_LISTEN) {
  897. svc_tcp_accept(svsk);
  898. svc_sock_received(svsk);
  899. return 0;
  900. }
  901. if (test_and_clear_bit(SK_CHNGBUF, &svsk->sk_flags))
  902. /* sndbuf needs to have room for one request
  903. * per thread, otherwise we can stall even when the
  904. * network isn't a bottleneck.
  905. *
  906. * We count all threads rather than threads in a
  907. * particular pool, which provides an upper bound
  908. * on the number of threads which will access the socket.
  909. *
  910. * rcvbuf just needs to be able to hold a few requests.
  911. * Normally they will be removed from the queue
  912. * as soon a a complete request arrives.
  913. */
  914. svc_sock_setbufsize(svsk->sk_sock,
  915. (serv->sv_nrthreads+3) * serv->sv_max_mesg,
  916. 3 * serv->sv_max_mesg);
  917. clear_bit(SK_DATA, &svsk->sk_flags);
  918. /* Receive data. If we haven't got the record length yet, get
  919. * the next four bytes. Otherwise try to gobble up as much as
  920. * possible up to the complete record length.
  921. */
  922. if (svsk->sk_tcplen < 4) {
  923. unsigned long want = 4 - svsk->sk_tcplen;
  924. struct kvec iov;
  925. iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
  926. iov.iov_len = want;
  927. if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0)
  928. goto error;
  929. svsk->sk_tcplen += len;
  930. if (len < want) {
  931. dprintk("svc: short recvfrom while reading record length (%d of %lu)\n",
  932. len, want);
  933. svc_sock_received(svsk);
  934. return -EAGAIN; /* record header not complete */
  935. }
  936. svsk->sk_reclen = ntohl(svsk->sk_reclen);
  937. if (!(svsk->sk_reclen & 0x80000000)) {
  938. /* FIXME: technically, a record can be fragmented,
  939. * and non-terminal fragments will not have the top
  940. * bit set in the fragment length header.
  941. * But apparently no known nfs clients send fragmented
  942. * records. */
  943. if (net_ratelimit())
  944. printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx"
  945. " (non-terminal)\n",
  946. (unsigned long) svsk->sk_reclen);
  947. goto err_delete;
  948. }
  949. svsk->sk_reclen &= 0x7fffffff;
  950. dprintk("svc: TCP record, %d bytes\n", svsk->sk_reclen);
  951. if (svsk->sk_reclen > serv->sv_max_mesg) {
  952. if (net_ratelimit())
  953. printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx"
  954. " (large)\n",
  955. (unsigned long) svsk->sk_reclen);
  956. goto err_delete;
  957. }
  958. }
  959. /* Check whether enough data is available */
  960. len = svc_recv_available(svsk);
  961. if (len < 0)
  962. goto error;
  963. if (len < svsk->sk_reclen) {
  964. dprintk("svc: incomplete TCP record (%d of %d)\n",
  965. len, svsk->sk_reclen);
  966. svc_sock_received(svsk);
  967. return -EAGAIN; /* record not complete */
  968. }
  969. len = svsk->sk_reclen;
  970. set_bit(SK_DATA, &svsk->sk_flags);
  971. vec = rqstp->rq_vec;
  972. vec[0] = rqstp->rq_arg.head[0];
  973. vlen = PAGE_SIZE;
  974. pnum = 1;
  975. while (vlen < len) {
  976. vec[pnum].iov_base = page_address(rqstp->rq_pages[pnum]);
  977. vec[pnum].iov_len = PAGE_SIZE;
  978. pnum++;
  979. vlen += PAGE_SIZE;
  980. }
  981. rqstp->rq_respages = &rqstp->rq_pages[pnum];
  982. /* Now receive data */
  983. len = svc_recvfrom(rqstp, vec, pnum, len);
  984. if (len < 0)
  985. goto error;
  986. dprintk("svc: TCP complete record (%d bytes)\n", len);
  987. rqstp->rq_arg.len = len;
  988. rqstp->rq_arg.page_base = 0;
  989. if (len <= rqstp->rq_arg.head[0].iov_len) {
  990. rqstp->rq_arg.head[0].iov_len = len;
  991. rqstp->rq_arg.page_len = 0;
  992. } else {
  993. rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
  994. }
  995. rqstp->rq_skbuff = NULL;
  996. rqstp->rq_prot = IPPROTO_TCP;
  997. /* Reset TCP read info */
  998. svsk->sk_reclen = 0;
  999. svsk->sk_tcplen = 0;
  1000. svc_sock_received(svsk);
  1001. if (serv->sv_stats)
  1002. serv->sv_stats->nettcpcnt++;
  1003. return len;
  1004. err_delete:
  1005. svc_delete_socket(svsk);
  1006. return -EAGAIN;
  1007. error:
  1008. if (len == -EAGAIN) {
  1009. dprintk("RPC: TCP recvfrom got EAGAIN\n");
  1010. svc_sock_received(svsk);
  1011. } else {
  1012. printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
  1013. svsk->sk_server->sv_name, -len);
  1014. goto err_delete;
  1015. }
  1016. return len;
  1017. }
  1018. /*
  1019. * Send out data on TCP socket.
  1020. */
  1021. static int
  1022. svc_tcp_sendto(struct svc_rqst *rqstp)
  1023. {
  1024. struct xdr_buf *xbufp = &rqstp->rq_res;
  1025. int sent;
  1026. __be32 reclen;
  1027. /* Set up the first element of the reply kvec.
  1028. * Any other kvecs that may be in use have been taken
  1029. * care of by the server implementation itself.
  1030. */
  1031. reclen = htonl(0x80000000|((xbufp->len ) - 4));
  1032. memcpy(xbufp->head[0].iov_base, &reclen, 4);
  1033. if (test_bit(SK_DEAD, &rqstp->rq_sock->sk_flags))
  1034. return -ENOTCONN;
  1035. sent = svc_sendto(rqstp, &rqstp->rq_res);
  1036. if (sent != xbufp->len) {
  1037. printk(KERN_NOTICE "rpc-srv/tcp: %s: %s %d when sending %d bytes - shutting down socket\n",
  1038. rqstp->rq_sock->sk_server->sv_name,
  1039. (sent<0)?"got error":"sent only",
  1040. sent, xbufp->len);
  1041. set_bit(SK_CLOSE, &rqstp->rq_sock->sk_flags);
  1042. svc_sock_enqueue(rqstp->rq_sock);
  1043. sent = -EAGAIN;
  1044. }
  1045. return sent;
  1046. }
  1047. static void
  1048. svc_tcp_init(struct svc_sock *svsk)
  1049. {
  1050. struct sock *sk = svsk->sk_sk;
  1051. struct tcp_sock *tp = tcp_sk(sk);
  1052. svsk->sk_recvfrom = svc_tcp_recvfrom;
  1053. svsk->sk_sendto = svc_tcp_sendto;
  1054. if (sk->sk_state == TCP_LISTEN) {
  1055. dprintk("setting up TCP socket for listening\n");
  1056. sk->sk_data_ready = svc_tcp_listen_data_ready;
  1057. set_bit(SK_CONN, &svsk->sk_flags);
  1058. } else {
  1059. dprintk("setting up TCP socket for reading\n");
  1060. sk->sk_state_change = svc_tcp_state_change;
  1061. sk->sk_data_ready = svc_tcp_data_ready;
  1062. sk->sk_write_space = svc_write_space;
  1063. svsk->sk_reclen = 0;
  1064. svsk->sk_tcplen = 0;
  1065. tp->nonagle = 1; /* disable Nagle's algorithm */
  1066. /* initialise setting must have enough space to
  1067. * receive and respond to one request.
  1068. * svc_tcp_recvfrom will re-adjust if necessary
  1069. */
  1070. svc_sock_setbufsize(svsk->sk_sock,
  1071. 3 * svsk->sk_server->sv_max_mesg,
  1072. 3 * svsk->sk_server->sv_max_mesg);
  1073. set_bit(SK_CHNGBUF, &svsk->sk_flags);
  1074. set_bit(SK_DATA, &svsk->sk_flags);
  1075. if (sk->sk_state != TCP_ESTABLISHED)
  1076. set_bit(SK_CLOSE, &svsk->sk_flags);
  1077. }
  1078. }
  1079. void
  1080. svc_sock_update_bufs(struct svc_serv *serv)
  1081. {
  1082. /*
  1083. * The number of server threads has changed. Update
  1084. * rcvbuf and sndbuf accordingly on all sockets
  1085. */
  1086. struct list_head *le;
  1087. spin_lock_bh(&serv->sv_lock);
  1088. list_for_each(le, &serv->sv_permsocks) {
  1089. struct svc_sock *svsk =
  1090. list_entry(le, struct svc_sock, sk_list);
  1091. set_bit(SK_CHNGBUF, &svsk->sk_flags);
  1092. }
  1093. list_for_each(le, &serv->sv_tempsocks) {
  1094. struct svc_sock *svsk =
  1095. list_entry(le, struct svc_sock, sk_list);
  1096. set_bit(SK_CHNGBUF, &svsk->sk_flags);
  1097. }
  1098. spin_unlock_bh(&serv->sv_lock);
  1099. }
  1100. /*
  1101. * Receive the next request on any socket. This code is carefully
  1102. * organised not to touch any cachelines in the shared svc_serv
  1103. * structure, only cachelines in the local svc_pool.
  1104. */
  1105. int
  1106. svc_recv(struct svc_rqst *rqstp, long timeout)
  1107. {
  1108. struct svc_sock *svsk =NULL;
  1109. struct svc_serv *serv = rqstp->rq_server;
  1110. struct svc_pool *pool = rqstp->rq_pool;
  1111. int len, i;
  1112. int pages;
  1113. struct xdr_buf *arg;
  1114. DECLARE_WAITQUEUE(wait, current);
  1115. dprintk("svc: server %p waiting for data (to = %ld)\n",
  1116. rqstp, timeout);
  1117. if (rqstp->rq_sock)
  1118. printk(KERN_ERR
  1119. "svc_recv: service %p, socket not NULL!\n",
  1120. rqstp);
  1121. if (waitqueue_active(&rqstp->rq_wait))
  1122. printk(KERN_ERR
  1123. "svc_recv: service %p, wait queue active!\n",
  1124. rqstp);
  1125. /* now allocate needed pages. If we get a failure, sleep briefly */
  1126. pages = (serv->sv_max_mesg + PAGE_SIZE) / PAGE_SIZE;
  1127. for (i=0; i < pages ; i++)
  1128. while (rqstp->rq_pages[i] == NULL) {
  1129. struct page *p = alloc_page(GFP_KERNEL);
  1130. if (!p)
  1131. schedule_timeout_uninterruptible(msecs_to_jiffies(500));
  1132. rqstp->rq_pages[i] = p;
  1133. }
  1134. rqstp->rq_pages[i++] = NULL; /* this might be seen in nfs_read_actor */
  1135. BUG_ON(pages >= RPCSVC_MAXPAGES);
  1136. /* Make arg->head point to first page and arg->pages point to rest */
  1137. arg = &rqstp->rq_arg;
  1138. arg->head[0].iov_base = page_address(rqstp->rq_pages[0]);
  1139. arg->head[0].iov_len = PAGE_SIZE;
  1140. arg->pages = rqstp->rq_pages + 1;
  1141. arg->page_base = 0;
  1142. /* save at least one page for response */
  1143. arg->page_len = (pages-2)*PAGE_SIZE;
  1144. arg->len = (pages-1)*PAGE_SIZE;
  1145. arg->tail[0].iov_len = 0;
  1146. try_to_freeze();
  1147. cond_resched();
  1148. if (signalled())
  1149. return -EINTR;
  1150. spin_lock_bh(&pool->sp_lock);
  1151. if ((svsk = svc_sock_dequeue(pool)) != NULL) {
  1152. rqstp->rq_sock = svsk;
  1153. atomic_inc(&svsk->sk_inuse);
  1154. rqstp->rq_reserved = serv->sv_max_mesg;
  1155. atomic_add(rqstp->rq_reserved, &svsk->sk_reserved);
  1156. } else {
  1157. /* No data pending. Go to sleep */
  1158. svc_thread_enqueue(pool, rqstp);
  1159. /*
  1160. * We have to be able to interrupt this wait
  1161. * to bring down the daemons ...
  1162. */
  1163. set_current_state(TASK_INTERRUPTIBLE);
  1164. add_wait_queue(&rqstp->rq_wait, &wait);
  1165. spin_unlock_bh(&pool->sp_lock);
  1166. schedule_timeout(timeout);
  1167. try_to_freeze();
  1168. spin_lock_bh(&pool->sp_lock);
  1169. remove_wait_queue(&rqstp->rq_wait, &wait);
  1170. if (!(svsk = rqstp->rq_sock)) {
  1171. svc_thread_dequeue(pool, rqstp);
  1172. spin_unlock_bh(&pool->sp_lock);
  1173. dprintk("svc: server %p, no data yet\n", rqstp);
  1174. return signalled()? -EINTR : -EAGAIN;
  1175. }
  1176. }
  1177. spin_unlock_bh(&pool->sp_lock);
  1178. dprintk("svc: server %p, pool %u, socket %p, inuse=%d\n",
  1179. rqstp, pool->sp_id, svsk, atomic_read(&svsk->sk_inuse));
  1180. len = svsk->sk_recvfrom(rqstp);
  1181. dprintk("svc: got len=%d\n", len);
  1182. /* No data, incomplete (TCP) read, or accept() */
  1183. if (len == 0 || len == -EAGAIN) {
  1184. rqstp->rq_res.len = 0;
  1185. svc_sock_release(rqstp);
  1186. return -EAGAIN;
  1187. }
  1188. svsk->sk_lastrecv = get_seconds();
  1189. clear_bit(SK_OLD, &svsk->sk_flags);
  1190. rqstp->rq_secure = ntohs(rqstp->rq_addr.sin_port) < 1024;
  1191. rqstp->rq_chandle.defer = svc_defer;
  1192. if (serv->sv_stats)
  1193. serv->sv_stats->netcnt++;
  1194. return len;
  1195. }
  1196. /*
  1197. * Drop request
  1198. */
  1199. void
  1200. svc_drop(struct svc_rqst *rqstp)
  1201. {
  1202. dprintk("svc: socket %p dropped request\n", rqstp->rq_sock);
  1203. svc_sock_release(rqstp);
  1204. }
  1205. /*
  1206. * Return reply to client.
  1207. */
  1208. int
  1209. svc_send(struct svc_rqst *rqstp)
  1210. {
  1211. struct svc_sock *svsk;
  1212. int len;
  1213. struct xdr_buf *xb;
  1214. if ((svsk = rqstp->rq_sock) == NULL) {
  1215. printk(KERN_WARNING "NULL socket pointer in %s:%d\n",
  1216. __FILE__, __LINE__);
  1217. return -EFAULT;
  1218. }
  1219. /* release the receive skb before sending the reply */
  1220. svc_release_skb(rqstp);
  1221. /* calculate over-all length */
  1222. xb = & rqstp->rq_res;
  1223. xb->len = xb->head[0].iov_len +
  1224. xb->page_len +
  1225. xb->tail[0].iov_len;
  1226. /* Grab svsk->sk_mutex to serialize outgoing data. */
  1227. mutex_lock(&svsk->sk_mutex);
  1228. if (test_bit(SK_DEAD, &svsk->sk_flags))
  1229. len = -ENOTCONN;
  1230. else
  1231. len = svsk->sk_sendto(rqstp);
  1232. mutex_unlock(&svsk->sk_mutex);
  1233. svc_sock_release(rqstp);
  1234. if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN)
  1235. return 0;
  1236. return len;
  1237. }
  1238. /*
  1239. * Timer function to close old temporary sockets, using
  1240. * a mark-and-sweep algorithm.
  1241. */
  1242. static void
  1243. svc_age_temp_sockets(unsigned long closure)
  1244. {
  1245. struct svc_serv *serv = (struct svc_serv *)closure;
  1246. struct svc_sock *svsk;
  1247. struct list_head *le, *next;
  1248. LIST_HEAD(to_be_aged);
  1249. dprintk("svc_age_temp_sockets\n");
  1250. if (!spin_trylock_bh(&serv->sv_lock)) {
  1251. /* busy, try again 1 sec later */
  1252. dprintk("svc_age_temp_sockets: busy\n");
  1253. mod_timer(&serv->sv_temptimer, jiffies + HZ);
  1254. return;
  1255. }
  1256. list_for_each_safe(le, next, &serv->sv_tempsocks) {
  1257. svsk = list_entry(le, struct svc_sock, sk_list);
  1258. if (!test_and_set_bit(SK_OLD, &svsk->sk_flags))
  1259. continue;
  1260. if (atomic_read(&svsk->sk_inuse) || test_bit(SK_BUSY, &svsk->sk_flags))
  1261. continue;
  1262. atomic_inc(&svsk->sk_inuse);
  1263. list_move(le, &to_be_aged);
  1264. set_bit(SK_CLOSE, &svsk->sk_flags);
  1265. set_bit(SK_DETACHED, &svsk->sk_flags);
  1266. }
  1267. spin_unlock_bh(&serv->sv_lock);
  1268. while (!list_empty(&to_be_aged)) {
  1269. le = to_be_aged.next;
  1270. /* fiddling the sk_list node is safe 'cos we're SK_DETACHED */
  1271. list_del_init(le);
  1272. svsk = list_entry(le, struct svc_sock, sk_list);
  1273. dprintk("queuing svsk %p for closing, %lu seconds old\n",
  1274. svsk, get_seconds() - svsk->sk_lastrecv);
  1275. /* a thread will dequeue and close it soon */
  1276. svc_sock_enqueue(svsk);
  1277. svc_sock_put(svsk);
  1278. }
  1279. mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ);
  1280. }
  1281. /*
  1282. * Initialize socket for RPC use and create svc_sock struct
  1283. * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF.
  1284. */
  1285. static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
  1286. struct socket *sock,
  1287. int *errp, int flags)
  1288. {
  1289. struct svc_sock *svsk;
  1290. struct sock *inet;
  1291. int pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
  1292. int is_temporary = flags & SVC_SOCK_TEMPORARY;
  1293. dprintk("svc: svc_setup_socket %p\n", sock);
  1294. if (!(svsk = kzalloc(sizeof(*svsk), GFP_KERNEL))) {
  1295. *errp = -ENOMEM;
  1296. return NULL;
  1297. }
  1298. inet = sock->sk;
  1299. /* Register socket with portmapper */
  1300. if (*errp >= 0 && pmap_register)
  1301. *errp = svc_register(serv, inet->sk_protocol,
  1302. ntohs(inet_sk(inet)->sport));
  1303. if (*errp < 0) {
  1304. kfree(svsk);
  1305. return NULL;
  1306. }
  1307. set_bit(SK_BUSY, &svsk->sk_flags);
  1308. inet->sk_user_data = svsk;
  1309. svsk->sk_sock = sock;
  1310. svsk->sk_sk = inet;
  1311. svsk->sk_ostate = inet->sk_state_change;
  1312. svsk->sk_odata = inet->sk_data_ready;
  1313. svsk->sk_owspace = inet->sk_write_space;
  1314. svsk->sk_server = serv;
  1315. atomic_set(&svsk->sk_inuse, 1);
  1316. svsk->sk_lastrecv = get_seconds();
  1317. spin_lock_init(&svsk->sk_defer_lock);
  1318. INIT_LIST_HEAD(&svsk->sk_deferred);
  1319. INIT_LIST_HEAD(&svsk->sk_ready);
  1320. mutex_init(&svsk->sk_mutex);
  1321. /* Initialize the socket */
  1322. if (sock->type == SOCK_DGRAM)
  1323. svc_udp_init(svsk);
  1324. else
  1325. svc_tcp_init(svsk);
  1326. spin_lock_bh(&serv->sv_lock);
  1327. if (is_temporary) {
  1328. set_bit(SK_TEMP, &svsk->sk_flags);
  1329. list_add(&svsk->sk_list, &serv->sv_tempsocks);
  1330. serv->sv_tmpcnt++;
  1331. if (serv->sv_temptimer.function == NULL) {
  1332. /* setup timer to age temp sockets */
  1333. setup_timer(&serv->sv_temptimer, svc_age_temp_sockets,
  1334. (unsigned long)serv);
  1335. mod_timer(&serv->sv_temptimer,
  1336. jiffies + svc_conn_age_period * HZ);
  1337. }
  1338. } else {
  1339. clear_bit(SK_TEMP, &svsk->sk_flags);
  1340. list_add(&svsk->sk_list, &serv->sv_permsocks);
  1341. }
  1342. spin_unlock_bh(&serv->sv_lock);
  1343. dprintk("svc: svc_setup_socket created %p (inet %p)\n",
  1344. svsk, svsk->sk_sk);
  1345. return svsk;
  1346. }
  1347. int svc_addsock(struct svc_serv *serv,
  1348. int fd,
  1349. char *name_return,
  1350. int *proto)
  1351. {
  1352. int err = 0;
  1353. struct socket *so = sockfd_lookup(fd, &err);
  1354. struct svc_sock *svsk = NULL;
  1355. if (!so)
  1356. return err;
  1357. if (so->sk->sk_family != AF_INET)
  1358. err = -EAFNOSUPPORT;
  1359. else if (so->sk->sk_protocol != IPPROTO_TCP &&
  1360. so->sk->sk_protocol != IPPROTO_UDP)
  1361. err = -EPROTONOSUPPORT;
  1362. else if (so->state > SS_UNCONNECTED)
  1363. err = -EISCONN;
  1364. else {
  1365. svsk = svc_setup_socket(serv, so, &err, SVC_SOCK_DEFAULTS);
  1366. if (svsk) {
  1367. svc_sock_received(svsk);
  1368. err = 0;
  1369. }
  1370. }
  1371. if (err) {
  1372. sockfd_put(so);
  1373. return err;
  1374. }
  1375. if (proto) *proto = so->sk->sk_protocol;
  1376. return one_sock_name(name_return, svsk);
  1377. }
  1378. EXPORT_SYMBOL_GPL(svc_addsock);
  1379. /*
  1380. * Create socket for RPC service.
  1381. */
  1382. static int svc_create_socket(struct svc_serv *serv, int protocol,
  1383. struct sockaddr_in *sin, int flags)
  1384. {
  1385. struct svc_sock *svsk;
  1386. struct socket *sock;
  1387. int error;
  1388. int type;
  1389. dprintk("svc: svc_create_socket(%s, %d, %u.%u.%u.%u:%d)\n",
  1390. serv->sv_program->pg_name, protocol,
  1391. NIPQUAD(sin->sin_addr.s_addr),
  1392. ntohs(sin->sin_port));
  1393. if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
  1394. printk(KERN_WARNING "svc: only UDP and TCP "
  1395. "sockets supported\n");
  1396. return -EINVAL;
  1397. }
  1398. type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
  1399. if ((error = sock_create_kern(PF_INET, type, protocol, &sock)) < 0)
  1400. return error;
  1401. svc_reclassify_socket(sock);
  1402. if (type == SOCK_STREAM)
  1403. sock->sk->sk_reuse = 1; /* allow address reuse */
  1404. error = kernel_bind(sock, (struct sockaddr *) sin,
  1405. sizeof(*sin));
  1406. if (error < 0)
  1407. goto bummer;
  1408. if (protocol == IPPROTO_TCP) {
  1409. if ((error = kernel_listen(sock, 64)) < 0)
  1410. goto bummer;
  1411. }
  1412. if ((svsk = svc_setup_socket(serv, sock, &error, flags)) != NULL) {
  1413. svc_sock_received(svsk);
  1414. return ntohs(inet_sk(svsk->sk_sk)->sport);
  1415. }
  1416. bummer:
  1417. dprintk("svc: svc_create_socket error = %d\n", -error);
  1418. sock_release(sock);
  1419. return error;
  1420. }
  1421. /*
  1422. * Remove a dead socket
  1423. */
  1424. static void
  1425. svc_delete_socket(struct svc_sock *svsk)
  1426. {
  1427. struct svc_serv *serv;
  1428. struct sock *sk;
  1429. dprintk("svc: svc_delete_socket(%p)\n", svsk);
  1430. serv = svsk->sk_server;
  1431. sk = svsk->sk_sk;
  1432. sk->sk_state_change = svsk->sk_ostate;
  1433. sk->sk_data_ready = svsk->sk_odata;
  1434. sk->sk_write_space = svsk->sk_owspace;
  1435. spin_lock_bh(&serv->sv_lock);
  1436. if (!test_and_set_bit(SK_DETACHED, &svsk->sk_flags))
  1437. list_del_init(&svsk->sk_list);
  1438. /*
  1439. * We used to delete the svc_sock from whichever list
  1440. * it's sk_ready node was on, but we don't actually
  1441. * need to. This is because the only time we're called
  1442. * while still attached to a queue, the queue itself
  1443. * is about to be destroyed (in svc_destroy).
  1444. */
  1445. if (!test_and_set_bit(SK_DEAD, &svsk->sk_flags)) {
  1446. BUG_ON(atomic_read(&svsk->sk_inuse)<2);
  1447. atomic_dec(&svsk->sk_inuse);
  1448. if (test_bit(SK_TEMP, &svsk->sk_flags))
  1449. serv->sv_tmpcnt--;
  1450. }
  1451. spin_unlock_bh(&serv->sv_lock);
  1452. }
  1453. void svc_close_socket(struct svc_sock *svsk)
  1454. {
  1455. set_bit(SK_CLOSE, &svsk->sk_flags);
  1456. if (test_and_set_bit(SK_BUSY, &svsk->sk_flags))
  1457. /* someone else will have to effect the close */
  1458. return;
  1459. atomic_inc(&svsk->sk_inuse);
  1460. svc_delete_socket(svsk);
  1461. clear_bit(SK_BUSY, &svsk->sk_flags);
  1462. svc_sock_put(svsk);
  1463. }
  1464. /**
  1465. * svc_makesock - Make a socket for nfsd and lockd
  1466. * @serv: RPC server structure
  1467. * @protocol: transport protocol to use
  1468. * @port: port to use
  1469. * @flags: requested socket characteristics
  1470. *
  1471. */
  1472. int svc_makesock(struct svc_serv *serv, int protocol, unsigned short port,
  1473. int flags)
  1474. {
  1475. struct sockaddr_in sin = {
  1476. .sin_family = AF_INET,
  1477. .sin_addr.s_addr = INADDR_ANY,
  1478. .sin_port = htons(port),
  1479. };
  1480. dprintk("svc: creating socket proto = %d\n", protocol);
  1481. return svc_create_socket(serv, protocol, &sin, flags);
  1482. }
  1483. /*
  1484. * Handle defer and revisit of requests
  1485. */
  1486. static void svc_revisit(struct cache_deferred_req *dreq, int too_many)
  1487. {
  1488. struct svc_deferred_req *dr = container_of(dreq, struct svc_deferred_req, handle);
  1489. struct svc_sock *svsk;
  1490. if (too_many) {
  1491. svc_sock_put(dr->svsk);
  1492. kfree(dr);
  1493. return;
  1494. }
  1495. dprintk("revisit queued\n");
  1496. svsk = dr->svsk;
  1497. dr->svsk = NULL;
  1498. spin_lock_bh(&svsk->sk_defer_lock);
  1499. list_add(&dr->handle.recent, &svsk->sk_deferred);
  1500. spin_unlock_bh(&svsk->sk_defer_lock);
  1501. set_bit(SK_DEFERRED, &svsk->sk_flags);
  1502. svc_sock_enqueue(svsk);
  1503. svc_sock_put(svsk);
  1504. }
  1505. static struct cache_deferred_req *
  1506. svc_defer(struct cache_req *req)
  1507. {
  1508. struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle);
  1509. int size = sizeof(struct svc_deferred_req) + (rqstp->rq_arg.len);
  1510. struct svc_deferred_req *dr;
  1511. if (rqstp->rq_arg.page_len)
  1512. return NULL; /* if more than a page, give up FIXME */
  1513. if (rqstp->rq_deferred) {
  1514. dr = rqstp->rq_deferred;
  1515. rqstp->rq_deferred = NULL;
  1516. } else {
  1517. int skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
  1518. /* FIXME maybe discard if size too large */
  1519. dr = kmalloc(size, GFP_KERNEL);
  1520. if (dr == NULL)
  1521. return NULL;
  1522. dr->handle.owner = rqstp->rq_server;
  1523. dr->prot = rqstp->rq_prot;
  1524. dr->addr = rqstp->rq_addr;
  1525. dr->daddr = rqstp->rq_daddr;
  1526. dr->argslen = rqstp->rq_arg.len >> 2;
  1527. memcpy(dr->args, rqstp->rq_arg.head[0].iov_base-skip, dr->argslen<<2);
  1528. }
  1529. atomic_inc(&rqstp->rq_sock->sk_inuse);
  1530. dr->svsk = rqstp->rq_sock;
  1531. dr->handle.revisit = svc_revisit;
  1532. return &dr->handle;
  1533. }
  1534. /*
  1535. * recv data from a deferred request into an active one
  1536. */
  1537. static int svc_deferred_recv(struct svc_rqst *rqstp)
  1538. {
  1539. struct svc_deferred_req *dr = rqstp->rq_deferred;
  1540. rqstp->rq_arg.head[0].iov_base = dr->args;
  1541. rqstp->rq_arg.head[0].iov_len = dr->argslen<<2;
  1542. rqstp->rq_arg.page_len = 0;
  1543. rqstp->rq_arg.len = dr->argslen<<2;
  1544. rqstp->rq_prot = dr->prot;
  1545. rqstp->rq_addr = dr->addr;
  1546. rqstp->rq_daddr = dr->daddr;
  1547. rqstp->rq_respages = rqstp->rq_pages;
  1548. return dr->argslen<<2;
  1549. }
  1550. static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk)
  1551. {
  1552. struct svc_deferred_req *dr = NULL;
  1553. if (!test_bit(SK_DEFERRED, &svsk->sk_flags))
  1554. return NULL;
  1555. spin_lock_bh(&svsk->sk_defer_lock);
  1556. clear_bit(SK_DEFERRED, &svsk->sk_flags);
  1557. if (!list_empty(&svsk->sk_deferred)) {
  1558. dr = list_entry(svsk->sk_deferred.next,
  1559. struct svc_deferred_req,
  1560. handle.recent);
  1561. list_del_init(&dr->handle.recent);
  1562. set_bit(SK_DEFERRED, &svsk->sk_flags);
  1563. }
  1564. spin_unlock_bh(&svsk->sk_defer_lock);
  1565. return dr;
  1566. }