svcsock.c 44 KB

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