svcsock.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793
  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 pmap_reg);
  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 msghdr msg;
  502. struct socket *sock;
  503. int len, alen;
  504. rqstp->rq_addrlen = sizeof(rqstp->rq_addr);
  505. sock = rqstp->rq_sock->sk_sock;
  506. msg.msg_name = &rqstp->rq_addr;
  507. msg.msg_namelen = sizeof(rqstp->rq_addr);
  508. msg.msg_control = NULL;
  509. msg.msg_controllen = 0;
  510. msg.msg_flags = MSG_DONTWAIT;
  511. len = kernel_recvmsg(sock, &msg, iov, nr, buflen, MSG_DONTWAIT);
  512. /* sock_recvmsg doesn't fill in the name/namelen, so we must..
  513. * possibly we should cache this in the svc_sock structure
  514. * at accept time. FIXME
  515. */
  516. alen = sizeof(rqstp->rq_addr);
  517. kernel_getpeername(sock, (struct sockaddr *)&rqstp->rq_addr, &alen);
  518. dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",
  519. rqstp->rq_sock, iov[0].iov_base, iov[0].iov_len, len);
  520. return len;
  521. }
  522. /*
  523. * Set socket snd and rcv buffer lengths
  524. */
  525. static inline void
  526. svc_sock_setbufsize(struct socket *sock, unsigned int snd, unsigned int rcv)
  527. {
  528. #if 0
  529. mm_segment_t oldfs;
  530. oldfs = get_fs(); set_fs(KERNEL_DS);
  531. sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
  532. (char*)&snd, sizeof(snd));
  533. sock_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
  534. (char*)&rcv, sizeof(rcv));
  535. #else
  536. /* sock_setsockopt limits use to sysctl_?mem_max,
  537. * which isn't acceptable. Until that is made conditional
  538. * on not having CAP_SYS_RESOURCE or similar, we go direct...
  539. * DaveM said I could!
  540. */
  541. lock_sock(sock->sk);
  542. sock->sk->sk_sndbuf = snd * 2;
  543. sock->sk->sk_rcvbuf = rcv * 2;
  544. sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK|SOCK_RCVBUF_LOCK;
  545. release_sock(sock->sk);
  546. #endif
  547. }
  548. /*
  549. * INET callback when data has been received on the socket.
  550. */
  551. static void
  552. svc_udp_data_ready(struct sock *sk, int count)
  553. {
  554. struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
  555. if (svsk) {
  556. dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n",
  557. svsk, sk, count, test_bit(SK_BUSY, &svsk->sk_flags));
  558. set_bit(SK_DATA, &svsk->sk_flags);
  559. svc_sock_enqueue(svsk);
  560. }
  561. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  562. wake_up_interruptible(sk->sk_sleep);
  563. }
  564. /*
  565. * INET callback when space is newly available on the socket.
  566. */
  567. static void
  568. svc_write_space(struct sock *sk)
  569. {
  570. struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
  571. if (svsk) {
  572. dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
  573. svsk, sk, test_bit(SK_BUSY, &svsk->sk_flags));
  574. svc_sock_enqueue(svsk);
  575. }
  576. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) {
  577. dprintk("RPC svc_write_space: someone sleeping on %p\n",
  578. svsk);
  579. wake_up_interruptible(sk->sk_sleep);
  580. }
  581. }
  582. /*
  583. * Receive a datagram from a UDP socket.
  584. */
  585. static int
  586. svc_udp_recvfrom(struct svc_rqst *rqstp)
  587. {
  588. struct svc_sock *svsk = rqstp->rq_sock;
  589. struct svc_serv *serv = svsk->sk_server;
  590. struct sk_buff *skb;
  591. int err, len;
  592. if (test_and_clear_bit(SK_CHNGBUF, &svsk->sk_flags))
  593. /* udp sockets need large rcvbuf as all pending
  594. * requests are still in that buffer. sndbuf must
  595. * also be large enough that there is enough space
  596. * for one reply per thread. We count all threads
  597. * rather than threads in a particular pool, which
  598. * provides an upper bound on the number of threads
  599. * which will access the socket.
  600. */
  601. svc_sock_setbufsize(svsk->sk_sock,
  602. (serv->sv_nrthreads+3) * serv->sv_max_mesg,
  603. (serv->sv_nrthreads+3) * serv->sv_max_mesg);
  604. if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) {
  605. svc_sock_received(svsk);
  606. return svc_deferred_recv(rqstp);
  607. }
  608. if (test_bit(SK_CLOSE, &svsk->sk_flags)) {
  609. svc_delete_socket(svsk);
  610. return 0;
  611. }
  612. clear_bit(SK_DATA, &svsk->sk_flags);
  613. while ((skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err)) == NULL) {
  614. if (err == -EAGAIN) {
  615. svc_sock_received(svsk);
  616. return err;
  617. }
  618. /* possibly an icmp error */
  619. dprintk("svc: recvfrom returned error %d\n", -err);
  620. }
  621. if (skb->tstamp.off_sec == 0) {
  622. struct timeval tv;
  623. tv.tv_sec = xtime.tv_sec;
  624. tv.tv_usec = xtime.tv_nsec / NSEC_PER_USEC;
  625. skb_set_timestamp(skb, &tv);
  626. /* Don't enable netstamp, sunrpc doesn't
  627. need that much accuracy */
  628. }
  629. skb_get_timestamp(skb, &svsk->sk_sk->sk_stamp);
  630. set_bit(SK_DATA, &svsk->sk_flags); /* there may be more data... */
  631. /*
  632. * Maybe more packets - kick another thread ASAP.
  633. */
  634. svc_sock_received(svsk);
  635. len = skb->len - sizeof(struct udphdr);
  636. rqstp->rq_arg.len = len;
  637. rqstp->rq_prot = IPPROTO_UDP;
  638. /* Get sender address */
  639. rqstp->rq_addr.sin_family = AF_INET;
  640. rqstp->rq_addr.sin_port = skb->h.uh->source;
  641. rqstp->rq_addr.sin_addr.s_addr = skb->nh.iph->saddr;
  642. rqstp->rq_daddr = skb->nh.iph->daddr;
  643. if (skb_is_nonlinear(skb)) {
  644. /* we have to copy */
  645. local_bh_disable();
  646. if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) {
  647. local_bh_enable();
  648. /* checksum error */
  649. skb_free_datagram(svsk->sk_sk, skb);
  650. return 0;
  651. }
  652. local_bh_enable();
  653. skb_free_datagram(svsk->sk_sk, skb);
  654. } else {
  655. /* we can use it in-place */
  656. rqstp->rq_arg.head[0].iov_base = skb->data + sizeof(struct udphdr);
  657. rqstp->rq_arg.head[0].iov_len = len;
  658. if (skb_checksum_complete(skb)) {
  659. skb_free_datagram(svsk->sk_sk, skb);
  660. return 0;
  661. }
  662. rqstp->rq_skbuff = skb;
  663. }
  664. rqstp->rq_arg.page_base = 0;
  665. if (len <= rqstp->rq_arg.head[0].iov_len) {
  666. rqstp->rq_arg.head[0].iov_len = len;
  667. rqstp->rq_arg.page_len = 0;
  668. rqstp->rq_respages = rqstp->rq_pages+1;
  669. } else {
  670. rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
  671. rqstp->rq_respages = rqstp->rq_pages + 1 +
  672. (rqstp->rq_arg.page_len + PAGE_SIZE - 1)/ PAGE_SIZE;
  673. }
  674. if (serv->sv_stats)
  675. serv->sv_stats->netudpcnt++;
  676. return len;
  677. }
  678. static int
  679. svc_udp_sendto(struct svc_rqst *rqstp)
  680. {
  681. int error;
  682. error = svc_sendto(rqstp, &rqstp->rq_res);
  683. if (error == -ECONNREFUSED)
  684. /* ICMP error on earlier request. */
  685. error = svc_sendto(rqstp, &rqstp->rq_res);
  686. return error;
  687. }
  688. static void
  689. svc_udp_init(struct svc_sock *svsk)
  690. {
  691. svsk->sk_sk->sk_data_ready = svc_udp_data_ready;
  692. svsk->sk_sk->sk_write_space = svc_write_space;
  693. svsk->sk_recvfrom = svc_udp_recvfrom;
  694. svsk->sk_sendto = svc_udp_sendto;
  695. /* initialise setting must have enough space to
  696. * receive and respond to one request.
  697. * svc_udp_recvfrom will re-adjust if necessary
  698. */
  699. svc_sock_setbufsize(svsk->sk_sock,
  700. 3 * svsk->sk_server->sv_max_mesg,
  701. 3 * svsk->sk_server->sv_max_mesg);
  702. set_bit(SK_DATA, &svsk->sk_flags); /* might have come in before data_ready set up */
  703. set_bit(SK_CHNGBUF, &svsk->sk_flags);
  704. }
  705. /*
  706. * A data_ready event on a listening socket means there's a connection
  707. * pending. Do not use state_change as a substitute for it.
  708. */
  709. static void
  710. svc_tcp_listen_data_ready(struct sock *sk, int count_unused)
  711. {
  712. struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
  713. dprintk("svc: socket %p TCP (listen) state change %d\n",
  714. sk, sk->sk_state);
  715. /*
  716. * This callback may called twice when a new connection
  717. * is established as a child socket inherits everything
  718. * from a parent LISTEN socket.
  719. * 1) data_ready method of the parent socket will be called
  720. * when one of child sockets become ESTABLISHED.
  721. * 2) data_ready method of the child socket may be called
  722. * when it receives data before the socket is accepted.
  723. * In case of 2, we should ignore it silently.
  724. */
  725. if (sk->sk_state == TCP_LISTEN) {
  726. if (svsk) {
  727. set_bit(SK_CONN, &svsk->sk_flags);
  728. svc_sock_enqueue(svsk);
  729. } else
  730. printk("svc: socket %p: no user data\n", sk);
  731. }
  732. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  733. wake_up_interruptible_all(sk->sk_sleep);
  734. }
  735. /*
  736. * A state change on a connected socket means it's dying or dead.
  737. */
  738. static void
  739. svc_tcp_state_change(struct sock *sk)
  740. {
  741. struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
  742. dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
  743. sk, sk->sk_state, sk->sk_user_data);
  744. if (!svsk)
  745. printk("svc: socket %p: no user data\n", sk);
  746. else {
  747. set_bit(SK_CLOSE, &svsk->sk_flags);
  748. svc_sock_enqueue(svsk);
  749. }
  750. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  751. wake_up_interruptible_all(sk->sk_sleep);
  752. }
  753. static void
  754. svc_tcp_data_ready(struct sock *sk, int count)
  755. {
  756. struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
  757. dprintk("svc: socket %p TCP data ready (svsk %p)\n",
  758. sk, sk->sk_user_data);
  759. if (svsk) {
  760. set_bit(SK_DATA, &svsk->sk_flags);
  761. svc_sock_enqueue(svsk);
  762. }
  763. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  764. wake_up_interruptible(sk->sk_sleep);
  765. }
  766. /*
  767. * Accept a TCP connection
  768. */
  769. static void
  770. svc_tcp_accept(struct svc_sock *svsk)
  771. {
  772. struct sockaddr_in sin;
  773. struct svc_serv *serv = svsk->sk_server;
  774. struct socket *sock = svsk->sk_sock;
  775. struct socket *newsock;
  776. struct svc_sock *newsvsk;
  777. int err, slen;
  778. dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
  779. if (!sock)
  780. return;
  781. clear_bit(SK_CONN, &svsk->sk_flags);
  782. err = kernel_accept(sock, &newsock, O_NONBLOCK);
  783. if (err < 0) {
  784. if (err == -ENOMEM)
  785. printk(KERN_WARNING "%s: no more sockets!\n",
  786. serv->sv_name);
  787. else if (err != -EAGAIN && net_ratelimit())
  788. printk(KERN_WARNING "%s: accept failed (err %d)!\n",
  789. serv->sv_name, -err);
  790. return;
  791. }
  792. set_bit(SK_CONN, &svsk->sk_flags);
  793. svc_sock_enqueue(svsk);
  794. slen = sizeof(sin);
  795. err = kernel_getpeername(newsock, (struct sockaddr *) &sin, &slen);
  796. if (err < 0) {
  797. if (net_ratelimit())
  798. printk(KERN_WARNING "%s: peername failed (err %d)!\n",
  799. serv->sv_name, -err);
  800. goto failed; /* aborted connection or whatever */
  801. }
  802. /* Ideally, we would want to reject connections from unauthorized
  803. * hosts here, but when we get encription, the IP of the host won't
  804. * tell us anything. For now just warn about unpriv connections.
  805. */
  806. if (ntohs(sin.sin_port) >= 1024) {
  807. dprintk(KERN_WARNING
  808. "%s: connect from unprivileged port: %u.%u.%u.%u:%d\n",
  809. serv->sv_name,
  810. NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
  811. }
  812. dprintk("%s: connect from %u.%u.%u.%u:%04x\n", serv->sv_name,
  813. NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
  814. /* make sure that a write doesn't block forever when
  815. * low on memory
  816. */
  817. newsock->sk->sk_sndtimeo = HZ*30;
  818. if (!(newsvsk = svc_setup_socket(serv, newsock, &err, 0)))
  819. goto failed;
  820. /* make sure that we don't have too many active connections.
  821. * If we have, something must be dropped.
  822. *
  823. * There's no point in trying to do random drop here for
  824. * DoS prevention. The NFS clients does 1 reconnect in 15
  825. * seconds. An attacker can easily beat that.
  826. *
  827. * The only somewhat efficient mechanism would be if drop
  828. * old connections from the same IP first. But right now
  829. * we don't even record the client IP in svc_sock.
  830. */
  831. if (serv->sv_tmpcnt > (serv->sv_nrthreads+3)*20) {
  832. struct svc_sock *svsk = NULL;
  833. spin_lock_bh(&serv->sv_lock);
  834. if (!list_empty(&serv->sv_tempsocks)) {
  835. if (net_ratelimit()) {
  836. /* Try to help the admin */
  837. printk(KERN_NOTICE "%s: too many open TCP "
  838. "sockets, consider increasing the "
  839. "number of nfsd threads\n",
  840. serv->sv_name);
  841. printk(KERN_NOTICE "%s: last TCP connect from "
  842. "%u.%u.%u.%u:%d\n",
  843. serv->sv_name,
  844. NIPQUAD(sin.sin_addr.s_addr),
  845. ntohs(sin.sin_port));
  846. }
  847. /*
  848. * Always select the oldest socket. It's not fair,
  849. * but so is life
  850. */
  851. svsk = list_entry(serv->sv_tempsocks.prev,
  852. struct svc_sock,
  853. sk_list);
  854. set_bit(SK_CLOSE, &svsk->sk_flags);
  855. atomic_inc(&svsk->sk_inuse);
  856. }
  857. spin_unlock_bh(&serv->sv_lock);
  858. if (svsk) {
  859. svc_sock_enqueue(svsk);
  860. svc_sock_put(svsk);
  861. }
  862. }
  863. if (serv->sv_stats)
  864. serv->sv_stats->nettcpconn++;
  865. return;
  866. failed:
  867. sock_release(newsock);
  868. return;
  869. }
  870. /*
  871. * Receive data from a TCP socket.
  872. */
  873. static int
  874. svc_tcp_recvfrom(struct svc_rqst *rqstp)
  875. {
  876. struct svc_sock *svsk = rqstp->rq_sock;
  877. struct svc_serv *serv = svsk->sk_server;
  878. int len;
  879. struct kvec *vec;
  880. int pnum, vlen;
  881. dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
  882. svsk, test_bit(SK_DATA, &svsk->sk_flags),
  883. test_bit(SK_CONN, &svsk->sk_flags),
  884. test_bit(SK_CLOSE, &svsk->sk_flags));
  885. if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) {
  886. svc_sock_received(svsk);
  887. return svc_deferred_recv(rqstp);
  888. }
  889. if (test_bit(SK_CLOSE, &svsk->sk_flags)) {
  890. svc_delete_socket(svsk);
  891. return 0;
  892. }
  893. if (svsk->sk_sk->sk_state == TCP_LISTEN) {
  894. svc_tcp_accept(svsk);
  895. svc_sock_received(svsk);
  896. return 0;
  897. }
  898. if (test_and_clear_bit(SK_CHNGBUF, &svsk->sk_flags))
  899. /* sndbuf needs to have room for one request
  900. * per thread, otherwise we can stall even when the
  901. * network isn't a bottleneck.
  902. *
  903. * We count all threads rather than threads in a
  904. * particular pool, which provides an upper bound
  905. * on the number of threads which will access the socket.
  906. *
  907. * rcvbuf just needs to be able to hold a few requests.
  908. * Normally they will be removed from the queue
  909. * as soon a a complete request arrives.
  910. */
  911. svc_sock_setbufsize(svsk->sk_sock,
  912. (serv->sv_nrthreads+3) * serv->sv_max_mesg,
  913. 3 * serv->sv_max_mesg);
  914. clear_bit(SK_DATA, &svsk->sk_flags);
  915. /* Receive data. If we haven't got the record length yet, get
  916. * the next four bytes. Otherwise try to gobble up as much as
  917. * possible up to the complete record length.
  918. */
  919. if (svsk->sk_tcplen < 4) {
  920. unsigned long want = 4 - svsk->sk_tcplen;
  921. struct kvec iov;
  922. iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
  923. iov.iov_len = want;
  924. if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0)
  925. goto error;
  926. svsk->sk_tcplen += len;
  927. if (len < want) {
  928. dprintk("svc: short recvfrom while reading record length (%d of %lu)\n",
  929. len, want);
  930. svc_sock_received(svsk);
  931. return -EAGAIN; /* record header not complete */
  932. }
  933. svsk->sk_reclen = ntohl(svsk->sk_reclen);
  934. if (!(svsk->sk_reclen & 0x80000000)) {
  935. /* FIXME: technically, a record can be fragmented,
  936. * and non-terminal fragments will not have the top
  937. * bit set in the fragment length header.
  938. * But apparently no known nfs clients send fragmented
  939. * records. */
  940. if (net_ratelimit())
  941. printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx"
  942. " (non-terminal)\n",
  943. (unsigned long) svsk->sk_reclen);
  944. goto err_delete;
  945. }
  946. svsk->sk_reclen &= 0x7fffffff;
  947. dprintk("svc: TCP record, %d bytes\n", svsk->sk_reclen);
  948. if (svsk->sk_reclen > serv->sv_max_mesg) {
  949. if (net_ratelimit())
  950. printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx"
  951. " (large)\n",
  952. (unsigned long) svsk->sk_reclen);
  953. goto err_delete;
  954. }
  955. }
  956. /* Check whether enough data is available */
  957. len = svc_recv_available(svsk);
  958. if (len < 0)
  959. goto error;
  960. if (len < svsk->sk_reclen) {
  961. dprintk("svc: incomplete TCP record (%d of %d)\n",
  962. len, svsk->sk_reclen);
  963. svc_sock_received(svsk);
  964. return -EAGAIN; /* record not complete */
  965. }
  966. len = svsk->sk_reclen;
  967. set_bit(SK_DATA, &svsk->sk_flags);
  968. vec = rqstp->rq_vec;
  969. vec[0] = rqstp->rq_arg.head[0];
  970. vlen = PAGE_SIZE;
  971. pnum = 1;
  972. while (vlen < len) {
  973. vec[pnum].iov_base = page_address(rqstp->rq_pages[pnum]);
  974. vec[pnum].iov_len = PAGE_SIZE;
  975. pnum++;
  976. vlen += PAGE_SIZE;
  977. }
  978. rqstp->rq_respages = &rqstp->rq_pages[pnum];
  979. /* Now receive data */
  980. len = svc_recvfrom(rqstp, vec, pnum, len);
  981. if (len < 0)
  982. goto error;
  983. dprintk("svc: TCP complete record (%d bytes)\n", len);
  984. rqstp->rq_arg.len = len;
  985. rqstp->rq_arg.page_base = 0;
  986. if (len <= rqstp->rq_arg.head[0].iov_len) {
  987. rqstp->rq_arg.head[0].iov_len = len;
  988. rqstp->rq_arg.page_len = 0;
  989. } else {
  990. rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
  991. }
  992. rqstp->rq_skbuff = NULL;
  993. rqstp->rq_prot = IPPROTO_TCP;
  994. /* Reset TCP read info */
  995. svsk->sk_reclen = 0;
  996. svsk->sk_tcplen = 0;
  997. svc_sock_received(svsk);
  998. if (serv->sv_stats)
  999. serv->sv_stats->nettcpcnt++;
  1000. return len;
  1001. err_delete:
  1002. svc_delete_socket(svsk);
  1003. return -EAGAIN;
  1004. error:
  1005. if (len == -EAGAIN) {
  1006. dprintk("RPC: TCP recvfrom got EAGAIN\n");
  1007. svc_sock_received(svsk);
  1008. } else {
  1009. printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
  1010. svsk->sk_server->sv_name, -len);
  1011. goto err_delete;
  1012. }
  1013. return len;
  1014. }
  1015. /*
  1016. * Send out data on TCP socket.
  1017. */
  1018. static int
  1019. svc_tcp_sendto(struct svc_rqst *rqstp)
  1020. {
  1021. struct xdr_buf *xbufp = &rqstp->rq_res;
  1022. int sent;
  1023. __be32 reclen;
  1024. /* Set up the first element of the reply kvec.
  1025. * Any other kvecs that may be in use have been taken
  1026. * care of by the server implementation itself.
  1027. */
  1028. reclen = htonl(0x80000000|((xbufp->len ) - 4));
  1029. memcpy(xbufp->head[0].iov_base, &reclen, 4);
  1030. if (test_bit(SK_DEAD, &rqstp->rq_sock->sk_flags))
  1031. return -ENOTCONN;
  1032. sent = svc_sendto(rqstp, &rqstp->rq_res);
  1033. if (sent != xbufp->len) {
  1034. printk(KERN_NOTICE "rpc-srv/tcp: %s: %s %d when sending %d bytes - shutting down socket\n",
  1035. rqstp->rq_sock->sk_server->sv_name,
  1036. (sent<0)?"got error":"sent only",
  1037. sent, xbufp->len);
  1038. set_bit(SK_CLOSE, &rqstp->rq_sock->sk_flags);
  1039. svc_sock_enqueue(rqstp->rq_sock);
  1040. sent = -EAGAIN;
  1041. }
  1042. return sent;
  1043. }
  1044. static void
  1045. svc_tcp_init(struct svc_sock *svsk)
  1046. {
  1047. struct sock *sk = svsk->sk_sk;
  1048. struct tcp_sock *tp = tcp_sk(sk);
  1049. svsk->sk_recvfrom = svc_tcp_recvfrom;
  1050. svsk->sk_sendto = svc_tcp_sendto;
  1051. if (sk->sk_state == TCP_LISTEN) {
  1052. dprintk("setting up TCP socket for listening\n");
  1053. sk->sk_data_ready = svc_tcp_listen_data_ready;
  1054. set_bit(SK_CONN, &svsk->sk_flags);
  1055. } else {
  1056. dprintk("setting up TCP socket for reading\n");
  1057. sk->sk_state_change = svc_tcp_state_change;
  1058. sk->sk_data_ready = svc_tcp_data_ready;
  1059. sk->sk_write_space = svc_write_space;
  1060. svsk->sk_reclen = 0;
  1061. svsk->sk_tcplen = 0;
  1062. tp->nonagle = 1; /* disable Nagle's algorithm */
  1063. /* initialise setting must have enough space to
  1064. * receive and respond to one request.
  1065. * svc_tcp_recvfrom will re-adjust if necessary
  1066. */
  1067. svc_sock_setbufsize(svsk->sk_sock,
  1068. 3 * svsk->sk_server->sv_max_mesg,
  1069. 3 * svsk->sk_server->sv_max_mesg);
  1070. set_bit(SK_CHNGBUF, &svsk->sk_flags);
  1071. set_bit(SK_DATA, &svsk->sk_flags);
  1072. if (sk->sk_state != TCP_ESTABLISHED)
  1073. set_bit(SK_CLOSE, &svsk->sk_flags);
  1074. }
  1075. }
  1076. void
  1077. svc_sock_update_bufs(struct svc_serv *serv)
  1078. {
  1079. /*
  1080. * The number of server threads has changed. Update
  1081. * rcvbuf and sndbuf accordingly on all sockets
  1082. */
  1083. struct list_head *le;
  1084. spin_lock_bh(&serv->sv_lock);
  1085. list_for_each(le, &serv->sv_permsocks) {
  1086. struct svc_sock *svsk =
  1087. list_entry(le, struct svc_sock, sk_list);
  1088. set_bit(SK_CHNGBUF, &svsk->sk_flags);
  1089. }
  1090. list_for_each(le, &serv->sv_tempsocks) {
  1091. struct svc_sock *svsk =
  1092. list_entry(le, struct svc_sock, sk_list);
  1093. set_bit(SK_CHNGBUF, &svsk->sk_flags);
  1094. }
  1095. spin_unlock_bh(&serv->sv_lock);
  1096. }
  1097. /*
  1098. * Receive the next request on any socket. This code is carefully
  1099. * organised not to touch any cachelines in the shared svc_serv
  1100. * structure, only cachelines in the local svc_pool.
  1101. */
  1102. int
  1103. svc_recv(struct svc_rqst *rqstp, long timeout)
  1104. {
  1105. struct svc_sock *svsk =NULL;
  1106. struct svc_serv *serv = rqstp->rq_server;
  1107. struct svc_pool *pool = rqstp->rq_pool;
  1108. int len, i;
  1109. int pages;
  1110. struct xdr_buf *arg;
  1111. DECLARE_WAITQUEUE(wait, current);
  1112. dprintk("svc: server %p waiting for data (to = %ld)\n",
  1113. rqstp, timeout);
  1114. if (rqstp->rq_sock)
  1115. printk(KERN_ERR
  1116. "svc_recv: service %p, socket not NULL!\n",
  1117. rqstp);
  1118. if (waitqueue_active(&rqstp->rq_wait))
  1119. printk(KERN_ERR
  1120. "svc_recv: service %p, wait queue active!\n",
  1121. rqstp);
  1122. /* now allocate needed pages. If we get a failure, sleep briefly */
  1123. pages = (serv->sv_max_mesg + PAGE_SIZE) / PAGE_SIZE;
  1124. for (i=0; i < pages ; i++)
  1125. while (rqstp->rq_pages[i] == NULL) {
  1126. struct page *p = alloc_page(GFP_KERNEL);
  1127. if (!p)
  1128. schedule_timeout_uninterruptible(msecs_to_jiffies(500));
  1129. rqstp->rq_pages[i] = p;
  1130. }
  1131. rqstp->rq_pages[i++] = NULL; /* this might be seen in nfs_read_actor */
  1132. BUG_ON(pages >= RPCSVC_MAXPAGES);
  1133. /* Make arg->head point to first page and arg->pages point to rest */
  1134. arg = &rqstp->rq_arg;
  1135. arg->head[0].iov_base = page_address(rqstp->rq_pages[0]);
  1136. arg->head[0].iov_len = PAGE_SIZE;
  1137. arg->pages = rqstp->rq_pages + 1;
  1138. arg->page_base = 0;
  1139. /* save at least one page for response */
  1140. arg->page_len = (pages-2)*PAGE_SIZE;
  1141. arg->len = (pages-1)*PAGE_SIZE;
  1142. arg->tail[0].iov_len = 0;
  1143. try_to_freeze();
  1144. cond_resched();
  1145. if (signalled())
  1146. return -EINTR;
  1147. spin_lock_bh(&pool->sp_lock);
  1148. if ((svsk = svc_sock_dequeue(pool)) != NULL) {
  1149. rqstp->rq_sock = svsk;
  1150. atomic_inc(&svsk->sk_inuse);
  1151. rqstp->rq_reserved = serv->sv_max_mesg;
  1152. atomic_add(rqstp->rq_reserved, &svsk->sk_reserved);
  1153. } else {
  1154. /* No data pending. Go to sleep */
  1155. svc_thread_enqueue(pool, rqstp);
  1156. /*
  1157. * We have to be able to interrupt this wait
  1158. * to bring down the daemons ...
  1159. */
  1160. set_current_state(TASK_INTERRUPTIBLE);
  1161. add_wait_queue(&rqstp->rq_wait, &wait);
  1162. spin_unlock_bh(&pool->sp_lock);
  1163. schedule_timeout(timeout);
  1164. try_to_freeze();
  1165. spin_lock_bh(&pool->sp_lock);
  1166. remove_wait_queue(&rqstp->rq_wait, &wait);
  1167. if (!(svsk = rqstp->rq_sock)) {
  1168. svc_thread_dequeue(pool, rqstp);
  1169. spin_unlock_bh(&pool->sp_lock);
  1170. dprintk("svc: server %p, no data yet\n", rqstp);
  1171. return signalled()? -EINTR : -EAGAIN;
  1172. }
  1173. }
  1174. spin_unlock_bh(&pool->sp_lock);
  1175. dprintk("svc: server %p, pool %u, socket %p, inuse=%d\n",
  1176. rqstp, pool->sp_id, svsk, atomic_read(&svsk->sk_inuse));
  1177. len = svsk->sk_recvfrom(rqstp);
  1178. dprintk("svc: got len=%d\n", len);
  1179. /* No data, incomplete (TCP) read, or accept() */
  1180. if (len == 0 || len == -EAGAIN) {
  1181. rqstp->rq_res.len = 0;
  1182. svc_sock_release(rqstp);
  1183. return -EAGAIN;
  1184. }
  1185. svsk->sk_lastrecv = get_seconds();
  1186. clear_bit(SK_OLD, &svsk->sk_flags);
  1187. rqstp->rq_secure = ntohs(rqstp->rq_addr.sin_port) < 1024;
  1188. rqstp->rq_chandle.defer = svc_defer;
  1189. if (serv->sv_stats)
  1190. serv->sv_stats->netcnt++;
  1191. return len;
  1192. }
  1193. /*
  1194. * Drop request
  1195. */
  1196. void
  1197. svc_drop(struct svc_rqst *rqstp)
  1198. {
  1199. dprintk("svc: socket %p dropped request\n", rqstp->rq_sock);
  1200. svc_sock_release(rqstp);
  1201. }
  1202. /*
  1203. * Return reply to client.
  1204. */
  1205. int
  1206. svc_send(struct svc_rqst *rqstp)
  1207. {
  1208. struct svc_sock *svsk;
  1209. int len;
  1210. struct xdr_buf *xb;
  1211. if ((svsk = rqstp->rq_sock) == NULL) {
  1212. printk(KERN_WARNING "NULL socket pointer in %s:%d\n",
  1213. __FILE__, __LINE__);
  1214. return -EFAULT;
  1215. }
  1216. /* release the receive skb before sending the reply */
  1217. svc_release_skb(rqstp);
  1218. /* calculate over-all length */
  1219. xb = & rqstp->rq_res;
  1220. xb->len = xb->head[0].iov_len +
  1221. xb->page_len +
  1222. xb->tail[0].iov_len;
  1223. /* Grab svsk->sk_mutex to serialize outgoing data. */
  1224. mutex_lock(&svsk->sk_mutex);
  1225. if (test_bit(SK_DEAD, &svsk->sk_flags))
  1226. len = -ENOTCONN;
  1227. else
  1228. len = svsk->sk_sendto(rqstp);
  1229. mutex_unlock(&svsk->sk_mutex);
  1230. svc_sock_release(rqstp);
  1231. if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN)
  1232. return 0;
  1233. return len;
  1234. }
  1235. /*
  1236. * Timer function to close old temporary sockets, using
  1237. * a mark-and-sweep algorithm.
  1238. */
  1239. static void
  1240. svc_age_temp_sockets(unsigned long closure)
  1241. {
  1242. struct svc_serv *serv = (struct svc_serv *)closure;
  1243. struct svc_sock *svsk;
  1244. struct list_head *le, *next;
  1245. LIST_HEAD(to_be_aged);
  1246. dprintk("svc_age_temp_sockets\n");
  1247. if (!spin_trylock_bh(&serv->sv_lock)) {
  1248. /* busy, try again 1 sec later */
  1249. dprintk("svc_age_temp_sockets: busy\n");
  1250. mod_timer(&serv->sv_temptimer, jiffies + HZ);
  1251. return;
  1252. }
  1253. list_for_each_safe(le, next, &serv->sv_tempsocks) {
  1254. svsk = list_entry(le, struct svc_sock, sk_list);
  1255. if (!test_and_set_bit(SK_OLD, &svsk->sk_flags))
  1256. continue;
  1257. if (atomic_read(&svsk->sk_inuse) || test_bit(SK_BUSY, &svsk->sk_flags))
  1258. continue;
  1259. atomic_inc(&svsk->sk_inuse);
  1260. list_move(le, &to_be_aged);
  1261. set_bit(SK_CLOSE, &svsk->sk_flags);
  1262. set_bit(SK_DETACHED, &svsk->sk_flags);
  1263. }
  1264. spin_unlock_bh(&serv->sv_lock);
  1265. while (!list_empty(&to_be_aged)) {
  1266. le = to_be_aged.next;
  1267. /* fiddling the sk_list node is safe 'cos we're SK_DETACHED */
  1268. list_del_init(le);
  1269. svsk = list_entry(le, struct svc_sock, sk_list);
  1270. dprintk("queuing svsk %p for closing, %lu seconds old\n",
  1271. svsk, get_seconds() - svsk->sk_lastrecv);
  1272. /* a thread will dequeue and close it soon */
  1273. svc_sock_enqueue(svsk);
  1274. svc_sock_put(svsk);
  1275. }
  1276. mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ);
  1277. }
  1278. /*
  1279. * Initialize socket for RPC use and create svc_sock struct
  1280. * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF.
  1281. */
  1282. static struct svc_sock *
  1283. svc_setup_socket(struct svc_serv *serv, struct socket *sock,
  1284. int *errp, int pmap_register)
  1285. {
  1286. struct svc_sock *svsk;
  1287. struct sock *inet;
  1288. dprintk("svc: svc_setup_socket %p\n", sock);
  1289. if (!(svsk = kzalloc(sizeof(*svsk), GFP_KERNEL))) {
  1290. *errp = -ENOMEM;
  1291. return NULL;
  1292. }
  1293. inet = sock->sk;
  1294. /* Register socket with portmapper */
  1295. if (*errp >= 0 && pmap_register)
  1296. *errp = svc_register(serv, inet->sk_protocol,
  1297. ntohs(inet_sk(inet)->sport));
  1298. if (*errp < 0) {
  1299. kfree(svsk);
  1300. return NULL;
  1301. }
  1302. set_bit(SK_BUSY, &svsk->sk_flags);
  1303. inet->sk_user_data = svsk;
  1304. svsk->sk_sock = sock;
  1305. svsk->sk_sk = inet;
  1306. svsk->sk_ostate = inet->sk_state_change;
  1307. svsk->sk_odata = inet->sk_data_ready;
  1308. svsk->sk_owspace = inet->sk_write_space;
  1309. svsk->sk_server = serv;
  1310. atomic_set(&svsk->sk_inuse, 1);
  1311. svsk->sk_lastrecv = get_seconds();
  1312. spin_lock_init(&svsk->sk_defer_lock);
  1313. INIT_LIST_HEAD(&svsk->sk_deferred);
  1314. INIT_LIST_HEAD(&svsk->sk_ready);
  1315. mutex_init(&svsk->sk_mutex);
  1316. /* Initialize the socket */
  1317. if (sock->type == SOCK_DGRAM)
  1318. svc_udp_init(svsk);
  1319. else
  1320. svc_tcp_init(svsk);
  1321. spin_lock_bh(&serv->sv_lock);
  1322. if (!pmap_register) {
  1323. set_bit(SK_TEMP, &svsk->sk_flags);
  1324. list_add(&svsk->sk_list, &serv->sv_tempsocks);
  1325. serv->sv_tmpcnt++;
  1326. if (serv->sv_temptimer.function == NULL) {
  1327. /* setup timer to age temp sockets */
  1328. setup_timer(&serv->sv_temptimer, svc_age_temp_sockets,
  1329. (unsigned long)serv);
  1330. mod_timer(&serv->sv_temptimer,
  1331. jiffies + svc_conn_age_period * HZ);
  1332. }
  1333. } else {
  1334. clear_bit(SK_TEMP, &svsk->sk_flags);
  1335. list_add(&svsk->sk_list, &serv->sv_permsocks);
  1336. }
  1337. spin_unlock_bh(&serv->sv_lock);
  1338. dprintk("svc: svc_setup_socket created %p (inet %p)\n",
  1339. svsk, svsk->sk_sk);
  1340. clear_bit(SK_BUSY, &svsk->sk_flags);
  1341. svc_sock_enqueue(svsk);
  1342. return svsk;
  1343. }
  1344. int svc_addsock(struct svc_serv *serv,
  1345. int fd,
  1346. char *name_return,
  1347. int *proto)
  1348. {
  1349. int err = 0;
  1350. struct socket *so = sockfd_lookup(fd, &err);
  1351. struct svc_sock *svsk = NULL;
  1352. if (!so)
  1353. return err;
  1354. if (so->sk->sk_family != AF_INET)
  1355. err = -EAFNOSUPPORT;
  1356. else if (so->sk->sk_protocol != IPPROTO_TCP &&
  1357. so->sk->sk_protocol != IPPROTO_UDP)
  1358. err = -EPROTONOSUPPORT;
  1359. else if (so->state > SS_UNCONNECTED)
  1360. err = -EISCONN;
  1361. else {
  1362. svsk = svc_setup_socket(serv, so, &err, 1);
  1363. if (svsk)
  1364. err = 0;
  1365. }
  1366. if (err) {
  1367. sockfd_put(so);
  1368. return err;
  1369. }
  1370. if (proto) *proto = so->sk->sk_protocol;
  1371. return one_sock_name(name_return, svsk);
  1372. }
  1373. EXPORT_SYMBOL_GPL(svc_addsock);
  1374. /*
  1375. * Create socket for RPC service.
  1376. */
  1377. static int
  1378. svc_create_socket(struct svc_serv *serv, int protocol, struct sockaddr_in *sin)
  1379. {
  1380. struct svc_sock *svsk;
  1381. struct socket *sock;
  1382. int error;
  1383. int type;
  1384. dprintk("svc: svc_create_socket(%s, %d, %u.%u.%u.%u:%d)\n",
  1385. serv->sv_program->pg_name, protocol,
  1386. NIPQUAD(sin->sin_addr.s_addr),
  1387. ntohs(sin->sin_port));
  1388. if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
  1389. printk(KERN_WARNING "svc: only UDP and TCP "
  1390. "sockets supported\n");
  1391. return -EINVAL;
  1392. }
  1393. type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
  1394. if ((error = sock_create_kern(PF_INET, type, protocol, &sock)) < 0)
  1395. return error;
  1396. svc_reclassify_socket(sock);
  1397. if (type == SOCK_STREAM)
  1398. sock->sk->sk_reuse = 1; /* allow address reuse */
  1399. error = kernel_bind(sock, (struct sockaddr *) sin,
  1400. sizeof(*sin));
  1401. if (error < 0)
  1402. goto bummer;
  1403. if (protocol == IPPROTO_TCP) {
  1404. if ((error = kernel_listen(sock, 64)) < 0)
  1405. goto bummer;
  1406. }
  1407. if ((svsk = svc_setup_socket(serv, sock, &error, 1)) != NULL)
  1408. return 0;
  1409. bummer:
  1410. dprintk("svc: svc_create_socket error = %d\n", -error);
  1411. sock_release(sock);
  1412. return error;
  1413. }
  1414. /*
  1415. * Remove a dead socket
  1416. */
  1417. static void
  1418. svc_delete_socket(struct svc_sock *svsk)
  1419. {
  1420. struct svc_serv *serv;
  1421. struct sock *sk;
  1422. dprintk("svc: svc_delete_socket(%p)\n", svsk);
  1423. serv = svsk->sk_server;
  1424. sk = svsk->sk_sk;
  1425. sk->sk_state_change = svsk->sk_ostate;
  1426. sk->sk_data_ready = svsk->sk_odata;
  1427. sk->sk_write_space = svsk->sk_owspace;
  1428. spin_lock_bh(&serv->sv_lock);
  1429. if (!test_and_set_bit(SK_DETACHED, &svsk->sk_flags))
  1430. list_del_init(&svsk->sk_list);
  1431. /*
  1432. * We used to delete the svc_sock from whichever list
  1433. * it's sk_ready node was on, but we don't actually
  1434. * need to. This is because the only time we're called
  1435. * while still attached to a queue, the queue itself
  1436. * is about to be destroyed (in svc_destroy).
  1437. */
  1438. if (!test_and_set_bit(SK_DEAD, &svsk->sk_flags)) {
  1439. BUG_ON(atomic_read(&svsk->sk_inuse)<2);
  1440. atomic_dec(&svsk->sk_inuse);
  1441. if (test_bit(SK_TEMP, &svsk->sk_flags))
  1442. serv->sv_tmpcnt--;
  1443. }
  1444. spin_unlock_bh(&serv->sv_lock);
  1445. }
  1446. void svc_close_socket(struct svc_sock *svsk)
  1447. {
  1448. set_bit(SK_CLOSE, &svsk->sk_flags);
  1449. if (test_and_set_bit(SK_BUSY, &svsk->sk_flags))
  1450. /* someone else will have to effect the close */
  1451. return;
  1452. atomic_inc(&svsk->sk_inuse);
  1453. svc_delete_socket(svsk);
  1454. clear_bit(SK_BUSY, &svsk->sk_flags);
  1455. svc_sock_put(svsk);
  1456. }
  1457. /*
  1458. * Make a socket for nfsd and lockd
  1459. */
  1460. int
  1461. svc_makesock(struct svc_serv *serv, int protocol, unsigned short port)
  1462. {
  1463. struct sockaddr_in sin;
  1464. dprintk("svc: creating socket proto = %d\n", protocol);
  1465. sin.sin_family = AF_INET;
  1466. sin.sin_addr.s_addr = INADDR_ANY;
  1467. sin.sin_port = htons(port);
  1468. return svc_create_socket(serv, protocol, &sin);
  1469. }
  1470. /*
  1471. * Handle defer and revisit of requests
  1472. */
  1473. static void svc_revisit(struct cache_deferred_req *dreq, int too_many)
  1474. {
  1475. struct svc_deferred_req *dr = container_of(dreq, struct svc_deferred_req, handle);
  1476. struct svc_sock *svsk;
  1477. if (too_many) {
  1478. svc_sock_put(dr->svsk);
  1479. kfree(dr);
  1480. return;
  1481. }
  1482. dprintk("revisit queued\n");
  1483. svsk = dr->svsk;
  1484. dr->svsk = NULL;
  1485. spin_lock_bh(&svsk->sk_defer_lock);
  1486. list_add(&dr->handle.recent, &svsk->sk_deferred);
  1487. spin_unlock_bh(&svsk->sk_defer_lock);
  1488. set_bit(SK_DEFERRED, &svsk->sk_flags);
  1489. svc_sock_enqueue(svsk);
  1490. svc_sock_put(svsk);
  1491. }
  1492. static struct cache_deferred_req *
  1493. svc_defer(struct cache_req *req)
  1494. {
  1495. struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle);
  1496. int size = sizeof(struct svc_deferred_req) + (rqstp->rq_arg.len);
  1497. struct svc_deferred_req *dr;
  1498. if (rqstp->rq_arg.page_len)
  1499. return NULL; /* if more than a page, give up FIXME */
  1500. if (rqstp->rq_deferred) {
  1501. dr = rqstp->rq_deferred;
  1502. rqstp->rq_deferred = NULL;
  1503. } else {
  1504. int skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
  1505. /* FIXME maybe discard if size too large */
  1506. dr = kmalloc(size, GFP_KERNEL);
  1507. if (dr == NULL)
  1508. return NULL;
  1509. dr->handle.owner = rqstp->rq_server;
  1510. dr->prot = rqstp->rq_prot;
  1511. dr->addr = rqstp->rq_addr;
  1512. dr->daddr = rqstp->rq_daddr;
  1513. dr->argslen = rqstp->rq_arg.len >> 2;
  1514. memcpy(dr->args, rqstp->rq_arg.head[0].iov_base-skip, dr->argslen<<2);
  1515. }
  1516. atomic_inc(&rqstp->rq_sock->sk_inuse);
  1517. dr->svsk = rqstp->rq_sock;
  1518. dr->handle.revisit = svc_revisit;
  1519. return &dr->handle;
  1520. }
  1521. /*
  1522. * recv data from a deferred request into an active one
  1523. */
  1524. static int svc_deferred_recv(struct svc_rqst *rqstp)
  1525. {
  1526. struct svc_deferred_req *dr = rqstp->rq_deferred;
  1527. rqstp->rq_arg.head[0].iov_base = dr->args;
  1528. rqstp->rq_arg.head[0].iov_len = dr->argslen<<2;
  1529. rqstp->rq_arg.page_len = 0;
  1530. rqstp->rq_arg.len = dr->argslen<<2;
  1531. rqstp->rq_prot = dr->prot;
  1532. rqstp->rq_addr = dr->addr;
  1533. rqstp->rq_daddr = dr->daddr;
  1534. rqstp->rq_respages = rqstp->rq_pages;
  1535. return dr->argslen<<2;
  1536. }
  1537. static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk)
  1538. {
  1539. struct svc_deferred_req *dr = NULL;
  1540. if (!test_bit(SK_DEFERRED, &svsk->sk_flags))
  1541. return NULL;
  1542. spin_lock_bh(&svsk->sk_defer_lock);
  1543. clear_bit(SK_DEFERRED, &svsk->sk_flags);
  1544. if (!list_empty(&svsk->sk_deferred)) {
  1545. dr = list_entry(svsk->sk_deferred.next,
  1546. struct svc_deferred_req,
  1547. handle.recent);
  1548. list_del_init(&dr->handle.recent);
  1549. set_bit(SK_DEFERRED, &svsk->sk_flags);
  1550. }
  1551. spin_unlock_bh(&svsk->sk_defer_lock);
  1552. return dr;
  1553. }