svcsock.c 45 KB

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