svcsock.c 44 KB

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