svcsock.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838
  1. /*
  2. * linux/net/sunrpc/svcsock.c
  3. *
  4. * These are the RPC server socket internals.
  5. *
  6. * The server scheduling algorithm does not always distribute the load
  7. * evenly when servicing a single client. May need to modify the
  8. * svc_sock_enqueue procedure...
  9. *
  10. * TCP support is largely untested and may be a little slow. The problem
  11. * is that we currently do two separate recvfrom's, one for the 4-byte
  12. * record length, and the second for the actual record. This could possibly
  13. * be improved by always reading a minimum size of around 100 bytes and
  14. * tucking any superfluous bytes away in a temporary store. Still, that
  15. * leaves write requests out in the rain. An alternative may be to peek at
  16. * the first skb in the queue, and if it matches the next TCP sequence
  17. * number, to extract the record marker. Yuck.
  18. *
  19. * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
  20. */
  21. #include <linux/sched.h>
  22. #include <linux/errno.h>
  23. #include <linux/fcntl.h>
  24. #include <linux/net.h>
  25. #include <linux/in.h>
  26. #include <linux/inet.h>
  27. #include <linux/udp.h>
  28. #include <linux/tcp.h>
  29. #include <linux/unistd.h>
  30. #include <linux/slab.h>
  31. #include <linux/netdevice.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/file.h>
  34. #include <linux/freezer.h>
  35. #include <net/sock.h>
  36. #include <net/checksum.h>
  37. #include <net/ip.h>
  38. #include <net/tcp_states.h>
  39. #include <asm/uaccess.h>
  40. #include <asm/ioctls.h>
  41. #include <linux/sunrpc/types.h>
  42. #include <linux/sunrpc/clnt.h>
  43. #include <linux/sunrpc/xdr.h>
  44. #include <linux/sunrpc/svcsock.h>
  45. #include <linux/sunrpc/stats.h>
  46. /* SMP locking strategy:
  47. *
  48. * svc_pool->sp_lock protects most of the fields of that pool.
  49. * svc_serv->sv_lock protects sv_tempsocks, sv_permsocks, sv_tmpcnt.
  50. * when both need to be taken (rare), svc_serv->sv_lock is first.
  51. * BKL protects svc_serv->sv_nrthread.
  52. * svc_sock->sk_defer_lock protects the svc_sock->sk_deferred list
  53. * svc_sock->sk_flags.SK_BUSY prevents a svc_sock being enqueued multiply.
  54. *
  55. * Some flags can be set to certain values at any time
  56. * providing that certain rules are followed:
  57. *
  58. * SK_CONN, SK_DATA, can be set or cleared at any time.
  59. * after a set, svc_sock_enqueue must be called.
  60. * after a clear, the socket must be read/accepted
  61. * if this succeeds, it must be set again.
  62. * SK_CLOSE can set at any time. It is never cleared.
  63. * sk_inuse contains a bias of '1' until SK_DEAD is set.
  64. * so when sk_inuse hits zero, we know the socket is dead
  65. * and no-one is using it.
  66. * SK_DEAD can only be set while SK_BUSY is held which ensures
  67. * no other thread will be using the socket or will try to
  68. * set SK_DEAD.
  69. *
  70. */
  71. #define RPCDBG_FACILITY RPCDBG_SVCSOCK
  72. static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
  73. int *errp, int flags);
  74. static void svc_delete_socket(struct svc_sock *svsk);
  75. static void svc_udp_data_ready(struct sock *, int);
  76. static int svc_udp_recvfrom(struct svc_rqst *);
  77. static int svc_udp_sendto(struct svc_rqst *);
  78. static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk);
  79. static int svc_deferred_recv(struct svc_rqst *rqstp);
  80. static struct cache_deferred_req *svc_defer(struct cache_req *req);
  81. /* apparently the "standard" is that clients close
  82. * idle connections after 5 minutes, servers after
  83. * 6 minutes
  84. * http://www.connectathon.org/talks96/nfstcp.pdf
  85. */
  86. static int svc_conn_age_period = 6*60;
  87. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  88. static struct lock_class_key svc_key[2];
  89. static struct lock_class_key svc_slock_key[2];
  90. static inline void svc_reclassify_socket(struct socket *sock)
  91. {
  92. struct sock *sk = sock->sk;
  93. BUG_ON(sk->sk_lock.owner != NULL);
  94. switch (sk->sk_family) {
  95. case AF_INET:
  96. sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD",
  97. &svc_slock_key[0], "sk_lock-AF_INET-NFSD", &svc_key[0]);
  98. break;
  99. case AF_INET6:
  100. sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD",
  101. &svc_slock_key[1], "sk_lock-AF_INET6-NFSD", &svc_key[1]);
  102. break;
  103. default:
  104. BUG();
  105. }
  106. }
  107. #else
  108. static inline void svc_reclassify_socket(struct socket *sock)
  109. {
  110. }
  111. #endif
  112. static char *__svc_print_addr(struct sockaddr *addr, char *buf, size_t len)
  113. {
  114. switch (addr->sa_family) {
  115. case AF_INET:
  116. snprintf(buf, len, "%u.%u.%u.%u, port=%u",
  117. NIPQUAD(((struct sockaddr_in *) addr)->sin_addr),
  118. htons(((struct sockaddr_in *) addr)->sin_port));
  119. break;
  120. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  121. case AF_INET6:
  122. snprintf(buf, len, "%x:%x:%x:%x:%x:%x:%x:%x, port=%u",
  123. NIP6(((struct sockaddr_in6 *) addr)->sin6_addr),
  124. htons(((struct sockaddr_in6 *) addr)->sin6_port));
  125. break;
  126. #endif
  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((struct sockaddr *) &rqstp->rq_addr, 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. /*
  394. * Generic sendto routine
  395. */
  396. static int
  397. svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr)
  398. {
  399. struct svc_sock *svsk = rqstp->rq_sock;
  400. struct socket *sock = svsk->sk_sock;
  401. int slen;
  402. char buffer[CMSG_SPACE(sizeof(struct in_pktinfo))];
  403. struct cmsghdr *cmh = (struct cmsghdr *)buffer;
  404. struct in_pktinfo *pki = (struct in_pktinfo *)CMSG_DATA(cmh);
  405. int len = 0;
  406. int result;
  407. int size;
  408. struct page **ppage = xdr->pages;
  409. size_t base = xdr->page_base;
  410. unsigned int pglen = xdr->page_len;
  411. unsigned int flags = MSG_MORE;
  412. char buf[RPC_MAX_ADDRBUFLEN];
  413. slen = xdr->len;
  414. if (rqstp->rq_prot == IPPROTO_UDP) {
  415. /* set the source and destination */
  416. struct msghdr msg;
  417. msg.msg_name = &rqstp->rq_addr;
  418. msg.msg_namelen = sizeof(rqstp->rq_addr);
  419. msg.msg_iov = NULL;
  420. msg.msg_iovlen = 0;
  421. msg.msg_flags = MSG_MORE;
  422. msg.msg_control = cmh;
  423. msg.msg_controllen = sizeof(buffer);
  424. cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
  425. cmh->cmsg_level = SOL_IP;
  426. cmh->cmsg_type = IP_PKTINFO;
  427. pki->ipi_ifindex = 0;
  428. pki->ipi_spec_dst.s_addr = rqstp->rq_daddr;
  429. if (sock_sendmsg(sock, &msg, 0) < 0)
  430. goto out;
  431. }
  432. /* send head */
  433. if (slen == xdr->head[0].iov_len)
  434. flags = 0;
  435. len = kernel_sendpage(sock, rqstp->rq_respages[0], 0,
  436. xdr->head[0].iov_len, flags);
  437. if (len != xdr->head[0].iov_len)
  438. goto out;
  439. slen -= xdr->head[0].iov_len;
  440. if (slen == 0)
  441. goto out;
  442. /* send page data */
  443. size = PAGE_SIZE - base < pglen ? PAGE_SIZE - base : pglen;
  444. while (pglen > 0) {
  445. if (slen == size)
  446. flags = 0;
  447. result = kernel_sendpage(sock, *ppage, base, size, flags);
  448. if (result > 0)
  449. len += result;
  450. if (result != size)
  451. goto out;
  452. slen -= size;
  453. pglen -= size;
  454. size = PAGE_SIZE < pglen ? PAGE_SIZE : pglen;
  455. base = 0;
  456. ppage++;
  457. }
  458. /* send tail */
  459. if (xdr->tail[0].iov_len) {
  460. result = kernel_sendpage(sock, rqstp->rq_respages[0],
  461. ((unsigned long)xdr->tail[0].iov_base)
  462. & (PAGE_SIZE-1),
  463. xdr->tail[0].iov_len, 0);
  464. if (result > 0)
  465. len += result;
  466. }
  467. out:
  468. dprintk("svc: socket %p sendto([%p %Zu... ], %d) = %d (addr %s)\n",
  469. rqstp->rq_sock, xdr->head[0].iov_base, xdr->head[0].iov_len,
  470. xdr->len, len, svc_print_addr(rqstp, buf, sizeof(buf)));
  471. return len;
  472. }
  473. /*
  474. * Report socket names for nfsdfs
  475. */
  476. static int one_sock_name(char *buf, struct svc_sock *svsk)
  477. {
  478. int len;
  479. switch(svsk->sk_sk->sk_family) {
  480. case AF_INET:
  481. len = sprintf(buf, "ipv4 %s %u.%u.%u.%u %d\n",
  482. svsk->sk_sk->sk_protocol==IPPROTO_UDP?
  483. "udp" : "tcp",
  484. NIPQUAD(inet_sk(svsk->sk_sk)->rcv_saddr),
  485. inet_sk(svsk->sk_sk)->num);
  486. break;
  487. default:
  488. len = sprintf(buf, "*unknown-%d*\n",
  489. svsk->sk_sk->sk_family);
  490. }
  491. return len;
  492. }
  493. int
  494. svc_sock_names(char *buf, struct svc_serv *serv, char *toclose)
  495. {
  496. struct svc_sock *svsk, *closesk = NULL;
  497. int len = 0;
  498. if (!serv)
  499. return 0;
  500. spin_lock_bh(&serv->sv_lock);
  501. list_for_each_entry(svsk, &serv->sv_permsocks, sk_list) {
  502. int onelen = one_sock_name(buf+len, svsk);
  503. if (toclose && strcmp(toclose, buf+len) == 0)
  504. closesk = svsk;
  505. else
  506. len += onelen;
  507. }
  508. spin_unlock_bh(&serv->sv_lock);
  509. if (closesk)
  510. /* Should unregister with portmap, but you cannot
  511. * unregister just one protocol...
  512. */
  513. svc_close_socket(closesk);
  514. else if (toclose)
  515. return -ENOENT;
  516. return len;
  517. }
  518. EXPORT_SYMBOL(svc_sock_names);
  519. /*
  520. * Check input queue length
  521. */
  522. static int
  523. svc_recv_available(struct svc_sock *svsk)
  524. {
  525. struct socket *sock = svsk->sk_sock;
  526. int avail, err;
  527. err = kernel_sock_ioctl(sock, TIOCINQ, (unsigned long) &avail);
  528. return (err >= 0)? avail : err;
  529. }
  530. /*
  531. * Generic recvfrom routine.
  532. */
  533. static int
  534. svc_recvfrom(struct svc_rqst *rqstp, struct kvec *iov, int nr, int buflen)
  535. {
  536. struct svc_sock *svsk = rqstp->rq_sock;
  537. struct msghdr msg = {
  538. .msg_flags = MSG_DONTWAIT,
  539. };
  540. int len;
  541. len = kernel_recvmsg(svsk->sk_sock, &msg, iov, nr, buflen,
  542. msg.msg_flags);
  543. /* sock_recvmsg doesn't fill in the name/namelen, so we must..
  544. */
  545. memcpy(&rqstp->rq_addr, &svsk->sk_remote, svsk->sk_remotelen);
  546. rqstp->rq_addrlen = svsk->sk_remotelen;
  547. dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",
  548. svsk, iov[0].iov_base, iov[0].iov_len, len);
  549. return len;
  550. }
  551. /*
  552. * Set socket snd and rcv buffer lengths
  553. */
  554. static inline void
  555. svc_sock_setbufsize(struct socket *sock, unsigned int snd, unsigned int rcv)
  556. {
  557. #if 0
  558. mm_segment_t oldfs;
  559. oldfs = get_fs(); set_fs(KERNEL_DS);
  560. sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
  561. (char*)&snd, sizeof(snd));
  562. sock_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
  563. (char*)&rcv, sizeof(rcv));
  564. #else
  565. /* sock_setsockopt limits use to sysctl_?mem_max,
  566. * which isn't acceptable. Until that is made conditional
  567. * on not having CAP_SYS_RESOURCE or similar, we go direct...
  568. * DaveM said I could!
  569. */
  570. lock_sock(sock->sk);
  571. sock->sk->sk_sndbuf = snd * 2;
  572. sock->sk->sk_rcvbuf = rcv * 2;
  573. sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK|SOCK_RCVBUF_LOCK;
  574. release_sock(sock->sk);
  575. #endif
  576. }
  577. /*
  578. * INET callback when data has been received on the socket.
  579. */
  580. static void
  581. svc_udp_data_ready(struct sock *sk, int count)
  582. {
  583. struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
  584. if (svsk) {
  585. dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n",
  586. svsk, sk, count, test_bit(SK_BUSY, &svsk->sk_flags));
  587. set_bit(SK_DATA, &svsk->sk_flags);
  588. svc_sock_enqueue(svsk);
  589. }
  590. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  591. wake_up_interruptible(sk->sk_sleep);
  592. }
  593. /*
  594. * INET callback when space is newly available on the socket.
  595. */
  596. static void
  597. svc_write_space(struct sock *sk)
  598. {
  599. struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
  600. if (svsk) {
  601. dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
  602. svsk, sk, test_bit(SK_BUSY, &svsk->sk_flags));
  603. svc_sock_enqueue(svsk);
  604. }
  605. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) {
  606. dprintk("RPC svc_write_space: someone sleeping on %p\n",
  607. svsk);
  608. wake_up_interruptible(sk->sk_sleep);
  609. }
  610. }
  611. /*
  612. * Receive a datagram from a UDP socket.
  613. */
  614. static int
  615. svc_udp_recvfrom(struct svc_rqst *rqstp)
  616. {
  617. struct svc_sock *svsk = rqstp->rq_sock;
  618. struct svc_serv *serv = svsk->sk_server;
  619. struct sk_buff *skb;
  620. int err, len;
  621. if (test_and_clear_bit(SK_CHNGBUF, &svsk->sk_flags))
  622. /* udp sockets need large rcvbuf as all pending
  623. * requests are still in that buffer. sndbuf must
  624. * also be large enough that there is enough space
  625. * for one reply per thread. We count all threads
  626. * rather than threads in a particular pool, which
  627. * provides an upper bound on the number of threads
  628. * which will access the socket.
  629. */
  630. svc_sock_setbufsize(svsk->sk_sock,
  631. (serv->sv_nrthreads+3) * serv->sv_max_mesg,
  632. (serv->sv_nrthreads+3) * serv->sv_max_mesg);
  633. if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) {
  634. svc_sock_received(svsk);
  635. return svc_deferred_recv(rqstp);
  636. }
  637. if (test_bit(SK_CLOSE, &svsk->sk_flags)) {
  638. svc_delete_socket(svsk);
  639. return 0;
  640. }
  641. clear_bit(SK_DATA, &svsk->sk_flags);
  642. while ((skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err)) == NULL) {
  643. if (err == -EAGAIN) {
  644. svc_sock_received(svsk);
  645. return err;
  646. }
  647. /* possibly an icmp error */
  648. dprintk("svc: recvfrom returned error %d\n", -err);
  649. }
  650. if (skb->tstamp.off_sec == 0) {
  651. struct timeval tv;
  652. tv.tv_sec = xtime.tv_sec;
  653. tv.tv_usec = xtime.tv_nsec / NSEC_PER_USEC;
  654. skb_set_timestamp(skb, &tv);
  655. /* Don't enable netstamp, sunrpc doesn't
  656. need that much accuracy */
  657. }
  658. skb_get_timestamp(skb, &svsk->sk_sk->sk_stamp);
  659. set_bit(SK_DATA, &svsk->sk_flags); /* there may be more data... */
  660. /*
  661. * Maybe more packets - kick another thread ASAP.
  662. */
  663. svc_sock_received(svsk);
  664. len = skb->len - sizeof(struct udphdr);
  665. rqstp->rq_arg.len = len;
  666. rqstp->rq_prot = IPPROTO_UDP;
  667. /* Get sender address */
  668. rqstp->rq_addr.sin_family = AF_INET;
  669. rqstp->rq_addr.sin_port = skb->h.uh->source;
  670. rqstp->rq_addr.sin_addr.s_addr = skb->nh.iph->saddr;
  671. rqstp->rq_daddr = skb->nh.iph->daddr;
  672. if (skb_is_nonlinear(skb)) {
  673. /* we have to copy */
  674. local_bh_disable();
  675. if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) {
  676. local_bh_enable();
  677. /* checksum error */
  678. skb_free_datagram(svsk->sk_sk, skb);
  679. return 0;
  680. }
  681. local_bh_enable();
  682. skb_free_datagram(svsk->sk_sk, skb);
  683. } else {
  684. /* we can use it in-place */
  685. rqstp->rq_arg.head[0].iov_base = skb->data + sizeof(struct udphdr);
  686. rqstp->rq_arg.head[0].iov_len = len;
  687. if (skb_checksum_complete(skb)) {
  688. skb_free_datagram(svsk->sk_sk, skb);
  689. return 0;
  690. }
  691. rqstp->rq_skbuff = skb;
  692. }
  693. rqstp->rq_arg.page_base = 0;
  694. if (len <= rqstp->rq_arg.head[0].iov_len) {
  695. rqstp->rq_arg.head[0].iov_len = len;
  696. rqstp->rq_arg.page_len = 0;
  697. rqstp->rq_respages = rqstp->rq_pages+1;
  698. } else {
  699. rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
  700. rqstp->rq_respages = rqstp->rq_pages + 1 +
  701. (rqstp->rq_arg.page_len + PAGE_SIZE - 1)/ PAGE_SIZE;
  702. }
  703. if (serv->sv_stats)
  704. serv->sv_stats->netudpcnt++;
  705. return len;
  706. }
  707. static int
  708. svc_udp_sendto(struct svc_rqst *rqstp)
  709. {
  710. int error;
  711. error = svc_sendto(rqstp, &rqstp->rq_res);
  712. if (error == -ECONNREFUSED)
  713. /* ICMP error on earlier request. */
  714. error = svc_sendto(rqstp, &rqstp->rq_res);
  715. return error;
  716. }
  717. static void
  718. svc_udp_init(struct svc_sock *svsk)
  719. {
  720. svsk->sk_sk->sk_data_ready = svc_udp_data_ready;
  721. svsk->sk_sk->sk_write_space = svc_write_space;
  722. svsk->sk_recvfrom = svc_udp_recvfrom;
  723. svsk->sk_sendto = svc_udp_sendto;
  724. /* initialise setting must have enough space to
  725. * receive and respond to one request.
  726. * svc_udp_recvfrom will re-adjust if necessary
  727. */
  728. svc_sock_setbufsize(svsk->sk_sock,
  729. 3 * svsk->sk_server->sv_max_mesg,
  730. 3 * svsk->sk_server->sv_max_mesg);
  731. set_bit(SK_DATA, &svsk->sk_flags); /* might have come in before data_ready set up */
  732. set_bit(SK_CHNGBUF, &svsk->sk_flags);
  733. }
  734. /*
  735. * A data_ready event on a listening socket means there's a connection
  736. * pending. Do not use state_change as a substitute for it.
  737. */
  738. static void
  739. svc_tcp_listen_data_ready(struct sock *sk, int count_unused)
  740. {
  741. struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
  742. dprintk("svc: socket %p TCP (listen) state change %d\n",
  743. sk, sk->sk_state);
  744. /*
  745. * This callback may called twice when a new connection
  746. * is established as a child socket inherits everything
  747. * from a parent LISTEN socket.
  748. * 1) data_ready method of the parent socket will be called
  749. * when one of child sockets become ESTABLISHED.
  750. * 2) data_ready method of the child socket may be called
  751. * when it receives data before the socket is accepted.
  752. * In case of 2, we should ignore it silently.
  753. */
  754. if (sk->sk_state == TCP_LISTEN) {
  755. if (svsk) {
  756. set_bit(SK_CONN, &svsk->sk_flags);
  757. svc_sock_enqueue(svsk);
  758. } else
  759. printk("svc: socket %p: no user data\n", sk);
  760. }
  761. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  762. wake_up_interruptible_all(sk->sk_sleep);
  763. }
  764. /*
  765. * A state change on a connected socket means it's dying or dead.
  766. */
  767. static void
  768. svc_tcp_state_change(struct sock *sk)
  769. {
  770. struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
  771. dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
  772. sk, sk->sk_state, sk->sk_user_data);
  773. if (!svsk)
  774. printk("svc: socket %p: no user data\n", sk);
  775. else {
  776. set_bit(SK_CLOSE, &svsk->sk_flags);
  777. svc_sock_enqueue(svsk);
  778. }
  779. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  780. wake_up_interruptible_all(sk->sk_sleep);
  781. }
  782. static void
  783. svc_tcp_data_ready(struct sock *sk, int count)
  784. {
  785. struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
  786. dprintk("svc: socket %p TCP data ready (svsk %p)\n",
  787. sk, sk->sk_user_data);
  788. if (svsk) {
  789. set_bit(SK_DATA, &svsk->sk_flags);
  790. svc_sock_enqueue(svsk);
  791. }
  792. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  793. wake_up_interruptible(sk->sk_sleep);
  794. }
  795. /*
  796. * Accept a TCP connection
  797. */
  798. static void
  799. svc_tcp_accept(struct svc_sock *svsk)
  800. {
  801. struct sockaddr_in sin;
  802. struct svc_serv *serv = svsk->sk_server;
  803. struct socket *sock = svsk->sk_sock;
  804. struct socket *newsock;
  805. struct svc_sock *newsvsk;
  806. int err, slen;
  807. char buf[RPC_MAX_ADDRBUFLEN];
  808. dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
  809. if (!sock)
  810. return;
  811. clear_bit(SK_CONN, &svsk->sk_flags);
  812. err = kernel_accept(sock, &newsock, O_NONBLOCK);
  813. if (err < 0) {
  814. if (err == -ENOMEM)
  815. printk(KERN_WARNING "%s: no more sockets!\n",
  816. serv->sv_name);
  817. else if (err != -EAGAIN && net_ratelimit())
  818. printk(KERN_WARNING "%s: accept failed (err %d)!\n",
  819. serv->sv_name, -err);
  820. return;
  821. }
  822. set_bit(SK_CONN, &svsk->sk_flags);
  823. svc_sock_enqueue(svsk);
  824. slen = sizeof(sin);
  825. err = kernel_getpeername(newsock, (struct sockaddr *) &sin, &slen);
  826. if (err < 0) {
  827. if (net_ratelimit())
  828. printk(KERN_WARNING "%s: peername failed (err %d)!\n",
  829. serv->sv_name, -err);
  830. goto failed; /* aborted connection or whatever */
  831. }
  832. /* Ideally, we would want to reject connections from unauthorized
  833. * hosts here, but when we get encryption, the IP of the host won't
  834. * tell us anything. For now just warn about unpriv connections.
  835. */
  836. if (ntohs(sin.sin_port) >= 1024) {
  837. dprintk(KERN_WARNING
  838. "%s: connect from unprivileged port: %s\n",
  839. serv->sv_name,
  840. __svc_print_addr((struct sockaddr *) &sin, buf,
  841. sizeof(buf)));
  842. }
  843. dprintk("%s: connect from %s\n", serv->sv_name,
  844. __svc_print_addr((struct sockaddr *) &sin, buf,
  845. sizeof(buf)));
  846. /* make sure that a write doesn't block forever when
  847. * low on memory
  848. */
  849. newsock->sk->sk_sndtimeo = HZ*30;
  850. if (!(newsvsk = svc_setup_socket(serv, newsock, &err,
  851. (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY))))
  852. goto failed;
  853. memcpy(&newsvsk->sk_remote, &sin, slen);
  854. newsvsk->sk_remotelen = slen;
  855. svc_sock_received(newsvsk);
  856. /* make sure that we don't have too many active connections.
  857. * If we have, something must be dropped.
  858. *
  859. * There's no point in trying to do random drop here for
  860. * DoS prevention. The NFS clients does 1 reconnect in 15
  861. * seconds. An attacker can easily beat that.
  862. *
  863. * The only somewhat efficient mechanism would be if drop
  864. * old connections from the same IP first. But right now
  865. * we don't even record the client IP in svc_sock.
  866. */
  867. if (serv->sv_tmpcnt > (serv->sv_nrthreads+3)*20) {
  868. struct svc_sock *svsk = NULL;
  869. spin_lock_bh(&serv->sv_lock);
  870. if (!list_empty(&serv->sv_tempsocks)) {
  871. if (net_ratelimit()) {
  872. /* Try to help the admin */
  873. printk(KERN_NOTICE "%s: too many open TCP "
  874. "sockets, consider increasing the "
  875. "number of nfsd threads\n",
  876. serv->sv_name);
  877. printk(KERN_NOTICE
  878. "%s: last TCP connect from %s\n",
  879. serv->sv_name, buf);
  880. }
  881. /*
  882. * Always select the oldest socket. It's not fair,
  883. * but so is life
  884. */
  885. svsk = list_entry(serv->sv_tempsocks.prev,
  886. struct svc_sock,
  887. sk_list);
  888. set_bit(SK_CLOSE, &svsk->sk_flags);
  889. atomic_inc(&svsk->sk_inuse);
  890. }
  891. spin_unlock_bh(&serv->sv_lock);
  892. if (svsk) {
  893. svc_sock_enqueue(svsk);
  894. svc_sock_put(svsk);
  895. }
  896. }
  897. if (serv->sv_stats)
  898. serv->sv_stats->nettcpconn++;
  899. return;
  900. failed:
  901. sock_release(newsock);
  902. return;
  903. }
  904. /*
  905. * Receive data from a TCP socket.
  906. */
  907. static int
  908. svc_tcp_recvfrom(struct svc_rqst *rqstp)
  909. {
  910. struct svc_sock *svsk = rqstp->rq_sock;
  911. struct svc_serv *serv = svsk->sk_server;
  912. int len;
  913. struct kvec *vec;
  914. int pnum, vlen;
  915. dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
  916. svsk, test_bit(SK_DATA, &svsk->sk_flags),
  917. test_bit(SK_CONN, &svsk->sk_flags),
  918. test_bit(SK_CLOSE, &svsk->sk_flags));
  919. if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) {
  920. svc_sock_received(svsk);
  921. return svc_deferred_recv(rqstp);
  922. }
  923. if (test_bit(SK_CLOSE, &svsk->sk_flags)) {
  924. svc_delete_socket(svsk);
  925. return 0;
  926. }
  927. if (svsk->sk_sk->sk_state == TCP_LISTEN) {
  928. svc_tcp_accept(svsk);
  929. svc_sock_received(svsk);
  930. return 0;
  931. }
  932. if (test_and_clear_bit(SK_CHNGBUF, &svsk->sk_flags))
  933. /* sndbuf needs to have room for one request
  934. * per thread, otherwise we can stall even when the
  935. * network isn't a bottleneck.
  936. *
  937. * We count all threads rather than threads in a
  938. * particular pool, which provides an upper bound
  939. * on the number of threads which will access the socket.
  940. *
  941. * rcvbuf just needs to be able to hold a few requests.
  942. * Normally they will be removed from the queue
  943. * as soon a a complete request arrives.
  944. */
  945. svc_sock_setbufsize(svsk->sk_sock,
  946. (serv->sv_nrthreads+3) * serv->sv_max_mesg,
  947. 3 * serv->sv_max_mesg);
  948. clear_bit(SK_DATA, &svsk->sk_flags);
  949. /* Receive data. If we haven't got the record length yet, get
  950. * the next four bytes. Otherwise try to gobble up as much as
  951. * possible up to the complete record length.
  952. */
  953. if (svsk->sk_tcplen < 4) {
  954. unsigned long want = 4 - svsk->sk_tcplen;
  955. struct kvec iov;
  956. iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
  957. iov.iov_len = want;
  958. if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0)
  959. goto error;
  960. svsk->sk_tcplen += len;
  961. if (len < want) {
  962. dprintk("svc: short recvfrom while reading record length (%d of %lu)\n",
  963. len, want);
  964. svc_sock_received(svsk);
  965. return -EAGAIN; /* record header not complete */
  966. }
  967. svsk->sk_reclen = ntohl(svsk->sk_reclen);
  968. if (!(svsk->sk_reclen & 0x80000000)) {
  969. /* FIXME: technically, a record can be fragmented,
  970. * and non-terminal fragments will not have the top
  971. * bit set in the fragment length header.
  972. * But apparently no known nfs clients send fragmented
  973. * records. */
  974. if (net_ratelimit())
  975. printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx"
  976. " (non-terminal)\n",
  977. (unsigned long) svsk->sk_reclen);
  978. goto err_delete;
  979. }
  980. svsk->sk_reclen &= 0x7fffffff;
  981. dprintk("svc: TCP record, %d bytes\n", svsk->sk_reclen);
  982. if (svsk->sk_reclen > serv->sv_max_mesg) {
  983. if (net_ratelimit())
  984. printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx"
  985. " (large)\n",
  986. (unsigned long) svsk->sk_reclen);
  987. goto err_delete;
  988. }
  989. }
  990. /* Check whether enough data is available */
  991. len = svc_recv_available(svsk);
  992. if (len < 0)
  993. goto error;
  994. if (len < svsk->sk_reclen) {
  995. dprintk("svc: incomplete TCP record (%d of %d)\n",
  996. len, svsk->sk_reclen);
  997. svc_sock_received(svsk);
  998. return -EAGAIN; /* record not complete */
  999. }
  1000. len = svsk->sk_reclen;
  1001. set_bit(SK_DATA, &svsk->sk_flags);
  1002. vec = rqstp->rq_vec;
  1003. vec[0] = rqstp->rq_arg.head[0];
  1004. vlen = PAGE_SIZE;
  1005. pnum = 1;
  1006. while (vlen < len) {
  1007. vec[pnum].iov_base = page_address(rqstp->rq_pages[pnum]);
  1008. vec[pnum].iov_len = PAGE_SIZE;
  1009. pnum++;
  1010. vlen += PAGE_SIZE;
  1011. }
  1012. rqstp->rq_respages = &rqstp->rq_pages[pnum];
  1013. /* Now receive data */
  1014. len = svc_recvfrom(rqstp, vec, pnum, len);
  1015. if (len < 0)
  1016. goto error;
  1017. dprintk("svc: TCP complete record (%d bytes)\n", len);
  1018. rqstp->rq_arg.len = len;
  1019. rqstp->rq_arg.page_base = 0;
  1020. if (len <= rqstp->rq_arg.head[0].iov_len) {
  1021. rqstp->rq_arg.head[0].iov_len = len;
  1022. rqstp->rq_arg.page_len = 0;
  1023. } else {
  1024. rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
  1025. }
  1026. rqstp->rq_skbuff = NULL;
  1027. rqstp->rq_prot = IPPROTO_TCP;
  1028. /* Reset TCP read info */
  1029. svsk->sk_reclen = 0;
  1030. svsk->sk_tcplen = 0;
  1031. svc_sock_received(svsk);
  1032. if (serv->sv_stats)
  1033. serv->sv_stats->nettcpcnt++;
  1034. return len;
  1035. err_delete:
  1036. svc_delete_socket(svsk);
  1037. return -EAGAIN;
  1038. error:
  1039. if (len == -EAGAIN) {
  1040. dprintk("RPC: TCP recvfrom got EAGAIN\n");
  1041. svc_sock_received(svsk);
  1042. } else {
  1043. printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
  1044. svsk->sk_server->sv_name, -len);
  1045. goto err_delete;
  1046. }
  1047. return len;
  1048. }
  1049. /*
  1050. * Send out data on TCP socket.
  1051. */
  1052. static int
  1053. svc_tcp_sendto(struct svc_rqst *rqstp)
  1054. {
  1055. struct xdr_buf *xbufp = &rqstp->rq_res;
  1056. int sent;
  1057. __be32 reclen;
  1058. /* Set up the first element of the reply kvec.
  1059. * Any other kvecs that may be in use have been taken
  1060. * care of by the server implementation itself.
  1061. */
  1062. reclen = htonl(0x80000000|((xbufp->len ) - 4));
  1063. memcpy(xbufp->head[0].iov_base, &reclen, 4);
  1064. if (test_bit(SK_DEAD, &rqstp->rq_sock->sk_flags))
  1065. return -ENOTCONN;
  1066. sent = svc_sendto(rqstp, &rqstp->rq_res);
  1067. if (sent != xbufp->len) {
  1068. printk(KERN_NOTICE "rpc-srv/tcp: %s: %s %d when sending %d bytes - shutting down socket\n",
  1069. rqstp->rq_sock->sk_server->sv_name,
  1070. (sent<0)?"got error":"sent only",
  1071. sent, xbufp->len);
  1072. set_bit(SK_CLOSE, &rqstp->rq_sock->sk_flags);
  1073. svc_sock_enqueue(rqstp->rq_sock);
  1074. sent = -EAGAIN;
  1075. }
  1076. return sent;
  1077. }
  1078. static void
  1079. svc_tcp_init(struct svc_sock *svsk)
  1080. {
  1081. struct sock *sk = svsk->sk_sk;
  1082. struct tcp_sock *tp = tcp_sk(sk);
  1083. svsk->sk_recvfrom = svc_tcp_recvfrom;
  1084. svsk->sk_sendto = svc_tcp_sendto;
  1085. if (sk->sk_state == TCP_LISTEN) {
  1086. dprintk("setting up TCP socket for listening\n");
  1087. sk->sk_data_ready = svc_tcp_listen_data_ready;
  1088. set_bit(SK_CONN, &svsk->sk_flags);
  1089. } else {
  1090. dprintk("setting up TCP socket for reading\n");
  1091. sk->sk_state_change = svc_tcp_state_change;
  1092. sk->sk_data_ready = svc_tcp_data_ready;
  1093. sk->sk_write_space = svc_write_space;
  1094. svsk->sk_reclen = 0;
  1095. svsk->sk_tcplen = 0;
  1096. tp->nonagle = 1; /* disable Nagle's algorithm */
  1097. /* initialise setting must have enough space to
  1098. * receive and respond to one request.
  1099. * svc_tcp_recvfrom will re-adjust if necessary
  1100. */
  1101. svc_sock_setbufsize(svsk->sk_sock,
  1102. 3 * svsk->sk_server->sv_max_mesg,
  1103. 3 * svsk->sk_server->sv_max_mesg);
  1104. set_bit(SK_CHNGBUF, &svsk->sk_flags);
  1105. set_bit(SK_DATA, &svsk->sk_flags);
  1106. if (sk->sk_state != TCP_ESTABLISHED)
  1107. set_bit(SK_CLOSE, &svsk->sk_flags);
  1108. }
  1109. }
  1110. void
  1111. svc_sock_update_bufs(struct svc_serv *serv)
  1112. {
  1113. /*
  1114. * The number of server threads has changed. Update
  1115. * rcvbuf and sndbuf accordingly on all sockets
  1116. */
  1117. struct list_head *le;
  1118. spin_lock_bh(&serv->sv_lock);
  1119. list_for_each(le, &serv->sv_permsocks) {
  1120. struct svc_sock *svsk =
  1121. list_entry(le, struct svc_sock, sk_list);
  1122. set_bit(SK_CHNGBUF, &svsk->sk_flags);
  1123. }
  1124. list_for_each(le, &serv->sv_tempsocks) {
  1125. struct svc_sock *svsk =
  1126. list_entry(le, struct svc_sock, sk_list);
  1127. set_bit(SK_CHNGBUF, &svsk->sk_flags);
  1128. }
  1129. spin_unlock_bh(&serv->sv_lock);
  1130. }
  1131. /*
  1132. * Receive the next request on any socket. This code is carefully
  1133. * organised not to touch any cachelines in the shared svc_serv
  1134. * structure, only cachelines in the local svc_pool.
  1135. */
  1136. int
  1137. svc_recv(struct svc_rqst *rqstp, long timeout)
  1138. {
  1139. struct svc_sock *svsk =NULL;
  1140. struct svc_serv *serv = rqstp->rq_server;
  1141. struct svc_pool *pool = rqstp->rq_pool;
  1142. int len, i;
  1143. int pages;
  1144. struct xdr_buf *arg;
  1145. DECLARE_WAITQUEUE(wait, current);
  1146. dprintk("svc: server %p waiting for data (to = %ld)\n",
  1147. rqstp, timeout);
  1148. if (rqstp->rq_sock)
  1149. printk(KERN_ERR
  1150. "svc_recv: service %p, socket not NULL!\n",
  1151. rqstp);
  1152. if (waitqueue_active(&rqstp->rq_wait))
  1153. printk(KERN_ERR
  1154. "svc_recv: service %p, wait queue active!\n",
  1155. rqstp);
  1156. /* now allocate needed pages. If we get a failure, sleep briefly */
  1157. pages = (serv->sv_max_mesg + PAGE_SIZE) / PAGE_SIZE;
  1158. for (i=0; i < pages ; i++)
  1159. while (rqstp->rq_pages[i] == NULL) {
  1160. struct page *p = alloc_page(GFP_KERNEL);
  1161. if (!p)
  1162. schedule_timeout_uninterruptible(msecs_to_jiffies(500));
  1163. rqstp->rq_pages[i] = p;
  1164. }
  1165. rqstp->rq_pages[i++] = NULL; /* this might be seen in nfs_read_actor */
  1166. BUG_ON(pages >= RPCSVC_MAXPAGES);
  1167. /* Make arg->head point to first page and arg->pages point to rest */
  1168. arg = &rqstp->rq_arg;
  1169. arg->head[0].iov_base = page_address(rqstp->rq_pages[0]);
  1170. arg->head[0].iov_len = PAGE_SIZE;
  1171. arg->pages = rqstp->rq_pages + 1;
  1172. arg->page_base = 0;
  1173. /* save at least one page for response */
  1174. arg->page_len = (pages-2)*PAGE_SIZE;
  1175. arg->len = (pages-1)*PAGE_SIZE;
  1176. arg->tail[0].iov_len = 0;
  1177. try_to_freeze();
  1178. cond_resched();
  1179. if (signalled())
  1180. return -EINTR;
  1181. spin_lock_bh(&pool->sp_lock);
  1182. if ((svsk = svc_sock_dequeue(pool)) != NULL) {
  1183. rqstp->rq_sock = svsk;
  1184. atomic_inc(&svsk->sk_inuse);
  1185. rqstp->rq_reserved = serv->sv_max_mesg;
  1186. atomic_add(rqstp->rq_reserved, &svsk->sk_reserved);
  1187. } else {
  1188. /* No data pending. Go to sleep */
  1189. svc_thread_enqueue(pool, rqstp);
  1190. /*
  1191. * We have to be able to interrupt this wait
  1192. * to bring down the daemons ...
  1193. */
  1194. set_current_state(TASK_INTERRUPTIBLE);
  1195. add_wait_queue(&rqstp->rq_wait, &wait);
  1196. spin_unlock_bh(&pool->sp_lock);
  1197. schedule_timeout(timeout);
  1198. try_to_freeze();
  1199. spin_lock_bh(&pool->sp_lock);
  1200. remove_wait_queue(&rqstp->rq_wait, &wait);
  1201. if (!(svsk = rqstp->rq_sock)) {
  1202. svc_thread_dequeue(pool, rqstp);
  1203. spin_unlock_bh(&pool->sp_lock);
  1204. dprintk("svc: server %p, no data yet\n", rqstp);
  1205. return signalled()? -EINTR : -EAGAIN;
  1206. }
  1207. }
  1208. spin_unlock_bh(&pool->sp_lock);
  1209. dprintk("svc: server %p, pool %u, socket %p, inuse=%d\n",
  1210. rqstp, pool->sp_id, svsk, atomic_read(&svsk->sk_inuse));
  1211. len = svsk->sk_recvfrom(rqstp);
  1212. dprintk("svc: got len=%d\n", len);
  1213. /* No data, incomplete (TCP) read, or accept() */
  1214. if (len == 0 || len == -EAGAIN) {
  1215. rqstp->rq_res.len = 0;
  1216. svc_sock_release(rqstp);
  1217. return -EAGAIN;
  1218. }
  1219. svsk->sk_lastrecv = get_seconds();
  1220. clear_bit(SK_OLD, &svsk->sk_flags);
  1221. rqstp->rq_secure = ntohs(rqstp->rq_addr.sin_port) < 1024;
  1222. rqstp->rq_chandle.defer = svc_defer;
  1223. if (serv->sv_stats)
  1224. serv->sv_stats->netcnt++;
  1225. return len;
  1226. }
  1227. /*
  1228. * Drop request
  1229. */
  1230. void
  1231. svc_drop(struct svc_rqst *rqstp)
  1232. {
  1233. dprintk("svc: socket %p dropped request\n", rqstp->rq_sock);
  1234. svc_sock_release(rqstp);
  1235. }
  1236. /*
  1237. * Return reply to client.
  1238. */
  1239. int
  1240. svc_send(struct svc_rqst *rqstp)
  1241. {
  1242. struct svc_sock *svsk;
  1243. int len;
  1244. struct xdr_buf *xb;
  1245. if ((svsk = rqstp->rq_sock) == NULL) {
  1246. printk(KERN_WARNING "NULL socket pointer in %s:%d\n",
  1247. __FILE__, __LINE__);
  1248. return -EFAULT;
  1249. }
  1250. /* release the receive skb before sending the reply */
  1251. svc_release_skb(rqstp);
  1252. /* calculate over-all length */
  1253. xb = & rqstp->rq_res;
  1254. xb->len = xb->head[0].iov_len +
  1255. xb->page_len +
  1256. xb->tail[0].iov_len;
  1257. /* Grab svsk->sk_mutex to serialize outgoing data. */
  1258. mutex_lock(&svsk->sk_mutex);
  1259. if (test_bit(SK_DEAD, &svsk->sk_flags))
  1260. len = -ENOTCONN;
  1261. else
  1262. len = svsk->sk_sendto(rqstp);
  1263. mutex_unlock(&svsk->sk_mutex);
  1264. svc_sock_release(rqstp);
  1265. if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN)
  1266. return 0;
  1267. return len;
  1268. }
  1269. /*
  1270. * Timer function to close old temporary sockets, using
  1271. * a mark-and-sweep algorithm.
  1272. */
  1273. static void
  1274. svc_age_temp_sockets(unsigned long closure)
  1275. {
  1276. struct svc_serv *serv = (struct svc_serv *)closure;
  1277. struct svc_sock *svsk;
  1278. struct list_head *le, *next;
  1279. LIST_HEAD(to_be_aged);
  1280. dprintk("svc_age_temp_sockets\n");
  1281. if (!spin_trylock_bh(&serv->sv_lock)) {
  1282. /* busy, try again 1 sec later */
  1283. dprintk("svc_age_temp_sockets: busy\n");
  1284. mod_timer(&serv->sv_temptimer, jiffies + HZ);
  1285. return;
  1286. }
  1287. list_for_each_safe(le, next, &serv->sv_tempsocks) {
  1288. svsk = list_entry(le, struct svc_sock, sk_list);
  1289. if (!test_and_set_bit(SK_OLD, &svsk->sk_flags))
  1290. continue;
  1291. if (atomic_read(&svsk->sk_inuse) || test_bit(SK_BUSY, &svsk->sk_flags))
  1292. continue;
  1293. atomic_inc(&svsk->sk_inuse);
  1294. list_move(le, &to_be_aged);
  1295. set_bit(SK_CLOSE, &svsk->sk_flags);
  1296. set_bit(SK_DETACHED, &svsk->sk_flags);
  1297. }
  1298. spin_unlock_bh(&serv->sv_lock);
  1299. while (!list_empty(&to_be_aged)) {
  1300. le = to_be_aged.next;
  1301. /* fiddling the sk_list node is safe 'cos we're SK_DETACHED */
  1302. list_del_init(le);
  1303. svsk = list_entry(le, struct svc_sock, sk_list);
  1304. dprintk("queuing svsk %p for closing, %lu seconds old\n",
  1305. svsk, get_seconds() - svsk->sk_lastrecv);
  1306. /* a thread will dequeue and close it soon */
  1307. svc_sock_enqueue(svsk);
  1308. svc_sock_put(svsk);
  1309. }
  1310. mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ);
  1311. }
  1312. /*
  1313. * Initialize socket for RPC use and create svc_sock struct
  1314. * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF.
  1315. */
  1316. static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
  1317. struct socket *sock,
  1318. int *errp, int flags)
  1319. {
  1320. struct svc_sock *svsk;
  1321. struct sock *inet;
  1322. int pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
  1323. int is_temporary = flags & SVC_SOCK_TEMPORARY;
  1324. dprintk("svc: svc_setup_socket %p\n", sock);
  1325. if (!(svsk = kzalloc(sizeof(*svsk), GFP_KERNEL))) {
  1326. *errp = -ENOMEM;
  1327. return NULL;
  1328. }
  1329. inet = sock->sk;
  1330. /* Register socket with portmapper */
  1331. if (*errp >= 0 && pmap_register)
  1332. *errp = svc_register(serv, inet->sk_protocol,
  1333. ntohs(inet_sk(inet)->sport));
  1334. if (*errp < 0) {
  1335. kfree(svsk);
  1336. return NULL;
  1337. }
  1338. set_bit(SK_BUSY, &svsk->sk_flags);
  1339. inet->sk_user_data = svsk;
  1340. svsk->sk_sock = sock;
  1341. svsk->sk_sk = inet;
  1342. svsk->sk_ostate = inet->sk_state_change;
  1343. svsk->sk_odata = inet->sk_data_ready;
  1344. svsk->sk_owspace = inet->sk_write_space;
  1345. svsk->sk_server = serv;
  1346. atomic_set(&svsk->sk_inuse, 1);
  1347. svsk->sk_lastrecv = get_seconds();
  1348. spin_lock_init(&svsk->sk_defer_lock);
  1349. INIT_LIST_HEAD(&svsk->sk_deferred);
  1350. INIT_LIST_HEAD(&svsk->sk_ready);
  1351. mutex_init(&svsk->sk_mutex);
  1352. /* Initialize the socket */
  1353. if (sock->type == SOCK_DGRAM)
  1354. svc_udp_init(svsk);
  1355. else
  1356. svc_tcp_init(svsk);
  1357. spin_lock_bh(&serv->sv_lock);
  1358. if (is_temporary) {
  1359. set_bit(SK_TEMP, &svsk->sk_flags);
  1360. list_add(&svsk->sk_list, &serv->sv_tempsocks);
  1361. serv->sv_tmpcnt++;
  1362. if (serv->sv_temptimer.function == NULL) {
  1363. /* setup timer to age temp sockets */
  1364. setup_timer(&serv->sv_temptimer, svc_age_temp_sockets,
  1365. (unsigned long)serv);
  1366. mod_timer(&serv->sv_temptimer,
  1367. jiffies + svc_conn_age_period * HZ);
  1368. }
  1369. } else {
  1370. clear_bit(SK_TEMP, &svsk->sk_flags);
  1371. list_add(&svsk->sk_list, &serv->sv_permsocks);
  1372. }
  1373. spin_unlock_bh(&serv->sv_lock);
  1374. dprintk("svc: svc_setup_socket created %p (inet %p)\n",
  1375. svsk, svsk->sk_sk);
  1376. return svsk;
  1377. }
  1378. int svc_addsock(struct svc_serv *serv,
  1379. int fd,
  1380. char *name_return,
  1381. int *proto)
  1382. {
  1383. int err = 0;
  1384. struct socket *so = sockfd_lookup(fd, &err);
  1385. struct svc_sock *svsk = NULL;
  1386. if (!so)
  1387. return err;
  1388. if (so->sk->sk_family != AF_INET)
  1389. err = -EAFNOSUPPORT;
  1390. else if (so->sk->sk_protocol != IPPROTO_TCP &&
  1391. so->sk->sk_protocol != IPPROTO_UDP)
  1392. err = -EPROTONOSUPPORT;
  1393. else if (so->state > SS_UNCONNECTED)
  1394. err = -EISCONN;
  1395. else {
  1396. svsk = svc_setup_socket(serv, so, &err, SVC_SOCK_DEFAULTS);
  1397. if (svsk) {
  1398. svc_sock_received(svsk);
  1399. err = 0;
  1400. }
  1401. }
  1402. if (err) {
  1403. sockfd_put(so);
  1404. return err;
  1405. }
  1406. if (proto) *proto = so->sk->sk_protocol;
  1407. return one_sock_name(name_return, svsk);
  1408. }
  1409. EXPORT_SYMBOL_GPL(svc_addsock);
  1410. /*
  1411. * Create socket for RPC service.
  1412. */
  1413. static int svc_create_socket(struct svc_serv *serv, int protocol,
  1414. struct sockaddr_in *sin, int flags)
  1415. {
  1416. struct svc_sock *svsk;
  1417. struct socket *sock;
  1418. int error;
  1419. int type;
  1420. char buf[RPC_MAX_ADDRBUFLEN];
  1421. dprintk("svc: svc_create_socket(%s, %d, %s)\n",
  1422. serv->sv_program->pg_name, protocol,
  1423. __svc_print_addr((struct sockaddr *) sin, buf,
  1424. sizeof(buf)));
  1425. if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
  1426. printk(KERN_WARNING "svc: only UDP and TCP "
  1427. "sockets supported\n");
  1428. return -EINVAL;
  1429. }
  1430. type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
  1431. if ((error = sock_create_kern(PF_INET, type, protocol, &sock)) < 0)
  1432. return error;
  1433. svc_reclassify_socket(sock);
  1434. if (type == SOCK_STREAM)
  1435. sock->sk->sk_reuse = 1; /* allow address reuse */
  1436. error = kernel_bind(sock, (struct sockaddr *) sin,
  1437. sizeof(*sin));
  1438. if (error < 0)
  1439. goto bummer;
  1440. if (protocol == IPPROTO_TCP) {
  1441. if ((error = kernel_listen(sock, 64)) < 0)
  1442. goto bummer;
  1443. }
  1444. if ((svsk = svc_setup_socket(serv, sock, &error, flags)) != NULL) {
  1445. svc_sock_received(svsk);
  1446. return ntohs(inet_sk(svsk->sk_sk)->sport);
  1447. }
  1448. bummer:
  1449. dprintk("svc: svc_create_socket error = %d\n", -error);
  1450. sock_release(sock);
  1451. return error;
  1452. }
  1453. /*
  1454. * Remove a dead socket
  1455. */
  1456. static void
  1457. svc_delete_socket(struct svc_sock *svsk)
  1458. {
  1459. struct svc_serv *serv;
  1460. struct sock *sk;
  1461. dprintk("svc: svc_delete_socket(%p)\n", svsk);
  1462. serv = svsk->sk_server;
  1463. sk = svsk->sk_sk;
  1464. sk->sk_state_change = svsk->sk_ostate;
  1465. sk->sk_data_ready = svsk->sk_odata;
  1466. sk->sk_write_space = svsk->sk_owspace;
  1467. spin_lock_bh(&serv->sv_lock);
  1468. if (!test_and_set_bit(SK_DETACHED, &svsk->sk_flags))
  1469. list_del_init(&svsk->sk_list);
  1470. /*
  1471. * We used to delete the svc_sock from whichever list
  1472. * it's sk_ready node was on, but we don't actually
  1473. * need to. This is because the only time we're called
  1474. * while still attached to a queue, the queue itself
  1475. * is about to be destroyed (in svc_destroy).
  1476. */
  1477. if (!test_and_set_bit(SK_DEAD, &svsk->sk_flags)) {
  1478. BUG_ON(atomic_read(&svsk->sk_inuse)<2);
  1479. atomic_dec(&svsk->sk_inuse);
  1480. if (test_bit(SK_TEMP, &svsk->sk_flags))
  1481. serv->sv_tmpcnt--;
  1482. }
  1483. spin_unlock_bh(&serv->sv_lock);
  1484. }
  1485. void svc_close_socket(struct svc_sock *svsk)
  1486. {
  1487. set_bit(SK_CLOSE, &svsk->sk_flags);
  1488. if (test_and_set_bit(SK_BUSY, &svsk->sk_flags))
  1489. /* someone else will have to effect the close */
  1490. return;
  1491. atomic_inc(&svsk->sk_inuse);
  1492. svc_delete_socket(svsk);
  1493. clear_bit(SK_BUSY, &svsk->sk_flags);
  1494. svc_sock_put(svsk);
  1495. }
  1496. /**
  1497. * svc_makesock - Make a socket for nfsd and lockd
  1498. * @serv: RPC server structure
  1499. * @protocol: transport protocol to use
  1500. * @port: port to use
  1501. * @flags: requested socket characteristics
  1502. *
  1503. */
  1504. int svc_makesock(struct svc_serv *serv, int protocol, unsigned short port,
  1505. int flags)
  1506. {
  1507. struct sockaddr_in sin = {
  1508. .sin_family = AF_INET,
  1509. .sin_addr.s_addr = INADDR_ANY,
  1510. .sin_port = htons(port),
  1511. };
  1512. dprintk("svc: creating socket proto = %d\n", protocol);
  1513. return svc_create_socket(serv, protocol, &sin, flags);
  1514. }
  1515. /*
  1516. * Handle defer and revisit of requests
  1517. */
  1518. static void svc_revisit(struct cache_deferred_req *dreq, int too_many)
  1519. {
  1520. struct svc_deferred_req *dr = container_of(dreq, struct svc_deferred_req, handle);
  1521. struct svc_sock *svsk;
  1522. if (too_many) {
  1523. svc_sock_put(dr->svsk);
  1524. kfree(dr);
  1525. return;
  1526. }
  1527. dprintk("revisit queued\n");
  1528. svsk = dr->svsk;
  1529. dr->svsk = NULL;
  1530. spin_lock_bh(&svsk->sk_defer_lock);
  1531. list_add(&dr->handle.recent, &svsk->sk_deferred);
  1532. spin_unlock_bh(&svsk->sk_defer_lock);
  1533. set_bit(SK_DEFERRED, &svsk->sk_flags);
  1534. svc_sock_enqueue(svsk);
  1535. svc_sock_put(svsk);
  1536. }
  1537. static struct cache_deferred_req *
  1538. svc_defer(struct cache_req *req)
  1539. {
  1540. struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle);
  1541. int size = sizeof(struct svc_deferred_req) + (rqstp->rq_arg.len);
  1542. struct svc_deferred_req *dr;
  1543. if (rqstp->rq_arg.page_len)
  1544. return NULL; /* if more than a page, give up FIXME */
  1545. if (rqstp->rq_deferred) {
  1546. dr = rqstp->rq_deferred;
  1547. rqstp->rq_deferred = NULL;
  1548. } else {
  1549. int skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
  1550. /* FIXME maybe discard if size too large */
  1551. dr = kmalloc(size, GFP_KERNEL);
  1552. if (dr == NULL)
  1553. return NULL;
  1554. dr->handle.owner = rqstp->rq_server;
  1555. dr->prot = rqstp->rq_prot;
  1556. memcpy(&dr->addr, &rqstp->rq_addr, rqstp->rq_addrlen);
  1557. dr->addrlen = rqstp->rq_addrlen;
  1558. dr->daddr = rqstp->rq_daddr;
  1559. dr->argslen = rqstp->rq_arg.len >> 2;
  1560. memcpy(dr->args, rqstp->rq_arg.head[0].iov_base-skip, dr->argslen<<2);
  1561. }
  1562. atomic_inc(&rqstp->rq_sock->sk_inuse);
  1563. dr->svsk = rqstp->rq_sock;
  1564. dr->handle.revisit = svc_revisit;
  1565. return &dr->handle;
  1566. }
  1567. /*
  1568. * recv data from a deferred request into an active one
  1569. */
  1570. static int svc_deferred_recv(struct svc_rqst *rqstp)
  1571. {
  1572. struct svc_deferred_req *dr = rqstp->rq_deferred;
  1573. rqstp->rq_arg.head[0].iov_base = dr->args;
  1574. rqstp->rq_arg.head[0].iov_len = dr->argslen<<2;
  1575. rqstp->rq_arg.page_len = 0;
  1576. rqstp->rq_arg.len = dr->argslen<<2;
  1577. rqstp->rq_prot = dr->prot;
  1578. memcpy(&rqstp->rq_addr, &dr->addr, dr->addrlen);
  1579. rqstp->rq_addrlen = dr->addrlen;
  1580. rqstp->rq_daddr = dr->daddr;
  1581. rqstp->rq_respages = rqstp->rq_pages;
  1582. return dr->argslen<<2;
  1583. }
  1584. static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk)
  1585. {
  1586. struct svc_deferred_req *dr = NULL;
  1587. if (!test_bit(SK_DEFERRED, &svsk->sk_flags))
  1588. return NULL;
  1589. spin_lock_bh(&svsk->sk_defer_lock);
  1590. clear_bit(SK_DEFERRED, &svsk->sk_flags);
  1591. if (!list_empty(&svsk->sk_deferred)) {
  1592. dr = list_entry(svsk->sk_deferred.next,
  1593. struct svc_deferred_req,
  1594. handle.recent);
  1595. list_del_init(&dr->handle.recent);
  1596. set_bit(SK_DEFERRED, &svsk->sk_flags);
  1597. }
  1598. spin_unlock_bh(&svsk->sk_defer_lock);
  1599. return dr;
  1600. }