svcsock.c 47 KB

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