svcsock.c 44 KB

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