svcsock.c 49 KB

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