svcsock.c 49 KB

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