svcsock.c 49 KB

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