svcsock.c 53 KB

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