svcsock.c 44 KB

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