svcsock.c 50 KB

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