svcsock.c 49 KB

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