svcsock.c 53 KB

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