svcsock.c 42 KB

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